~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/heap/ha_heap.cc

  • Committer: LinuxJedi
  • Date: 2010-09-01 17:46:04 UTC
  • mto: (1750.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1751.
  • Revision ID: linuxjedi@linuxjedi-laptop-20100901174604-dvdwv3vey32ffbrk
Add a data_dictionary table for the MySQL protocol plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
#include "heap_priv.h"
17
17
#include <drizzled/error.h>
60
60
    hp_panic(HA_PANIC_CLOSE);
61
61
  }
62
62
 
63
 
  virtual Cursor *create(Table &table)
 
63
  virtual Cursor *create(TableShare &table)
64
64
  {
65
65
    return new ha_heap(*this, table);
66
66
  }
93
93
                           const TableIdentifier &identifier,
94
94
                           message::Table &table_message);
95
95
 
 
96
  /* Temp only engine, so do not return values. */
 
97
  void doGetTableNames(CachedDirectory &, const SchemaIdentifier& , set<string>&) { };
 
98
 
96
99
  uint32_t max_supported_keys()          const { return MAX_KEY; }
97
100
  uint32_t max_supported_key_part_length() const { return MAX_KEY_LENGTH; }
98
101
 
104
107
  bool doDoesTableExist(Session& session, const TableIdentifier &identifier);
105
108
  void doGetTableIdentifiers(CachedDirectory &directory,
106
109
                             const SchemaIdentifier &schema_identifier,
107
 
                             TableIdentifier::vector &set_of_identifiers);
 
110
                             TableIdentifiers &set_of_identifiers);
108
111
};
109
112
 
110
113
void HeapEngine::doGetTableIdentifiers(CachedDirectory&,
111
114
                                       const SchemaIdentifier&,
112
 
                                       TableIdentifier::vector&)
 
115
                                       TableIdentifiers&)
113
116
{
114
117
}
115
118
 
116
119
bool HeapEngine::doDoesTableExist(Session& session, const TableIdentifier &identifier)
117
120
{
118
 
  return session.getMessageCache().doesTableMessageExist(identifier);
 
121
  return session.doesTableMessageExist(identifier);
119
122
}
120
123
 
121
124
int HeapEngine::doGetTableDefinition(Session &session,
122
125
                                     const TableIdentifier &identifier,
123
126
                                     message::Table &table_proto)
124
127
{
125
 
  if (session.getMessageCache().getTableMessage(identifier, table_proto))
 
128
  if (session.getTableMessage(identifier, table_proto))
126
129
    return EEXIST;
127
130
 
128
131
  return ENOENT;
133
136
*/
134
137
int HeapEngine::doDropTable(Session &session, const TableIdentifier &identifier)
135
138
{
136
 
  session.getMessageCache().removeTableMessage(identifier);
 
139
  session.removeTableMessage(identifier);
137
140
 
138
141
  int error= heap_delete_table(identifier.getPath().c_str());
139
142
 
158
161
*****************************************************************************/
159
162
 
160
163
ha_heap::ha_heap(plugin::StorageEngine &engine_arg,
161
 
                 Table &table_arg)
 
164
                 TableShare &table_arg)
162
165
  :Cursor(engine_arg, table_arg), file(0), records_changed(0), key_stat_version(0),
163
166
  internal_table(0)
164
167
{}
176
179
*/
177
180
#define MEMORY_STATS_UPDATE_THRESHOLD 10
178
181
 
179
 
int ha_heap::doOpen(const drizzled::TableIdentifier &identifier, int mode, uint32_t test_if_locked)
 
182
int ha_heap::open(const char *name, int mode, uint32_t test_if_locked)
180
183
{
181
 
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(identifier.getPath().c_str(), mode)) && errno == ENOENT))
 
184
  if ((test_if_locked & HA_OPEN_INTERNAL_TABLE) || (!(file= heap_open(name, mode)) && errno == ENOENT))
182
185
  {
183
186
    internal_table= test(test_if_locked & HA_OPEN_INTERNAL_TABLE);
184
187
    file= 0;
185
188
    HP_SHARE *internal_share= NULL;
186
189
    message::Table create_proto;
187
190
 
188
 
    if (not heap_storage_engine->heap_create_table(getTable()->in_use,
189
 
                                                   identifier.getPath().c_str(),
190
 
                                                   getTable(),
191
 
                                                   internal_table,
192
 
                                                   create_proto,
193
 
                                                   &internal_share))
 
191
    if (!heap_storage_engine->heap_create_table(table->in_use, name, table,
 
192
                                                internal_table,
 
193
                                                create_proto,
 
194
                                                &internal_share))
194
195
    {
195
196
        file= internal_table ?
196
197
          heap_open_from_share(internal_share, mode) :
240
241
 
241
242
Cursor *ha_heap::clone(memory::Root *)
242
243
{
243
 
  Cursor *new_handler= getTable()->getMutableShare()->db_type()->getCursor(*getTable());
244
 
  TableIdentifier identifier(getTable()->getShare()->getSchemaName(),
245
 
                             getTable()->getShare()->getTableName(),
246
 
                             getTable()->getShare()->getPath());
 
244
  Cursor *new_handler= table->getMutableShare()->db_type()->getCursor(*(table->getMutableShare()));
 
245
  TableIdentifier identifier(table->getShare()->getSchemaName(),
 
246
                             table->getShare()->getTableName(),
 
247
                             table->getShare()->getPath());
247
248
 
248
 
  if (new_handler && !new_handler->ha_open(identifier, getTable()->db_stat,
 
249
  if (new_handler && !new_handler->ha_open(identifier, table, file->getShare()->name.c_str(), table->db_stat,
249
250
                                           HA_OPEN_IGNORE_IF_LOCKED))
250
251
    return new_handler;
251
252
  return NULL;
276
277
 
277
278
void ha_heap::set_keys_for_scanning(void)
278
279
{
 
280
  btree_keys.reset();
279
281
}
280
282
 
281
283
 
282
284
void ha_heap::update_key_stats()
283
285
{
284
 
  for (uint32_t i= 0; i < getTable()->getShare()->sizeKeys(); i++)
 
286
  for (uint32_t i= 0; i < table->getShare()->sizeKeys(); i++)
285
287
  {
286
 
    KeyInfo *key= &getTable()->key_info[i];
 
288
    KeyInfo *key= &table->key_info[i];
287
289
 
288
290
    if (!key->rec_per_key)
289
291
      continue;
310
312
int ha_heap::doInsertRecord(unsigned char * buf)
311
313
{
312
314
  int res;
313
 
  if (getTable()->next_number_field && buf == getTable()->getInsertRecord())
 
315
  if (table->next_number_field && buf == table->getInsertRecord())
314
316
  {
315
317
    if ((res= update_auto_increment()))
316
318
      return res;
350
352
  int res;
351
353
 
352
354
  res= heap_delete(file,buf);
353
 
  if (!res && getTable()->getShare()->getType() == message::Table::STANDARD &&
 
355
  if (!res && table->getShare()->getType() == message::Table::STANDARD &&
354
356
      ++records_changed*MEMORY_STATS_UPDATE_THRESHOLD > file->getShare()->records)
355
357
  {
356
358
    /*
369
371
  assert(inited==INDEX);
370
372
  ha_statistic_increment(&system_status_var::ha_read_key_count);
371
373
  int error = heap_rkey(file,buf,active_index, key, keypart_map, find_flag);
372
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
374
  table->status = error ? STATUS_NOT_FOUND : 0;
373
375
  return error;
374
376
}
375
377
 
380
382
  ha_statistic_increment(&system_status_var::ha_read_key_count);
381
383
  int error= heap_rkey(file, buf, active_index, key, keypart_map,
382
384
                       HA_READ_PREFIX_LAST);
383
 
  getTable()->status= error ? STATUS_NOT_FOUND : 0;
 
385
  table->status= error ? STATUS_NOT_FOUND : 0;
384
386
  return error;
385
387
}
386
388
 
390
392
{
391
393
  ha_statistic_increment(&system_status_var::ha_read_key_count);
392
394
  int error = heap_rkey(file, buf, index, key, keypart_map, find_flag);
393
 
  getTable()->status = error ? STATUS_NOT_FOUND : 0;
 
395
  table->status = error ? STATUS_NOT_FOUND : 0;
394
396
  return error;
395
397
}
396
398
 
399
401
  assert(inited==INDEX);
400
402
  ha_statistic_increment(&system_status_var::ha_read_next_count);
401
403
  int error=heap_rnext(file,buf);
402
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
404
  table->status=error ? STATUS_NOT_FOUND: 0;
403
405
  return error;
404
406
}
405
407
 
408
410
  assert(inited==INDEX);
409
411
  ha_statistic_increment(&system_status_var::ha_read_prev_count);
410
412
  int error=heap_rprev(file,buf);
411
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
413
  table->status=error ? STATUS_NOT_FOUND: 0;
412
414
  return error;
413
415
}
414
416
 
417
419
  assert(inited==INDEX);
418
420
  ha_statistic_increment(&system_status_var::ha_read_first_count);
419
421
  int error=heap_rfirst(file, buf, active_index);
420
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
422
  table->status=error ? STATUS_NOT_FOUND: 0;
421
423
  return error;
422
424
}
423
425
 
426
428
  assert(inited==INDEX);
427
429
  ha_statistic_increment(&system_status_var::ha_read_last_count);
428
430
  int error=heap_rlast(file, buf, active_index);
429
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
431
  table->status=error ? STATUS_NOT_FOUND: 0;
430
432
  return error;
431
433
}
432
434
 
439
441
{
440
442
  ha_statistic_increment(&system_status_var::ha_read_rnd_next_count);
441
443
  int error=heap_scan(file, buf);
442
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
444
  table->status=error ? STATUS_NOT_FOUND: 0;
443
445
  return error;
444
446
}
445
447
 
450
452
  ha_statistic_increment(&system_status_var::ha_read_rnd_count);
451
453
  memcpy(&heap_position, pos, sizeof(HEAP_PTR));
452
454
  error=heap_rrnd(file, buf, heap_position);
453
 
  getTable()->status=error ? STATUS_NOT_FOUND: 0;
 
455
  table->status=error ? STATUS_NOT_FOUND: 0;
454
456
  return error;
455
457
}
456
458
 
499
501
int ha_heap::delete_all_rows()
500
502
{
501
503
  heap_clear(file);
502
 
  if (getTable()->getShare()->getType() == message::Table::STANDARD)
 
504
  if (table->getShare()->getType() == message::Table::STANDARD)
503
505
  {
504
506
    /*
505
507
       We can perform this safely since only one writer at the time is
626
628
 
627
629
int HeapEngine::doRenameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
628
630
{
629
 
  session.getMessageCache().renameTableMessage(from, to);
 
631
  session.renameTableMessage(from, to);
630
632
  return heap_rename(from.getPath().c_str(), to.getPath().c_str());
631
633
}
632
634
 
634
636
ha_rows ha_heap::records_in_range(uint32_t inx, key_range *min_key,
635
637
                                  key_range *max_key)
636
638
{
637
 
  KeyInfo *key= &getTable()->key_info[inx];
 
639
  KeyInfo *key= &table->key_info[inx];
638
640
 
639
641
  if (!min_key || !max_key ||
640
642
      min_key->length != max_key->length ||
667
669
 
668
670
  if (error == 0)
669
671
  {
670
 
    session.getMessageCache().storeTableMessage(identifier, create_proto);
 
672
    session.storeTableMessage(identifier, create_proto);
671
673
  }
672
674
 
673
675
  return error;
685
687
  uint32_t auto_key= 0, auto_key_type= 0;
686
688
  uint32_t max_key_fieldnr = 0, key_part_size = 0, next_field_pos = 0;
687
689
  uint32_t column_count= table_arg->getShare()->sizeFields();
 
690
  std::vector<HP_COLUMNDEF> columndef;
688
691
  std::vector<HP_KEYDEF> keydef;
 
692
  char buff[FN_REFLEN];
689
693
  int error;
690
694
  bool found_real_auto_increment= 0;
691
695
 
699
703
  if (num_rows > UINT32_MAX)
700
704
    return -1;
701
705
 
 
706
  columndef.resize(column_count);
 
707
 
 
708
  for (uint32_t column_idx= 0; column_idx < column_count; column_idx++)
 
709
  {
 
710
    Field* field= *(table_arg->getFields() + column_idx);
 
711
    HP_COLUMNDEF* column= &columndef[column_idx];
 
712
    column->type= (uint16_t)field->type();
 
713
    column->length= field->pack_length();
 
714
    column->offset= field->offset(field->getTable()->getInsertRecord());
 
715
 
 
716
    if (field->null_bit)
 
717
    {
 
718
      column->null_bit= field->null_bit;
 
719
      column->null_pos= (uint) (field->null_ptr - (unsigned char*) table_arg->getInsertRecord());
 
720
    }
 
721
    else
 
722
    {
 
723
      column->null_bit= 0;
 
724
      column->null_pos= 0;
 
725
    }
 
726
 
 
727
    if (field->type() == DRIZZLE_TYPE_VARCHAR)
 
728
    {
 
729
      column->length_bytes= (uint8_t)(((Field_varstring*)field)->length_bytes);
 
730
    }
 
731
    else
 
732
    {
 
733
      column->length_bytes= 0;
 
734
    }
 
735
  }
 
736
 
702
737
  for (key= parts= 0; key < keys; key++)
703
738
    parts+= table_arg->key_info[key].key_parts;
704
739
 
717
752
    keydef[key].flag=      (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
718
753
    keydef[key].seg=       seg;
719
754
 
 
755
    // case HA_KEY_ALG_HASH:
 
756
    keydef[key].algorithm= HA_KEY_ALG_HASH;
720
757
    mem_per_row_keys+= sizeof(char*) * 2; // = sizeof(HASH_INFO)
721
758
 
722
759
    for (; key_part != key_part_end; key_part++, seg++)
738
775
      next_field_pos= seg->start + seg->length;
739
776
      if (field->type() == DRIZZLE_TYPE_VARCHAR)
740
777
      {
741
 
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->pack_length_no_ptr());
 
778
        next_field_pos+= (uint8_t)(((Field_varstring*)field)->length_bytes);
742
779
      }
743
780
 
744
781
      if (next_field_pos > key_part_size) {
770
807
        auto_key= key+ 1;
771
808
        auto_key_type= field->key_type();
772
809
      }
773
 
      if ((uint)field->position() + 1 > max_key_fieldnr)
 
810
      if ((uint)field->field_index + 1 > max_key_fieldnr)
774
811
      {
775
812
        /* Do not use seg->fieldnr as it's not reliable in case of temp tables */
776
 
        max_key_fieldnr= field->position() + 1;
 
813
        max_key_fieldnr= field->field_index + 1;
777
814
      }
778
815
    }
779
816
  }
800
837
  hp_create_info.with_auto_increment= found_real_auto_increment;
801
838
  hp_create_info.internal_table= internal_table;
802
839
  hp_create_info.max_chunk_size= table_arg->getShare()->block_size;
 
840
  hp_create_info.is_dynamic= false;
803
841
 
804
 
  error= heap_create(table_name,
805
 
                     keys, &keydef[0],
806
 
                     column_count,
807
 
                     key_part_size,
808
 
                     table_arg->getShare()->getRecordLength(), mem_per_row_keys,
809
 
                     static_cast<uint32_t>(num_rows), /* We check for overflow above, so cast is fine here. */
810
 
                     0, // Factor out MIN
811
 
                     &hp_create_info, internal_share);
 
842
  error= heap_create(internal::fn_format(buff,table_name,"","",
 
843
                              MY_REPLACE_EXT|MY_UNPACK_FILENAME),
 
844
                    keys, &keydef[0],
 
845
                    column_count, &columndef[0],
 
846
                    max_key_fieldnr, key_part_size,
 
847
                    table_arg->getShare()->getRecordLength(), mem_per_row_keys,
 
848
                    static_cast<uint32_t>(num_rows), /* We check for overflow above, so cast is fine here. */
 
849
                    0, // Factor out MIN
 
850
                    &hp_create_info, internal_share);
812
851
 
813
852
  return (error);
814
853
}