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