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