~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2009-07-16 22:37:01 UTC
  • mto: This revision was merged to the branch mainline in revision 1100.
  • Revision ID: brian@gaz-20090716223701-vbbbo8dmgd2ljqqo
Refactor TableShare has to be behind class.

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
*/
58
58
Table *unused_tables;                           /* Used by mysql_test */
59
59
HASH open_cache;                                /* Used by mysql_test */
60
 
static HASH table_def_cache;
61
 
static pthread_mutex_t LOCK_table_share;
62
 
static bool table_def_inited= 0;
63
 
 
64
60
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
65
61
                             const char *alias,
66
62
                             char *cache_key, uint32_t cache_key_length);
97
93
 
98
94
void table_cache_free(void)
99
95
{
100
 
  if (table_def_inited)
101
 
  {
102
 
    close_cached_tables(NULL, NULL, false, false);
103
 
    if (!open_cache.records)                    // Safety first
104
 
      hash_free(&open_cache);
105
 
  }
 
96
  close_cached_tables(NULL, NULL, false, false);
 
97
  if (!open_cache.records)                      // Safety first
 
98
    hash_free(&open_cache);
106
99
}
107
100
 
108
101
uint32_t cached_open_tables(void)
111
104
}
112
105
 
113
106
 
114
 
 
115
 
/*****************************************************************************
116
 
  Functions to handle table definition cach (TableShare)
117
 
 *****************************************************************************/
118
 
 
119
 
unsigned char *table_def_key(const unsigned char *record,
120
 
                             size_t *length,
121
 
                             bool )
122
 
{
123
 
  TableShare *entry=(TableShare*) record;
124
 
  *length= entry->table_cache_key.length;
125
 
  return (unsigned char*) entry->table_cache_key.str;
126
 
}
127
 
 
128
 
 
129
 
static void table_def_free_entry(TableShare *share)
130
 
{
131
 
  share->free_table_share();
132
 
}
133
 
 
134
 
 
135
 
bool table_def_init(void)
136
 
{
137
 
  table_def_inited= 1;
138
 
  pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
139
 
 
140
 
  return hash_init(&table_def_cache, &my_charset_bin, (size_t)table_def_size,
141
 
                   0, 0, table_def_key,
142
 
                   (hash_free_key) table_def_free_entry, 0);
143
 
}
144
 
 
145
 
 
146
 
void table_def_free(void)
147
 
{
148
 
  if (table_def_inited)
149
 
  {
150
 
    table_def_inited= 0;
151
 
    pthread_mutex_destroy(&LOCK_table_share);
152
 
    hash_free(&table_def_cache);
153
 
  }
154
 
}
155
 
 
156
 
 
157
 
uint32_t cached_table_definitions(void)
158
 
{
159
 
  return table_def_cache.records;
160
 
}
161
 
 
162
 
 
163
 
/*
164
 
  Get TableShare for a table.
165
 
 
166
 
  get_table_share()
167
 
  session                       Thread handle
168
 
  table_list            Table that should be opened
169
 
  key                   Table cache key
170
 
  key_length            Length of key
171
 
  error                 out: Error code from open_table_def()
172
 
 
173
 
  IMPLEMENTATION
174
 
  Get a table definition from the table definition cache.
175
 
  If it doesn't exist, create a new from the table definition file.
176
 
 
177
 
  NOTES
178
 
  We must have wrlock on LOCK_open when we come here
179
 
  (To be changed later)
180
 
 
181
 
  RETURN
182
 
  0  Error
183
 
#  Share for table
184
 
*/
185
 
 
186
 
TableShare *get_table_share(Session *session, TableList *table_list, char *key,
187
 
                            uint32_t key_length, uint32_t, int *error)
188
 
{
189
 
  TableShare *share;
190
 
 
191
 
  *error= 0;
192
 
 
193
 
  /* Read table definition from cache */
194
 
  if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
195
 
                                        key_length)))
196
 
    goto found;
197
 
 
198
 
  if (!(share= alloc_table_share(table_list, key, key_length)))
199
 
  {
200
 
    return NULL;
201
 
  }
202
 
 
203
 
  /*
204
 
    Lock mutex to be able to read table definition from file without
205
 
    conflicts
206
 
  */
207
 
  (void) pthread_mutex_lock(&share->mutex);
208
 
 
209
 
  if (my_hash_insert(&table_def_cache, (unsigned char*) share))
210
 
  {
211
 
    share->free_table_share();
212
 
    return NULL;                                // return error
213
 
  }
214
 
  if (open_table_def(session, share))
215
 
  {
216
 
    *error= share->error;
217
 
    (void) hash_delete(&table_def_cache, (unsigned char*) share);
218
 
    return NULL;
219
 
  }
220
 
  share->ref_count++;                           // Mark in use
221
 
  (void) pthread_mutex_unlock(&share->mutex);
222
 
  return(share);
223
 
 
224
 
found:
225
 
  /*
226
 
    We found an existing table definition. Return it if we didn't get
227
 
    an error when reading the table definition from file.
228
 
  */
229
 
 
230
 
  /* We must do a lock to ensure that the structure is initialized */
231
 
  (void) pthread_mutex_lock(&share->mutex);
232
 
  if (share->error)
233
 
  {
234
 
    /* Table definition contained an error */
235
 
    share->open_table_error(share->error, share->open_errno, share->errarg);
236
 
    (void) pthread_mutex_unlock(&share->mutex);
237
 
 
238
 
    return NULL;
239
 
  }
240
 
 
241
 
  share->ref_count++;
242
 
  (void) pthread_mutex_unlock(&share->mutex);
243
 
 
244
 
  return share;
245
 
}
246
 
 
247
 
 
248
 
/*
249
 
  Mark that we are not using table share anymore.
250
 
 
251
 
  SYNOPSIS
252
 
  release_table_share()
253
 
  share         Table share
254
 
 
255
 
  IMPLEMENTATION
256
 
  If ref_count goes to zero and (we have done a refresh or if we have
257
 
  already too many open table shares) then delete the definition.
258
 
 
259
 
  If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
260
 
  that the table is deleted or the thread is killed.
261
 
*/
262
 
 
263
 
void release_table_share(TableShare *share)
264
 
{
265
 
  bool to_be_deleted= false;
266
 
 
267
 
  safe_mutex_assert_owner(&LOCK_open);
268
 
 
269
 
  pthread_mutex_lock(&share->mutex);
270
 
  if (!--share->ref_count)
271
 
    to_be_deleted= true;
272
 
 
273
 
  if (to_be_deleted)
274
 
  {
275
 
    hash_delete(&table_def_cache, (unsigned char*) share);
276
 
    return;
277
 
  }
278
 
  pthread_mutex_unlock(&share->mutex);
279
 
}
280
 
 
281
 
 
282
 
/*
283
 
  Check if table definition exits in cache
284
 
 
285
 
  SYNOPSIS
286
 
  get_cached_table_share()
287
 
  db                    Database name
288
 
  table_name            Table name
289
 
 
290
 
  RETURN
291
 
  0  Not cached
292
 
#  TableShare for table
293
 
*/
294
 
 
295
 
TableShare *get_cached_table_share(const char *db, const char *table_name)
296
 
{
297
 
  char key[NAME_LEN*2+2];
298
 
  uint32_t key_length;
299
 
  safe_mutex_assert_owner(&LOCK_open);
300
 
 
301
 
  key_length= TableShare::createKey(key, db, table_name);
302
 
 
303
 
  return (TableShare*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
304
 
}
305
 
 
306
 
 
307
107
/*
308
108
  Close file handle, but leave the table in the table cache
309
109
 
348
148
 
349
149
  table->file->close();
350
150
  table->db_stat= 0;                            // Mark file closed
351
 
  release_table_share(table->s);
 
151
  TableShare::release(table->s);
352
152
  table->s= share;
353
153
  table->file->change_table_ptr(table, table->s);
354
154
}
2279
2079
 
2280
2080
  safe_mutex_assert_owner(&LOCK_open);
2281
2081
retry:
2282
 
  if (!(share= get_table_share(session, table_list, cache_key,
2283
 
                                           cache_key_length,
2284
 
                                           table_list->i_s_requested_object,
2285
 
                                           &error)))
 
2082
  if (!(share= TableShare::getShare(session, table_list, cache_key,
 
2083
                                    cache_key_length,
 
2084
                                    table_list->i_s_requested_object,
 
2085
                                    &error)))
2286
2086
    return 1;
2287
2087
 
2288
2088
  while ((error= open_table_from_share(session, share, alias,
2321
2121
      if (share->ref_count != 1)
2322
2122
        goto err;
2323
2123
      /* Free share and wait until it's released by all threads */
2324
 
      release_table_share(share);
 
2124
      TableShare::release(share);
 
2125
 
2325
2126
      if (!session->killed)
2326
2127
      {
2327
2128
        drizzle_reset_errors(session, 1);         // Clear warnings
2411
2212
  return 0;
2412
2213
 
2413
2214
err:
2414
 
  release_table_share(share);
 
2215
  TableShare::release(share);
2415
2216
 
2416
2217
  return 1;
2417
2218
}
5084
4885
  char *key_pos= key;
5085
4886
  uint32_t key_length;
5086
4887
  Table *table;
5087
 
  TableShare *share;
5088
4888
  bool result= false; 
5089
4889
  bool signalled= false;
5090
4890
 
5147
4947
      hash_delete(&open_cache,(unsigned char*) unused_tables);
5148
4948
 
5149
4949
    /* Remove table from table definition cache if it's not in use */
5150
 
    if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
5151
 
                                          key_length)))
5152
 
    {
5153
 
      share->version= 0;                          // Mark for delete
5154
 
      if (share->ref_count == 0)
5155
 
      {
5156
 
        pthread_mutex_lock(&share->mutex);
5157
 
        hash_delete(&table_def_cache, (unsigned char*) share);
5158
 
      }
5159
 
    }
 
4950
    TableShare::release(key, key_length);
5160
4951
 
5161
4952
    if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
5162
4953
    {