~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/blitzdb/blitzdata.cc

Use TC's Meta Buffer for storing auto increment value instead of the system table. Fixed a valgrind warning too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
}
55
55
 
56
56
/* Similar to UNIX touch(1) but generates a TCHDB file. */
57
 
int BlitzData::create_table(const char *table_path, const char *ext) {
 
57
int BlitzData::create_table(drizzled::message::Table &proto,
 
58
                            const char *table_path, const char *ext) {
58
59
  TCHDB *table;
59
60
  int mode = (HDBOWRITER | HDBOCREAT);
60
61
 
61
62
  if ((table = open_table(table_path, ext, mode)) == NULL)
62
63
    return HA_ERR_CRASHED_ON_USAGE;
63
64
 
 
65
  if (strcmp(ext, BLITZ_DATA_EXT) == 0) {
 
66
    uint64_t autoinc = (proto.options().has_auto_increment_value())
 
67
                       ? proto.options().auto_increment_value() - 1 : 0;
 
68
    write_meta_autoinc(autoinc); 
 
69
  }
 
70
 
64
71
  if (!close_table(table))
65
72
    return HA_ERR_CRASHED_ON_USAGE;
66
73
 
175
182
  return (uint64_t)uint8korr(tc_meta_buffer);
176
183
}
177
184
 
 
185
uint64_t BlitzData::read_meta_autoinc(void) {
 
186
  assert(tc_meta_buffer);
 
187
  char *pos = tc_meta_buffer + sizeof (current_hidden_row_id);
 
188
  return (uint64_t)uint8korr(pos);
 
189
}
 
190
 
178
191
void BlitzData::write_meta_row_id(uint64_t row_id) {
179
192
  assert(tc_meta_buffer);
180
193
  int8store(tc_meta_buffer, row_id);
181
194
}
182
195
 
 
196
void BlitzData::write_meta_autoinc(uint64_t num) {
 
197
  assert(tc_meta_buffer);
 
198
  char *pos = tc_meta_buffer + sizeof(current_hidden_row_id);
 
199
  int8store(pos, num);
 
200
}
 
201
 
183
202
char *BlitzData::get_row(const char *key, const size_t klen, int *vlen) {
184
203
  return (char *)tchdbget(data_table, key, klen, vlen);
185
204
}
231
250
bool BlitzData::delete_all_rows() {
232
251
  return tchdbvanish(data_table);
233
252
}
234
 
 
235
 
uint64_t BlitzData::autoinc_in_system_table(void) {
236
 
  char *val;
237
 
  int length;
238
 
  uint64_t rv = 0;
239
 
 
240
 
  val = (char *)tchdbget(system_table, BLITZ_AUTOINC_KEY.c_str(),
241
 
                         BLITZ_AUTOINC_KEY.length(), &length);
242
 
  if (val == NULL)
243
 
    return 0;
244
 
 
245
 
  rv = uint8korr(val);
246
 
  free(val);
247
 
  return rv;
248
 
}
249
 
 
250
 
/* The following functions are for writing the latest auto increment
251
 
   value to the system table, which is also used for metadata storage.
252
 
   It is only used in two scenes: When a table is created or closed. */
253
 
bool BlitzData::flush_autoinc(uint64_t autoinc_val) {
254
 
  return flush_autoinc(system_table, autoinc_val);
255
 
}
256
 
 
257
 
bool BlitzData::flush_autoinc(TCHDB *prebuilt, uint64_t autoinc_val) {
258
 
  char buf[sizeof(autoinc_val)];
259
 
  int8store(buf, autoinc_val);
260
 
  return tchdbput(prebuilt, BLITZ_AUTOINC_KEY.c_str(),
261
 
                  BLITZ_AUTOINC_KEY.length(), buf, sizeof(autoinc_val));
262
 
}