1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Toru Maesaka
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.
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.
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
20
#ifndef STORAGE_BLITZ_HA_BLITZ_H
21
#define STORAGE_BLITZ_HA_BLITZ_H
23
#include <drizzled/server_includes.h>
24
#include <drizzled/session.h>
25
#include <drizzled/cursor.h>
26
#include <drizzled/table.h>
27
#include <drizzled/field.h>
28
#include <drizzled/field/blob.h>
29
#include <drizzled/error.h>
30
#include <drizzled/gettext.h>
31
#include <mysys/thr_lock.h>
36
#define BLITZ_DATAFILE_EXT ".bzd"
37
#define BLITZ_INDEX_FILE_EXT ".bzx"
38
#define BLITZ_SYSTEM_EXT ".bzs"
39
#define BLITZ_MAX_ROW_STACK 2048
40
#define BLITZ_MAX_KEY_LENGTH 128
44
const string BLITZ_TABLE_PROTO_KEY = "table_definition";
45
const string BLITZ_TABLE_PROTO_COMMENT_KEY = "table_definition_comment";
47
static const char *ha_blitz_exts[] = {
54
/* Multi Reader-Writer lock responsible for controlling concurrency
55
at the handler level. This class is implemented in blitzlock.cc */
60
pthread_mutex_t mutex;
61
pthread_cond_t condition;
71
void scan_update_begin();
72
void scan_update_end();
75
/* Handler that takes care of all I/O to the the data dictionary
76
that holds actual rows. */
79
TCHDB *data_table; /* Where the actual row data lives */
80
TCHDB *system_table; /* Keeps track of system info */
81
pthread_mutex_t id_lock; /* Lock for hidden id generation */
82
char *tc_meta_buffer; /* Tokyo Cabinet's Persistent Meta Buffer */
83
uint64_t current_hidden_id; /* Current row id */
88
bool startup(const char *table_path);
91
/* DATA DICTIONARY CREATION RELATED */
92
TCHDB *open_table(const char *path, const char *ext, int mode);
93
int create_table(const char *table_path, const char *ext);
94
bool close_table(TCHDB *table);
95
bool rename_table(const char *from, const char *to);
96
bool write_table_definition(TCHDB *system_table,
97
drizzled::message::Table &proto);
99
/* DATA DICTIONARY META INFO RELATED */
100
uint64_t nrecords(void);
102
/* DATA DICTIONARY READ RELATED*/
103
char *get_row(const char *key, const size_t klen, int *value_len);
104
char *next_key_and_row(const char *key, const size_t klen,
105
int *next_key_len, const char **value,
107
uint16_t fetch_position(unsigned char *position_buf,
108
unsigned char *key_ptr);
109
void store_position(unsigned char *ref, char *key, const size_t klen);
111
/* DATA DICTIONARY WRITE RELATED */
112
uint64_t next_hidden_row_id(void);
113
size_t generate_table_key(char *key_buffer);
114
bool overwrite_row(const char *key, const size_t klen,
115
const unsigned char *row, const size_t rlen);
116
bool delete_row(const char *key, const size_t klen);
117
bool delete_all_rows(void);
120
/* Object shared among all worker threads. Try to only add
121
data that will not be updated at runtime or those that
122
do not require locking. */
125
BlitzShare() : blitz_lock(), use_count(0) {}
128
THR_LOCK lock; /* Shared Drizzle Lock */
129
BlitzLock blitz_lock; /* Handler level lock for BlitzDB */
130
BlitzData dict; /* Utility class of BlitzDB */
131
std::string table_name; /* Name and Length of the table */
132
uint32_t use_count; /* Reference counter of this object */
133
bool fixed_length_table; /* Whether the table is fixed length */
136
class ha_blitz: public Cursor {
138
BlitzShare *share; /* Shared object among all threads */
139
THR_LOCK_DATA lock; /* Drizzle Lock */
142
bool table_scan; /* Whether a scan is occuring */
143
bool thread_locked; /* Whether the thread is locked */
144
uint32_t sql_command_type; /* Type of SQL command to process */
146
/* KEY GENERATION SPECIFIC VARIABLES */
147
char key_buffer[BLITZ_MAX_KEY_LENGTH]; /* Buffer for key generation */
148
size_t generated_key_length; /* Length of the generated key */
150
/* TABLE SCANNER SPECIFIC VARIABLES */
151
char *current_key; /* Current key in table scan */
152
const char *current_row; /* Current row in table scan */
153
int current_key_length; /* Length of the current key */
154
int current_row_length; /* Length of the current row */
155
char *updateable_key; /* Used in table scan */
156
int updateable_key_length; /* Length of updateable key */
158
/* ROW PROCESSING SPECIFIC VARIABLES */
159
unsigned char pack_buffer[BLITZ_MAX_ROW_STACK]; /* Pack Buffer */
160
unsigned char *secondary_row_buffer; /* For big rows */
161
size_t secondary_row_buffer_size; /* Reserved buffer size */
164
ha_blitz(drizzled::plugin::StorageEngine &engine_arg, TableShare &table_arg);
167
const char *table_type() const { return "BLITZ"; }
168
const char *index_type(uint32_t) { return "BTREE"; };
169
const char **bas_ext() const;
171
uint32_t index_flags(uint32_t inx, uint32_t part, bool all_parts) const;
173
int open(const char *name, int mode, uint32_t open_options);
175
int info(uint32_t flag);
177
int rnd_init(bool scan);
178
int rnd_next(unsigned char *buf);
180
int rnd_pos(unsigned char *buf, unsigned char *pos);
182
void position(const unsigned char *record);
184
int write_row(unsigned char *buf);
185
int update_row(const unsigned char *old_data, unsigned char *new_data);
186
int delete_row(const unsigned char *buf);
187
int delete_all_rows(void);
192
THR_LOCK_DATA **store_lock(Session *session, THR_LOCK_DATA **to,
193
enum thr_lock_type lock_type);
195
/* BLITZDB SPECIFIC THREAD SPECIFIC FUNCTIONS */
196
uint32_t max_row_length(void);
197
size_t pack_row(unsigned char *row_buffer, unsigned char *row_to_pack);
198
bool unpack_row(unsigned char *to, const char *from);
199
unsigned char *get_pack_buffer(void);
202
class BlitzEngine : public drizzled::plugin::StorageEngine {
204
BlitzEngine(const string &name_arg)
205
: drizzled::plugin::StorageEngine(name_arg,
207
HTON_STATS_RECORDS_IS_EXACT |
209
HTON_HAS_DATA_DICTIONARY) {
210
table_definition_ext = BLITZ_SYSTEM_EXT;
214
virtual Cursor *create(TableShare &table, MEM_ROOT *mem_root) {
215
return new (mem_root) ha_blitz(*this, table);
218
const char **bas_ext() const {
219
return ha_blitz_exts;
222
int doCreateTable(Session *session, const char *table_name,
223
Table &table_arg, drizzled::message::Table&);
225
int doRenameTable(Session *session, const char *from, const char *to);
227
int doDropTable(Session&, const string table_name);
229
int doGetTableDefinition(Session& session, const char *path, const char *db,
230
const char *table_name, const bool is_tmp,
231
drizzled::message::Table *table_proto);
233
void doGetTableNames(CachedDirectory &directory, string&,
234
set<string>& set_of_names);
236
uint32_t max_supported_keys() const { return 0; }
237
uint32_t max_supported_key_length() const { return 0; }
238
uint32_t max_supported_key_parts() const { return 0; }
239
uint32_t max_supported_key_part_length() const { return 0; }
242
#endif /* STORAGE_BLITZ_HA_BLITZ_H */