~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
1122.2.10 by Monty Taylor
Fixed all of the include guards.
26
#ifndef DRIZZLED_TABLE_SHARE_H
27
#define DRIZZLED_TABLE_SHARE_H
28
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
29
#include <string>
30
1122.2.13 by Monty Taylor
Header cleanup.
31
#include "drizzled/message/table.pb.h"
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
32
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
33
class TableShare
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
34
{
35
public:
1000.1.6 by Brian Aker
Simple bit for dead init
36
  TableShare() 
37
  {
38
    init();
39
  }                    /* Remove gcc warning */
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
40
1039.1.9 by Brian Aker
Partial Refactor of TableShare
41
  TableShare(const char *key,
42
             uint32_t key_length, const char *new_table_name,
43
             const char *new_path)
44
  {
45
    init(key, key_length, new_table_name, new_path);
46
  }
47
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
48
  /** Category of this table. */
49
  enum_table_category table_category;
50
1030.1.3 by Brian Aker
Final bits to structure alignment
51
  uint32_t open_count;			/* Number of tables in open list */
52
53
  /* The following is copied to each Table on OPEN */
54
  Field **field;
55
  Field **found_next_number_field;
56
  Field *timestamp_field;               /* Used only during open */
57
  KEY  *key_info;			/* data of keys in database */
58
  uint	*blob_field;			/* Index to blobs in Field arrray*/
59
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
60
  /* hash of field names (contains pointers to elements of field array) */
61
  HASH	name_hash;			/* hash of field names */
62
  MEM_ROOT mem_root;
63
  TYPELIB keynames;			/* Pointers to keynames */
64
  TYPELIB fieldnames;			/* Pointer to fieldnames */
65
  TYPELIB *intervals;			/* pointer to interval info */
66
  pthread_mutex_t mutex;                /* For locking the share  */
67
  pthread_cond_t cond;			/* To signal that share is ready */
68
69
70
  unsigned char	*default_values;		/* row with default values */
71
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
72
1103.6.2 by Padraig O'Sullivan
Removing references to MY_BITMAP throughout the code base and updating calls
73
  MyBitmap all_set;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
74
  /*
75
    Key which is used for looking-up table in table cache and in the list
76
    of thread's temporary tables. Has the form of:
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
77
    "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).
78
79
    Note that all three 'table_cache_key', 'db' and 'table_name' members
80
    must be set (and be non-zero) for tables in table cache. They also
81
    should correspond to each other.
82
    To ensure this one can use set_table_cache() methods.
83
  */
84
  LEX_STRING table_cache_key;
85
  LEX_STRING db;                        /* Pointer to db */
86
  LEX_STRING table_name;                /* Table name (for open) */
87
  LEX_STRING path;	/* Path to .frm file (from datadir) */
88
  LEX_STRING normalized_path;		/* unpack_filename(path) */
89
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
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
97
private:
1116.1.2 by Brian Aker
Remove options which are just for internal optimizations.
98
  /* Max rows is a hint to HEAP during a create tmp table */
99
  uint64_t max_rows;
100
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
101
  drizzled::message::Table *table_proto;
102
public:
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
  {
1183.1.18 by Brian Aker
Fixed references to doCreateTable()
118
    return (table_proto && table_proto->has_options()) ?  table_proto->options().comment().c_str() : NULL; 
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
119
  }
120
121
  inline uint32_t getCommentLength()
122
  {
123
    return (table_proto) ? table_proto->options().comment().length() : 0; 
124
  }
125
1188.2.1 by Stewart Smith
move key_block_size out of table_share and into the table proto. init it in parser
126
  inline bool hasKeyBlockSize()
127
  {
128
    return (table_proto) ? table_proto->options().has_key_block_size() : false;
129
  }
130
131
  inline uint32_t getKeyBlockSize()
132
  {
133
    return (table_proto) ? table_proto->options().key_block_size() : 0;
134
  }
135
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
136
  inline uint64_t getMaxRows()
137
  {
1116.1.2 by Brian Aker
Remove options which are just for internal optimizations.
138
    return max_rows;
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
139
  }
140
141
  inline void setMaxRows(uint64_t arg)
142
  {
1116.1.2 by Brian Aker
Remove options which are just for internal optimizations.
143
    max_rows= arg;
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
144
  }
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
145
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
146
  drizzled::plugin::StorageEngine *storage_engine;			/* storage engine plugin */
147
  inline drizzled::plugin::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).
148
  {
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
149
    return storage_engine;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
150
  }
151
  enum tmp_table_type tmp_table;
152
153
  uint32_t ref_count;       /* How many Table objects uses this */
154
  uint32_t null_bytes;
155
  uint32_t last_null_bit_pos;
156
  uint32_t fields;				/* Number of fields */
157
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
158
  uint32_t keys, key_parts;
159
  uint32_t max_key_length, max_unique_length, total_key_length;
160
  uint32_t uniques;                         /* Number of UNIQUE index */
161
  uint32_t null_fields;			/* number of null fields */
162
  uint32_t blob_fields;			/* number of blob fields */
163
  uint32_t timestamp_field_offset;		/* Field number for timestamp field */
164
  uint32_t varchar_fields;                  /* number of varchar fields */
165
  uint32_t db_create_options;		/* Create options from database */
166
  uint32_t db_options_in_use;		/* Options in use */
167
  uint32_t db_record_offset;		/* if HA_REC_IN_SEQ */
168
  uint32_t rowid_field_offset;		/* Field_nr +1 to rowid field */
1143.2.21 by Jay Pipes
Fixes Bug 440141 - Replication stream contains incorrect key field information for UPDATE statements.
169
  /**
170
   * @TODO 
171
   *
172
   * Currently the replication services component uses
173
   * the primary_key member to determine which field is the table's
174
   * primary key.  However, as it exists, because this member is scalar, it
175
   * only supports a single-column primary key. Is there a better way
176
   * to ask for the fields which are in a primary key?
177
   */
178
  uint32_t primary_key;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
179
  /* Index of auto-updated TIMESTAMP field in field array */
180
  uint32_t next_number_index;               /* autoincrement key number */
181
  uint32_t next_number_key_offset;          /* autoinc keypart offset in a key */
182
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
183
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
184
  uint32_t column_bitmap_size;
185
1039.1.9 by Brian Aker
Partial Refactor of TableShare
186
  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).
187
  bool db_low_byte_first;		/* Portable row format */
188
  bool crashed;
1039.1.9 by Brian Aker
Partial Refactor of TableShare
189
  bool name_lock;
190
  bool replace_with_name_lock;
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
191
  bool waiting_on_cond;                 /* Protection against free */
192
193
  /*
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
194
    Set of keys in use, implemented as a Bitmap.
195
    Excludes keys disabled by ALTER Table ... DISABLE KEYS.
1030.1.3 by Brian Aker
Final bits to structure alignment
196
  */
197
  key_map keys_in_use;
198
  key_map keys_for_keyread;
199
200
  /*
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
201
    Set share's table cache key and update its db and table name appropriately.
202
203
    SYNOPSIS
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
204
    set_table_cache_key()
205
    key_buff    Buffer with already built table cache key to be
206
    referenced from share.
207
    key_length  Key length.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
208
209
    NOTES
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
210
    Since 'key_buff' buffer will be referenced from share it should has same
211
    life-time as share itself.
212
    This method automatically ensures that TableShare::table_name/db have
213
    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).
214
  */
215
216
  void set_table_cache_key(char *key_buff, uint32_t key_length)
217
  {
218
    table_cache_key.str= key_buff;
219
    table_cache_key.length= key_length;
220
    /*
221
      Let us use the fact that the key is "db/0/table_name/0" + optional
222
      part for temporary tables.
223
    */
224
    db.str=            table_cache_key.str;
225
    db.length=         strlen(db.str);
226
    table_name.str=    db.str + db.length + 1;
227
    table_name.length= strlen(table_name.str);
228
  }
229
230
231
  /*
232
    Set share's table cache key and update its db and table name appropriately.
233
234
    SYNOPSIS
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
235
    set_table_cache_key()
236
    key_buff    Buffer to be used as storage for table cache key
237
    (should be at least key_length bytes).
238
    key         Value for table cache key.
239
    key_length  Key length.
798 by Brian Aker
Updated table_share tto its own file (yess, it will eventually go away).
240
241
    NOTE
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
242
    Since 'key_buff' buffer will be used as storage for table cache key
243
    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).
244
  */
245
246
  void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
247
  {
248
    memcpy(key_buff, key, key_length);
249
    set_table_cache_key(key_buff, key_length);
250
  }
251
252
  inline bool honor_global_locks()
253
  {
254
    return (table_category == TABLE_CATEGORY_USER);
255
  }
256
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
257
258
  /*
259
    Initialize share for temporary tables
260
261
    SYNOPSIS
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
262
    init()
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
263
    share	Share to fill
264
    key		Table_cache_key, as generated from create_table_def_key.
265
    must start with db name.
266
    key_length	Length of key
267
    table_name	Table name
268
    path	Path to file (possible in lower case) without .frm
269
270
    NOTES
271
    This is different from alloc_table_share() because temporary tables
272
    don't have to be shared between threads or put into the table def
273
    cache, so we can do some things notable simpler and faster
274
275
    If table is not put in session->temporary_tables (happens only when
276
    one uses OPEN TEMPORARY) then one can specify 'db' as key and
277
    use key_length= 0 as neither table_cache_key or key_length will be used).
278
  */
279
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
280
  void init()
281
  {
282
    init("", 0, "", "");
283
  }
284
285
  void init(const char *new_table_name,
286
            const char *new_path)
287
  {
288
    init("", 0, new_table_name, new_path);
289
  }
290
291
  void init(const char *key,
292
            uint32_t key_length, const char *new_table_name,
293
            const char *new_path)
1000.1.4 by Brian Aker
Turn init_tmp_table_share into a method (the memset of the object... cute).
294
  {
295
    memset(this, 0, sizeof(TableShare));
296
    init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
297
    table_category=         TABLE_CATEGORY_TEMPORARY;
298
    tmp_table=              INTERNAL_TMP_TABLE;
299
    db.str=                 (char*) key;
300
    db.length=		 strlen(key);
301
    table_cache_key.str=    (char*) key;
302
    table_cache_key.length= key_length;
303
    table_name.str=         (char*) new_table_name;
304
    table_name.length=      strlen(new_table_name);
305
    path.str=               (char*) new_path;
306
    normalized_path.str=    (char*) new_path;
307
    path.length= normalized_path.length= strlen(new_path);
308
309
    return;
310
  }
311
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
312
  /*
313
    Free table share and memory used by it
314
315
    SYNOPSIS
316
    free_table_share()
317
    share		Table share
318
319
    NOTES
320
    share->mutex must be locked when we come here if it's not a temp table
321
  */
322
323
  void free_table_share()
324
  {
325
    MEM_ROOT new_mem_root;
326
    assert(ref_count == 0);
327
328
    /*
329
      If someone is waiting for this to be deleted, inform it about this.
330
      Don't do a delete until we know that no one is refering to this anymore.
331
    */
332
    if (tmp_table == NO_TMP_TABLE)
333
    {
334
      /* share->mutex is locked in release_table_share() */
335
      while (waiting_on_cond)
336
      {
337
        pthread_cond_broadcast(&cond);
338
        pthread_cond_wait(&cond, &mutex);
339
      }
340
      /* No thread refers to this anymore */
341
      pthread_mutex_unlock(&mutex);
342
      pthread_mutex_destroy(&mutex);
343
      pthread_cond_destroy(&cond);
344
    }
345
    hash_free(&name_hash);
346
347
    storage_engine= NULL;
348
1116.1.1 by Brian Aker
Fix for Stewart's patch (includes hack to solve MAX rows problem).
349
    delete table_proto;
350
    table_proto= NULL;
351
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
352
    /* We must copy mem_root from share because share is allocated through it */
353
    memcpy(&new_mem_root, &mem_root, sizeof(new_mem_root));
354
    free_root(&new_mem_root, MYF(0));                 // Free's share
355
  }
356
1039.1.9 by Brian Aker
Partial Refactor of TableShare
357
  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
358
359
360
361
  /*
362
    Create a table cache key
363
364
    SYNOPSIS
365
    createKey()
366
    key			Create key here (must be of size MAX_DBKEY_LENGTH)
367
    table_list		Table definition
368
369
    IMPLEMENTATION
370
    The table cache_key is created from:
371
    db_name + \0
372
    table_name + \0
373
374
    if the table is a tmp table, we add the following to make each tmp table
375
    unique on the slave:
376
377
    4 bytes for master thread id
378
    4 bytes pseudo thread id
379
380
    RETURN
381
    Length of key
382
  */
383
1093.5.3 by Monty Taylor
Removed using namespace entries from bare headers.
384
  static inline uint32_t createKey(char *key, std::string& db_arg,
385
                                   std::string& table_name_arg)
1089.1.4 by Brian Aker
Optimization for createKey(). Removes the need to initialize some
386
  {
387
    return createKey(key, db_arg.c_str(), table_name_arg.c_str());
388
  }
389
390
  static inline uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
391
  {
392
    uint32_t key_length;
393
    char *key_pos= key;
394
395
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
396
    key_pos= strcpy(key_pos+1, table_name_arg) +
397
      strlen(table_name_arg);
398
    key_length= (uint32_t)(key_pos-key)+1;
399
400
    return key_length;
401
  }
1093.6.1 by Brian Aker
Refactor TableShare has to be behind class.
402
403
  static bool cacheStart(void);
404
  static void cacheStop(void);
405
  static void release(TableShare *share);
406
  static void release(const char *key, uint32_t key_length);
407
  static TableShare *getShare(const char *db, const char *table_name);
408
  static TableShare *getShare(Session *session, 
409
                              TableList *table_list, char *key,
410
                              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).
411
};
1122.2.10 by Monty Taylor
Fixed all of the include guards.
412
413
#endif /* DRIZZLED_TABLE_SHARE_H */