ha_tokudb.h 23.8 KB
Newer Older
1 2 3
#if !defined(HA_TOKUDB_H)
#define HA_TOKUDB_H

4
#include <db.h>
Zardosht Kasheff's avatar
Zardosht Kasheff committed
5
#include "hatoku_cmp.h"
6

7 8 9 10 11 12 13
class ha_tokudb;

typedef struct loader_context {
    THD* thd;
    char write_status_msg[200];
    ha_tokudb* ha;
} *LOADER_CONTEXT;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
14

15 16
typedef struct hot_optimize_context {
    THD *thd;
17
    char* write_status_msg;
18
    ha_tokudb *ha;
19
    uint progress_stage;
20 21 22 23
    uint current_table;
    uint num_tables;
} *HOT_OPTIMIZE_CONTEXT;

24 25 26 27 28 29 30
//
// This object stores table information that is to be shared
// among all ha_tokudb objects.
// There is one instance per table, shared among threads.
// Some of the variables here are the DB* pointers to indexes,
// and auto increment information.
//
31
typedef struct st_tokudb_share {
32 33 34 35 36 37
    char *table_name;
    uint table_name_length, use_count;
    pthread_mutex_t mutex;
    THR_LOCK lock;

    ulonglong auto_ident;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
38
    ulonglong last_auto_increment, auto_inc_create_value;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
39 40 41
    //
    // estimate on number of rows in table
    //
Zardosht Kasheff's avatar
Zardosht Kasheff committed
42
    ha_rows rows;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
43 44 45 46 47
    //
    // estimate on number of rows added in the process of a locked tables
    // this is so we can better estimate row count during a lock table
    //
    ha_rows rows_from_locked_table;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
48 49 50 51 52 53 54
    DB *status_block;
    //
    // DB that is indexed on the primary key
    //
    DB *file;
    //
    // array of all DB's that make up table, includes DB that
Zardosht Kasheff's avatar
Zardosht Kasheff committed
55 56
    // is indexed on the primary key, add 1 in case primary
    // key is hidden
Zardosht Kasheff's avatar
Zardosht Kasheff committed
57
    //
Zardosht Kasheff's avatar
Zardosht Kasheff committed
58
    DB *key_file[MAX_KEY +1];
59
    rw_lock_t key_file_lock;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
60
    uint status, version, capabilities;
61
    uint ref_length;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
62 63 64 65 66 67 68 69
    //
    // whether table has an auto increment column
    //
    bool has_auto_inc;
    //
    // index of auto increment column in table->field, if auto_inc exists
    //
    uint ai_field_index;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
70

71 72
    KEY_AND_COL_INFO kc_info;
    
73 74 75 76 77 78 79
    // 
    // we want the following optimization for bulk loads, if the table is empty, 
    // attempt to grab a table lock. emptiness check can be expensive, 
    // so we try it once for a table. After that, we keep this variable around 
    // to tell us to not try it again. 
    // 
    bool try_table_lock; 
80 81

    bool has_unique_keys;
82
    bool replace_into_fast;
83
    rw_lock_t num_DBs_lock;
Yoni Fogel's avatar
Yoni Fogel committed
84
    uint32_t num_DBs;
85 86
} TOKUDB_SHARE;

87 88
#define HA_TOKU_ORIG_VERSION 4
#define HA_TOKU_VERSION 4
Zardosht Kasheff's avatar
Zardosht Kasheff committed
89 90 91
//
// no capabilities yet
//
Zardosht Kasheff's avatar
Zardosht Kasheff committed
92
#define HA_TOKU_CAP 0
Zardosht Kasheff's avatar
Zardosht Kasheff committed
93 94 95 96 97 98

//
// These are keys that will be used for retrieving metadata in status.tokudb
// To get the version, one looks up the value associated with key hatoku_version
// in status.tokudb
//
Zardosht Kasheff's avatar
Zardosht Kasheff committed
99

Zardosht Kasheff's avatar
Zardosht Kasheff committed
100
typedef ulonglong HA_METADATA_KEY;
101
#define hatoku_old_version 0
Zardosht Kasheff's avatar
Zardosht Kasheff committed
102 103 104
#define hatoku_capabilities 1
#define hatoku_max_ai 2 //maximum auto increment value found so far
#define hatoku_ai_create_value 3
105
#define hatoku_key_name 4
106
#define hatoku_frm_data 5
107
#define hatoku_new_version 6
Zardosht Kasheff's avatar
Zardosht Kasheff committed
108

Zardosht Kasheff's avatar
Zardosht Kasheff committed
109
typedef struct st_filter_key_part_info {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
110 111
    uint offset;
    uint part_index;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
112
} FILTER_KEY_PART_INFO;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
113

Zardosht Kasheff's avatar
Zardosht Kasheff committed
114 115 116 117 118
typedef enum {
    lock_read = 0,
    lock_write
} TABLE_LOCK_TYPE;

119 120 121 122
// the number of rows bulk fetched in one callback grows exponentially
// with the bulk fetch iteration, so the max iteration is the max number
// of shifts we can perform on a 64 bit integer.
#define HA_TOKU_BULK_FETCH_ITERATION_MAX 63
123

124
class ha_tokudb : public handler {
125
private:
126 127 128
    THR_LOCK_DATA lock;         ///< MySQL lock
    TOKUDB_SHARE *share;        ///< Shared lock info

Zardosht Kasheff's avatar
Zardosht Kasheff committed
129 130 131 132 133
    //
    // last key returned by ha_tokudb's cursor
    //
    DBT last_key;
    //
Zardosht Kasheff's avatar
Zardosht Kasheff committed
134 135
    // pointer used for multi_alloc of key_buff, key_buff2, primary_key_buff
    //
136
    void *alloc_ptr;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
137 138 139 140 141
    //
    // buffer used to temporarily store a "packed row" 
    // data pointer of a DBT will end up pointing to this
    // see pack_row for usage
    //
142
    uchar *rec_buff;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
143 144 145 146
    //
    // number of bytes allocated in rec_buff
    //
    ulong alloced_rec_buff_length;
147 148 149 150 151
    //
    // same as above two, but for updates
    //
    uchar *rec_update_buff;
    ulong alloced_update_rec_buff_length;
Yoni Fogel's avatar
Yoni Fogel committed
152
    uint32_t max_key_length;
153 154

    uchar* range_query_buff; // range query buffer
Yoni Fogel's avatar
Yoni Fogel committed
155 156 157
    uint32_t size_range_query_buff; // size of the allocated range query buffer
    uint32_t bytes_used_in_range_query_buff; // number of bytes used in the range query buffer
    uint32_t curr_range_query_buff_offset; // current offset into the range query buffer for queries to read
158 159
    uint64_t bulk_fetch_iteration;
    uint64_t rows_fetched_using_bulk_fetch;
160 161
    bool doing_bulk_fetch;

Zardosht Kasheff's avatar
Zardosht Kasheff committed
162 163 164 165 166 167 168 169 170 171 172 173
    //
    // buffer used to temporarily store a "packed key" 
    // data pointer of a DBT will end up pointing to this
    //
    uchar *key_buff; 
    //
    // buffer used to temporarily store a "packed key" 
    // data pointer of a DBT will end up pointing to this
    // This is used in functions that require the packing
    // of more than one key
    //
    uchar *key_buff2; 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
174
    uchar *key_buff3; 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
175 176 177 178 179 180 181 182
    //
    // buffer used to temporarily store a "packed key" 
    // data pointer of a DBT will end up pointing to this
    // currently this is only used for a primary key in
    // the function update_row, hence the name. It 
    // does not carry any state throughout the class.
    //
    uchar *primary_key_buff;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
183

184 185 186 187
    //
    // ranges of prelocked area, used to know how much to bulk fetch
    //
    uchar *prelocked_left_range; 
Yoni Fogel's avatar
Yoni Fogel committed
188
    uint32_t prelocked_left_range_size;
189
    uchar *prelocked_right_range; 
Yoni Fogel's avatar
Yoni Fogel committed
190
    uint32_t prelocked_right_range_size;
191 192


193
    //
Zardosht Kasheff's avatar
Zardosht Kasheff committed
194
    // individual DBTs for each index
195
    //
196
    DBT mult_key_dbt[2*(MAX_KEY + 1)];
Zardosht Kasheff's avatar
Zardosht Kasheff committed
197
    DBT mult_rec_dbt[MAX_KEY + 1];
Yoni Fogel's avatar
Yoni Fogel committed
198 199 200
    uint32_t mult_put_flags[MAX_KEY + 1];
    uint32_t mult_del_flags[MAX_KEY + 1];
    uint32_t mult_dbt_flags[MAX_KEY + 1];
Zardosht Kasheff's avatar
Zardosht Kasheff committed
201
    
202

Zardosht Kasheff's avatar
Zardosht Kasheff committed
203 204 205 206 207 208 209
    //
    // when unpacking blobs, we need to store it in a temporary
    // buffer that will persist because MySQL just gets a pointer to the 
    // blob data, a pointer we need to ensure is valid until the next
    // query
    //
    uchar* blob_buff;
Yoni Fogel's avatar
Yoni Fogel committed
210
    uint32_t num_blob_bytes;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
211

Zardosht Kasheff's avatar
Zardosht Kasheff committed
212 213 214 215 216 217
    bool unpack_entire_row;

    //
    // buffers (and their sizes) that will hold the indexes
    // of fields that need to be read for a query
    //
Yoni Fogel's avatar
Yoni Fogel committed
218 219 220 221
    uint32_t* fixed_cols_for_query;
    uint32_t num_fixed_cols_for_query;
    uint32_t* var_cols_for_query;
    uint32_t num_var_cols_for_query;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
222 223 224
    bool read_blobs;
    bool read_key;

Zardosht Kasheff's avatar
Zardosht Kasheff committed
225 226 227
    //
    // transaction used by ha_tokudb's cursor
    //
228
    DB_TXN *transaction;
229
    bool is_fast_alter_running;
230 231 232

    // external_lock will set this true for read operations that will be closely followed by write operations.
    bool use_write_locks; // use write locks for reads
233

Zardosht Kasheff's avatar
Zardosht Kasheff committed
234 235 236
    //
    // instance of cursor being used for init_xxx and rnd_xxx functions
    //
237
    DBC *cursor;
Yoni Fogel's avatar
Yoni Fogel committed
238
    uint32_t cursor_flags; // flags for cursor
Zardosht Kasheff's avatar
Zardosht Kasheff committed
239 240 241
    //
    // flags that are returned in table_flags()
    //
Zardosht Kasheff's avatar
Zardosht Kasheff committed
242
    ulonglong int_table_flags;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
243
    // 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
244 245
    // count on the number of rows that gets changed, such as when write_row occurs
    // this is meant to help keep estimate on number of elements in DB
Zardosht Kasheff's avatar
Zardosht Kasheff committed
246 247 248
    // 
    ulonglong added_rows;
    ulonglong deleted_rows;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
249 250


Zardosht Kasheff's avatar
Zardosht Kasheff committed
251 252 253 254 255 256
    uint last_dup_key;
    //
    // if set to 0, then the primary key is not hidden
    // if non-zero (not necessarily 1), primary key is hidden
    //
    uint hidden_primary_key;
257
    bool key_read, using_ignore;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
258

Zardosht Kasheff's avatar
Zardosht Kasheff committed
259 260 261 262 263 264 265
    //
    // After a cursor encounters an error, the cursor will be unusable
    // In case MySQL attempts to do a cursor operation (such as rnd_next
    // or index_prev), we will gracefully return this error instead of crashing
    //
    int last_cursor_error;

Zardosht Kasheff's avatar
Zardosht Kasheff committed
266 267
    //
    // For instances where we successfully prelock a range or a table,
Yoni Fogel's avatar
Yoni Fogel committed
268
    // we set this to true so that successive cursor calls can know
Zardosht Kasheff's avatar
Zardosht Kasheff committed
269 270
    // know to limit the locking overhead in a call to the fractal tree
    //
Zardosht Kasheff's avatar
Zardosht Kasheff committed
271
    bool range_lock_grabbed;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
272

Zardosht Kasheff's avatar
Zardosht Kasheff committed
273 274 275 276
    //
    // For bulk inserts, we want option of not updating auto inc
    // until all inserts are done. By default, is false
    //
277 278
    bool delay_updating_ai_metadata; // if true, don't update auto-increment metadata until bulk load completes
    bool ai_metadata_update_required; // if true, autoincrement metadata must be updated 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
279

Zardosht Kasheff's avatar
Zardosht Kasheff committed
280 281 282 283 284 285 286
    //
    // buffer for updating the status of long insert, delete, and update
    // statements. Right now, the the messages are 
    // "[inserted|updated|deleted] about %llu rows",
    // so a buffer of 200 is good enough.
    //
    char write_status_msg[200]; //buffer of 200 should be a good upper bound.
287
    struct loader_context lc;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
288

289 290 291
    DB_LOADER* loader;
    bool abort_loader;
    int loader_error;
292 293

    bool num_DBs_locked_in_bulk;
Yoni Fogel's avatar
Yoni Fogel committed
294
    uint32_t lock_count;
295
    
296
    bool fix_rec_buff_for_blob(ulong length);
297
    bool fix_rec_update_buff_for_blob(ulong length);
298 299 300
    uchar current_ident[TOKUDB_HIDDEN_PRIMARY_KEY_LENGTH];

    ulong max_row_length(const uchar * buf);
301 302 303 304 305 306
    int pack_row_in_buff(
        DBT * row, 
        const uchar* record,
        uint index,
        uchar* row_buff
        );
Zardosht Kasheff's avatar
Zardosht Kasheff committed
307 308 309 310 311
    int pack_row(
        DBT * row, 
        const uchar* record,
        uint index
        );
312 313 314 315 316
    int pack_old_row_for_update(
        DBT * row, 
        const uchar* record,
        uint index
        );
Yoni Fogel's avatar
Yoni Fogel committed
317
    uint32_t place_key_into_mysql_buff(KEY* key_info, uchar * record, uchar* data);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
318
    void unpack_key(uchar * record, DBT const *key, uint index);
Yoni Fogel's avatar
Yoni Fogel committed
319
    uint32_t place_key_into_dbt_buff(KEY* key_info, uchar * buff, const uchar * record, bool* has_null, int key_length);
320
    DBT* create_dbt_key_from_key(DBT * key, KEY* key_info, uchar * buff, const uchar * record, bool* has_null, bool dont_pack_pk, int key_length = MAX_KEY_LENGTH);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
321
    DBT *create_dbt_key_from_table(DBT * key, uint keynr, uchar * buff, const uchar * record, bool* has_null, int key_length = MAX_KEY_LENGTH);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
322
    DBT* create_dbt_key_for_lookup(DBT * key, KEY* key_info, uchar * buff, const uchar * record, bool* has_null, int key_length = MAX_KEY_LENGTH);
323
    DBT *pack_key(DBT * key, uint keynr, uchar * buff, const uchar * key_ptr, uint key_length, int8_t inf_byte);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
324
    bool key_changed(uint keynr, const uchar * old_row, const uchar * new_row);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
325
    int handle_cursor_error(int error, int err_to_return, uint keynr);
326
    DBT *get_pos(DBT * to, uchar * pos);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
327
 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
328 329
    int open_main_dictionary(const char* name, bool is_read_only, DB_TXN* txn);
    int open_secondary_dictionary(DB** ptr, KEY* key_info, const char* name, bool is_read_only, DB_TXN* txn);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
330
    int acquire_table_lock (DB_TXN* trans, TABLE_LOCK_TYPE lt);
Yoni Fogel's avatar
Yoni Fogel committed
331
    int estimate_num_rows(DB* db, uint64_t* num_rows, DB_TXN* txn);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
332
    bool has_auto_increment_flag(uint* index);
333

334
    int write_frm_data(DB* db, DB_TXN* txn, const char* frm_name);
335
    int verify_frm_data(const char* frm_name, DB_TXN* trans);
336 337 338 339 340 341
    int remove_frm_data(DB *db, DB_TXN *txn);

    int write_to_status(DB* db, HA_METADATA_KEY curr_key_data, void* data, uint size, DB_TXN* txn);
    int remove_from_status(DB* db, HA_METADATA_KEY curr_key_data, DB_TXN* txn);

    int write_metadata(DB* db, void* key, uint key_size, void* data, uint data_size, DB_TXN* txn);
342
    int remove_metadata(DB* db, void* key_data, uint key_size, DB_TXN* transaction);
343

Zardosht Kasheff's avatar
Zardosht Kasheff committed
344
    int update_max_auto_inc(DB* db, ulonglong val);
345 346 347
    int remove_key_name_from_status(DB* status_block, char* key_name, DB_TXN* txn);
    int write_key_name_to_status(DB* status_block, char* key_name, DB_TXN* txn);
    int write_auto_inc_create(DB* db, ulonglong val, DB_TXN* txn);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
348
    void init_auto_increment();
349
    bool can_replace_into_be_fast(TABLE_SHARE* table_share, KEY_AND_COL_INFO* kc_info, uint pk);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
350 351 352 353 354 355
    int initialize_share(
        const char* name,
        int mode
        );

    void set_query_columns(uint keynr);
356
    int prelock_range (const key_range *start_key, const key_range *end_key);
357
    int create_txn(THD* thd, tokudb_trx_data* trx);
358
    bool may_table_be_empty(DB_TXN *txn);
359
    int delete_or_rename_table (const char* from_name, const char* to_name, bool is_delete);
360
    int delete_or_rename_dictionary( const char* from_name, const char* to_name, const char* index_name, bool is_key, DB_TXN* txn, bool is_delete);
361
    int truncate_dictionary( uint keynr, DB_TXN* txn );
362 363 364 365 366 367
    int create_secondary_dictionary(
        const char* name, 
        TABLE* form, 
        KEY* key_info, 
        DB_TXN* txn, 
        KEY_AND_COL_INFO* kc_info, 
Yoni Fogel's avatar
Yoni Fogel committed
368
        uint32_t keynr, 
369 370
        bool is_hot_index,
        enum row_type row_type
371
        );
372
    int create_main_dictionary(const char* name, TABLE* form, DB_TXN* txn, KEY_AND_COL_INFO* kc_info, enum row_type row_type);
373
    void trace_create_table_info(const char *name, TABLE * form);
374
    int is_index_unique(bool* is_unique, DB_TXN* txn, DB* db, KEY* key_info);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
375
    int is_val_unique(bool* is_unique, uchar* record, KEY* key_info, uint dict_index, DB_TXN* txn);
376
    int do_uniqueness_checks(uchar* record, DB_TXN* txn, THD* thd);
Yoni Fogel's avatar
Yoni Fogel committed
377
    void set_main_dict_put_flags(THD* thd, bool opt_eligible, uint32_t* put_flags);
378
    int insert_row_to_main_dictionary(uchar* record, DBT* pk_key, DBT* pk_val, DB_TXN* txn);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
379
    int insert_rows_to_dictionaries_mult(DBT* pk_key, DBT* pk_val, DB_TXN* txn, THD* thd);
380
    void test_row_packing(uchar* record, DBT* pk_key, DBT* pk_val);
Yoni Fogel's avatar
Yoni Fogel committed
381
    uint32_t fill_row_mutator(
382
        uchar* buf, 
Yoni Fogel's avatar
Yoni Fogel committed
383 384
        uint32_t* dropped_columns, 
        uint32_t num_dropped_columns,
385 386
        TABLE* altered_table,
        KEY_AND_COL_INFO* altered_kc_info,
Yoni Fogel's avatar
Yoni Fogel committed
387
        uint32_t keynr,
388 389
        bool is_add
        );
390

391 392 393
    // 0 <= active_index < table_share->keys || active_index == MAX_KEY
    // tokudb_active_index = active_index if active_index < table_share->keys, else tokudb_active_index = primary_key = table_share->keys
    uint tokudb_active_index;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
394
 
395 396
public:
    ha_tokudb(handlerton * hton, TABLE_SHARE * table_arg);
397 398 399 400
    ~ha_tokudb();

    const char *table_type() const;
    const char *index_type(uint inx);
401
    const char **bas_ext() const;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
402 403 404 405 406

    //
    // Returns a bit mask of capabilities of storage engine. Capabilities 
    // defined in sql/handler.h
    //
407 408
    ulonglong table_flags(void) const;
    
409
    ulong index_flags(uint inx, uint part, bool all_parts) const;
410

Zardosht Kasheff's avatar
Zardosht Kasheff committed
411 412 413
    //
    // Returns limit on the number of keys imposed by tokudb.
    //
414
    uint max_supported_keys() const {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
415
        return MAX_KEY;
416
    } 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
417

418
    uint extra_rec_buf_length() const {
419
        return TOKUDB_HIDDEN_PRIMARY_KEY_LENGTH;
420 421
    } 
    ha_rows estimate_rows_upper_bound();
Zardosht Kasheff's avatar
Zardosht Kasheff committed
422 423 424 425

    //
    // Returns the limit on the key length imposed by tokudb.
    //
426 427
    uint max_supported_key_length() const {
        return UINT_MAX32;
428
    } 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
429 430 431 432

    //
    // Returns limit on key part length imposed by tokudb.
    //
433
    uint max_supported_key_part_length() const {
434
        return UINT_MAX32;
435 436
    } 
    const key_map *keys_to_use_for_scanning() {
437 438 439 440
        return &key_map_full;
    }

    double scan_time();
441
    double keyread_time(uint index, uint ranges, ha_rows rows);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
442
    double read_time(uint index, uint ranges, ha_rows rows);
443 444 445

    int open(const char *name, int mode, uint test_if_locked);
    int close(void);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
446
    void update_create_info(HA_CREATE_INFO* create_info);
447 448 449
    int create(const char *name, TABLE * form, HA_CREATE_INFO * create_info);
    int delete_table(const char *name);
    int rename_table(const char *from, const char *to);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
450
    int optimize(THD * thd, HA_CHECK_OPT * check_opt);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
451
#if 0
452
    int analyze(THD * thd, HA_CHECK_OPT * check_opt);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
453
#endif
454 455 456 457
    int write_row(uchar * buf);
    int update_row(const uchar * old_data, uchar * new_data);
    int delete_row(const uchar * buf);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
458 459
    void start_bulk_insert(ha_rows rows);
    int end_bulk_insert();
460
    int end_bulk_insert(bool abort);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
461

Zardosht Kasheff's avatar
Zardosht Kasheff committed
462
    int prepare_index_scan();
Zardosht Kasheff's avatar
Zardosht Kasheff committed
463
    int prepare_index_key_scan( const uchar * key, uint key_len );
Zardosht Kasheff's avatar
Zardosht Kasheff committed
464
    int prepare_range_scan( const key_range *start_key, const key_range *end_key);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
465
    void column_bitmaps_signal();
466 467
    int index_init(uint index, bool sorted);
    int index_end();
Zardosht Kasheff's avatar
Zardosht Kasheff committed
468
    int index_next_same(uchar * buf, const uchar * key, uint keylen); 
469 470 471 472 473 474 475 476 477 478 479 480
    int index_read(uchar * buf, const uchar * key, uint key_len, enum ha_rkey_function find_flag);
    int index_read_last(uchar * buf, const uchar * key, uint key_len);
    int index_next(uchar * buf);
    int index_prev(uchar * buf);
    int index_first(uchar * buf);
    int index_last(uchar * buf);

    int rnd_init(bool scan);
    int rnd_end();
    int rnd_next(uchar * buf);
    int rnd_pos(uchar * buf, uchar * pos);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
481 482 483 484 485 486
    int read_range_first(const key_range *start_key,
                                 const key_range *end_key,
                                 bool eq_range, bool sorted);
    int read_range_next();


487 488 489 490 491 492 493 494 495
    void position(const uchar * record);
    int info(uint);
    int extra(enum ha_extra_function operation);
    int reset(void);
    int external_lock(THD * thd, int lock_type);
    int start_stmt(THD * thd, thr_lock_type lock_type);

    ha_rows records_in_range(uint inx, key_range * min_key, key_range * max_key);

Yoni Fogel's avatar
Yoni Fogel committed
496
    uint32_t get_cursor_isolation_flags(enum thr_lock_type lock_type, THD* thd);
497 498
    THR_LOCK_DATA **store_lock(THD * thd, THR_LOCK_DATA ** to, enum thr_lock_type lock_type);

499
    int get_status(DB_TXN* trans);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
500
    void init_hidden_prim_key_info();
501 502 503
    inline void get_auto_primary_key(uchar * to) {
        pthread_mutex_lock(&share->mutex);
        share->auto_ident++;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
504
        hpk_num_to_char(to, share->auto_ident);
505 506 507
        pthread_mutex_unlock(&share->mutex);
    }
    virtual void get_auto_increment(ulonglong offset, ulonglong increment, ulonglong nb_desired_values, ulonglong * first_value, ulonglong * nb_reserved_values);
508
    bool is_optimize_blocking();
Zardosht Kasheff's avatar
Zardosht Kasheff committed
509
    bool is_auto_inc_singleton();
510 511 512 513 514 515 516
    void print_error(int error, myf errflag);
    uint8 table_cache_type() {
        return HA_CACHE_TBL_TRANSACT;
    }
    bool primary_key_is_clustered() {
        return true;
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
517 518 519
    bool supports_clustered_keys() {
        return true;
    }
520 521 522
    int cmp_ref(const uchar * ref1, const uchar * ref2);
    bool check_if_incompatible_data(HA_CREATE_INFO * info, uint table_changes);

523
#if TOKU_INCLUDE_ALTER_56
524 525 526 527 528 529 530 531 532
 public:
    enum_alter_inplace_result check_if_supported_inplace_alter(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
    bool prepare_inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
    bool inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
    bool commit_inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha_alter_info, bool commit);
 private:
    int alter_table_add_index(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
    int alter_table_drop_index(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
    int alter_table_add_or_drop_column(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
533
    int alter_table_expand_varchar_offsets(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
534 535
    int alter_table_expand_columns(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
    int alter_table_expand_one_column(TABLE *altered_table, Alter_inplace_info *ha_alter_info, int expand_field_num);
536
    void print_alter_info(TABLE *altered_table, Alter_inplace_info *ha_alter_info);
537
    int setup_kc_info(TABLE *altered_table, KEY_AND_COL_INFO *kc_info);
538 539
    int new_row_descriptor(TABLE *table, TABLE *altered_table, Alter_inplace_info *ha_alter_info, uint32_t idx, DBT *row_descriptor);

540
 public:
541 542
#endif
#if TOKU_INCLUDE_ALTER_55
543
 public:
544 545
    int add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys, handler_add_index **add);
    int final_add_index(handler_add_index *add, bool commit);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
546 547 548
    int prepare_drop_index(TABLE *table_arg, uint *key_num, uint num_of_keys);
    int final_drop_index(TABLE *table_arg);

549
    bool is_alter_table_hot();
550 551
    void prepare_for_alter();
    int new_alter_table_frm_data(const uchar *frm_data, size_t frm_len);
552
    bool try_hot_alter_table();
553 554
#endif
#if TOKU_INCLUDE_ALTER_51
555 556 557 558
 public:
    int add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys);
    int prepare_drop_index(TABLE *table_arg, uint *key_num, uint num_of_keys);
    int final_drop_index(TABLE *table_arg);
559 560
#endif

561
#if defined(HA_GENERAL_ONLINE)
562
 private:
563 564 565 566 567 568
    void print_alter_info(
        TABLE *altered_table,
        HA_CREATE_INFO *create_info,
        HA_ALTER_FLAGS *alter_flags,
        uint table_changes
        );
569
 public:
570 571 572
    int check_if_supported_alter(TABLE *altered_table,
         HA_CREATE_INFO *create_info,
         HA_ALTER_FLAGS *alter_flags,
573
         HA_ALTER_INFO  *alter_info,
574 575 576 577 578 579 580 581 582 583 584 585 586 587
         uint table_changes
         );
    int alter_table_phase1(THD *thd,
                                   TABLE *altered_table,
                                   HA_CREATE_INFO *create_info,
                                   HA_ALTER_INFO *alter_info,
                                   HA_ALTER_FLAGS *alter_flags)
    {
      return 0;
    }
    int alter_table_phase2(THD *thd,
                                   TABLE *altered_table,
                                   HA_CREATE_INFO *create_info,
                                   HA_ALTER_INFO *alter_info,
588
                                   HA_ALTER_FLAGS *alter_flags);
589 590 591 592
    int alter_table_phase3(THD *thd, TABLE *table)
    {
      return 0;
    }
593
#endif
594

595 596 597 598 599 600 601 602 603 604
 private:
    int tokudb_add_index(
        TABLE *table_arg, 
        KEY *key_info, 
        uint num_of_keys, 
        DB_TXN* txn, 
        bool* inc_num_DBs,
        bool* modified_DB
        ); 
    void restore_add_index(TABLE* table_arg, uint num_of_keys, bool incremented_numDBs, bool modified_DBs);
605
    int drop_indexes(TABLE *table_arg, uint *key_num, uint num_of_keys, KEY *key_info, DB_TXN* txn);
606 607 608
    void restore_drop_indexes(TABLE *table_arg, uint *key_num, uint num_of_keys);

 public:
609 610
    // delete all rows from the table
    // effect: all dictionaries, including the main and indexes, should be empty
611
    int discard_or_import_tablespace(my_bool discard);
612
    int truncate();
613
    int delete_all_rows();
Zardosht Kasheff's avatar
Zardosht Kasheff committed
614 615
    void extract_hidden_primary_key(uint keynr, DBT const *found_key);
    void read_key_only(uchar * buf, uint keynr, DBT const *found_key);
616
    int read_row_callback (uchar * buf, uint keynr, DBT const *row, DBT const *found_key);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
617 618
    int read_primary_key(uchar * buf, uint keynr, DBT const *row, DBT const *found_key);
    int unpack_blobs(
Zardosht Kasheff's avatar
Zardosht Kasheff committed
619 620
        uchar* record,
        const uchar* from_tokudb_blob,
Yoni Fogel's avatar
Yoni Fogel committed
621
        uint32_t num_blob_bytes,
622
        bool check_bitmap
Zardosht Kasheff's avatar
Zardosht Kasheff committed
623
        );
Zardosht Kasheff's avatar
Zardosht Kasheff committed
624
    int unpack_row(
Zardosht Kasheff's avatar
Zardosht Kasheff committed
625 626 627 628 629
        uchar* record, 
        DBT const *row, 
        DBT const *key,
        uint index
        );
630

631 632 633 634
    int prefix_cmp_dbts( uint keynr, const DBT* first_key, const DBT* second_key) {
        return tokudb_prefix_cmp_dbt_key(share->key_file[keynr], first_key, second_key);
    }

635
    void track_progress(THD* thd);
636 637
    void set_loader_error(int err);
    void set_dup_value_for_pk(DBT* key);
638

Zardosht Kasheff's avatar
Zardosht Kasheff committed
639

Zardosht Kasheff's avatar
Zardosht Kasheff committed
640 641 642 643 644 645
    //
    // index into key_file that holds DB* that is indexed on
    // the primary_key. this->key_file[primary_index] == this->file
    //
    uint primary_key;

646 647
    int check(THD *thd, HA_CHECK_OPT *check_opt);

648 649 650 651 652 653 654
    int fill_range_query_buf(
        bool need_val, 
        DBT const *key, 
        DBT  const *row, 
        int direction,
        THD* thd
        );
655

656 657 658
#if MYSQL_VERSION_ID >= 50521
    enum row_type get_row_type() const;
#else
659
    enum row_type get_row_type();
660
#endif
661

662
private:
Zardosht Kasheff's avatar
Zardosht Kasheff committed
663
    int read_full_row(uchar * buf);
664
    int __close(int mutex_is_locked);
665 666 667
    int get_next(uchar* buf, int direction);
    int read_data_from_range_query_buff(uchar* buf, bool need_val);
    void invalidate_bulk_fetch();
668
    int delete_all_rows_internal();
669 670 671
#if TOKU_INCLUDE_WRITE_FRM_DATA
    int write_frm_data(const uchar *frm_data, size_t frm_len);
#endif
672 673
#if TOKU_INCLUDE_UPSERT
private:
674
    int fast_update(THD *thd, List<Item> &fields, List<Item> &values, Item *conds);
675 676
    bool check_fast_update(THD *thd, List<Item> &fields, List<Item> &values, Item *conds);
    int send_update_message(List<Item> &fields, List<Item> &values, Item *conds, DB_TXN *txn);
677
    int upsert(THD *thd, uchar *record, List<Item> &update_fields, List<Item> &update_values);
678 679 680
    bool check_upsert(THD *thd, List<Item> &update_fields, List<Item> &update_values);
    int send_upsert_message(THD *thd, uchar *record, List<Item> &update_fields, List<Item> &update_values, DB_TXN *txn);
#endif
681
};
682

683 684 685 686 687 688 689 690 691 692 693 694
#if MYSQL_VERSION_ID >= 50506

static inline void my_free(void *p, int arg) {
    my_free(p);
}

static inline void *memcpy_fixed(void *a, const void *b, size_t n) {
    return memcpy(a, b, n);
}

#endif

695 696
#endif