~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Monty Taylor
  • Date: 2009-03-06 03:33:24 UTC
  • mfrom: (916.1.2 merge)
  • Revision ID: mordred@inaugust.com-20090306033324-dcedf80g9qzywbvu
Merged Brian's merge... re-rotate the tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
#include <string>
46
46
#include <iostream>
47
47
#include <sstream>
 
48
#include <vector>
 
49
#include <algorithm>
48
50
 
49
51
using namespace std;
50
52
 
1222
1224
  Status functions
1223
1225
*****************************************************************************/
1224
1226
 
1225
 
static DYNAMIC_ARRAY all_status_vars;
 
1227
static vector<SHOW_VAR *> all_status_vars;
1226
1228
static bool status_vars_inited= 0;
1227
1229
int show_var_cmp(const void *var1, const void *var2)
1228
1230
{
1229
1231
  return strcmp(((SHOW_VAR*)var1)->name, ((SHOW_VAR*)var2)->name);
1230
1232
}
1231
1233
 
1232
 
/*
1233
 
  deletes all the SHOW_UNDEF elements from the array and calls
1234
 
  delete_dynamic() if it's completely empty.
1235
 
*/
1236
 
static void shrink_var_array(DYNAMIC_ARRAY *array)
 
1234
class show_var_cmp_functor
1237
1235
{
1238
 
  uint32_t a,b;
1239
 
  SHOW_VAR *all= dynamic_element(array, 0, SHOW_VAR *);
 
1236
  public:
 
1237
  show_var_cmp_functor() { }
 
1238
  inline bool operator()(const SHOW_VAR *var1, const SHOW_VAR *var2) const
 
1239
  {
 
1240
    int val= strcmp(var1->name, var2->name);
 
1241
    return (val < 0);
 
1242
  }
 
1243
};
1240
1244
 
1241
 
  for (a= b= 0; b < array->elements; b++)
1242
 
    if (all[b].type != SHOW_UNDEF)
1243
 
      all[a++]= all[b];
1244
 
  if (a)
 
1245
class show_var_remove_if
 
1246
{
 
1247
  public:
 
1248
  show_var_remove_if() { }
 
1249
  inline bool operator()(const SHOW_VAR *curr) const
1245
1250
  {
1246
 
    memset(all+a, 0, sizeof(SHOW_VAR)); // writing NULL-element to the end
1247
 
    array->elements= a;
 
1251
    return (curr->type == SHOW_UNDEF);
1248
1252
  }
1249
 
  else // array is completely empty - delete it
1250
 
    delete_dynamic(array);
1251
 
}
 
1253
};
1252
1254
 
1253
1255
/*
1254
1256
  Adds an array of SHOW_VAR entries to the output of SHOW STATUS
1266
1268
    As a special optimization, if add_status_vars() is called before
1267
1269
    init_status_vars(), it assumes "startup mode" - neither concurrent access
1268
1270
    to the array nor SHOW STATUS are possible (thus it skips locks and qsort)
1269
 
 
1270
 
    The last entry of the all_status_vars[] should always be {0,0,SHOW_UNDEF}
1271
1271
*/
1272
1272
int add_status_vars(SHOW_VAR *list)
1273
1273
{
1274
1274
  int res= 0;
1275
1275
  if (status_vars_inited)
1276
1276
    pthread_mutex_lock(&LOCK_status);
1277
 
  if (!all_status_vars.buffer && // array is not allocated yet - do it now
1278
 
      my_init_dynamic_array(&all_status_vars, sizeof(SHOW_VAR), 200, 20))
1279
 
  {
1280
 
    res= 1;
1281
 
    goto err;
1282
 
  }
1283
1277
  while (list->name)
1284
 
    res|= insert_dynamic(&all_status_vars, (unsigned char*)list++);
1285
 
  res|= insert_dynamic(&all_status_vars, (unsigned char*)list); // appending NULL-element
1286
 
  all_status_vars.elements--; // but next insert_dynamic should overwite it
 
1278
    all_status_vars.insert(all_status_vars.begin(), list++);
1287
1279
  if (status_vars_inited)
1288
 
    sort_dynamic(&all_status_vars, show_var_cmp);
1289
 
err:
 
1280
    sort(all_status_vars.begin(), all_status_vars.end(),
 
1281
         show_var_cmp_functor());
1290
1282
  if (status_vars_inited)
1291
1283
    pthread_mutex_unlock(&LOCK_status);
1292
1284
  return res;
1302
1294
*/
1303
1295
void init_status_vars()
1304
1296
{
1305
 
  status_vars_inited=1;
1306
 
  sort_dynamic(&all_status_vars, show_var_cmp);
 
1297
  status_vars_inited= 1;
 
1298
  sort(all_status_vars.begin(), all_status_vars.end(),
 
1299
       show_var_cmp_functor());
1307
1300
}
1308
1301
 
1309
1302
void reset_status_vars()
1310
1303
{
1311
 
  SHOW_VAR *ptr= (SHOW_VAR*) all_status_vars.buffer;
1312
 
  SHOW_VAR *last= ptr + all_status_vars.elements;
1313
 
  for (; ptr < last; ptr++)
 
1304
  vector<SHOW_VAR *>::iterator p= all_status_vars.begin();
 
1305
  while (p != all_status_vars.end())
1314
1306
  {
1315
1307
    /* Note that SHOW_LONG_NOFLUSH variables are not reset */
1316
 
    if (ptr->type == SHOW_LONG)
1317
 
      *(ulong*) ptr->value= 0;
 
1308
    if ((*p)->type == SHOW_LONG)
 
1309
      (*p)->value= 0;
 
1310
    ++p;
1318
1311
  }
1319
1312
}
1320
1313
 
1324
1317
  DESCRIPTION
1325
1318
    This function is not strictly required if all add_to_status/
1326
1319
    remove_status_vars are properly paired, but it's a safety measure that
1327
 
    deletes everything from the all_status_vars[] even if some
 
1320
    deletes everything from the all_status_vars vector even if some
1328
1321
    remove_status_vars were forgotten
1329
1322
*/
1330
1323
void free_status_vars()
1331
1324
{
1332
 
  delete_dynamic(&all_status_vars);
 
1325
  all_status_vars.clear();
1333
1326
}
1334
1327
 
1335
1328
/*
1351
1344
  if (status_vars_inited)
1352
1345
  {
1353
1346
    pthread_mutex_lock(&LOCK_status);
1354
 
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1355
 
    int a= 0, b= all_status_vars.elements, c= (a+b)/2;
 
1347
    SHOW_VAR *all= all_status_vars.front();
 
1348
    int a= 0, b= all_status_vars.size(), c= (a+b)/2;
1356
1349
 
1357
1350
    for (; list->name; list++)
1358
1351
    {
1359
1352
      int res= 0;
1360
 
      for (a= 0, b= all_status_vars.elements; b-a > 1; c= (a+b)/2)
 
1353
      for (a= 0, b= all_status_vars.size(); b-a > 1; c= (a+b)/2)
1361
1354
      {
1362
1355
        res= show_var_cmp(list, all+c);
1363
1356
        if (res < 0)
1370
1363
      if (res == 0)
1371
1364
        all[c].type= SHOW_UNDEF;
1372
1365
    }
1373
 
    shrink_var_array(&all_status_vars);
 
1366
    /* removes all the SHOW_UNDEF elements from the vector */
 
1367
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
 
1368
                            all_status_vars.end(),show_var_remove_if()),
 
1369
                            all_status_vars.end());
1374
1370
    pthread_mutex_unlock(&LOCK_status);
1375
1371
  }
1376
1372
  else
1377
1373
  {
1378
 
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
 
1374
    SHOW_VAR *all= all_status_vars.front();
1379
1375
    uint32_t i;
1380
1376
    for (; list->name; list++)
1381
1377
    {
1382
 
      for (i= 0; i < all_status_vars.elements; i++)
 
1378
      for (i= 0; i < all_status_vars.size(); i++)
1383
1379
      {
1384
1380
        if (show_var_cmp(list, all+i))
1385
1381
          continue;
1387
1383
        break;
1388
1384
      }
1389
1385
    }
1390
 
    shrink_var_array(&all_status_vars);
 
1386
    /* removes all the SHOW_UNDEF elements from the vector */
 
1387
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
 
1388
                            all_status_vars.end(),show_var_remove_if()),
 
1389
                            all_status_vars.end());
1391
1390
  }
1392
1391
}
1393
1392
 
3569
3568
  if (option_type == OPT_GLOBAL)
3570
3569
    calc_sum_of_all_status(&tmp);
3571
3570
  res= show_status_array(session, wild,
3572
 
                         (SHOW_VAR *)all_status_vars.buffer,
 
3571
                         (SHOW_VAR *) all_status_vars.front(),
3573
3572
                         option_type, tmp1, "", tables->table,
3574
3573
                         upper_case_names);
3575
3574
  pthread_mutex_unlock(&LOCK_status);