~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/haildb/haildb_engine.cc

  • Committer: Brian Aker
  • Date: 2010-10-12 06:13:30 UTC
  • mto: (1857.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1858.
  • Revision ID: brian@tangent.org-20101012061330-omrzr0yla4ybul3t
More testing around events.

Show diffs side-by-side

added added

removed removed

Lines of Context:
90
90
 
91
91
#include "drizzled/global_charset_info.h"
92
92
 
 
93
#include "haildb_version_func.h"
93
94
#include "haildb_datadict_dump_func.h"
94
95
#include "config_table_function.h"
95
96
#include "status_table_function.h"
96
97
 
97
 
#include <haildb.h>
 
98
#if defined(HAVE_HAILDB_H)
 
99
# include <haildb.h>
 
100
#else
 
101
# include <embedded_innodb-1.0/innodb.h>
 
102
#endif /* HAVE_HAILDB_H */
98
103
 
99
104
#include "haildb_engine.h"
100
105
 
150
155
 
151
156
  ~HailDBEngine();
152
157
 
153
 
  virtual Cursor *create(Table &table)
 
158
  virtual Cursor *create(TableShare &table)
154
159
  {
155
160
    return new HailDBCursor(*this, table);
156
161
  }
474
479
{
475
480
  uint64_t nr;
476
481
  ib_err_t err;
477
 
  ib_trx_t transaction= *get_trx(getTable()->in_use);
 
482
  ib_trx_t transaction= *get_trx(table->in_use);
478
483
  ib_cursor_attach_trx(cursor, transaction);
479
484
  tuple= ib_clust_read_tuple_create(cursor);
480
485
  err= ib_cursor_last(cursor);
485
490
  else
486
491
  {
487
492
    assert (err == DB_SUCCESS);
488
 
    err= ib_tuple_read_u64(tuple, getTable()->getShare()->fields, &nr);
 
493
    err= ib_tuple_read_u64(tuple, table->getShare()->fields, &nr);
489
494
    nr++;
490
495
  }
491
496
  ib_tuple_delete(tuple);
500
505
  int error;
501
506
 
502
507
  (void) extra(HA_EXTRA_KEYREAD);
503
 
  getTable()->mark_columns_used_by_index_no_reset(getTable()->getShare()->next_number_index);
504
 
  doStartIndexScan(getTable()->getShare()->next_number_index, 1);
505
 
  if (getTable()->getShare()->next_number_keypart == 0)
 
508
  table->mark_columns_used_by_index_no_reset(table->getShare()->next_number_index);
 
509
  doStartIndexScan(table->getShare()->next_number_index, 1);
 
510
  if (table->getShare()->next_number_keypart == 0)
506
511
  {                                             // Autoincrement at key-start
507
 
    error=index_last(getTable()->getUpdateRecord());
 
512
    error=index_last(table->getUpdateRecord());
508
513
  }
509
514
  else
510
515
  {
511
516
    unsigned char key[MAX_KEY_LENGTH];
512
 
    key_copy(key, getTable()->getInsertRecord(),
513
 
             getTable()->key_info + getTable()->getShare()->next_number_index,
514
 
             getTable()->getShare()->next_number_key_offset);
515
 
    error= index_read_map(getTable()->getUpdateRecord(), key,
516
 
                          make_prev_keypart_map(getTable()->getShare()->next_number_keypart),
 
517
    key_copy(key, table->getInsertRecord(),
 
518
             table->key_info + table->getShare()->next_number_index,
 
519
             table->getShare()->next_number_key_offset);
 
520
    error= index_read_map(table->getUpdateRecord(), key,
 
521
                          make_prev_keypart_map(table->getShare()->next_number_keypart),
517
522
                          HA_READ_PREFIX_LAST);
518
523
  }
519
524
 
520
525
  if (error)
521
526
    nr=1;
522
527
  else
523
 
    nr= ((uint64_t) getTable()->found_next_number_field->
524
 
         val_int_offset(getTable()->getShare()->rec_buff_length)+1);
 
528
    nr= ((uint64_t) table->found_next_number_field->
 
529
         val_int_offset(table->getShare()->rec_buff_length)+1);
525
530
  doEndIndexScan();
526
531
  (void) extra(HA_EXTRA_NO_KEYREAD);
527
532
 
528
 
  if (getTable()->getShare()->getTableProto()->options().auto_increment_value() > nr)
529
 
    nr= getTable()->getShare()->getTableProto()->options().auto_increment_value();
 
533
  if (table->getShare()->getTableProto()->options().auto_increment_value() > nr)
 
534
    nr= table->getShare()->getTableProto()->options().auto_increment_value();
530
535
 
531
536
  return nr;
532
537
}
559
564
{
560
565
  pthread_mutex_lock(&haildb_mutex);
561
566
 
562
 
  HailDBEngine *a_engine= static_cast<HailDBEngine *>(getEngine());
 
567
  HailDBEngine *a_engine= static_cast<HailDBEngine *>(engine);
563
568
  share= a_engine->findOpenTable(table_name);
564
569
 
565
570
  if (!share)
573
578
      return(NULL);
574
579
    }
575
580
 
576
 
    if (getTable()->found_next_number_field)
 
581
    if (table->found_next_number_field)
577
582
    {
578
583
      share->auto_increment_value.fetch_and_store(
579
584
                                  a_engine->getInitialAutoIncrementValue(this));
602
607
  pthread_mutex_lock(&haildb_mutex);
603
608
  if (!--share->use_count)
604
609
  {
605
 
    HailDBEngine *a_engine= static_cast<HailDBEngine *>(getEngine());
 
610
    HailDBEngine *a_engine= static_cast<HailDBEngine *>(engine);
606
611
    a_engine->deleteOpenTable(share->table_name);
607
612
    delete share;
608
613
  }
796
801
}
797
802
 
798
803
HailDBCursor::HailDBCursor(drizzled::plugin::StorageEngine &engine_arg,
799
 
                           Table &table_arg)
 
804
                           TableShare &table_arg)
800
805
  :Cursor(engine_arg, table_arg),
801
806
   ib_lock_mode(IB_LOCK_NONE),
802
807
   write_can_replace(false),
836
841
  lock.init(&share->lock);
837
842
 
838
843
 
839
 
  if (getTable()->getShare()->getPrimaryKey() != MAX_KEY)
840
 
    ref_length= getTable()->key_info[getTable()->getShare()->getPrimaryKey()].key_length;
 
844
  if (table->getShare()->getPrimaryKey() != MAX_KEY)
 
845
    ref_length= table->key_info[table->getShare()->getPrimaryKey()].key_length;
841
846
  else if (share->has_hidden_primary_key)
842
847
    ref_length= sizeof(uint64_t);
843
848
  else
844
849
  {
845
 
    unsigned int keynr= get_first_unique_index(*getTable());
846
 
    ref_length= getTable()->key_info[keynr].key_length;
 
850
    unsigned int keynr= get_first_unique_index(*table);
 
851
    ref_length= table->key_info[keynr].key_length;
847
852
  }
848
853
 
849
854
  in_table_scan= false;
1872
1877
  ib_err_t err;
1873
1878
  int ret= 0;
1874
1879
 
1875
 
  ib_trx_t transaction= *get_trx(getTable()->in_use);
 
1880
  ib_trx_t transaction= *get_trx(table->in_use);
1876
1881
 
1877
1882
  tuple= ib_clust_read_tuple_create(cursor);
1878
1883
 
1894
1899
     * yuck.
1895
1900
     */
1896
1901
 
1897
 
    HailDBEngine *storage_engine= static_cast<HailDBEngine*>(getEngine());
 
1902
    HailDBEngine *storage_engine= static_cast<HailDBEngine*>(engine);
1898
1903
    err= ib_cursor_reset(cursor);
1899
1904
    storage_engine->doCommit(current_session, true);
1900
1905
    storage_engine->doStartTransaction(current_session, START_TRANS_NO_OPTIONS);
1901
 
    transaction= *get_trx(getTable()->in_use);
 
1906
    transaction= *get_trx(table->in_use);
1902
1907
    assert(err == DB_SUCCESS);
1903
1908
    ib_cursor_attach_trx(cursor, transaction);
1904
1909
    err= ib_cursor_first(cursor);
1907
1912
  assert(err == DB_SUCCESS || err == DB_END_OF_INDEX);
1908
1913
 
1909
1914
 
1910
 
  if (getTable()->next_number_field)
 
1915
  if (table->next_number_field)
1911
1916
  {
1912
1917
    update_auto_increment();
1913
1918
 
1914
 
    uint64_t temp_auto= getTable()->next_number_field->val_int();
 
1919
    uint64_t temp_auto= table->next_number_field->val_int();
1915
1920
 
1916
 
    if (temp_auto <= innobase_get_int_col_max_value(getTable()->next_number_field))
 
1921
    if (temp_auto <= innobase_get_int_col_max_value(table->next_number_field))
1917
1922
    {
1918
1923
      while (true)
1919
1924
      {
1935
1940
 
1936
1941
  }
1937
1942
 
1938
 
  write_row_to_haildb_tuple(getTable()->getFields(), tuple);
 
1943
  write_row_to_haildb_tuple(table->getFields(), tuple);
1939
1944
 
1940
1945
  if (share->has_hidden_primary_key)
1941
1946
  {
1942
 
    err= ib_tuple_write_u64(tuple, getTable()->getShare()->fields, share->hidden_pkey_auto_increment_value.fetch_and_increment());
 
1947
    err= ib_tuple_write_u64(tuple, table->getShare()->fields, share->hidden_pkey_auto_increment_value.fetch_and_increment());
1943
1948
  }
1944
1949
 
1945
1950
  err= ib_cursor_insert_row(cursor, tuple);
1948
1953
  {
1949
1954
    if (write_can_replace)
1950
1955
    {
1951
 
      store_key_value_from_haildb(getTable()->key_info + getTable()->getShare()->getPrimaryKey(),
 
1956
      store_key_value_from_haildb(table->key_info + table->getShare()->getPrimaryKey(),
1952
1957
                                  ref, ref_length, record);
1953
1958
 
1954
1959
      ib_tpl_t search_tuple= ib_clust_search_tuple_create(cursor);
1955
1960
 
1956
1961
      fill_ib_search_tpl_from_drizzle_key(search_tuple,
1957
 
                                          getTable()->key_info + 0,
 
1962
                                          table->key_info + 0,
1958
1963
                                          ref, ref_length);
1959
1964
 
1960
1965
      int res;
1968
1973
      err= ib_cursor_first(cursor);
1969
1974
      assert(err == DB_SUCCESS || err == DB_END_OF_INDEX);
1970
1975
 
1971
 
      write_row_to_haildb_tuple(getTable()->getFields(), tuple);
 
1976
      write_row_to_haildb_tuple(table->getFields(), tuple);
1972
1977
 
1973
1978
      err= ib_cursor_insert_row(cursor, tuple);
1974
1979
      assert(err==DB_SUCCESS); // probably be nice and process errors
1997
2002
  err= ib_tuple_copy(update_tuple, tuple);
1998
2003
  assert(err == DB_SUCCESS);
1999
2004
 
2000
 
  write_row_to_haildb_tuple(getTable()->getFields(), update_tuple);
 
2005
  write_row_to_haildb_tuple(table->getFields(), update_tuple);
2001
2006
 
2002
2007
  err= ib_cursor_update_row(cursor, tuple, update_tuple);
2003
2008
 
2031
2036
     so only support TRUNCATE and not DELETE FROM t;
2032
2037
     (this is what ha_haildb does)
2033
2038
  */
2034
 
  if (session_sql_command(getTable()->in_use) != SQLCOM_TRUNCATE)
 
2039
  if (session_sql_command(table->in_use) != SQLCOM_TRUNCATE)
2035
2040
    return HA_ERR_WRONG_COMMAND;
2036
2041
 
2037
2042
  ib_id_t id;
2046
2051
  {
2047
2052
    ib_err_t rollback_err= ib_trx_rollback(transaction);
2048
2053
 
2049
 
    push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2054
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2050
2055
                        ER_CANT_DELETE_FILE,
2051
2056
                        _("Cannot Lock HailDB Data Dictionary. HailDB Error %d (%s)\n"),
2052
2057
                        err, ib_strerror(err));
2087
2092
    doEndTableScan();
2088
2093
  in_table_scan= true;
2089
2094
 
2090
 
  transaction= *get_trx(getTable()->in_use);
 
2095
  transaction= *get_trx(table->in_use);
2091
2096
 
2092
2097
  assert(transaction != NULL);
2093
2098
 
2206
2211
    err= ib_cursor_next(cursor);
2207
2212
 
2208
2213
  tuple= ib_tuple_clear(tuple);
2209
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2214
  ret= read_row_from_haildb(buf, cursor, tuple, table,
2210
2215
                            share->has_hidden_primary_key,
2211
2216
                            &hidden_autoinc_pkey_position);
2212
2217
 
2241
2246
  else
2242
2247
  {
2243
2248
    unsigned int keynr;
2244
 
    if (getTable()->getShare()->getPrimaryKey() != MAX_KEY)
2245
 
      keynr= getTable()->getShare()->getPrimaryKey();
 
2249
    if (table->getShare()->getPrimaryKey() != MAX_KEY)
 
2250
      keynr= table->getShare()->getPrimaryKey();
2246
2251
    else
2247
 
      keynr= get_first_unique_index(*getTable());
 
2252
      keynr= get_first_unique_index(*table);
2248
2253
 
2249
2254
    fill_ib_search_tpl_from_drizzle_key(search_tuple,
2250
 
                                        getTable()->key_info + keynr,
 
2255
                                        table->key_info + keynr,
2251
2256
                                        pos, ref_length);
2252
2257
  }
2253
2258
 
2263
2268
  tuple= ib_tuple_clear(tuple);
2264
2269
 
2265
2270
  if (ret == 0)
2266
 
    ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2271
    ret= read_row_from_haildb(buf, cursor, tuple, table,
2267
2272
                              share->has_hidden_primary_key,
2268
2273
                              &hidden_autoinc_pkey_position);
2269
2274
 
2334
2339
  else
2335
2340
  {
2336
2341
    unsigned int keynr;
2337
 
    if (getTable()->getShare()->getPrimaryKey() != MAX_KEY)
2338
 
      keynr= getTable()->getShare()->getPrimaryKey();
 
2342
    if (table->getShare()->getPrimaryKey() != MAX_KEY)
 
2343
      keynr= table->getShare()->getPrimaryKey();
2339
2344
    else
2340
 
      keynr= get_first_unique_index(*getTable());
 
2345
      keynr= get_first_unique_index(*table);
2341
2346
 
2342
 
    store_key_value_from_haildb(getTable()->key_info + keynr,
 
2347
    store_key_value_from_haildb(table->key_info + keynr,
2343
2348
                                ref, ref_length, record);
2344
2349
  }
2345
2350
 
2348
2353
 
2349
2354
double HailDBCursor::scan_time()
2350
2355
{
2351
 
  ib_table_stats_t table_stats;
2352
 
  ib_err_t err;
2353
 
 
2354
 
  err= ib_get_table_statistics(cursor, &table_stats, sizeof(table_stats));
2355
 
 
2356
 
  /* Approximate I/O seeks for full table scan */
2357
 
  return (double) (table_stats.stat_clustered_index_size / 16384);
 
2356
  return 0.1;
2358
2357
}
2359
2358
 
2360
2359
int HailDBCursor::info(uint32_t flag)
2361
2360
{
2362
 
  ib_table_stats_t table_stats;
2363
 
  ib_err_t err;
2364
 
 
2365
 
  if (flag & HA_STATUS_VARIABLE)
2366
 
  {
2367
 
    err= ib_get_table_statistics(cursor, &table_stats, sizeof(table_stats));
2368
 
 
2369
 
    stats.records= table_stats.stat_n_rows;
2370
 
 
2371
 
    if (table_stats.stat_n_rows < 2)
2372
 
      stats.records= 2;
2373
 
 
2374
 
    stats.deleted= 0;
2375
 
    stats.data_file_length= table_stats.stat_clustered_index_size;
2376
 
    stats.index_file_length= table_stats.stat_sum_of_other_index_sizes;
2377
 
 
2378
 
    stats.mean_rec_length= stats.data_file_length / stats.records;
2379
 
  }
 
2361
  stats.records= 2;
2380
2362
 
2381
2363
  if (flag & HA_STATUS_AUTO)
2382
2364
    stats.auto_increment_value= 1;
2385
2367
 
2386
2368
int HailDBCursor::doStartIndexScan(uint32_t keynr, bool)
2387
2369
{
2388
 
  ib_trx_t transaction= *get_trx(getTable()->in_use);
 
2370
  ib_trx_t transaction= *get_trx(table->in_use);
2389
2371
 
2390
2372
  active_index= keynr;
2391
2373
 
2399
2381
  {
2400
2382
    ib_err_t err;
2401
2383
    ib_id_t index_id;
2402
 
    err= ib_index_get_id(table_path_to_haildb_name(getShare()->getPath()),
2403
 
                         getShare()->getKeyInfo(keynr).name,
 
2384
    err= ib_index_get_id(table_path_to_haildb_name(table_share->getPath()),
 
2385
                         table_share->getKeyInfo(keynr).name,
2404
2386
                         &index_id);
2405
2387
    if (err != DB_SUCCESS)
2406
2388
      return -1;
2547
2529
    search_tuple= ib_sec_search_tuple_create(cursor);
2548
2530
 
2549
2531
  fill_ib_search_tpl_from_drizzle_key(search_tuple,
2550
 
                                      getTable()->key_info + active_index,
 
2532
                                      table->key_info + active_index,
2551
2533
                                      key_ptr, key_len);
2552
2534
 
2553
2535
  err= ib_cursor_moveto(cursor, search_tuple, search_mode, &res);
2555
2537
 
2556
2538
  if ((err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX))
2557
2539
  {
2558
 
    getTable()->status= STATUS_NOT_FOUND;
 
2540
    table->status= STATUS_NOT_FOUND;
2559
2541
    return HA_ERR_KEY_NOT_FOUND;
2560
2542
  }
2561
2543
 
2565
2547
  }
2566
2548
 
2567
2549
  tuple= ib_tuple_clear(tuple);
2568
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2550
  ret= read_row_from_haildb(buf, cursor, tuple, table,
2569
2551
                            share->has_hidden_primary_key,
2570
2552
                            &hidden_autoinc_pkey_position,
2571
2553
                            (allocate_blobs)? &blobroot : NULL);
2572
2554
  if (ret == 0)
2573
 
    getTable()->status= 0;
 
2555
    table->status= 0;
2574
2556
  else
2575
 
    getTable()->status= STATUS_NOT_FOUND;
 
2557
    table->status= STATUS_NOT_FOUND;
2576
2558
 
2577
2559
  advance_cursor= true;
2578
2560
 
2594
2576
  /* works only with key prefixes */
2595
2577
  assert(((keypart_map_arg + 1) & keypart_map_arg) == 0);
2596
2578
 
2597
 
  KeyPartInfo *key_part_found= getTable()->getShare()->getKeyInfo(key_position).key_part;
2598
 
  KeyPartInfo *end_key_part_found= key_part_found + getTable()->getShare()->getKeyInfo(key_position).key_parts;
 
2579
  KeyPartInfo *key_part_found= table->getShare()->getKeyInfo(key_position).key_part;
 
2580
  KeyPartInfo *end_key_part_found= key_part_found + table->getShare()->getKeyInfo(key_position).key_parts;
2599
2581
  uint32_t length= 0;
2600
2582
 
2601
2583
  while (key_part_found < end_key_part_found && keypart_map_arg)
2654
2636
  }
2655
2637
 
2656
2638
  tuple= ib_tuple_clear(tuple);
2657
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2639
  ret= read_row_from_haildb(buf, cursor, tuple, table,
2658
2640
                            share->has_hidden_primary_key,
2659
2641
                            &hidden_autoinc_pkey_position);
2660
2642
 
2687
2669
  }
2688
2670
 
2689
2671
  tuple= ib_tuple_clear(tuple);
2690
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2672
  ret= read_row_from_haildb(buf, cursor, tuple, table,
2691
2673
                            share->has_hidden_primary_key,
2692
2674
                            &hidden_autoinc_pkey_position);
2693
2675
 
2707
2689
    return ib_err_t_to_drizzle_error(err);
2708
2690
 
2709
2691
  tuple= ib_tuple_clear(tuple);
2710
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2692
  ret= read_row_from_haildb(buf, cursor, tuple, table,
2711
2693
                            share->has_hidden_primary_key,
2712
2694
                            &hidden_autoinc_pkey_position);
2713
2695
 
2727
2709
    return ib_err_t_to_drizzle_error(err);
2728
2710
 
2729
2711
  tuple= ib_tuple_clear(tuple);
2730
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2712
  ret= read_row_from_haildb(buf, cursor, tuple, table,
2731
2713
                            share->has_hidden_primary_key,
2732
2714
                            &hidden_autoinc_pkey_position);
2733
2715
  advance_cursor= true;
3237
3219
  haildb_engine= new HailDBEngine("InnoDB");
3238
3220
  context.add(haildb_engine);
3239
3221
 
 
3222
  haildb_version_func_initialize(context);
3240
3223
  haildb_datadict_dump_func_initialize(context);
3241
3224
  config_table_function_initialize(context);
3242
3225
  status_table_function_initialize(context);