~drizzle-trunk/drizzle/development

798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2009 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
21
/*
22
  This class is shared between different table objects. There is one
23
  instance of table share per one table in the database.
24
*/
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
25
26
#include <string>
27
28
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
29
class TableShare
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
30
{
31
public:
1000.1.6 by Brian Aker
Simple bit for dead init
32
  TableShare() 
33
  {
34
    init();
35
  }                    /* Remove gcc warning */
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
36
1039.1.9 by Brian Aker
Partial Refactor of TableShare
37
  TableShare(const char *key,
38
             uint32_t key_length, const char *new_table_name,
39
             const char *new_path)
40
  {
41
    init(key, key_length, new_table_name, new_path);
42
  }
43
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
44
  /** Category of this table. */
45
  enum_table_category table_category;
46
1030.1.3 by Brian Aker
Final bits to structure alignment
47
  uint32_t open_count;			/* Number of tables in open list */
48
49
  /* The following is copied to each Table on OPEN */
50
  Field **field;
51
  Field **found_next_number_field;
52
  Field *timestamp_field;               /* Used only during open */
53
  KEY  *key_info;			/* data of keys in database */
54
  uint	*blob_field;			/* Index to blobs in Field arrray*/
55
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
56
  /* hash of field names (contains pointers to elements of field array) */
57
  HASH	name_hash;			/* hash of field names */
58
  MEM_ROOT mem_root;
59
  TYPELIB keynames;			/* Pointers to keynames */
60
  TYPELIB fieldnames;			/* Pointer to fieldnames */
61
  TYPELIB *intervals;			/* pointer to interval info */
62
  pthread_mutex_t mutex;                /* For locking the share  */
63
  pthread_cond_t cond;			/* To signal that share is ready */
64
65
66
  unsigned char	*default_values;		/* row with default values */
67
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
68
1005.2.3 by Monty Taylor
Further reversion of P.
69
  MY_BITMAP all_set;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
70
  /*
71
    Key which is used for looking-up table in table cache and in the list
72
    of thread's temporary tables. Has the form of:
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
73
    "database_name\0table_name\0" + optional part for temporary tables.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
74
75
    Note that all three 'table_cache_key', 'db' and 'table_name' members
76
    must be set (and be non-zero) for tables in table cache. They also
77
    should correspond to each other.
78
    To ensure this one can use set_table_cache() methods.
79
  */
80
  LEX_STRING table_cache_key;
81
  LEX_STRING db;                        /* Pointer to db */
82
  LEX_STRING table_name;                /* Table name (for open) */
83
  LEX_STRING path;	/* Path to .frm file (from datadir) */
84
  LEX_STRING normalized_path;		/* unpack_filename(path) */
85
  LEX_STRING connect_string;
86
87
  uint32_t   block_size;                   /* create information */
1030.1.3 by Brian Aker
Final bits to structure alignment
88
  uint32_t   version;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
89
  uint32_t   timestamp_offset;		/* Set to offset+1 of record */
90
  uint32_t   reclength;			/* Recordlength */
1030.1.3 by Brian Aker
Final bits to structure alignment
91
  uint32_t   stored_rec_length;         /* Stored record length*/
92
  enum row_type row_type;		/* How rows are stored */
93
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
94
private:
95
  uint64_t max_rows_hack; // We can't use proto in a "tmp" table because of a lack of release mechanisms
96
  drizzled::message::Table *table_proto;
97
public:
98
99
  inline bool hasOptions()
100
  {
101
    return (table_proto) ? table_proto->has_options() : false;
102
  }
103
104
  /* This is only used in one location currently */
105
  inline void setTableProto(drizzled::message::Table *arg)
106
  {
107
    assert(table_proto == NULL);
108
    table_proto= arg;
109
  }
110
111
  inline bool hasComment()
112
  {
113
    return (table_proto) ?  table_proto->options().has_comment() : false; 
114
  }
115
116
  inline const char *getComment()
117
  {
118
    return (table_proto) ?  table_proto->options().comment().c_str() : NULL; 
119
  }
120
121
  inline uint32_t getCommentLength()
122
  {
123
    return (table_proto) ? table_proto->options().comment().length() : 0; 
124
  }
125
126
127
  inline uint32_t getAverageRowLength()
128
  {
129
    return (table_proto) ? table_proto->options().avg_row_length() : 0;
130
  }
131
132
  inline bool hasAverageRowLength()
133
  {
134
    return (table_proto) ? table_proto->options().has_avg_row_length() : false;
135
  }
136
137
  inline bool hasMaxRows()
138
  {
139
    return (table_proto) ? table_proto->options().has_max_rows() : false;
140
  }
141
142
  inline uint64_t getMaxRows()
143
  {
144
    return (table_proto) ? table_proto->options().max_rows() : max_rows_hack;
145
  }
146
147
  inline void setMaxRows(uint64_t arg)
148
  {
149
    if (table_proto)
150
    {
151
      drizzled::message::Table::TableOptions *table_options;
152
153
      table_options= table_proto->mutable_options();
154
      table_options->set_max_rows(arg);
155
    }
156
    else
157
    {
158
      max_rows_hack= arg;
159
    }
160
  }
161
162
  inline bool hasMinRows()
163
  {
164
    return (table_proto) ? table_proto->options().has_min_rows() : false;
165
  }
166
167
  inline uint64_t getMinRows()
168
  {
169
    return (table_proto) ? table_proto->options().min_rows() : 0;
170
  }
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
171
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
172
  StorageEngine *storage_engine;			/* storage engine plugin */
960.2.24 by Monty Taylor
Changed handlerton to StorageEngine.
173
  inline StorageEngine *db_type() const	/* table_type for handler */
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
174
  {
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
175
    return storage_engine;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
176
  }
177
  enum tmp_table_type tmp_table;
178
  enum ha_choice page_checksum;
179
180
  uint32_t ref_count;       /* How many Table objects uses this */
181
  uint32_t key_block_size;			/* create key_block_size, if used */
182
  uint32_t null_bytes;
183
  uint32_t last_null_bit_pos;
184
  uint32_t fields;				/* Number of fields */
185
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
186
  uint32_t keys, key_parts;
187
  uint32_t max_key_length, max_unique_length, total_key_length;
188
  uint32_t uniques;                         /* Number of UNIQUE index */
189
  uint32_t null_fields;			/* number of null fields */
190
  uint32_t blob_fields;			/* number of blob fields */
191
  uint32_t timestamp_field_offset;		/* Field number for timestamp field */
192
  uint32_t varchar_fields;                  /* number of varchar fields */
193
  uint32_t db_create_options;		/* Create options from database */
194
  uint32_t db_options_in_use;		/* Options in use */
195
  uint32_t db_record_offset;		/* if HA_REC_IN_SEQ */
196
  uint32_t rowid_field_offset;		/* Field_nr +1 to rowid field */
197
  /* Index of auto-updated TIMESTAMP field in field array */
198
  uint32_t primary_key;
199
  uint32_t next_number_index;               /* autoincrement key number */
200
  uint32_t next_number_key_offset;          /* autoinc keypart offset in a key */
201
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
202
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
203
  uint32_t column_bitmap_size;
204
1039.1.9 by Brian Aker
Partial Refactor of TableShare
205
  uint8_t blob_ptr_size;			/* 4 or 8 */
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
206
  bool db_low_byte_first;		/* Portable row format */
207
  bool crashed;
1039.1.9 by Brian Aker
Partial Refactor of TableShare
208
  bool name_lock;
209
  bool replace_with_name_lock;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
210
  bool waiting_on_cond;                 /* Protection against free */
211
212
  /*
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
213
    Set of keys in use, implemented as a Bitmap.
214
    Excludes keys disabled by ALTER Table ... DISABLE KEYS.
1030.1.3 by Brian Aker
Final bits to structure alignment
215
  */
216
  key_map keys_in_use;
217
  key_map keys_for_keyread;
218
219
  /*
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
220
    Set share's table cache key and update its db and table name appropriately.
221
222
    SYNOPSIS
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
223
    set_table_cache_key()
224
    key_buff    Buffer with already built table cache key to be
225
    referenced from share.
226
    key_length  Key length.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
227
228
    NOTES
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
229
    Since 'key_buff' buffer will be referenced from share it should has same
230
    life-time as share itself.
231
    This method automatically ensures that TableShare::table_name/db have
232
    appropriate values by using table cache key as their source.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
233
  */
234
235
  void set_table_cache_key(char *key_buff, uint32_t key_length)
236
  {
237
    table_cache_key.str= key_buff;
238
    table_cache_key.length= key_length;
239
    /*
240
      Let us use the fact that the key is "db/0/table_name/0" + optional
241
      part for temporary tables.
242
    */
243
    db.str=            table_cache_key.str;
244
    db.length=         strlen(db.str);
245
    table_name.str=    db.str + db.length + 1;
246
    table_name.length= strlen(table_name.str);
247
  }
248
249
250
  /*
251
    Set share's table cache key and update its db and table name appropriately.
252
253
    SYNOPSIS
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
254
    set_table_cache_key()
255
    key_buff    Buffer to be used as storage for table cache key
256
    (should be at least key_length bytes).
257
    key         Value for table cache key.
258
    key_length  Key length.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
259
260
    NOTE
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
261
    Since 'key_buff' buffer will be used as storage for table cache key
262
    it should has same life-time as share itself.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
263
  */
264
265
  void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
266
  {
267
    memcpy(key_buff, key, key_length);
268
    set_table_cache_key(key_buff, key_length);
269
  }
270
271
  inline bool honor_global_locks()
272
  {
273
    return (table_category == TABLE_CATEGORY_USER);
274
  }
275
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
276
277
  /*
278
    Initialize share for temporary tables
279
280
    SYNOPSIS
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
281
    init()
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
282
    share	Share to fill
283
    key		Table_cache_key, as generated from create_table_def_key.
284
    must start with db name.
285
    key_length	Length of key
286
    table_name	Table name
287
    path	Path to file (possible in lower case) without .frm
288
289
    NOTES
290
    This is different from alloc_table_share() because temporary tables
291
    don't have to be shared between threads or put into the table def
292
    cache, so we can do some things notable simpler and faster
293
294
    If table is not put in session->temporary_tables (happens only when
295
    one uses OPEN TEMPORARY) then one can specify 'db' as key and
296
    use key_length= 0 as neither table_cache_key or key_length will be used).
297
  */
298
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
299
  void init()
300
  {
301
    init("", 0, "", "");
302
  }
303
304
  void init(const char *new_table_name,
305
            const char *new_path)
306
  {
307
    init("", 0, new_table_name, new_path);
308
  }
309
310
  void init(const char *key,
311
            uint32_t key_length, const char *new_table_name,
312
            const char *new_path)
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
313
  {
314
    memset(this, 0, sizeof(TableShare));
315
    init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
316
    table_category=         TABLE_CATEGORY_TEMPORARY;
317
    tmp_table=              INTERNAL_TMP_TABLE;
318
    db.str=                 (char*) key;
319
    db.length=		 strlen(key);
320
    table_cache_key.str=    (char*) key;
321
    table_cache_key.length= key_length;
322
    table_name.str=         (char*) new_table_name;
323
    table_name.length=      strlen(new_table_name);
324
    path.str=               (char*) new_path;
325
    normalized_path.str=    (char*) new_path;
326
    path.length= normalized_path.length= strlen(new_path);
327
328
    return;
329
  }
330
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
331
  /*
332
    Free table share and memory used by it
333
334
    SYNOPSIS
335
    free_table_share()
336
    share		Table share
337
338
    NOTES
339
    share->mutex must be locked when we come here if it's not a temp table
340
  */
341
342
  void free_table_share()
343
  {
344
    MEM_ROOT new_mem_root;
345
    assert(ref_count == 0);
346
347
    /*
348
      If someone is waiting for this to be deleted, inform it about this.
349
      Don't do a delete until we know that no one is refering to this anymore.
350
    */
351
    if (tmp_table == NO_TMP_TABLE)
352
    {
353
      /* share->mutex is locked in release_table_share() */
354
      while (waiting_on_cond)
355
      {
356
        pthread_cond_broadcast(&cond);
357
        pthread_cond_wait(&cond, &mutex);
358
      }
359
      /* No thread refers to this anymore */
360
      pthread_mutex_unlock(&mutex);
361
      pthread_mutex_destroy(&mutex);
362
      pthread_cond_destroy(&cond);
363
    }
364
    hash_free(&name_hash);
365
366
    storage_engine= NULL;
367
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
368
    delete table_proto;
369
    table_proto= NULL;
370
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
371
    /* We must copy mem_root from share because share is allocated through it */
372
    memcpy(&new_mem_root, &mem_root, sizeof(new_mem_root));
373
    free_root(&new_mem_root, MYF(0));                 // Free's share
374
  }
375
1039.1.9 by Brian Aker
Partial Refactor of TableShare
376
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
377
378
379
380
  /*
381
    Create a table cache key
382
383
    SYNOPSIS
384
    createKey()
385
    key			Create key here (must be of size MAX_DBKEY_LENGTH)
386
    table_list		Table definition
387
388
    IMPLEMENTATION
389
    The table cache_key is created from:
390
    db_name + \0
391
    table_name + \0
392
393
    if the table is a tmp table, we add the following to make each tmp table
394
    unique on the slave:
395
396
    4 bytes for master thread id
397
    4 bytes pseudo thread id
398
399
    RETURN
400
    Length of key
401
  */
402
1093.5.3 by Monty Taylor
Removed using namespace entries from bare headers.
403
  static inline uint32_t createKey(char *key, std::string& db_arg,
404
                                   std::string& table_name_arg)
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
405
  {
406
    return createKey(key, db_arg.c_str(), table_name_arg.c_str());
407
  }
408
409
  static inline uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
410
  {
411
    uint32_t key_length;
412
    char *key_pos= key;
413
414
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
415
    key_pos= strcpy(key_pos+1, table_name_arg) +
416
      strlen(table_name_arg);
417
    key_length= (uint32_t)(key_pos-key)+1;
418
419
    return key_length;
420
  }
1093.6.1 by Brian Aker
Refactor TableShare has to be behind class.
421
422
  static bool cacheStart(void);
423
  static void cacheStop(void);
424
  static void release(TableShare *share);
425
  static void release(const char *key, uint32_t key_length);
426
  static TableShare *getShare(const char *db, const char *table_name);
427
  static TableShare *getShare(Session *session, 
428
                              TableList *table_list, char *key,
429
                              uint32_t key_length, uint32_t, int *error);
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
430
};