~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
  LEX_STRING comment;			/* Comment about table */
68
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
69
1005.2.3 by Monty Taylor
Further reversion of P.
70
  MY_BITMAP all_set;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
71
  /*
72
    Key which is used for looking-up table in table cache and in the list
73
    of thread's temporary tables. Has the form of:
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
74
    "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).
75
76
    Note that all three 'table_cache_key', 'db' and 'table_name' members
77
    must be set (and be non-zero) for tables in table cache. They also
78
    should correspond to each other.
79
    To ensure this one can use set_table_cache() methods.
80
  */
81
  LEX_STRING table_cache_key;
82
  LEX_STRING db;                        /* Pointer to db */
83
  LEX_STRING table_name;                /* Table name (for open) */
84
  LEX_STRING path;	/* Path to .frm file (from datadir) */
85
  LEX_STRING normalized_path;		/* unpack_filename(path) */
86
  LEX_STRING connect_string;
87
88
  uint32_t   avg_row_length;		/* create information */
89
  uint32_t   block_size;                   /* create information */
1030.1.3 by Brian Aker
Final bits to structure alignment
90
  uint32_t   version;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
91
  uint32_t   timestamp_offset;		/* Set to offset+1 of record */
92
  uint32_t   reclength;			/* Recordlength */
1030.1.3 by Brian Aker
Final bits to structure alignment
93
  uint32_t   stored_rec_length;         /* Stored record length*/
94
  enum row_type row_type;		/* How rows are stored */
95
96
  ha_rows min_rows;		/* create information */
97
  ha_rows max_rows;		/* create information */
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
98
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
99
  StorageEngine *storage_engine;			/* storage engine plugin */
960.2.24 by Monty Taylor
Changed handlerton to StorageEngine.
100
  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).
101
  {
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
102
    return storage_engine;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
103
  }
104
  enum tmp_table_type tmp_table;
105
  enum ha_choice page_checksum;
106
107
  uint32_t ref_count;       /* How many Table objects uses this */
108
  uint32_t key_block_size;			/* create key_block_size, if used */
109
  uint32_t null_bytes;
110
  uint32_t last_null_bit_pos;
111
  uint32_t fields;				/* Number of fields */
112
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
113
  uint32_t keys, key_parts;
114
  uint32_t max_key_length, max_unique_length, total_key_length;
115
  uint32_t uniques;                         /* Number of UNIQUE index */
116
  uint32_t null_fields;			/* number of null fields */
117
  uint32_t blob_fields;			/* number of blob fields */
118
  uint32_t timestamp_field_offset;		/* Field number for timestamp field */
119
  uint32_t varchar_fields;                  /* number of varchar fields */
120
  uint32_t db_create_options;		/* Create options from database */
121
  uint32_t db_options_in_use;		/* Options in use */
122
  uint32_t db_record_offset;		/* if HA_REC_IN_SEQ */
123
  uint32_t rowid_field_offset;		/* Field_nr +1 to rowid field */
124
  /* Index of auto-updated TIMESTAMP field in field array */
125
  uint32_t primary_key;
126
  uint32_t next_number_index;               /* autoincrement key number */
127
  uint32_t next_number_key_offset;          /* autoinc keypart offset in a key */
128
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
129
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
130
  uint32_t column_bitmap_size;
131
1039.1.9 by Brian Aker
Partial Refactor of TableShare
132
  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).
133
  bool db_low_byte_first;		/* Portable row format */
134
  bool crashed;
1039.1.9 by Brian Aker
Partial Refactor of TableShare
135
  bool name_lock;
136
  bool replace_with_name_lock;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
137
  bool waiting_on_cond;                 /* Protection against free */
138
139
  /*
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
140
    Set of keys in use, implemented as a Bitmap.
141
    Excludes keys disabled by ALTER Table ... DISABLE KEYS.
1030.1.3 by Brian Aker
Final bits to structure alignment
142
  */
143
  key_map keys_in_use;
144
  key_map keys_for_keyread;
145
146
  /*
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
147
    Set share's table cache key and update its db and table name appropriately.
148
149
    SYNOPSIS
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
150
    set_table_cache_key()
151
    key_buff    Buffer with already built table cache key to be
152
    referenced from share.
153
    key_length  Key length.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
154
155
    NOTES
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
156
    Since 'key_buff' buffer will be referenced from share it should has same
157
    life-time as share itself.
158
    This method automatically ensures that TableShare::table_name/db have
159
    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).
160
  */
161
162
  void set_table_cache_key(char *key_buff, uint32_t key_length)
163
  {
164
    table_cache_key.str= key_buff;
165
    table_cache_key.length= key_length;
166
    /*
167
      Let us use the fact that the key is "db/0/table_name/0" + optional
168
      part for temporary tables.
169
    */
170
    db.str=            table_cache_key.str;
171
    db.length=         strlen(db.str);
172
    table_name.str=    db.str + db.length + 1;
173
    table_name.length= strlen(table_name.str);
174
  }
175
176
177
  /*
178
    Set share's table cache key and update its db and table name appropriately.
179
180
    SYNOPSIS
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
181
    set_table_cache_key()
182
    key_buff    Buffer to be used as storage for table cache key
183
    (should be at least key_length bytes).
184
    key         Value for table cache key.
185
    key_length  Key length.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
186
187
    NOTE
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
188
    Since 'key_buff' buffer will be used as storage for table cache key
189
    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).
190
  */
191
192
  void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
193
  {
194
    memcpy(key_buff, key, key_length);
195
    set_table_cache_key(key_buff, key_length);
196
  }
197
198
  inline bool honor_global_locks()
199
  {
200
    return (table_category == TABLE_CATEGORY_USER);
201
  }
202
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
203
204
  /*
205
    Initialize share for temporary tables
206
207
    SYNOPSIS
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
208
    init()
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
209
    share	Share to fill
210
    key		Table_cache_key, as generated from create_table_def_key.
211
    must start with db name.
212
    key_length	Length of key
213
    table_name	Table name
214
    path	Path to file (possible in lower case) without .frm
215
216
    NOTES
217
    This is different from alloc_table_share() because temporary tables
218
    don't have to be shared between threads or put into the table def
219
    cache, so we can do some things notable simpler and faster
220
221
    If table is not put in session->temporary_tables (happens only when
222
    one uses OPEN TEMPORARY) then one can specify 'db' as key and
223
    use key_length= 0 as neither table_cache_key or key_length will be used).
224
  */
225
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
226
  void init()
227
  {
228
    init("", 0, "", "");
229
  }
230
231
  void init(const char *new_table_name,
232
            const char *new_path)
233
  {
234
    init("", 0, new_table_name, new_path);
235
  }
236
237
  void init(const char *key,
238
            uint32_t key_length, const char *new_table_name,
239
            const char *new_path)
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
240
  {
241
    memset(this, 0, sizeof(TableShare));
242
    init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
243
    table_category=         TABLE_CATEGORY_TEMPORARY;
244
    tmp_table=              INTERNAL_TMP_TABLE;
245
    db.str=                 (char*) key;
246
    db.length=		 strlen(key);
247
    table_cache_key.str=    (char*) key;
248
    table_cache_key.length= key_length;
249
    table_name.str=         (char*) new_table_name;
250
    table_name.length=      strlen(new_table_name);
251
    path.str=               (char*) new_path;
252
    normalized_path.str=    (char*) new_path;
253
    path.length= normalized_path.length= strlen(new_path);
254
255
    return;
256
  }
257
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
258
  /*
259
    Free table share and memory used by it
260
261
    SYNOPSIS
262
    free_table_share()
263
    share		Table share
264
265
    NOTES
266
    share->mutex must be locked when we come here if it's not a temp table
267
  */
268
269
  void free_table_share()
270
  {
271
    MEM_ROOT new_mem_root;
272
    assert(ref_count == 0);
273
274
    /*
275
      If someone is waiting for this to be deleted, inform it about this.
276
      Don't do a delete until we know that no one is refering to this anymore.
277
    */
278
    if (tmp_table == NO_TMP_TABLE)
279
    {
280
      /* share->mutex is locked in release_table_share() */
281
      while (waiting_on_cond)
282
      {
283
        pthread_cond_broadcast(&cond);
284
        pthread_cond_wait(&cond, &mutex);
285
      }
286
      /* No thread refers to this anymore */
287
      pthread_mutex_unlock(&mutex);
288
      pthread_mutex_destroy(&mutex);
289
      pthread_cond_destroy(&cond);
290
    }
291
    hash_free(&name_hash);
292
293
    storage_engine= NULL;
294
295
    /* We must copy mem_root from share because share is allocated through it */
296
    memcpy(&new_mem_root, &mem_root, sizeof(new_mem_root));
297
    free_root(&new_mem_root, MYF(0));                 // Free's share
298
  }
299
1039.1.9 by Brian Aker
Partial Refactor of TableShare
300
  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
301
302
303
304
  /*
305
    Create a table cache key
306
307
    SYNOPSIS
308
    createKey()
309
    key			Create key here (must be of size MAX_DBKEY_LENGTH)
310
    table_list		Table definition
311
312
    IMPLEMENTATION
313
    The table cache_key is created from:
314
    db_name + \0
315
    table_name + \0
316
317
    if the table is a tmp table, we add the following to make each tmp table
318
    unique on the slave:
319
320
    4 bytes for master thread id
321
    4 bytes pseudo thread id
322
323
    RETURN
324
    Length of key
325
  */
326
1093.5.3 by Monty Taylor
Removed using namespace entries from bare headers.
327
  static inline uint32_t createKey(char *key, std::string& db_arg,
328
                                   std::string& table_name_arg)
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
329
  {
330
    return createKey(key, db_arg.c_str(), table_name_arg.c_str());
331
  }
332
333
  static inline uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
334
  {
335
    uint32_t key_length;
336
    char *key_pos= key;
337
338
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
339
    key_pos= strcpy(key_pos+1, table_name_arg) +
340
      strlen(table_name_arg);
341
    key_length= (uint32_t)(key_pos-key)+1;
342
343
    return key_length;
344
  }
1093.6.1 by Brian Aker
Refactor TableShare has to be behind class.
345
346
  static bool cacheStart(void);
347
  static void cacheStop(void);
348
  static void release(TableShare *share);
349
  static void release(const char *key, uint32_t key_length);
350
  static TableShare *getShare(const char *db, const char *table_name);
351
  static TableShare *getShare(Session *session, 
352
                              TableList *table_list, char *key,
353
                              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).
354
};