~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/set_var.cc

  • Committer: Brian Aker
  • Date: 2009-08-03 15:48:31 UTC
  • mfrom: (1106.4.3 no-multi-keycache)
  • mto: This revision was merged to the branch mainline in revision 1108.
  • Revision ID: brian@gaz-20090803154831-fngxv9aby0rjkyn9
Merge in removal of multi-keycache code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
using namespace std;
72
72
 
73
73
extern const CHARSET_INFO *character_set_filesystem;
74
 
extern I_List<NAMED_LIST> key_caches;
75
74
extern size_t my_thread_stack_size;
76
75
 
77
76
static DYNAMIC_ARRAY fixed_show_vars;
110
109
static bool get_unsigned64(Session *session, set_var *var);
111
110
bool throw_bounds_warning(Session *session, bool fixed, bool unsignd,
112
111
                          const std::string &name, int64_t val);
113
 
static KEY_CACHE *create_key_cache(const char *name, uint32_t length);
114
112
static unsigned char *get_error_count(Session *session);
115
113
static unsigned char *get_warning_count(Session *session);
116
114
static unsigned char *get_tmpdir(Session *session);
1299
1297
}
1300
1298
 
1301
1299
 
1302
 
LEX_STRING default_key_cache_base= {(char *) "default", 7 };
1303
 
 
1304
 
static KEY_CACHE zero_key_cache;
1305
 
 
1306
 
KEY_CACHE *get_key_cache(const LEX_STRING *cache_name)
1307
 
{
1308
 
  safe_mutex_assert_owner(&LOCK_global_system_variables);
1309
 
  if (!cache_name || ! cache_name->length)
1310
 
    cache_name= &default_key_cache_base;
1311
 
  return ((KEY_CACHE*) find_named(&key_caches,
1312
 
                                  cache_name->str, cache_name->length, 0));
1313
 
}
1314
 
 
1315
 
 
1316
1300
unsigned char *sys_var_key_cache_param::value_ptr(Session *, enum_var_type,
1317
 
                                                  const LEX_STRING *base)
1318
 
{
1319
 
  KEY_CACHE *key_cache= get_key_cache(base);
1320
 
  if (!key_cache)
1321
 
    key_cache= &zero_key_cache;
1322
 
  return (unsigned char*) key_cache + offset ;
1323
 
}
 
1301
                                                  const LEX_STRING *)
 
1302
{
 
1303
  return (unsigned char*) dflt_key_cache + offset ;
 
1304
}
 
1305
 
 
1306
/**
 
1307
  Resize key cache.
 
1308
*/
 
1309
static int resize_key_cache_with_lock(KEY_CACHE *key_cache)
 
1310
{
 
1311
  assert(key_cache->key_cache_inited);
 
1312
 
 
1313
  pthread_mutex_lock(&LOCK_global_system_variables);
 
1314
  long tmp_buff_size= (long) key_cache->param_buff_size;
 
1315
  long tmp_block_size= (long) key_cache->param_block_size;
 
1316
  uint32_t division_limit= key_cache->param_division_limit;
 
1317
  uint32_t age_threshold=  key_cache->param_age_threshold;
 
1318
  pthread_mutex_unlock(&LOCK_global_system_variables);
 
1319
 
 
1320
  return(!resize_key_cache(key_cache, tmp_block_size,
 
1321
                           tmp_buff_size,
 
1322
                           division_limit, age_threshold));
 
1323
}
 
1324
 
1324
1325
 
1325
1326
 
1326
1327
bool sys_var_key_buffer_size::update(Session *session, set_var *var)
1327
1328
{
1328
1329
  uint64_t tmp= var->save_result.uint64_t_value;
1329
 
  LEX_STRING *base_name= &var->base;
1330
1330
  KEY_CACHE *key_cache;
1331
1331
  bool error= 0;
1332
1332
 
1333
 
  /* If no basename, assume it's for the key cache named 'default' */
1334
 
  if (!base_name->length)
1335
 
    base_name= &default_key_cache_base;
1336
 
 
1337
1333
  pthread_mutex_lock(&LOCK_global_system_variables);
1338
 
  key_cache= get_key_cache(base_name);
1339
 
 
1340
 
  if (!key_cache)
1341
 
  {
1342
 
    /* Key cache didn't exists */
1343
 
    if (!tmp)                                   // Tried to delete cache
1344
 
      goto end;                                 // Ok, nothing to do
1345
 
    if (!(key_cache= create_key_cache(base_name->str, base_name->length)))
1346
 
    {
1347
 
      error= 1;
1348
 
      goto end;
1349
 
    }
1350
 
  }
 
1334
  key_cache= dflt_key_cache;
1351
1335
 
1352
1336
  /*
1353
1337
    Abort if some other thread is changing the key cache
1357
1341
  if (key_cache->in_init)
1358
1342
    goto end;
1359
1343
 
1360
 
  if (!tmp)                                     // Zero size means delete
 
1344
  if (tmp == 0)                                 // Zero size means delete
1361
1345
  {
1362
 
    if (key_cache == dflt_key_cache)
1363
 
    {
1364
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1365
 
                          ER_WARN_CANT_DROP_DEFAULT_KEYCACHE,
1366
 
                          ER(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE));
1367
 
      goto end;                                 // Ignore default key cache
1368
 
    }
1369
 
 
1370
 
    if (key_cache->key_cache_inited)            // If initied
1371
 
    {
1372
 
      /*
1373
 
        Move tables using this key cache to the default key cache
1374
 
        and clear the old key cache.
1375
 
      */
1376
 
      NAMED_LIST *list;
1377
 
      key_cache= (KEY_CACHE *) find_named(&key_caches, base_name->str,
1378
 
                                              base_name->length, &list);
1379
 
      key_cache->in_init= 1;
1380
 
      pthread_mutex_unlock(&LOCK_global_system_variables);
1381
 
      error= reassign_keycache_tables(session, key_cache, dflt_key_cache);
1382
 
      pthread_mutex_lock(&LOCK_global_system_variables);
1383
 
      key_cache->in_init= 0;
1384
 
    }
1385
 
    /*
1386
 
      We don't delete the key cache as some running threads my still be
1387
 
      in the key cache code with a pointer to the deleted (empty) key cache
1388
 
    */
1389
 
    goto end;
 
1346
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1347
                        ER_WARN_CANT_DROP_DEFAULT_KEYCACHE,
 
1348
                        ER(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE));
 
1349
    goto end;                                   // Ignore default key cache
1390
1350
  }
1391
1351
 
1392
1352
  key_cache->param_buff_size=
1396
1356
  key_cache->in_init= 1;
1397
1357
  pthread_mutex_unlock(&LOCK_global_system_variables);
1398
1358
 
1399
 
  if (!key_cache->key_cache_inited)
1400
 
    error= (bool) (ha_init_key_cache("", key_cache));
1401
 
  else
1402
 
    error= (bool)(ha_resize_key_cache(key_cache));
 
1359
  error= (bool)(resize_key_cache_with_lock(key_cache));
1403
1360
 
1404
1361
  pthread_mutex_lock(&LOCK_global_system_variables);
1405
1362
  key_cache->in_init= 0;
1419
1376
bool sys_var_key_cache_uint32_t::update(Session *session, set_var *var)
1420
1377
{
1421
1378
  uint64_t tmp= (uint64_t) var->value->val_int();
1422
 
  LEX_STRING *base_name= &var->base;
1423
1379
  bool error= 0;
1424
1380
 
1425
 
  if (!base_name->length)
1426
 
    base_name= &default_key_cache_base;
1427
 
 
1428
1381
  pthread_mutex_lock(&LOCK_global_system_variables);
1429
 
  KEY_CACHE *key_cache= get_key_cache(base_name);
1430
 
 
1431
 
  if (!key_cache && !(key_cache= create_key_cache(base_name->str,
1432
 
                                                  base_name->length)))
1433
 
  {
1434
 
    error= 1;
1435
 
    goto end;
1436
 
  }
1437
1382
 
1438
1383
  /*
1439
1384
    Abort if some other thread is changing the key cache
1440
1385
    TODO: This should be changed so that we wait until the previous
1441
1386
    assignment is done and then do the new assign
1442
1387
  */
1443
 
  if (key_cache->in_init)
 
1388
  if (dflt_key_cache->in_init)
1444
1389
    goto end;
1445
1390
 
1446
 
  *((uint32_t*) (((char*) key_cache) + offset))=
 
1391
  *((uint32_t*) (((char*) dflt_key_cache) + offset))=
1447
1392
    (uint32_t) fix_unsigned(session, tmp, option_limits);
1448
1393
 
1449
1394
  /*
1450
1395
    Don't create a new key cache if it didn't exist
1451
1396
    (key_caches are created only when the user sets block_size)
1452
1397
  */
1453
 
  key_cache->in_init= 1;
 
1398
  dflt_key_cache->in_init= 1;
1454
1399
 
1455
1400
  pthread_mutex_unlock(&LOCK_global_system_variables);
1456
1401
 
1457
 
  error= (bool) (ha_resize_key_cache(key_cache));
 
1402
  error= (bool) (resize_key_cache_with_lock(dflt_key_cache));
1458
1403
 
1459
1404
  pthread_mutex_lock(&LOCK_global_system_variables);
1460
 
  key_cache->in_init= 0;
 
1405
  dflt_key_cache->in_init= 0;
1461
1406
 
1462
1407
end:
1463
1408
  pthread_mutex_unlock(&LOCK_global_system_variables);
1929
1874
}
1930
1875
 
1931
1876
 
1932
 
NAMED_LIST::NAMED_LIST(I_List<NAMED_LIST> *links, const char *name_arg,
1933
 
                       uint32_t name_length_arg, unsigned char* data_arg)
1934
 
    :data(data_arg)
1935
 
{
1936
 
  name.assign(name_arg, name_length_arg);
1937
 
  links->push_back(this);
1938
 
}
1939
 
 
1940
 
 
1941
 
bool NAMED_LIST::cmp(const char *name_cmp, uint32_t length)
1942
 
{
1943
 
  return length == name.length() && !name.compare(name_cmp);
1944
 
}
1945
 
 
1946
 
 
1947
1877
/*
1948
1878
  Initialize the system variables
1949
1879
 
2320
2250
 
2321
2251
 
2322
2252
/****************************************************************************
2323
 
  Named list handling
2324
 
****************************************************************************/
2325
 
 
2326
 
unsigned char* find_named(I_List<NAMED_LIST> *list, const char *name, uint32_t length,
2327
 
                NAMED_LIST **found)
2328
 
{
2329
 
  I_List_iterator<NAMED_LIST> it(*list);
2330
 
  NAMED_LIST *element;
2331
 
  while ((element= it++))
2332
 
  {
2333
 
    if (element->cmp(name, length))
2334
 
    {
2335
 
      if (found)
2336
 
        *found= element;
2337
 
      return element->data;
2338
 
    }
2339
 
  }
2340
 
  return 0;
2341
 
}
2342
 
 
2343
 
 
2344
 
void delete_elements(I_List<NAMED_LIST> *list,
2345
 
                     void (*free_element)(const char *name, unsigned char*))
2346
 
{
2347
 
  NAMED_LIST *element;
2348
 
  while ((element= list->get()))
2349
 
  {
2350
 
    (*free_element)(element->name.c_str(), element->data);
2351
 
    delete element;
2352
 
  }
2353
 
  return;
2354
 
}
2355
 
 
2356
 
 
2357
 
/* Key cache functions */
2358
 
 
2359
 
static KEY_CACHE *create_key_cache(const char *name, uint32_t length)
2360
 
{
2361
 
  KEY_CACHE *key_cache;
2362
 
 
2363
 
  if ((key_cache= (KEY_CACHE*) malloc(sizeof(KEY_CACHE))))
2364
 
  {
2365
 
    memset(key_cache, 0, sizeof(KEY_CACHE));
2366
 
    if (!new NAMED_LIST(&key_caches, name, length, (unsigned char*) key_cache))
2367
 
    {
2368
 
      free((char*) key_cache);
2369
 
      key_cache= 0;
2370
 
    }
2371
 
    else
2372
 
    {
2373
 
      /*
2374
 
        Set default values for a key cache
2375
 
        The values in dflt_key_cache_var is set by my_getopt() at startup
2376
 
 
2377
 
        We don't set 'buff_size' as this is used to enable the key cache
2378
 
      */
2379
 
      key_cache->param_block_size=     dflt_key_cache_var.param_block_size;
2380
 
      key_cache->param_division_limit= dflt_key_cache_var.param_division_limit;
2381
 
      key_cache->param_age_threshold=  dflt_key_cache_var.param_age_threshold;
2382
 
    }
2383
 
  }
2384
 
  return(key_cache);
2385
 
}
2386
 
 
2387
 
 
2388
 
KEY_CACHE *get_or_create_key_cache(const char *name, uint32_t length)
2389
 
{
2390
 
  LEX_STRING key_cache_name;
2391
 
  KEY_CACHE *key_cache;
2392
 
 
2393
 
  key_cache_name.str= (char *) name;
2394
 
  key_cache_name.length= length;
2395
 
  pthread_mutex_lock(&LOCK_global_system_variables);
2396
 
  if (!(key_cache= get_key_cache(&key_cache_name)))
2397
 
    key_cache= create_key_cache(name, length);
2398
 
  pthread_mutex_unlock(&LOCK_global_system_variables);
2399
 
  return key_cache;
2400
 
}
2401
 
 
2402
 
 
2403
 
void free_key_cache(const char *, KEY_CACHE *key_cache)
2404
 
{
2405
 
  ha_end_key_cache(key_cache);
2406
 
  free((char*) key_cache);
2407
 
}
2408
 
 
2409
 
 
2410
 
bool process_key_caches(process_key_cache_t func)
2411
 
{
2412
 
  I_List_iterator<NAMED_LIST> it(key_caches);
2413
 
  NAMED_LIST *element;
2414
 
 
2415
 
  while ((element= it++))
2416
 
  {
2417
 
    KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
2418
 
    func(element->name.c_str(), key_cache);
2419
 
  }
2420
 
  return 0;
2421
 
}
2422
 
 
2423
 
/****************************************************************************
2424
2253
  Used templates
2425
2254
****************************************************************************/
2426
2255