~drizzle-trunk/drizzle/development

1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1239.3.28 by Toru Maesaka
Added UNIQUE constraint check for UPDATE queries on VARCHAR key(s) and some tests for it.
4
 *  Copyright (C) 2009 - 2010 Toru Maesaka
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
1239.5.2 by Stewart Smith
fix cpplint warning r.e. header guard for BlitzDB
20
#ifndef PLUGIN_BLITZDB_HA_BLITZ_H
21
#define PLUGIN_BLITZDB_HA_BLITZ_H
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
22
1239.3.18 by Toru Maesaka
Merged Drizzle's Trunk
23
#include "drizzled/session.h"
24
#include "drizzled/cursor.h"
25
#include "drizzled/table.h"
26
#include "drizzled/field.h"
27
#include "drizzled/field/blob.h"
28
#include "drizzled/atomics.h"
29
#include "drizzled/error.h"
30
#include "drizzled/gettext.h"
1239.4.3 by Monty Taylor
Updated for out of tree builds.
31
#include "drizzled/cached_directory.h"
1239.3.121 by Toru Maesaka
Use internal::my_rename instead of std::rename to keep FreeBSD happy.
32
#include "drizzled/internal/my_sys.h"
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
33
#include <tchdb.h>
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
34
#include <tcbdb.h>
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
35
36
#include <string>
1239.3.102 by Toru Maesaka
Fixed warnings generated in the Build Farm and updated tests to make TC version differences irrelevant.
37
#include <sys/stat.h>
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
38
1239.3.129 by Toru Maesaka
Added a command line option for optimizing BlitzDB.
39
/* File Extensions */
1239.3.76 by Toru Maesaka
Base work for implementing ha_blitz::records_in_range().
40
#define BLITZ_DATA_EXT         ".bzd"
41
#define BLITZ_INDEX_EXT        ".bzx"
42
#define BLITZ_SYSTEM_EXT       ".bzs"
1239.3.129 by Toru Maesaka
Added a command line option for optimizing BlitzDB.
43
44
/* Constants for BlitzDB */
1239.3.76 by Toru Maesaka
Base work for implementing ha_blitz::records_in_range().
45
#define BLITZ_LOCK_SLOTS       16
1239.3.100 by Toru Maesaka
Allow NULL to be duplicately inserted into a UNIQUE INDEX. Added some tests for it as well. More patches to come.
46
#define BLITZ_MAX_INDEX        8
1239.3.76 by Toru Maesaka
Base work for implementing ha_blitz::records_in_range().
47
#define BLITZ_MAX_META_LEN     128
48
#define BLITZ_MAX_ROW_STACK    2048
49
#define BLITZ_MAX_KEY_LEN      1024 
1239.3.81 by Toru Maesaka
Added BlitzTree::next_logical_key() for HA_READ_AFTER_KEY search mode. BlitzDB is now HA_READ_RANGE compatible.
50
#define BLITZ_WORST_CASE_RANGE 4
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
51
1239.3.129 by Toru Maesaka
Added a command line option for optimizing BlitzDB.
52
/* Constants for TC */
53
#define BLITZ_TC_EXTRA_MMAP_SIZE (1024 * 1024 * 256)
54
#define BLITZ_TC_BUCKETS 1000000
55
1239.5.4 by Stewart Smith
cpplint for BlitzDB: Do not use namespace using-directives in headers.
56
const std::string BLITZ_TABLE_PROTO_KEY = "table_definition";
57
const std::string BLITZ_TABLE_PROTO_COMMENT_KEY = "table_definition_comment";
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
58
1239.3.128 by Toru Maesaka
Added base work for sysvar handling in BlitzDB.
59
extern uint64_t blitz_estimated_rows;
60
1239.3.108 by Toru Maesaka
Removed dead function declaration and worked on BlitzCursor object.
61
/* Class Prototype */
62
class BlitzLock;
63
class BlitzData;
64
class BlitzKeyPart;
65
class BlitzCursor;
66
class BlitzTree;
67
class BlitzShare;
68
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
69
/* Multi Reader-Writer lock responsible for controlling concurrency
70
   at the handler level. This class is implemented in blitzlock.cc */
71
class BlitzLock {
72
private:
73
  int scanner_count;
74
  int updater_count;
1239.3.37 by Toru Maesaka
Added a slotted lock mechanism to BlitzLock. This is used to atomically update the index file(s) and the data dictionary with concurrency in mind.
75
  pthread_cond_t condition;
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
76
  pthread_mutex_t mutex;
1239.3.37 by Toru Maesaka
Added a slotted lock mechanism to BlitzLock. This is used to atomically update the index file(s) and the data dictionary with concurrency in mind.
77
  pthread_mutex_t slots[BLITZ_LOCK_SLOTS];
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
78
79
public:
80
  BlitzLock();
81
  ~BlitzLock();
82
1239.3.37 by Toru Maesaka
Added a slotted lock mechanism to BlitzLock. This is used to atomically update the index file(s) and the data dictionary with concurrency in mind.
83
  /* Slotted Lock Mechanism for Concurrently and Atomically
84
     updating the index and data dictionary at the same time. */
85
  uint32_t slot_id(const void *data, size_t len);
86
  int slotted_lock(const uint32_t slot_id);
87
  int slotted_unlock(const uint32_t slot_id);
88
89
  /* Multi Reader-Writer Lock Mechanism. */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
90
  void update_begin();
91
  void update_end();
92
  void scan_begin();
93
  void scan_end();
94
  void scan_update_begin();
95
  void scan_update_end();
96
};
97
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
98
/* Handler that takes care of all I/O to the data dictionary
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
99
   that holds actual rows. */
100
class BlitzData {
101
private:
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
102
  TCHDB *data_table;    /* Where the actual row data lives */
103
  TCHDB *system_table;  /* Keeps track of system info */
104
  char *tc_meta_buffer; /* Tokyo Cabinet's Persistent Meta Buffer */
1239.3.6 by Toru Maesaka
Use Drizzle's atomic memory access wrapper to generate hidden row IDs.
105
  drizzled::atomic<uint64_t> current_hidden_row_id;
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
106
107
public:
1239.3.132 by Toru Maesaka
Refactored the data file creation routine in BlitzData class.
108
  BlitzData() : data_table(NULL), system_table(NULL), tc_meta_buffer(NULL) {
109
    current_hidden_row_id = 0;
110
  }
1239.3.8 by Toru Maesaka
Remove redundant calculation in row packing mechanism.
111
  ~BlitzData() {}
1239.3.34 by Toru Maesaka
Separated the codebase for Data Dictionary and System Table. TC object is no longer exposed to the handler.
112
  int startup(const char *table_path);
113
  int shutdown(void);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
114
115
  /* DATA DICTIONARY CREATION RELATED */
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
116
  int create_data_table(drizzled::message::Table &proto,
1239.3.88 by Toru Maesaka
Minor cleanup and interface changes to suit the updated Drizzle Storage API.
117
                        drizzled::Table &table,
1627.2.6 by Monty Taylor
Merged in constification of TableIdentifier from Brian.
118
                        const drizzled::TableIdentifier &identifier);
1239.3.88 by Toru Maesaka
Minor cleanup and interface changes to suit the updated Drizzle Storage API.
119
1239.3.34 by Toru Maesaka
Separated the codebase for Data Dictionary and System Table. TC object is no longer exposed to the handler.
120
  int open_data_table(const char *path, const int mode);
121
  int close_data_table(void);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
122
  bool rename_table(const char *from, const char *to);
123
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
124
  /* DATA DICTIONARY METADATA RELATED */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
125
  uint64_t nrecords(void);
1239.3.19 by Toru Maesaka
Removed all thr_lock related code. Supported primary key based deletion and added a test for it.
126
  uint64_t table_size(void);
1239.3.30 by Toru Maesaka
Added a wrapper to access tc_meta_buffer. Our new policy is to not directly use it.
127
  uint64_t read_meta_row_id(void);
1239.3.31 by Toru Maesaka
Use TC's Meta Buffer for storing auto increment value instead of the system table. Fixed a valgrind warning too.
128
  uint64_t read_meta_autoinc(void);
1239.3.32 by Toru Maesaka
Write the number of keys to TC's Meta Buffer on table creation. Made minor changes to BlitzTree's interface.
129
  uint32_t read_meta_keycount(void);
1239.3.30 by Toru Maesaka
Added a wrapper to access tc_meta_buffer. Our new policy is to not directly use it.
130
  void write_meta_row_id(uint64_t row_id);
1239.3.31 by Toru Maesaka
Use TC's Meta Buffer for storing auto increment value instead of the system table. Fixed a valgrind warning too.
131
  void write_meta_autoinc(uint64_t num);
1239.3.32 by Toru Maesaka
Write the number of keys to TC's Meta Buffer on table creation. Made minor changes to BlitzTree's interface.
132
  void write_meta_keycount(uint32_t nkeys);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
133
134
  /* DATA DICTIONARY READ RELATED*/
135
  char *get_row(const char *key, const size_t klen, int *value_len);
136
  char *next_key_and_row(const char *key, const size_t klen,
137
                         int *next_key_len, const char **value,
1239.3.16 by Toru Maesaka
Added index_init() and index_first() for Primary Keys.
138
                         int *value_len);
139
  char *first_row(int *row_len);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
140
141
  /* DATA DICTIONARY WRITE RELATED */
142
  uint64_t next_hidden_row_id(void);
1239.3.12 by Toru Maesaka
Resolve Primary Key and cleanup BlitzData API. Index Support Prep Work 2.
143
  int write_row(const char *key, const size_t klen,
144
                const unsigned char *row, const size_t rlen);
145
  int write_unique_row(const char *key, const size_t klen,
146
                       const unsigned char *row, const size_t rlen);
1239.3.66 by Toru Maesaka
Started working on completing ha_blitz::delete_row(). Refactored index_read_idx() to suit the new comparator. Index update tests are disabled until ha_blitz::update_row() is completed.
147
  int delete_row(const char *key, const size_t klen);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
148
  bool delete_all_rows(void);
1239.3.34 by Toru Maesaka
Separated the codebase for Data Dictionary and System Table. TC object is no longer exposed to the handler.
149
150
  /* SYSTEM TABLE RELATED */
1239.3.88 by Toru Maesaka
Minor cleanup and interface changes to suit the updated Drizzle Storage API.
151
  int create_system_table(const std::string &path);
152
  int open_system_table(const std::string &path, const int mode);
1239.3.34 by Toru Maesaka
Separated the codebase for Data Dictionary and System Table. TC object is no longer exposed to the handler.
153
  int close_system_table(void);
154
  bool write_table_definition(drizzled::message::Table &proto);
155
  char *get_system_entry(const char *key, const size_t klen, int *vlen);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
156
};
157
1239.3.40 by Toru Maesaka
Extract useful key information from the internal KEY structure at open() and keep it in BlitzDB.
158
/* This class is only used by the BlitzTree object which has a long life
1239.3.112 by Toru Maesaka
Merged Drizzle's Trunk.
159
   span. In general we use the Cursor's local KeyPartInfo array for
1239.3.40 by Toru Maesaka
Extract useful key information from the internal KEY structure at open() and keep it in BlitzDB.
160
   obtaining key information. We create our own array of key information
161
   because there is no guarantee that the pointer to the internal key_info
162
   array will always be alive. */
163
class BlitzKeyPart {
164
public:
165
  BlitzKeyPart() : offset(0), null_pos(0), flag(0), length(0), type(0),
166
                   null_bitmask(0) {}
167
  ~BlitzKeyPart() {}
168
169
  uint32_t offset;      /* Offset of the key in the row */
170
  uint32_t null_pos;    /* Offset of the NULL indicator in the row */
171
  uint16_t flag;
172
  uint16_t length;      /* Length of the key */
173
  uint8_t type;         /* Type of the key */
174
  uint8_t null_bitmask; /* Bitmask to test for NULL */
175
};
176
1239.3.106 by Toru Maesaka
First step in splitting BlitzTree's cursor object.
177
class BlitzCursor {
178
public:
1239.3.108 by Toru Maesaka
Removed dead function declaration and worked on BlitzCursor object.
179
  BlitzCursor() : tree(NULL), cursor(NULL), moved(false),
180
                  active(false) {}
1239.3.106 by Toru Maesaka
First step in splitting BlitzTree's cursor object.
181
  ~BlitzCursor() {}
182
1239.3.108 by Toru Maesaka
Removed dead function declaration and worked on BlitzCursor object.
183
  BlitzTree *tree; /* Tree that this instance works on */
184
  BDBCUR *cursor;  /* Raw cursor to TC */
185
  bool moved;      /* Whether the key was implicitly moved */
186
  bool active;     /* Whether this cursor is active */
1239.3.109 by Toru Maesaka
Still not optimal but the cursor object is now separated between workers. This build survived sysbench's read-only OLTP test.
187
188
  /* B+TREE READ RELATED */
189
  char *first_key(int *key_len);
190
  char *final_key(int *key_len);
191
  char *next_key(int *key_len);
192
  char *prev_key(int *key_ken);
193
  char *next_logical_key(int *key_len);
194
  char *prev_logical_key(int *key_len);
195
196
  char *find_key(const int search_mode, const char *key,
197
                 const int klen, int *rv_len);
198
199
  /* B+TREE UPDATE RELATED */
200
  int delete_position(void);
1239.3.106 by Toru Maesaka
First step in splitting BlitzTree's cursor object.
201
};
202
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
203
/* Class that reprensents a BTREE index. Takes care of all I/O
1239.3.86 by Toru Maesaka
Merged up to rev:1360. drizzled::TableIdentifier Support.
204
   to the B+Tree index structure */
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
205
class BlitzTree {
206
private:
207
  TCBDB *btree;
1239.3.38 by Toru Maesaka
More work on indexing component. PK now gets a tree.
208
209
public:
1239.3.110 by Toru Maesaka
Removed old cursor creation/destroy code.
210
  BlitzTree() : length(0), nparts(0), type(0), unique(false) {}
1239.3.38 by Toru Maesaka
More work on indexing component. PK now gets a tree.
211
  ~BlitzTree() {}
212
213
  /* METADATA */
1239.3.40 by Toru Maesaka
Extract useful key information from the internal KEY structure at open() and keep it in BlitzDB.
214
  BlitzKeyPart *parts; /* Array of Key Part(s) */
1239.3.45 by Toru Maesaka
Optimized key generation (much leaner keys). Wrote a workaround for a HA_KEYTYPE_VARTEXT1 bug in Drizzle's Field_varstring. Added tests for a VARCHAR field less than 255 bytes.
215
  int length;          /* Length of the entire key */
1239.3.40 by Toru Maesaka
Extract useful key information from the internal KEY structure at open() and keep it in BlitzDB.
216
  int nparts;          /* Number of parts in this key */
1239.3.54 by Toru Maesaka
Supported secondary index insertion, Added meta information on the B+Tree cursor and some tests.
217
  int type;            /* Type of the key */
1239.3.40 by Toru Maesaka
Extract useful key information from the internal KEY structure at open() and keep it in BlitzDB.
218
  bool unique;         /* Whether this key is unique */
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
219
220
  /* BTREE INDEX CREATION RELATED */
1239.3.32 by Toru Maesaka
Write the number of keys to TC's Meta Buffer on table creation. Made minor changes to BlitzTree's interface.
221
  int open(const char *path, const int key_num, int mode);
222
  int create(const char *path, const int key_num);
1239.3.35 by Toru Maesaka
Made index file(s) droppable and renamable.
223
  int drop(const char *path, const int key_num);
224
  int rename(const char *from, const char *to, const int key_num);
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
225
  int close(void);
1239.3.11 by Toru Maesaka
Added appropriate locking for ALTER TABLE and test case for it. Record the number of indexes in BlitzShare.
226
1239.3.61 by Toru Maesaka
Worked on reducing the execution path of row insertion and supported NULL for key values. Added tests for NULL values in full index scan.
227
  /* KEY HANDLING */
1239.3.113 by Toru Maesaka
Improved cursor resource management. TC's cursor allocation only happens once per worker now.
228
  bool create_cursor(BlitzCursor *cursor);
1239.3.106 by Toru Maesaka
First step in splitting BlitzTree's cursor object.
229
  void destroy_cursor(BlitzCursor *cursor);
1239.3.61 by Toru Maesaka
Worked on reducing the execution path of row insertion and supported NULL for key values. Added tests for NULL values in full index scan.
230
1239.3.11 by Toru Maesaka
Added appropriate locking for ALTER TABLE and test case for it. Record the number of indexes in BlitzShare.
231
  /* BTREE INDEX WRITE RELATED */
1239.3.72 by Toru Maesaka
Fixed a critical concurrency bug that would cause inconsistency. Reworked BlitzTree and refactored memory management a little.
232
  int write(const char *key, const size_t klen);
233
  int write_unique(const char *key, const size_t klen);
1239.3.68 by Toru Maesaka
ha_blitz::delete_row() can now delete all index entries. Added basic tests for it as well. More to come.
234
  int delete_key(const char *key, const int klen);
1239.3.38 by Toru Maesaka
More work on indexing component. PK now gets a tree.
235
  int delete_all(void);
1239.3.45 by Toru Maesaka
Optimized key generation (much leaner keys). Wrote a workaround for a HA_KEYTYPE_VARTEXT1 bug in Drizzle's Field_varstring. Added tests for a VARCHAR field less than 255 bytes.
236
1239.3.10 by Toru Maesaka
Started hacking on indexing. Prep Work 1.
237
  /* BTREE METADATA RELATED */
238
  uint64_t records(void); 
239
};
240
1239.3.77 by Toru Maesaka
Isolated key comparison code. It's now a function that can be used anywhere in BlitzDB.
241
/* Callback function for TC's B+Tree key comparison. */
242
extern int blitz_keycmp_cb(const char *a, int alen,
243
                           const char *b, int blen, void *opaque);
244
/* Comparison function for Drizzle types. */
245
extern int packed_key_cmp(BlitzTree *, const char *a, const char *b,
246
                          int *a_real_len, int *b_real_len);
247
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
248
/* Object shared among all worker threads. Try to only add
249
   data that will not be updated at runtime or those that
250
   do not require locking. */
251
class BlitzShare {
252
public:
1239.3.11 by Toru Maesaka
Added appropriate locking for ALTER TABLE and test case for it. Record the number of indexes in BlitzShare.
253
  BlitzShare() : blitz_lock(), use_count(0), nkeys(0) {}
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
254
  ~BlitzShare() {}
255
1239.3.22 by Toru Maesaka
Added Auto Increment support on INSERT and tests for it. Nextup: UPDATE.
256
  drizzled::atomic<uint64_t> auto_increment_value;
257
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
258
  BlitzLock blitz_lock;    /* Handler level lock for BlitzDB */
259
  BlitzData dict;          /* Utility class of BlitzDB */
1239.3.36 by Toru Maesaka
Added code to allocate/deallocate b+tree indexes.
260
  BlitzTree *btrees;       /* Array of BTREE indexes */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
261
  std::string table_name;  /* Name and Length of the table */
262
  uint32_t use_count;      /* Reference counter of this object */
1239.3.11 by Toru Maesaka
Added appropriate locking for ALTER TABLE and test case for it. Record the number of indexes in BlitzShare.
263
  uint32_t nkeys;          /* Number of indexes in this table */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
264
  bool fixed_length_table; /* Whether the table is fixed length */
1239.3.4 by Toru Maesaka
Reworked position() and rnd_pos(). Started reworking update_row() for UPDATES with ORDER BY.
265
  bool primary_key_exists; /* Whether a PK exists in this table */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
266
};
267
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
268
class ha_blitz: public drizzled::Cursor {
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
269
private:
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
270
  BlitzShare *share;            /* Shared object among all threads */
1239.3.113 by Toru Maesaka
Improved cursor resource management. TC's cursor allocation only happens once per worker now.
271
  BlitzCursor *btree_cursor;    /* Array of B+Tree Cursor */
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
272
  drizzled::THR_LOCK_DATA lock; /* Drizzle Lock */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
273
274
  /* THREAD STATE */
1239.3.25 by Toru Maesaka
Obtain memory for key packing from heap instead from the stack. Minor memory handling changes in write_row() and code-cosmetic changes.
275
  bool table_scan;           /* Whether a table scan is occuring */
1239.3.69 by Toru Maesaka
First attempt at completing update_row(). Still needs cleaning and tuning but the flow is there and doesn't break existing tests.
276
  bool table_based;          /* Whether the query involves rnd_xxx() */
1239.3.25 by Toru Maesaka
Obtain memory for key packing from heap instead from the stack. Minor memory handling changes in write_row() and code-cosmetic changes.
277
  bool thread_locked;        /* Whether the thread is locked */
278
  uint32_t sql_command_type; /* Type of SQL command to process */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
279
1239.3.53 by Toru Maesaka
Removed dead variables, shuffled ha_blitz class around and added a 1KB buffer for exclusive use on keeping track of keys.
280
  /* RECORDED KEY IN TABLE OR INDEX READ */
281
  char *held_key;            /* Points to held_key_buf or an allocated key */
282
  char *held_key_buf;        /* Buffer used to copy an unallocated key */
283
  int held_key_len;          /* Length of the key being held */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
284
1239.3.53 by Toru Maesaka
Removed dead variables, shuffled ha_blitz class around and added a 1KB buffer for exclusive use on keeping track of keys.
285
  /* TABLE SCANNER VARIABLES */
1239.3.25 by Toru Maesaka
Obtain memory for key packing from heap instead from the stack. Minor memory handling changes in write_row() and code-cosmetic changes.
286
  char *current_key;         /* Current key in table scan */
287
  const char *current_row;   /* Current row in table scan */
288
  int current_key_len;       /* Length of the current key */
289
  int current_row_len;       /* Length of the current row */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
290
1239.3.72 by Toru Maesaka
Fixed a critical concurrency bug that would cause inconsistency. Reworked BlitzTree and refactored memory management a little.
291
  /* KEY PROCESSING BUFFERS */
292
  char *key_buffer;          /* Key generation buffer */
293
  char *key_merge_buffer;    /* Key Merge buffer for B+Tree */
294
  size_t key_merge_buffer_len;  /* Size of the merge buffer */
295
1239.3.53 by Toru Maesaka
Removed dead variables, shuffled ha_blitz class around and added a 1KB buffer for exclusive use on keeping track of keys.
296
  /* ROW PROCESSING VARIABLES */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
297
  unsigned char pack_buffer[BLITZ_MAX_ROW_STACK]; /* Pack Buffer */
298
  unsigned char *secondary_row_buffer;            /* For big rows */
299
  size_t secondary_row_buffer_size;               /* Reserved buffer size */
1239.3.13 by Toru Maesaka
Added basic index key handling. Started working on PRIMARY KEY support and added tests for it.
300
  int errkey_id;
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
301
302
public:
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
303
  ha_blitz(drizzled::plugin::StorageEngine &engine_arg,
1869.1.4 by Brian Aker
TableShare is no longer in the house (i.e. we no longer directly have a copy
304
           drizzled::Table &table_arg);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
305
  ~ha_blitz() {}
306
1239.3.17 by Toru Maesaka
Implemented index_read() and index_read_idx() for PK and added a simple needle in a haystack test.
307
  /* TABLE CONTROL RELATED FUNCTIONS */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
308
  const char **bas_ext() const;
1239.3.17 by Toru Maesaka
Implemented index_read() and index_read_idx() for PK and added a simple needle in a haystack test.
309
  const char *index_type(uint32_t key_num);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
310
  int open(const char *name, int mode, uint32_t open_options);
311
  int close(void);
312
  int info(uint32_t flag);
313
1239.3.16 by Toru Maesaka
Added index_init() and index_first() for Primary Keys.
314
  /* TABLE SCANNER RELATED FUNCTIONS */
1239.3.105 by Toru Maesaka
Merged Drizzle's Trunk.
315
  int doStartTableScan(bool scan);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
316
  int rnd_next(unsigned char *buf);
1239.3.105 by Toru Maesaka
Merged Drizzle's Trunk.
317
  int doEndTableScan(void);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
318
  int rnd_pos(unsigned char *buf, unsigned char *pos);
319
320
  void position(const unsigned char *record);
321
1239.3.16 by Toru Maesaka
Added index_init() and index_first() for Primary Keys.
322
  /* INDEX RELATED FUNCTIONS */
1239.3.105 by Toru Maesaka
Merged Drizzle's Trunk.
323
  int doStartIndexScan(uint32_t key_num, bool sorted);
1239.3.16 by Toru Maesaka
Added index_init() and index_first() for Primary Keys.
324
  int index_first(unsigned char *buf);
1239.3.47 by Toru Maesaka
Implemented forward index scan and basic tests for it. More tests to come.
325
  int index_next(unsigned char *buf);
1239.3.50 by Toru Maesaka
Implemented reverse index scan and added tests for it. Fixed a bug in delete_all_rows().
326
  int index_prev(unsigned char *buf);
327
  int index_last(unsigned char *buf);
1239.3.17 by Toru Maesaka
Implemented index_read() and index_read_idx() for PK and added a simple needle in a haystack test.
328
  int index_read(unsigned char *buf, const unsigned char *key,
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
329
                 uint32_t key_len, enum drizzled::ha_rkey_function find_flag);
1239.3.17 by Toru Maesaka
Implemented index_read() and index_read_idx() for PK and added a simple needle in a haystack test.
330
  int index_read_idx(unsigned char *buf, uint32_t key_num,
331
                     const unsigned char *key, uint32_t key_len,
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
332
                     enum drizzled::ha_rkey_function find_flag);
1239.3.105 by Toru Maesaka
Merged Drizzle's Trunk.
333
  int doEndIndexScan(void);
1239.3.122 by Toru Maesaka
Implemented Cursor::enable_indexes() and Cursor::disable_indexes(). Added a test file it as well.
334
  int enable_indexes(uint32_t mode);  /* For ALTER ... ENABLE KEYS */
335
  int disable_indexes(uint32_t mode); /* For ALTER ... DISABLE KEYS */
1239.3.16 by Toru Maesaka
Added index_init() and index_first() for Primary Keys.
336
1239.3.76 by Toru Maesaka
Base work for implementing ha_blitz::records_in_range().
337
  drizzled::ha_rows records_in_range(uint32_t key_num,
338
                                     drizzled::key_range *min_key,
339
                                     drizzled::key_range *max_key);
340
1239.3.16 by Toru Maesaka
Added index_init() and index_first() for Primary Keys.
341
  /* UPDATE RELATED FUNCTIONS */
1239.3.98 by Toru Maesaka
Merged Trunk.
342
  int doInsertRecord(unsigned char *buf);
343
  int doUpdateRecord(const unsigned char *old_data, unsigned char *new_data);
344
  int doDeleteRecord(const unsigned char *buf);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
345
  int delete_all_rows(void);
1239.3.22 by Toru Maesaka
Added Auto Increment support on INSERT and tests for it. Nextup: UPDATE.
346
  virtual void get_auto_increment(uint64_t offset, uint64_t increment,
347
                                  uint64_t nb_desired_values,
348
                                  uint64_t *first_value,
349
                                  uint64_t *nb_reserved_values);
350
  int reset_auto_increment(uint64_t value);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
351
1239.3.29 by Toru Maesaka
Added UNIQUE constraint check on fixed length fields and more tests. Moved get_share() to ha_blitz class.
352
  /* UTILITY FUNCTIONS (BLITZDB SPECIFIC) */
353
  BlitzShare *get_share(const char *table_name);
1239.3.90 by Toru Maesaka
free_share() is now a member of Cursor::ha_blitz.
354
  int free_share(void);
1239.3.29 by Toru Maesaka
Added UNIQUE constraint check on fixed length fields and more tests. Moved get_share() to ha_blitz class.
355
1239.3.28 by Toru Maesaka
Added UNIQUE constraint check for UPDATE queries on VARCHAR key(s) and some tests for it.
356
  /* LOCK RELATED FUNCTIONS (BLITZDB SPECIFIC) */
1239.3.127 by Toru Maesaka
Added more index related tests and renamed BlitzDB's lock functions.
357
  int blitz_optimal_lock();
358
  int blitz_optimal_unlock();
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
359
  uint32_t max_row_length(void);
1239.3.28 by Toru Maesaka
Added UNIQUE constraint check for UPDATE queries on VARCHAR key(s) and some tests for it.
360
361
  /* INDEX KEY RELATED FUNCTIONS (BLITZDB SPECIFIC) */
1239.3.39 by Toru Maesaka
Reworked the key packer to support null fields and deleted now a redundant function: pack_index_key_from_row().
362
  size_t make_primary_key(char *pack_to, const unsigned char *row);
363
  size_t make_index_key(char *pack_to, int key_num, const unsigned char *row);
1239.3.47 by Toru Maesaka
Implemented forward index scan and basic tests for it. More tests to come.
364
  size_t btree_key_length(const char *key, const int key_num);
1239.3.23 by Toru Maesaka
Supported VARCHAR to be usable as index key(s). Also added basic tests where VARCHAR is used as PK.
365
  char *native_to_blitz_key(const unsigned char *native_key,
366
                            const int key_num, int *return_key_length);
1239.3.72 by Toru Maesaka
Fixed a critical concurrency bug that would cause inconsistency. Reworked BlitzTree and refactored memory management a little.
367
  char *merge_key(const char *a, const size_t a_len, const char *b,
368
                  const size_t b_len, size_t *merged_len);
1239.3.53 by Toru Maesaka
Removed dead variables, shuffled ha_blitz class around and added a 1KB buffer for exclusive use on keeping track of keys.
369
  void keep_track_of_key(const char *key, const int klen);
1239.3.28 by Toru Maesaka
Added UNIQUE constraint check for UPDATE queries on VARCHAR key(s) and some tests for it.
370
371
  /* ROW RELATED FUNCTIONS (BLITZDB SPECIFIC) */
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
372
  size_t pack_row(unsigned char *row_buffer, unsigned char *row_to_pack);
1239.3.8 by Toru Maesaka
Remove redundant calculation in row packing mechanism.
373
  bool unpack_row(unsigned char *to, const char *from, const size_t from_len);
374
  unsigned char *get_pack_buffer(const size_t size);
1239.3.28 by Toru Maesaka
Added UNIQUE constraint check for UPDATE queries on VARCHAR key(s) and some tests for it.
375
376
  /* COMAPARISON LOGIC (BLITZDB SPECIFIC) */
377
  int compare_rows_for_unique_violation(const unsigned char *old_row,
378
                                        const unsigned char *new_row);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
379
};
380
1239.5.2 by Stewart Smith
fix cpplint warning r.e. header guard for BlitzDB
381
#endif /* PLUGIN_BLITZDB_HA_BLITZ_H */