~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.h

  • Committer: Brian Aker
  • Date: 2008-07-28 18:01:38 UTC
  • Revision ID: brian@tangent.org-20080728180138-q2pxlq0qiapvqsdn
Remove YEAR field type

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
*/
25
 
 
26
 
#ifndef DRIZZLED_TABLE_SHARE_H
27
 
#define DRIZZLED_TABLE_SHARE_H
28
 
 
29
 
#include <string>
30
 
 
31
 
#include "drizzled/typelib.h"
32
 
#include "drizzled/my_hash.h"
33
 
#include "drizzled/memory/root.h"
34
 
#include "drizzled/message/table.pb.h"
35
 
 
36
 
class TableShare
37
 
{
38
 
public:
39
 
  TableShare() :
40
 
    table_category(TABLE_UNKNOWN_CATEGORY),
41
 
    open_count(0),
42
 
    field(NULL),
43
 
    found_next_number_field(NULL),
44
 
    timestamp_field(NULL),
45
 
    key_info(NULL),
46
 
    blob_field(NULL),
47
 
    intervals(NULL),
48
 
    default_values(NULL),
49
 
    block_size(0),
50
 
    version(0),
51
 
    timestamp_offset(0),
52
 
    reclength(0),
53
 
    stored_rec_length(0),
54
 
    row_type(ROW_TYPE_DEFAULT),
55
 
    max_rows(0),
56
 
    table_proto(NULL),
57
 
    storage_engine(NULL),
58
 
    tmp_table(NO_TMP_TABLE),
59
 
    ref_count(0),
60
 
    null_bytes(0),
61
 
    last_null_bit_pos(0),
62
 
    fields(0),
63
 
    rec_buff_length(0),
64
 
    keys(0),
65
 
    key_parts(0),
66
 
    max_key_length(0),
67
 
    max_unique_length(0),
68
 
    total_key_length(0),
69
 
    uniques(0),
70
 
    null_fields(0),
71
 
    blob_fields(0),
72
 
    timestamp_field_offset(0),
73
 
    varchar_fields(0),
74
 
    db_create_options(0),
75
 
    db_options_in_use(0),
76
 
    db_record_offset(0),
77
 
    rowid_field_offset(0),
78
 
    primary_key(0),
79
 
    next_number_index(0),
80
 
    next_number_key_offset(0),
81
 
    next_number_keypart(0),
82
 
    error(0),
83
 
    open_errno(0),
84
 
    errarg(0),
85
 
    column_bitmap_size(0),
86
 
    blob_ptr_size(0),
87
 
    db_low_byte_first(false),
88
 
    name_lock(false),
89
 
    replace_with_name_lock(false),
90
 
    waiting_on_cond(false),
91
 
    keys_in_use(0),
92
 
    keys_for_keyread(0)
93
 
  {
94
 
    init();
95
 
  }
96
 
 
97
 
  TableShare(const char *key,
98
 
             uint32_t key_length,
99
 
             const char *new_table_name,
100
 
             const char *new_path) :
101
 
    table_category(TABLE_UNKNOWN_CATEGORY),
102
 
    open_count(0),
103
 
    field(NULL),
104
 
    found_next_number_field(NULL),
105
 
    timestamp_field(NULL),
106
 
    key_info(NULL),
107
 
    blob_field(NULL),
108
 
    intervals(NULL),
109
 
    default_values(NULL),
110
 
    block_size(0),
111
 
    version(0),
112
 
    timestamp_offset(0),
113
 
    reclength(0),
114
 
    stored_rec_length(0),
115
 
    row_type(ROW_TYPE_DEFAULT),
116
 
    max_rows(0),
117
 
    table_proto(NULL),
118
 
    storage_engine(NULL),
119
 
    tmp_table(NO_TMP_TABLE),
120
 
    ref_count(0),
121
 
    null_bytes(0),
122
 
    last_null_bit_pos(0),
123
 
    fields(0),
124
 
    rec_buff_length(0),
125
 
    keys(0),
126
 
    key_parts(0),
127
 
    max_key_length(0),
128
 
    max_unique_length(0),
129
 
    total_key_length(0),
130
 
    uniques(0),
131
 
    null_fields(0),
132
 
    blob_fields(0),
133
 
    timestamp_field_offset(0),
134
 
    varchar_fields(0),
135
 
    db_create_options(0),
136
 
    db_options_in_use(0),
137
 
    db_record_offset(0),
138
 
    rowid_field_offset(0),
139
 
    primary_key(0),
140
 
    next_number_index(0),
141
 
    next_number_key_offset(0),
142
 
    next_number_keypart(0),
143
 
    error(0),
144
 
    open_errno(0),
145
 
    errarg(0),
146
 
    column_bitmap_size(0),
147
 
    blob_ptr_size(0),
148
 
    db_low_byte_first(false),
149
 
    name_lock(false),
150
 
    replace_with_name_lock(false),
151
 
    waiting_on_cond(false),
152
 
    keys_in_use(0),
153
 
    keys_for_keyread(0)
154
 
  {
155
 
    init(key, key_length, new_table_name, new_path);
156
 
  }
157
 
 
158
 
  /** Category of this table. */
159
 
  enum_table_category table_category;
160
 
 
161
 
  uint32_t open_count;                  /* Number of tables in open list */
162
 
 
163
 
  /* The following is copied to each Table on OPEN */
164
 
  Field **field;
165
 
  Field **found_next_number_field;
166
 
  Field *timestamp_field;               /* Used only during open */
167
 
  KEY  *key_info;                       /* data of keys in database */
168
 
  uint  *blob_field;                    /* Index to blobs in Field arrray*/
169
 
 
170
 
  /* hash of field names (contains pointers to elements of field array) */
171
 
  HASH  name_hash;                      /* hash of field names */
172
 
  drizzled::memory::Root mem_root;
173
 
  TYPELIB keynames;                     /* Pointers to keynames */
174
 
  TYPELIB fieldnames;                   /* Pointer to fieldnames */
175
 
  TYPELIB *intervals;                   /* pointer to interval info */
176
 
  pthread_mutex_t mutex;                /* For locking the share  */
177
 
  pthread_cond_t cond;                  /* To signal that share is ready */
178
 
 
179
 
  unsigned char *default_values;                /* row with default values */
180
 
  const CHARSET_INFO *table_charset; /* Default charset of string fields */
181
 
 
182
 
  MyBitmap all_set;
183
 
  /*
184
 
    Key which is used for looking-up table in table cache and in the list
185
 
    of thread's temporary tables. Has the form of:
186
 
    "database_name\0table_name\0" + optional part for temporary tables.
187
 
 
188
 
    Note that all three 'table_cache_key', 'db' and 'table_name' members
189
 
    must be set (and be non-zero) for tables in table cache. They also
190
 
    should correspond to each other.
191
 
    To ensure this one can use set_table_cache() methods.
192
 
  */
193
 
  LEX_STRING table_cache_key;
194
 
  LEX_STRING db;                        /* Pointer to db */
195
 
  LEX_STRING table_name;                /* Table name (for open) */
196
 
  LEX_STRING path;      /* Path to .frm file (from datadir) */
197
 
  LEX_STRING normalized_path;           /* unpack_filename(path) */
198
 
 
199
 
  uint32_t   block_size;                   /* create information */
200
 
  uint64_t   version;
201
 
  uint32_t   timestamp_offset;          /* Set to offset+1 of record */
202
 
  uint32_t   reclength;                 /* Recordlength */
203
 
  uint32_t   stored_rec_length;         /* Stored record length*/
204
 
  enum row_type row_type;               /* How rows are stored */
205
 
 
206
 
private:
207
 
  /* Max rows is a hint to HEAP during a create tmp table */
208
 
  uint64_t max_rows;
209
 
 
210
 
  drizzled::message::Table *table_proto;
211
 
public:
212
 
 
213
 
  /* This is only used in one location currently */
214
 
  inline void setTableProto(drizzled::message::Table *arg)
215
 
  {
216
 
    assert(table_proto == NULL);
217
 
    table_proto= arg;
218
 
  }
219
 
 
220
 
  inline bool hasComment()
221
 
  {
222
 
    return (table_proto) ?  table_proto->options().has_comment() : false; 
223
 
  }
224
 
 
225
 
  inline const char *getComment()
226
 
  {
227
 
    return (table_proto && table_proto->has_options()) ?  table_proto->options().comment().c_str() : NULL; 
228
 
  }
229
 
 
230
 
  inline uint32_t getCommentLength()
231
 
  {
232
 
    return (table_proto) ? table_proto->options().comment().length() : 0; 
233
 
  }
234
 
 
235
 
  inline bool hasKeyBlockSize()
236
 
  {
237
 
    return (table_proto) ? table_proto->options().has_key_block_size() : false;
238
 
  }
239
 
 
240
 
  inline uint32_t getKeyBlockSize()
241
 
  {
242
 
    return (table_proto) ? table_proto->options().key_block_size() : 0;
243
 
  }
244
 
 
245
 
  inline uint64_t getMaxRows()
246
 
  {
247
 
    return max_rows;
248
 
  }
249
 
 
250
 
  inline void setMaxRows(uint64_t arg)
251
 
  {
252
 
    max_rows= arg;
253
 
  }
254
 
 
255
 
  drizzled::plugin::StorageEngine *storage_engine;                      /* storage engine plugin */
256
 
  inline drizzled::plugin::StorageEngine *db_type() const       /* table_type for handler */
257
 
  {
258
 
    return storage_engine;
259
 
  }
260
 
  inline drizzled::plugin::StorageEngine *getEngine() const     /* table_type for handler */
261
 
  {
262
 
    return storage_engine;
263
 
  }
264
 
  enum tmp_table_type tmp_table;
265
 
 
266
 
  uint32_t ref_count;       /* How many Table objects uses this */
267
 
  uint32_t null_bytes;
268
 
  uint32_t last_null_bit_pos;
269
 
  uint32_t fields;                              /* Number of fields */
270
 
  uint32_t rec_buff_length;                 /* Size of table->record[] buffer */
271
 
  uint32_t keys, key_parts;
272
 
  uint32_t max_key_length, max_unique_length, total_key_length;
273
 
  uint32_t uniques;                         /* Number of UNIQUE index */
274
 
  uint32_t null_fields;                 /* number of null fields */
275
 
  uint32_t blob_fields;                 /* number of blob fields */
276
 
  uint32_t timestamp_field_offset;              /* Field number for timestamp field */
277
 
  uint32_t varchar_fields;                  /* number of varchar fields */
278
 
  uint32_t db_create_options;           /* Create options from database */
279
 
  uint32_t db_options_in_use;           /* Options in use */
280
 
  uint32_t db_record_offset;            /* if HA_REC_IN_SEQ */
281
 
  uint32_t rowid_field_offset;          /* Field_nr +1 to rowid field */
282
 
  /**
283
 
   * @TODO 
284
 
   *
285
 
   * Currently the replication services component uses
286
 
   * the primary_key member to determine which field is the table's
287
 
   * primary key.  However, as it exists, because this member is scalar, it
288
 
   * only supports a single-column primary key. Is there a better way
289
 
   * to ask for the fields which are in a primary key?
290
 
   */
291
 
  uint32_t primary_key;
292
 
  /* Index of auto-updated TIMESTAMP field in field array */
293
 
  uint32_t next_number_index;               /* autoincrement key number */
294
 
  uint32_t next_number_key_offset;          /* autoinc keypart offset in a key */
295
 
  uint32_t next_number_keypart;             /* autoinc keypart number in a key */
296
 
  uint32_t error, open_errno, errarg;       /* error from open_table_def() */
297
 
  uint32_t column_bitmap_size;
298
 
 
299
 
  uint8_t blob_ptr_size;                        /* 4 or 8 */
300
 
  bool db_low_byte_first;               /* Portable row format */
301
 
  bool name_lock;
302
 
  bool replace_with_name_lock;
303
 
  bool waiting_on_cond;                 /* Protection against free */
304
 
 
305
 
  /*
306
 
    Set of keys in use, implemented as a Bitmap.
307
 
    Excludes keys disabled by ALTER Table ... DISABLE KEYS.
308
 
  */
309
 
  key_map keys_in_use;
310
 
  key_map keys_for_keyread;
311
 
 
312
 
  /*
313
 
    Set share's table cache key and update its db and table name appropriately.
314
 
 
315
 
    SYNOPSIS
316
 
    set_table_cache_key()
317
 
    key_buff    Buffer with already built table cache key to be
318
 
    referenced from share.
319
 
    key_length  Key length.
320
 
 
321
 
    NOTES
322
 
    Since 'key_buff' buffer will be referenced from share it should has same
323
 
    life-time as share itself.
324
 
    This method automatically ensures that TableShare::table_name/db have
325
 
    appropriate values by using table cache key as their source.
326
 
  */
327
 
 
328
 
  void set_table_cache_key(char *key_buff, uint32_t key_length)
329
 
  {
330
 
    table_cache_key.str= key_buff;
331
 
    table_cache_key.length= key_length;
332
 
    /*
333
 
      Let us use the fact that the key is "db/0/table_name/0" + optional
334
 
      part for temporary tables.
335
 
    */
336
 
    db.str=            table_cache_key.str;
337
 
    db.length=         strlen(db.str);
338
 
    table_name.str=    db.str + db.length + 1;
339
 
    table_name.length= strlen(table_name.str);
340
 
  }
341
 
 
342
 
 
343
 
  /*
344
 
    Set share's table cache key and update its db and table name appropriately.
345
 
 
346
 
    SYNOPSIS
347
 
    set_table_cache_key()
348
 
    key_buff    Buffer to be used as storage for table cache key
349
 
    (should be at least key_length bytes).
350
 
    key         Value for table cache key.
351
 
    key_length  Key length.
352
 
 
353
 
    NOTE
354
 
    Since 'key_buff' buffer will be used as storage for table cache key
355
 
    it should has same life-time as share itself.
356
 
  */
357
 
 
358
 
  void set_table_cache_key(char *key_buff, const char *key, uint32_t key_length)
359
 
  {
360
 
    memcpy(key_buff, key, key_length);
361
 
    set_table_cache_key(key_buff, key_length);
362
 
  }
363
 
 
364
 
  inline bool honor_global_locks()
365
 
  {
366
 
    return (table_category == TABLE_CATEGORY_USER);
367
 
  }
368
 
 
369
 
 
370
 
  /*
371
 
    Initialize share for temporary tables
372
 
 
373
 
    SYNOPSIS
374
 
    init()
375
 
    share       Share to fill
376
 
    key         Table_cache_key, as generated from create_table_def_key.
377
 
    must start with db name.
378
 
    key_length  Length of key
379
 
    table_name  Table name
380
 
    path        Path to file (possible in lower case) without .frm
381
 
 
382
 
    NOTES
383
 
    This is different from alloc_table_share() because temporary tables
384
 
    don't have to be shared between threads or put into the table def
385
 
    cache, so we can do some things notable simpler and faster
386
 
 
387
 
    If table is not put in session->temporary_tables (happens only when
388
 
    one uses OPEN TEMPORARY) then one can specify 'db' as key and
389
 
    use key_length= 0 as neither table_cache_key or key_length will be used).
390
 
  */
391
 
 
392
 
  void init()
393
 
  {
394
 
    init("", 0, "", "");
395
 
  }
396
 
 
397
 
  void init(const char *new_table_name,
398
 
            const char *new_path)
399
 
  {
400
 
    init("", 0, new_table_name, new_path);
401
 
  }
402
 
 
403
 
  void init(const char *key,
404
 
            uint32_t key_length, const char *new_table_name,
405
 
            const char *new_path)
406
 
  {
407
 
    memset(this, 0, sizeof(TableShare));
408
 
    drizzled::memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
409
 
    table_category=         TABLE_CATEGORY_TEMPORARY;
410
 
    tmp_table=              INTERNAL_TMP_TABLE;
411
 
    db.str=                 (char*) key;
412
 
    db.length=           strlen(key);
413
 
    table_cache_key.str=    (char*) key;
414
 
    table_cache_key.length= key_length;
415
 
    table_name.str=         (char*) new_table_name;
416
 
    table_name.length=      strlen(new_table_name);
417
 
    path.str=               (char*) new_path;
418
 
    normalized_path.str=    (char*) new_path;
419
 
    path.length= normalized_path.length= strlen(new_path);
420
 
 
421
 
    return;
422
 
  }
423
 
 
424
 
  /*
425
 
    Free table share and memory used by it
426
 
 
427
 
    SYNOPSIS
428
 
    free_table_share()
429
 
    share               Table share
430
 
 
431
 
    NOTES
432
 
    share->mutex must be locked when we come here if it's not a temp table
433
 
  */
434
 
 
435
 
  void free_table_share()
436
 
  {
437
 
    drizzled::memory::Root new_mem_root;
438
 
    assert(ref_count == 0);
439
 
 
440
 
    /*
441
 
      If someone is waiting for this to be deleted, inform it about this.
442
 
      Don't do a delete until we know that no one is refering to this anymore.
443
 
    */
444
 
    if (tmp_table == NO_TMP_TABLE)
445
 
    {
446
 
      /* share->mutex is locked in release_table_share() */
447
 
      while (waiting_on_cond)
448
 
      {
449
 
        pthread_cond_broadcast(&cond);
450
 
        pthread_cond_wait(&cond, &mutex);
451
 
      }
452
 
      /* No thread refers to this anymore */
453
 
      pthread_mutex_unlock(&mutex);
454
 
      pthread_mutex_destroy(&mutex);
455
 
      pthread_cond_destroy(&cond);
456
 
    }
457
 
    hash_free(&name_hash);
458
 
 
459
 
    storage_engine= NULL;
460
 
 
461
 
    delete table_proto;
462
 
    table_proto= NULL;
463
 
 
464
 
    /* We must copy mem_root from share because share is allocated through it */
465
 
    memcpy(&new_mem_root, &mem_root, sizeof(new_mem_root));
466
 
    free_root(&new_mem_root, MYF(0));                 // Free's share
467
 
  }
468
 
 
469
 
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
470
 
 
471
 
 
472
 
 
473
 
  /*
474
 
    Create a table cache key
475
 
 
476
 
    SYNOPSIS
477
 
    createKey()
478
 
    key                 Create key here (must be of size MAX_DBKEY_LENGTH)
479
 
    table_list          Table definition
480
 
 
481
 
    IMPLEMENTATION
482
 
    The table cache_key is created from:
483
 
    db_name + \0
484
 
    table_name + \0
485
 
 
486
 
    if the table is a tmp table, we add the following to make each tmp table
487
 
    unique on the slave:
488
 
 
489
 
    4 bytes for master thread id
490
 
    4 bytes pseudo thread id
491
 
 
492
 
    RETURN
493
 
    Length of key
494
 
  */
495
 
 
496
 
  static inline uint32_t createKey(char *key, std::string& db_arg,
497
 
                                   std::string& table_name_arg)
498
 
  {
499
 
    return createKey(key, db_arg.c_str(), table_name_arg.c_str());
500
 
  }
501
 
 
502
 
  static inline uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
503
 
  {
504
 
    uint32_t key_length;
505
 
    char *key_pos= key;
506
 
 
507
 
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
508
 
    key_pos= strcpy(key_pos+1, table_name_arg) +
509
 
      strlen(table_name_arg);
510
 
    key_length= (uint32_t)(key_pos-key)+1;
511
 
 
512
 
    return key_length;
513
 
  }
514
 
 
515
 
  static void cacheStart(void);
516
 
  static void cacheStop(void);
517
 
  static void release(TableShare *share);
518
 
  static void release(const char *key, uint32_t key_length);
519
 
  static TableShare *getShare(const char *db, const char *table_name);
520
 
  static TableShare *getShare(Session *session, 
521
 
                              TableList *table_list, char *key,
522
 
                              uint32_t key_length, uint32_t, int *error);
523
 
};
524
 
 
525
 
#endif /* DRIZZLED_TABLE_SHARE_H */