~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-22 23:33:58 UTC
  • mfrom: (1869.1.7 refactor)
  • Revision ID: brian@tangent.org-20101022233358-kmtrpm1yvmmyaame
Merge in overhaul to how cursor and table are handled. Cursor now only knows
about table, and will always have a table and engine reference.

This cleans up a number of ownership issues, the biggest being that it now
creates the space needed for the next big refactor in locks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
150
150
 
151
151
  ~HailDBEngine();
152
152
 
153
 
  virtual Cursor *create(TableShare &table)
 
153
  virtual Cursor *create(Table &table)
154
154
  {
155
155
    return new HailDBCursor(*this, table);
156
156
  }
474
474
{
475
475
  uint64_t nr;
476
476
  ib_err_t err;
477
 
  ib_trx_t transaction= *get_trx(table->in_use);
 
477
  ib_trx_t transaction= *get_trx(getTable()->in_use);
478
478
  ib_cursor_attach_trx(cursor, transaction);
479
479
  tuple= ib_clust_read_tuple_create(cursor);
480
480
  err= ib_cursor_last(cursor);
485
485
  else
486
486
  {
487
487
    assert (err == DB_SUCCESS);
488
 
    err= ib_tuple_read_u64(tuple, table->getShare()->fields, &nr);
 
488
    err= ib_tuple_read_u64(tuple, getTable()->getShare()->fields, &nr);
489
489
    nr++;
490
490
  }
491
491
  ib_tuple_delete(tuple);
500
500
  int error;
501
501
 
502
502
  (void) extra(HA_EXTRA_KEYREAD);
503
 
  table->mark_columns_used_by_index_no_reset(table->getShare()->next_number_index);
504
 
  doStartIndexScan(table->getShare()->next_number_index, 1);
505
 
  if (table->getShare()->next_number_keypart == 0)
 
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)
506
506
  {                                             // Autoincrement at key-start
507
 
    error=index_last(table->getUpdateRecord());
 
507
    error=index_last(getTable()->getUpdateRecord());
508
508
  }
509
509
  else
510
510
  {
511
511
    unsigned char key[MAX_KEY_LENGTH];
512
 
    key_copy(key, table->getInsertRecord(),
513
 
             table->key_info + table->getShare()->next_number_index,
514
 
             table->getShare()->next_number_key_offset);
515
 
    error= index_read_map(table->getUpdateRecord(), key,
516
 
                          make_prev_keypart_map(table->getShare()->next_number_keypart),
 
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
517
                          HA_READ_PREFIX_LAST);
518
518
  }
519
519
 
520
520
  if (error)
521
521
    nr=1;
522
522
  else
523
 
    nr= ((uint64_t) table->found_next_number_field->
524
 
         val_int_offset(table->getShare()->rec_buff_length)+1);
 
523
    nr= ((uint64_t) getTable()->found_next_number_field->
 
524
         val_int_offset(getTable()->getShare()->rec_buff_length)+1);
525
525
  doEndIndexScan();
526
526
  (void) extra(HA_EXTRA_NO_KEYREAD);
527
527
 
528
 
  if (table->getShare()->getTableProto()->options().auto_increment_value() > nr)
529
 
    nr= table->getShare()->getTableProto()->options().auto_increment_value();
 
528
  if (getTable()->getShare()->getTableProto()->options().auto_increment_value() > nr)
 
529
    nr= getTable()->getShare()->getTableProto()->options().auto_increment_value();
530
530
 
531
531
  return nr;
532
532
}
559
559
{
560
560
  pthread_mutex_lock(&haildb_mutex);
561
561
 
562
 
  HailDBEngine *a_engine= static_cast<HailDBEngine *>(engine);
 
562
  HailDBEngine *a_engine= static_cast<HailDBEngine *>(getEngine());
563
563
  share= a_engine->findOpenTable(table_name);
564
564
 
565
565
  if (!share)
573
573
      return(NULL);
574
574
    }
575
575
 
576
 
    if (table->found_next_number_field)
 
576
    if (getTable()->found_next_number_field)
577
577
    {
578
578
      share->auto_increment_value.fetch_and_store(
579
579
                                  a_engine->getInitialAutoIncrementValue(this));
602
602
  pthread_mutex_lock(&haildb_mutex);
603
603
  if (!--share->use_count)
604
604
  {
605
 
    HailDBEngine *a_engine= static_cast<HailDBEngine *>(engine);
 
605
    HailDBEngine *a_engine= static_cast<HailDBEngine *>(getEngine());
606
606
    a_engine->deleteOpenTable(share->table_name);
607
607
    delete share;
608
608
  }
796
796
}
797
797
 
798
798
HailDBCursor::HailDBCursor(drizzled::plugin::StorageEngine &engine_arg,
799
 
                           TableShare &table_arg)
 
799
                           Table &table_arg)
800
800
  :Cursor(engine_arg, table_arg),
801
801
   ib_lock_mode(IB_LOCK_NONE),
802
802
   write_can_replace(false),
836
836
  lock.init(&share->lock);
837
837
 
838
838
 
839
 
  if (table->getShare()->getPrimaryKey() != MAX_KEY)
840
 
    ref_length= table->key_info[table->getShare()->getPrimaryKey()].key_length;
 
839
  if (getTable()->getShare()->getPrimaryKey() != MAX_KEY)
 
840
    ref_length= getTable()->key_info[getTable()->getShare()->getPrimaryKey()].key_length;
841
841
  else if (share->has_hidden_primary_key)
842
842
    ref_length= sizeof(uint64_t);
843
843
  else
844
844
  {
845
 
    unsigned int keynr= get_first_unique_index(*table);
846
 
    ref_length= table->key_info[keynr].key_length;
 
845
    unsigned int keynr= get_first_unique_index(*getTable());
 
846
    ref_length= getTable()->key_info[keynr].key_length;
847
847
  }
848
848
 
849
849
  in_table_scan= false;
1872
1872
  ib_err_t err;
1873
1873
  int ret= 0;
1874
1874
 
1875
 
  ib_trx_t transaction= *get_trx(table->in_use);
 
1875
  ib_trx_t transaction= *get_trx(getTable()->in_use);
1876
1876
 
1877
1877
  tuple= ib_clust_read_tuple_create(cursor);
1878
1878
 
1894
1894
     * yuck.
1895
1895
     */
1896
1896
 
1897
 
    HailDBEngine *storage_engine= static_cast<HailDBEngine*>(engine);
 
1897
    HailDBEngine *storage_engine= static_cast<HailDBEngine*>(getEngine());
1898
1898
    err= ib_cursor_reset(cursor);
1899
1899
    storage_engine->doCommit(current_session, true);
1900
1900
    storage_engine->doStartTransaction(current_session, START_TRANS_NO_OPTIONS);
1901
 
    transaction= *get_trx(table->in_use);
 
1901
    transaction= *get_trx(getTable()->in_use);
1902
1902
    assert(err == DB_SUCCESS);
1903
1903
    ib_cursor_attach_trx(cursor, transaction);
1904
1904
    err= ib_cursor_first(cursor);
1907
1907
  assert(err == DB_SUCCESS || err == DB_END_OF_INDEX);
1908
1908
 
1909
1909
 
1910
 
  if (table->next_number_field)
 
1910
  if (getTable()->next_number_field)
1911
1911
  {
1912
1912
    update_auto_increment();
1913
1913
 
1914
 
    uint64_t temp_auto= table->next_number_field->val_int();
 
1914
    uint64_t temp_auto= getTable()->next_number_field->val_int();
1915
1915
 
1916
 
    if (temp_auto <= innobase_get_int_col_max_value(table->next_number_field))
 
1916
    if (temp_auto <= innobase_get_int_col_max_value(getTable()->next_number_field))
1917
1917
    {
1918
1918
      while (true)
1919
1919
      {
1935
1935
 
1936
1936
  }
1937
1937
 
1938
 
  write_row_to_haildb_tuple(table->getFields(), tuple);
 
1938
  write_row_to_haildb_tuple(getTable()->getFields(), tuple);
1939
1939
 
1940
1940
  if (share->has_hidden_primary_key)
1941
1941
  {
1942
 
    err= ib_tuple_write_u64(tuple, table->getShare()->fields, share->hidden_pkey_auto_increment_value.fetch_and_increment());
 
1942
    err= ib_tuple_write_u64(tuple, getTable()->getShare()->fields, share->hidden_pkey_auto_increment_value.fetch_and_increment());
1943
1943
  }
1944
1944
 
1945
1945
  err= ib_cursor_insert_row(cursor, tuple);
1948
1948
  {
1949
1949
    if (write_can_replace)
1950
1950
    {
1951
 
      store_key_value_from_haildb(table->key_info + table->getShare()->getPrimaryKey(),
 
1951
      store_key_value_from_haildb(getTable()->key_info + getTable()->getShare()->getPrimaryKey(),
1952
1952
                                  ref, ref_length, record);
1953
1953
 
1954
1954
      ib_tpl_t search_tuple= ib_clust_search_tuple_create(cursor);
1955
1955
 
1956
1956
      fill_ib_search_tpl_from_drizzle_key(search_tuple,
1957
 
                                          table->key_info + 0,
 
1957
                                          getTable()->key_info + 0,
1958
1958
                                          ref, ref_length);
1959
1959
 
1960
1960
      int res;
1968
1968
      err= ib_cursor_first(cursor);
1969
1969
      assert(err == DB_SUCCESS || err == DB_END_OF_INDEX);
1970
1970
 
1971
 
      write_row_to_haildb_tuple(table->getFields(), tuple);
 
1971
      write_row_to_haildb_tuple(getTable()->getFields(), tuple);
1972
1972
 
1973
1973
      err= ib_cursor_insert_row(cursor, tuple);
1974
1974
      assert(err==DB_SUCCESS); // probably be nice and process errors
1997
1997
  err= ib_tuple_copy(update_tuple, tuple);
1998
1998
  assert(err == DB_SUCCESS);
1999
1999
 
2000
 
  write_row_to_haildb_tuple(table->getFields(), update_tuple);
 
2000
  write_row_to_haildb_tuple(getTable()->getFields(), update_tuple);
2001
2001
 
2002
2002
  err= ib_cursor_update_row(cursor, tuple, update_tuple);
2003
2003
 
2031
2031
     so only support TRUNCATE and not DELETE FROM t;
2032
2032
     (this is what ha_haildb does)
2033
2033
  */
2034
 
  if (session_sql_command(table->in_use) != SQLCOM_TRUNCATE)
 
2034
  if (session_sql_command(getTable()->in_use) != SQLCOM_TRUNCATE)
2035
2035
    return HA_ERR_WRONG_COMMAND;
2036
2036
 
2037
2037
  ib_id_t id;
2046
2046
  {
2047
2047
    ib_err_t rollback_err= ib_trx_rollback(transaction);
2048
2048
 
2049
 
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2049
    push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2050
2050
                        ER_CANT_DELETE_FILE,
2051
2051
                        _("Cannot Lock HailDB Data Dictionary. HailDB Error %d (%s)\n"),
2052
2052
                        err, ib_strerror(err));
2087
2087
    doEndTableScan();
2088
2088
  in_table_scan= true;
2089
2089
 
2090
 
  transaction= *get_trx(table->in_use);
 
2090
  transaction= *get_trx(getTable()->in_use);
2091
2091
 
2092
2092
  assert(transaction != NULL);
2093
2093
 
2206
2206
    err= ib_cursor_next(cursor);
2207
2207
 
2208
2208
  tuple= ib_tuple_clear(tuple);
2209
 
  ret= read_row_from_haildb(buf, cursor, tuple, table,
 
2209
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
2210
2210
                            share->has_hidden_primary_key,
2211
2211
                            &hidden_autoinc_pkey_position);
2212
2212
 
2241
2241
  else
2242
2242
  {
2243
2243
    unsigned int keynr;
2244
 
    if (table->getShare()->getPrimaryKey() != MAX_KEY)
2245
 
      keynr= table->getShare()->getPrimaryKey();
 
2244
    if (getTable()->getShare()->getPrimaryKey() != MAX_KEY)
 
2245
      keynr= getTable()->getShare()->getPrimaryKey();
2246
2246
    else
2247
 
      keynr= get_first_unique_index(*table);
 
2247
      keynr= get_first_unique_index(*getTable());
2248
2248
 
2249
2249
    fill_ib_search_tpl_from_drizzle_key(search_tuple,
2250
 
                                        table->key_info + keynr,
 
2250
                                        getTable()->key_info + keynr,
2251
2251
                                        pos, ref_length);
2252
2252
  }
2253
2253
 
2263
2263
  tuple= ib_tuple_clear(tuple);
2264
2264
 
2265
2265
  if (ret == 0)
2266
 
    ret= read_row_from_haildb(buf, cursor, tuple, table,
 
2266
    ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
2267
2267
                              share->has_hidden_primary_key,
2268
2268
                              &hidden_autoinc_pkey_position);
2269
2269
 
2334
2334
  else
2335
2335
  {
2336
2336
    unsigned int keynr;
2337
 
    if (table->getShare()->getPrimaryKey() != MAX_KEY)
2338
 
      keynr= table->getShare()->getPrimaryKey();
 
2337
    if (getTable()->getShare()->getPrimaryKey() != MAX_KEY)
 
2338
      keynr= getTable()->getShare()->getPrimaryKey();
2339
2339
    else
2340
 
      keynr= get_first_unique_index(*table);
 
2340
      keynr= get_first_unique_index(*getTable());
2341
2341
 
2342
 
    store_key_value_from_haildb(table->key_info + keynr,
 
2342
    store_key_value_from_haildb(getTable()->key_info + keynr,
2343
2343
                                ref, ref_length, record);
2344
2344
  }
2345
2345
 
2385
2385
 
2386
2386
int HailDBCursor::doStartIndexScan(uint32_t keynr, bool)
2387
2387
{
2388
 
  ib_trx_t transaction= *get_trx(table->in_use);
 
2388
  ib_trx_t transaction= *get_trx(getTable()->in_use);
2389
2389
 
2390
2390
  active_index= keynr;
2391
2391
 
2399
2399
  {
2400
2400
    ib_err_t err;
2401
2401
    ib_id_t index_id;
2402
 
    err= ib_index_get_id(table_path_to_haildb_name(table_share->getPath()),
2403
 
                         table_share->getKeyInfo(keynr).name,
 
2402
    err= ib_index_get_id(table_path_to_haildb_name(getShare()->getPath()),
 
2403
                         getShare()->getKeyInfo(keynr).name,
2404
2404
                         &index_id);
2405
2405
    if (err != DB_SUCCESS)
2406
2406
      return -1;
2547
2547
    search_tuple= ib_sec_search_tuple_create(cursor);
2548
2548
 
2549
2549
  fill_ib_search_tpl_from_drizzle_key(search_tuple,
2550
 
                                      table->key_info + active_index,
 
2550
                                      getTable()->key_info + active_index,
2551
2551
                                      key_ptr, key_len);
2552
2552
 
2553
2553
  err= ib_cursor_moveto(cursor, search_tuple, search_mode, &res);
2555
2555
 
2556
2556
  if ((err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX))
2557
2557
  {
2558
 
    table->status= STATUS_NOT_FOUND;
 
2558
    getTable()->status= STATUS_NOT_FOUND;
2559
2559
    return HA_ERR_KEY_NOT_FOUND;
2560
2560
  }
2561
2561
 
2565
2565
  }
2566
2566
 
2567
2567
  tuple= ib_tuple_clear(tuple);
2568
 
  ret= read_row_from_haildb(buf, cursor, tuple, table,
 
2568
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
2569
2569
                            share->has_hidden_primary_key,
2570
2570
                            &hidden_autoinc_pkey_position,
2571
2571
                            (allocate_blobs)? &blobroot : NULL);
2572
2572
  if (ret == 0)
2573
 
    table->status= 0;
 
2573
    getTable()->status= 0;
2574
2574
  else
2575
 
    table->status= STATUS_NOT_FOUND;
 
2575
    getTable()->status= STATUS_NOT_FOUND;
2576
2576
 
2577
2577
  advance_cursor= true;
2578
2578
 
2594
2594
  /* works only with key prefixes */
2595
2595
  assert(((keypart_map_arg + 1) & keypart_map_arg) == 0);
2596
2596
 
2597
 
  KeyPartInfo *key_part_found= table->getShare()->getKeyInfo(key_position).key_part;
2598
 
  KeyPartInfo *end_key_part_found= key_part_found + table->getShare()->getKeyInfo(key_position).key_parts;
 
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;
2599
2599
  uint32_t length= 0;
2600
2600
 
2601
2601
  while (key_part_found < end_key_part_found && keypart_map_arg)
2654
2654
  }
2655
2655
 
2656
2656
  tuple= ib_tuple_clear(tuple);
2657
 
  ret= read_row_from_haildb(buf, cursor, tuple, table,
 
2657
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
2658
2658
                            share->has_hidden_primary_key,
2659
2659
                            &hidden_autoinc_pkey_position);
2660
2660
 
2687
2687
  }
2688
2688
 
2689
2689
  tuple= ib_tuple_clear(tuple);
2690
 
  ret= read_row_from_haildb(buf, cursor, tuple, table,
 
2690
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
2691
2691
                            share->has_hidden_primary_key,
2692
2692
                            &hidden_autoinc_pkey_position);
2693
2693
 
2707
2707
    return ib_err_t_to_drizzle_error(err);
2708
2708
 
2709
2709
  tuple= ib_tuple_clear(tuple);
2710
 
  ret= read_row_from_haildb(buf, cursor, tuple, table,
 
2710
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
2711
2711
                            share->has_hidden_primary_key,
2712
2712
                            &hidden_autoinc_pkey_position);
2713
2713
 
2727
2727
    return ib_err_t_to_drizzle_error(err);
2728
2728
 
2729
2729
  tuple= ib_tuple_clear(tuple);
2730
 
  ret= read_row_from_haildb(buf, cursor, tuple, table,
 
2730
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
2731
2731
                            share->has_hidden_primary_key,
2732
2732
                            &hidden_autoinc_pkey_position);
2733
2733
  advance_cursor= true;