ha_tokudb_update_fun.cc 43.1 KB
Newer Older
1 2 3 4
// Update operation codes.  These codes get stuffed into update messages, so they can not change.
// The operations are currently stored in a single byte in the update message, so only 256 operations
// are supported.  When we need more, we can use the last (255) code to indicate that the operation code
// is expanded beyond 1 byte.
5
enum {
6
    UPDATE_OP_COL_ADD_OR_DROP = 0,
7

8
    UPDATE_OP_EXPAND_VARIABLE_OFFSETS = 1,
9
    UPDATE_OP_EXPAND_INT = 2,
10 11 12
    UPDATE_OP_EXPAND_UINT = 3,
    UPDATE_OP_EXPAND_CHAR = 4,
    UPDATE_OP_EXPAND_BINARY = 5,
13 14 15

    UPDATE_OP_SIMPLE_UPDATE = 10,
    UPDATE_OP_SIMPLE_UPSERT = 11,
16
};
17 18 19 20 21 22 23 24 25 26 27 28

// Field types used in the update messages
enum {
    UPDATE_TYPE_UNKNOWN = 0,
    UPDATE_TYPE_INT = 1,
    UPDATE_TYPE_UINT = 2,
    UPDATE_TYPE_CHAR = 3,
    UPDATE_TYPE_BINARY = 4,
    UPDATE_TYPE_VARCHAR = 5,
    UPDATE_TYPE_VARBINARY = 6,
};

29
#define UP_COL_ADD_OR_DROP UPDATE_OP_COL_ADD_OR_DROP
30

31
// add or drop column sub-operations
32 33 34
#define COL_DROP 0xaa
#define COL_ADD 0xbb

35
// add or drop column types
36 37 38 39 40 41
#define COL_FIXED 0xcc
#define COL_VAR 0xdd
#define COL_BLOB 0xee

#define STATIC_ROW_MUTATOR_SIZE 1+8+2+8+8+8

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
// how much space do I need for the mutators?
// static stuff first:
// operation 1 == UP_COL_ADD_OR_DROP
// 8 - old null, new null
// 2 - old num_offset, new num_offset
// 8 - old fixed_field size, new fixed_field_size
// 8 - old and new length of offsets
// 8 - old and new starting null bit position
// TOTAL: 27

// dynamic stuff:
// 4 - number of columns
// for each column:
// 1 - add or drop
// 1 - is nullable
// 4 - if nullable, position
// 1 - if add, whether default is null or not
// 1 - if fixed, var, or not
//  for fixed, entire default
//  for var, 4 bytes length, then entire default
//  for blob, nothing
// So, an upperbound is 4 + num_fields(12) + all default stuff

// static blob stuff:
// 4 - num blobs
// 1 byte for each num blobs in old table
// So, an upperbound is 4 + kc_info->num_blobs

// dynamic blob stuff:
// for each blob added:
// 1 - state if we are adding or dropping
// 4 - blob index
// if add, 1 len bytes
//  at most, 4 0's
// So, upperbound is num_blobs(1+4+1+4) = num_columns*10

78
// The expand varchar offsets message is used to expand the size of an offset from 1 to 2 bytes.
79 80 81
//     operation          1 == UPDATE_OP_EXPAND_VARIABLE_OFFSETS
//     n_offsets          4 number of offsets
//     offset_start       4 starting offset of the variable length field offsets 
82

83 84
// These expand messages are used to expand the size of a fixed length field.  
// The field type is encoded in the operation code.
85 86 87 88 89 90 91 92 93 94 95 96
//     operation          1 == UPDATE_OP_EXPAND_INT/UINT/CHAR/BINARY
//     offset             4 offset of the field
//     old length         4 the old length of the field's value
//     new length         4 the new length of the field's value

//     operation          1 == UPDATE_OP_EXPAND_CHAR/BINARY
//     offset             4 offset of the field
//     old length         4 the old length of the field's value
//     new length         4 the new length of the field's value
//     pad char           1

// Simple row descriptor:
97 98 99 100
//     fixed field offset 4 offset of the beginning of the fixed fields
//     var field offset   4 offset of the variable length offsets
//     var_offset_bytes   1 size of each variable length offset
//     num_var_fields     4 number of variable length offsets
101 102

// Field descriptor:
103 104 105 106 107
//     field type         4 see field types above
//     field num          4 unused for fixed length fields
//     field null num     4 bit 31 is 1 if the field is nullible and the remaining bits contain the null bit number
//     field offset       4 for fixed fields, this is the offset from begining of the row of the field
//     field length       4 for fixed fields, this is the length of the field
108 109 110 111 112 113 114 115 116 117 118 119 120 121

// Simple update operation:
//     update operation   4 == { '=', '+', '-' }
//         x = k
//         x = x + k
//         x = x - k
//     field descriptor
//     optional value:
//         value length   4 == N, length of the value
//         value          N value to add or subtract

// Simple update message:
//     Operation          1 == UPDATE_OP_UPDATE_FIELD
//     Simple row descriptor
122 123
//     Number of update ops 4 == N
//     Uupdate ops [N]
124 125 126 127 128 129 130

// Simple upsert message:
//     Operation          1 == UPDATE_OP_UPSERT
//     Insert row:
//         length         4 == N
//         data           N
//     Simple row descriptor
131 132
//     Number of update ops 4 == N
//     Update ops [N] 
133 134 135

#include "tokudb_buffer.h"
#include "tokudb_math.h"
136 137 138 139

//
// checks whether the bit at index pos in data is set or not
//
140
static inline bool is_overall_null_position_set(uchar* data, uint32_t pos) {
Yoni Fogel's avatar
Yoni Fogel committed
141
    uint32_t offset = pos/8;
142 143 144 145 146 147 148 149
    uchar remainder = pos%8; 
    uchar null_bit = 1<<remainder;
    return ((data[offset] & null_bit) != 0);
}

//
// sets the bit at index pos in data to 1 if is_null, 0 otherwise
// 
150
static inline void set_overall_null_position(uchar* data, uint32_t pos, bool is_null) {
Yoni Fogel's avatar
Yoni Fogel committed
151
    uint32_t offset = pos/8;
152 153 154 155 156 157 158 159 160 161
    uchar remainder = pos%8;
    uchar null_bit = 1<<remainder;
    if (is_null) {
        data[offset] |= null_bit;
    }
    else {
        data[offset] &= ~null_bit;
    }
}

162
static inline void copy_null_bits(
Yoni Fogel's avatar
Yoni Fogel committed
163 164 165
    uint32_t start_old_pos,
    uint32_t start_new_pos,
    uint32_t num_bits,
166 167 168 169
    uchar* old_null_bytes,
    uchar* new_null_bytes
    ) 
{
Yoni Fogel's avatar
Yoni Fogel committed
170 171 172
    for (uint32_t i = 0; i < num_bits; i++) {
        uint32_t curr_old_pos = i + start_old_pos;
        uint32_t curr_new_pos = i + start_new_pos;
173 174 175 176 177 178 179 180 181 182
        // copy over old null bytes
        if (is_overall_null_position_set(old_null_bytes,curr_old_pos)) {
            set_overall_null_position(new_null_bytes,curr_new_pos,true);
        }
        else {
            set_overall_null_position(new_null_bytes,curr_new_pos,false);
        }
    }
}

183
static inline void copy_var_fields(
Yoni Fogel's avatar
Yoni Fogel committed
184 185
    uint32_t start_old_num_var_field, //index of var fields that we should start writing
    uint32_t num_var_fields, // number of var fields to copy
186 187 188 189 190 191
    uchar* old_var_field_offset_ptr, //static ptr to where offset bytes begin in old row
    uchar old_num_offset_bytes, //number of offset bytes used in old row
    uchar* start_new_var_field_data_ptr, // where the new var data should be written
    uchar* start_new_var_field_offset_ptr, // where the new var offsets should be written
    uchar* new_var_field_data_ptr, // pointer to beginning of var fields in new row
    uchar* old_var_field_data_ptr, // pointer to beginning of var fields in old row
Yoni Fogel's avatar
Yoni Fogel committed
192 193 194
    uint32_t new_num_offset_bytes, // number of offset bytes used in new row
    uint32_t* num_data_bytes_written,
    uint32_t* num_offset_bytes_written
195 196 197 198
    ) 
{
    uchar* curr_new_var_field_data_ptr = start_new_var_field_data_ptr;
    uchar* curr_new_var_field_offset_ptr = start_new_var_field_offset_ptr;
Yoni Fogel's avatar
Yoni Fogel committed
199 200 201 202
    for (uint32_t i = 0; i < num_var_fields; i++) {
        uint32_t field_len;
        uint32_t start_read_offset;
        uint32_t curr_old = i + start_old_num_var_field;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        uchar* data_to_copy = NULL;
        // get the length and pointer to data that needs to be copied
        get_var_field_info(
            &field_len, 
            &start_read_offset, 
            curr_old, 
            old_var_field_offset_ptr, 
            old_num_offset_bytes
            );
        data_to_copy = old_var_field_data_ptr + start_read_offset;
        // now need to copy field_len bytes starting from data_to_copy
        curr_new_var_field_data_ptr = write_var_field(
            curr_new_var_field_offset_ptr,
            curr_new_var_field_data_ptr,
            new_var_field_data_ptr,
            data_to_copy,
            field_len,
            new_num_offset_bytes
            );
        curr_new_var_field_offset_ptr += new_num_offset_bytes;
    }
Yoni Fogel's avatar
Yoni Fogel committed
224 225
    *num_data_bytes_written = (uint32_t)(curr_new_var_field_data_ptr - start_new_var_field_data_ptr);
    *num_offset_bytes_written = (uint32_t)(curr_new_var_field_offset_ptr - start_new_var_field_offset_ptr);
226 227
}

228
static inline uint32_t copy_toku_blob(uchar* to_ptr, uchar* from_ptr, uint32_t len_bytes, bool skip) {
Yoni Fogel's avatar
Yoni Fogel committed
229
    uint32_t length = 0;
230 231 232 233 234 235 236 237 238 239
    if (!skip) {
        memcpy(to_ptr, from_ptr, len_bytes);
    }
    length = get_blob_field_len(from_ptr,len_bytes);
    if (!skip) {
        memcpy(to_ptr + len_bytes, from_ptr + len_bytes, length);
    }
    return (length + len_bytes);
}

240
static int tokudb_hcad_update_fun(
241 242 243 244 245 246 247 248
    DB* db,
    const DBT *key,
    const DBT *old_val, 
    const DBT *extra,
    void (*set_val)(const DBT *new_val, void *set_extra),
    void *set_extra
    ) 
{
Yoni Fogel's avatar
Yoni Fogel committed
249 250
    uint32_t max_num_bytes;
    uint32_t num_columns;
251
    DBT new_val;
Yoni Fogel's avatar
Yoni Fogel committed
252 253 254 255
    uint32_t num_bytes_left;
    uint32_t num_var_fields_to_copy;
    uint32_t num_data_bytes_written = 0;
    uint32_t num_offset_bytes_written = 0;
256 257 258 259 260 261 262 263 264
    int error;
    memset(&new_val, 0, sizeof(DBT));
    uchar operation;
    uchar* new_val_data = NULL;
    uchar* extra_pos = NULL;
    uchar* extra_pos_start = NULL;
    //
    // info for pointers into rows
    //
Yoni Fogel's avatar
Yoni Fogel committed
265 266
    uint32_t old_num_null_bytes;
    uint32_t new_num_null_bytes;
267 268
    uchar old_num_offset_bytes;
    uchar new_num_offset_bytes;
Yoni Fogel's avatar
Yoni Fogel committed
269 270 271 272
    uint32_t old_fixed_field_size;
    uint32_t new_fixed_field_size;
    uint32_t old_len_of_offsets;
    uint32_t new_len_of_offsets;
273 274 275

    uchar* old_fixed_field_ptr = NULL;
    uchar* new_fixed_field_ptr = NULL;
Yoni Fogel's avatar
Yoni Fogel committed
276 277
    uint32_t curr_old_fixed_offset;
    uint32_t curr_new_fixed_offset;
278 279 280

    uchar* old_null_bytes = NULL;
    uchar* new_null_bytes = NULL;
Yoni Fogel's avatar
Yoni Fogel committed
281 282 283 284 285 286 287
    uint32_t curr_old_null_pos;
    uint32_t curr_new_null_pos;    
    uint32_t old_null_bits_left;
    uint32_t new_null_bits_left;
    uint32_t overall_null_bits_left;

    uint32_t old_num_var_fields;
288
    // uint32_t new_num_var_fields;
Yoni Fogel's avatar
Yoni Fogel committed
289 290
    uint32_t curr_old_num_var_field;
    uint32_t curr_new_num_var_field;
291 292 293 294 295 296 297
    uchar* old_var_field_offset_ptr = NULL;
    uchar* new_var_field_offset_ptr = NULL;
    uchar* curr_new_var_field_offset_ptr = NULL;
    uchar* old_var_field_data_ptr = NULL;
    uchar* new_var_field_data_ptr = NULL;
    uchar* curr_new_var_field_data_ptr = NULL;

Yoni Fogel's avatar
Yoni Fogel committed
298
    uint32_t start_blob_offset;
299
    uchar* start_blob_ptr;
Yoni Fogel's avatar
Yoni Fogel committed
300
    uint32_t num_blob_bytes;
301 302 303 304 305 306 307 308 309 310 311 312 313 314

    // came across a delete, nothing to update
    if (old_val == NULL) {
        error = 0;
        goto cleanup;
    }

    extra_pos_start = (uchar *)extra->data;
    extra_pos = (uchar *)extra->data;

    operation = extra_pos[0];
    extra_pos++;
    assert(operation == UP_COL_ADD_OR_DROP);

Yoni Fogel's avatar
Yoni Fogel committed
315 316 317 318
    memcpy(&old_num_null_bytes, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
    memcpy(&new_num_null_bytes, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
319 320 321 322 323 324

    old_num_offset_bytes = extra_pos[0];
    extra_pos++;
    new_num_offset_bytes = extra_pos[0];
    extra_pos++;

Yoni Fogel's avatar
Yoni Fogel committed
325 326 327 328
    memcpy(&old_fixed_field_size, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
    memcpy(&new_fixed_field_size, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
329

Yoni Fogel's avatar
Yoni Fogel committed
330 331 332 333
    memcpy(&old_len_of_offsets, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
    memcpy(&new_len_of_offsets, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

    max_num_bytes = old_val->size + extra->size + new_len_of_offsets + new_fixed_field_size;
    new_val_data = (uchar *)my_malloc(
        max_num_bytes, 
        MYF(MY_FAE)
        );
    if (new_val_data == NULL) { goto cleanup; }

    old_fixed_field_ptr = (uchar *) old_val->data;
    old_fixed_field_ptr += old_num_null_bytes;
    new_fixed_field_ptr = new_val_data + new_num_null_bytes;
    curr_old_fixed_offset = 0;
    curr_new_fixed_offset = 0;

    old_num_var_fields = old_len_of_offsets/old_num_offset_bytes;
349
    // new_num_var_fields = new_len_of_offsets/new_num_offset_bytes;
350 351 352 353 354 355 356 357 358 359 360 361 362 363
    // following fields will change as we write the variable data
    old_var_field_offset_ptr = old_fixed_field_ptr + old_fixed_field_size;
    new_var_field_offset_ptr = new_fixed_field_ptr + new_fixed_field_size;
    old_var_field_data_ptr = old_var_field_offset_ptr + old_len_of_offsets;
    new_var_field_data_ptr = new_var_field_offset_ptr + new_len_of_offsets;
    curr_new_var_field_offset_ptr = new_var_field_offset_ptr;
    curr_new_var_field_data_ptr = new_var_field_data_ptr;
    curr_old_num_var_field = 0;
    curr_new_num_var_field = 0;

    old_null_bytes = (uchar *)old_val->data;
    new_null_bytes = new_val_data;

    
Yoni Fogel's avatar
Yoni Fogel committed
364 365 366 367
    memcpy(&curr_old_null_pos, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
    memcpy(&curr_new_null_pos, extra_pos, sizeof(uint32_t));
    extra_pos += sizeof(uint32_t);
368 369 370 371 372 373 374

    memcpy(&num_columns, extra_pos, sizeof(num_columns));
    extra_pos += sizeof(num_columns);
    
    //
    // now go through and apply the change into new_val_data
    //
Yoni Fogel's avatar
Yoni Fogel committed
375
    for (uint32_t i = 0; i < num_columns; i++) {
376 377 378 379 380 381 382 383
        uchar op_type = extra_pos[0];
        bool is_null_default = false;
        extra_pos++;

        assert(op_type == COL_DROP || op_type == COL_ADD);
        bool nullable = (extra_pos[0] != 0);
        extra_pos++;
        if (nullable) {
Yoni Fogel's avatar
Yoni Fogel committed
384 385 386 387
            uint32_t null_bit_position;
            memcpy(&null_bit_position, extra_pos, sizeof(uint32_t));
            extra_pos += sizeof(uint32_t);
            uint32_t num_bits;
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
            if (op_type == COL_DROP) {
                assert(curr_old_null_pos <= null_bit_position);
                num_bits = null_bit_position - curr_old_null_pos;
            }
            else {
                assert(curr_new_null_pos <= null_bit_position);
                num_bits = null_bit_position - curr_new_null_pos;
            }
            copy_null_bits(
                curr_old_null_pos,
                curr_new_null_pos,
                num_bits,
                old_null_bytes,
                new_null_bytes
                );
            // update the positions
            curr_new_null_pos += num_bits;
            curr_old_null_pos += num_bits;
            if (op_type == COL_DROP) {
                curr_old_null_pos++; // account for dropped column
            }
            else {
                is_null_default = (extra_pos[0] != 0);
                extra_pos++;
                set_overall_null_position(
                    new_null_bytes,
                    null_bit_position,
                    is_null_default
                    );
                curr_new_null_pos++; //account for added column
            }
        }
        uchar col_type = extra_pos[0];
        extra_pos++;
        if (col_type == COL_FIXED) {
Yoni Fogel's avatar
Yoni Fogel committed
423 424 425 426 427 428 429
            uint32_t col_offset;
            uint32_t col_size;
            uint32_t num_bytes_to_copy;
            memcpy(&col_offset, extra_pos, sizeof(uint32_t));
            extra_pos += sizeof(uint32_t);
            memcpy(&col_size, extra_pos, sizeof(uint32_t));
            extra_pos += sizeof(uint32_t);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

            if (op_type == COL_DROP) {
                num_bytes_to_copy = col_offset - curr_old_fixed_offset;
            }
            else {
                num_bytes_to_copy = col_offset - curr_new_fixed_offset;
            }
            memcpy(
                new_fixed_field_ptr + curr_new_fixed_offset,
                old_fixed_field_ptr + curr_old_fixed_offset, 
                num_bytes_to_copy
                );
            curr_old_fixed_offset += num_bytes_to_copy;
            curr_new_fixed_offset += num_bytes_to_copy;
            if (op_type == COL_DROP) {
                // move old_fixed_offset val to skip OVER column that is being dropped
                curr_old_fixed_offset += col_size;
            }
            else {
                if (is_null_default) {
                    // copy zeroes
                    memset(new_fixed_field_ptr + curr_new_fixed_offset, 0, col_size);
                }
                else {
                    // copy data from extra_pos into new row
                    memcpy(
                        new_fixed_field_ptr + curr_new_fixed_offset,
                        extra_pos,
                        col_size
                        );
                    extra_pos += col_size;
                }
                curr_new_fixed_offset += col_size;
            }
            
        }
        else if (col_type == COL_VAR) {
Yoni Fogel's avatar
Yoni Fogel committed
467 468 469
            uint32_t var_col_index;
            memcpy(&var_col_index, extra_pos, sizeof(uint32_t));
            extra_pos += sizeof(uint32_t);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
            if (op_type == COL_DROP) {
                num_var_fields_to_copy = var_col_index - curr_old_num_var_field;
            }
            else {
                num_var_fields_to_copy = var_col_index - curr_new_num_var_field;
            }
            copy_var_fields(
                curr_old_num_var_field,
                num_var_fields_to_copy,
                old_var_field_offset_ptr,
                old_num_offset_bytes,
                curr_new_var_field_data_ptr,
                curr_new_var_field_offset_ptr,
                new_var_field_data_ptr, // pointer to beginning of var fields in new row
                old_var_field_data_ptr, // pointer to beginning of var fields in old row
                new_num_offset_bytes, // number of offset bytes used in new row
                &num_data_bytes_written,
                &num_offset_bytes_written
                );
            curr_new_var_field_data_ptr += num_data_bytes_written;
            curr_new_var_field_offset_ptr += num_offset_bytes_written;
            curr_new_num_var_field += num_var_fields_to_copy;
            curr_old_num_var_field += num_var_fields_to_copy;
            if (op_type == COL_DROP) {
                curr_old_num_var_field++; // skip over dropped field
            }
            else {
                if (is_null_default) {
                    curr_new_var_field_data_ptr = write_var_field(
                        curr_new_var_field_offset_ptr,
                        curr_new_var_field_data_ptr,
                        new_var_field_data_ptr,
                        NULL, //copying no data
                        0, //copying 0 bytes
                        new_num_offset_bytes
                        );
                    curr_new_var_field_offset_ptr += new_num_offset_bytes;
                }
                else {
Yoni Fogel's avatar
Yoni Fogel committed
509
                    uint32_t data_length;
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
                    memcpy(&data_length, extra_pos, sizeof(data_length));
                    extra_pos += sizeof(data_length);
                    curr_new_var_field_data_ptr = write_var_field(
                        curr_new_var_field_offset_ptr,
                        curr_new_var_field_data_ptr,
                        new_var_field_data_ptr,
                        extra_pos, //copying data from mutator
                        data_length, //copying data_length bytes
                        new_num_offset_bytes
                        );
                    extra_pos += data_length;
                    curr_new_var_field_offset_ptr += new_num_offset_bytes;
                }
                curr_new_num_var_field++; //account for added column
            }
        }
        else if (col_type == COL_BLOB) {
            // handle blob data later
            continue;
        }
        else {
            assert(false);
        }
    }
    // finish copying the null stuff
    old_null_bits_left = 8*old_num_null_bytes - curr_old_null_pos;
    new_null_bits_left = 8*new_num_null_bytes - curr_new_null_pos;
    overall_null_bits_left = old_null_bits_left;
    set_if_smaller(overall_null_bits_left, new_null_bits_left);
    copy_null_bits(
        curr_old_null_pos,
        curr_new_null_pos,
        overall_null_bits_left,
        old_null_bytes,
        new_null_bytes
        );
    // finish copying fixed field stuff
    num_bytes_left = old_fixed_field_size - curr_old_fixed_offset;
    memcpy(
        new_fixed_field_ptr + curr_new_fixed_offset,
        old_fixed_field_ptr + curr_old_fixed_offset, 
        num_bytes_left
        );
    curr_old_fixed_offset += num_bytes_left;
    curr_new_fixed_offset += num_bytes_left;
    // sanity check
    assert(curr_new_fixed_offset == new_fixed_field_size);

    // finish copying var field stuff
    num_var_fields_to_copy = old_num_var_fields - curr_old_num_var_field;
    copy_var_fields(
        curr_old_num_var_field,
        num_var_fields_to_copy,
        old_var_field_offset_ptr,
        old_num_offset_bytes,
        curr_new_var_field_data_ptr,
        curr_new_var_field_offset_ptr,
        new_var_field_data_ptr, // pointer to beginning of var fields in new row
        old_var_field_data_ptr, // pointer to beginning of var fields in old row
        new_num_offset_bytes, // number of offset bytes used in new row
        &num_data_bytes_written,
        &num_offset_bytes_written
        );
    curr_new_var_field_offset_ptr += num_offset_bytes_written;
    curr_new_var_field_data_ptr += num_data_bytes_written;
    // sanity check
    assert(curr_new_var_field_offset_ptr == new_var_field_data_ptr);

    // start handling blobs
    get_blob_field_info(
        &start_blob_offset, 
        old_len_of_offsets,
        old_var_field_data_ptr,
        old_num_offset_bytes
        );
    start_blob_ptr = old_var_field_data_ptr + start_blob_offset;
    // if nothing else in extra, then there are no blobs to add or drop, so can copy blobs straight
    if ((extra_pos - extra_pos_start) == extra->size) {
        num_blob_bytes = old_val->size - (start_blob_ptr - old_null_bytes);
        memcpy(curr_new_var_field_data_ptr, start_blob_ptr, num_blob_bytes);
        curr_new_var_field_data_ptr += num_blob_bytes;
    }
    // else, there is blob information to process
    else {
        uchar* len_bytes = NULL;
Yoni Fogel's avatar
Yoni Fogel committed
595 596 597
        uint32_t curr_old_blob = 0;
        uint32_t curr_new_blob = 0;
        uint32_t num_old_blobs = 0;
598 599 600 601 602 603 604 605 606
        uchar* curr_old_blob_ptr = start_blob_ptr;
        memcpy(&num_old_blobs, extra_pos, sizeof(num_old_blobs));
        extra_pos += sizeof(num_old_blobs);
        len_bytes = extra_pos;
        extra_pos += num_old_blobs;
        // copy over blob fields one by one
        while ((extra_pos - extra_pos_start) < extra->size) {
            uchar op_type = extra_pos[0];
            extra_pos++;
Yoni Fogel's avatar
Yoni Fogel committed
607 608
            uint32_t num_blobs_to_copy = 0;
            uint32_t blob_index;
609 610 611 612 613 614 615 616 617
            memcpy(&blob_index, extra_pos, sizeof(blob_index));
            extra_pos += sizeof(blob_index);
            assert (op_type == COL_DROP || op_type == COL_ADD);
            if (op_type == COL_DROP) {
                num_blobs_to_copy = blob_index - curr_old_blob;
            }
            else {
                num_blobs_to_copy = blob_index - curr_new_blob;
            }
Yoni Fogel's avatar
Yoni Fogel committed
618 619
            for (uint32_t i = 0; i < num_blobs_to_copy; i++) {
                uint32_t num_bytes_written = copy_toku_blob(
620 621 622 623 624 625 626 627 628 629 630 631
                    curr_new_var_field_data_ptr,
                    curr_old_blob_ptr,
                    len_bytes[curr_old_blob + i],
                    false
                    );
                curr_old_blob_ptr += num_bytes_written;
                curr_new_var_field_data_ptr += num_bytes_written;
            }
            curr_old_blob += num_blobs_to_copy;
            curr_new_blob += num_blobs_to_copy;
            if (op_type == COL_DROP) {
                // skip over blob in row
Yoni Fogel's avatar
Yoni Fogel committed
632
                uint32_t num_bytes = copy_toku_blob(
633 634 635 636 637 638 639 640 641 642
                    NULL,
                    curr_old_blob_ptr,
                    len_bytes[curr_old_blob],
                    true
                    );
                curr_old_blob++;
                curr_old_blob_ptr += num_bytes;
            }
            else {
                // copy new data
Yoni Fogel's avatar
Yoni Fogel committed
643
                uint32_t new_len_bytes = extra_pos[0];
644
                extra_pos++;
Yoni Fogel's avatar
Yoni Fogel committed
645
                uint32_t num_bytes = copy_toku_blob(
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
                    curr_new_var_field_data_ptr,
                    extra_pos,
                    new_len_bytes,
                    false
                    );
                curr_new_blob++;
                curr_new_var_field_data_ptr += num_bytes;
                extra_pos += num_bytes;
            }                
        }
        num_blob_bytes = old_val->size - (curr_old_blob_ptr - old_null_bytes);
        memcpy(curr_new_var_field_data_ptr, curr_old_blob_ptr, num_blob_bytes);
        curr_new_var_field_data_ptr += num_blob_bytes;
    }
    new_val.data = new_val_data;
    new_val.size = curr_new_var_field_data_ptr - new_val_data;
    set_val(&new_val, set_extra);
    
    error = 0;
cleanup:
    my_free(new_val_data, MYF(MY_ALLOW_ZERO_PTR));
    return error;    
}
669

670
// Expand the variable offset array in the old row given the update mesage in the extra.
671
static int tokudb_expand_variable_offsets(
672 673 674 675 676 677 678 679 680
    DB* db,
    const DBT *key,
    const DBT *old_val, 
    const DBT *extra,
    void (*set_val)(const DBT *new_val, void *set_extra),
    void *set_extra
    ) 
{
    int error = 0;
681
    tokudb::buffer extra_val(extra->data, 0, extra->size);
682 683

    // decode the operation
684 685
    uchar operation;
    extra_val.consume(&operation, sizeof operation);
686
    assert(operation == UPDATE_OP_EXPAND_VARIABLE_OFFSETS);
687

688 689
    // decode number of offsets
    uint32_t number_of_offsets;
690
    extra_val.consume(&number_of_offsets, sizeof number_of_offsets);
691

692 693
    // decode the offset start
    uint32_t offset_start;
694
    extra_val.consume(&offset_start, sizeof offset_start);
695

696
    assert(extra_val.size() == extra_val.limit());
697

698
    DBT new_val; memset(&new_val, 0, sizeof new_val);
699 700

    if (old_val != NULL) {
701 702
        assert(offset_start + number_of_offsets < old_val->size);
    
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
        // compute the new val from the old val
        uchar *old_val_ptr = (uchar *)old_val->data;

        // allocate space for the new val's data
        uchar *new_val_ptr = (uchar *)my_malloc(number_of_offsets + old_val->size, MYF(MY_FAE));
        if (!new_val_ptr) {
            error = ENOMEM;
            goto cleanup;
        }
        new_val.data = new_val_ptr;
        
        // copy up to the start of the varchar offset
        memcpy(new_val_ptr, old_val_ptr, offset_start);
        new_val_ptr += offset_start;
        old_val_ptr += offset_start;
        
719
        // expand each offset from 1 to 2 bytes
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
        for (uint32_t i = 0; i < number_of_offsets; i++) {
            uint16_t new_offset = *old_val_ptr;
            int2store(new_val_ptr, new_offset);
            new_val_ptr += 2;
            old_val_ptr += 1;
        }
        
        // copy the rest of the row
        size_t n = old_val->size - (old_val_ptr - (uchar *)old_val->data);
        memcpy(new_val_ptr, old_val_ptr, n);
        new_val_ptr += n;
        old_val_ptr += n;
        new_val.size = new_val_ptr - (uchar *)new_val.data;

        assert(new_val_ptr == (uchar *)new_val.data + new_val.size);
        assert(old_val_ptr == (uchar *)old_val->data + old_val->size);

        // set the new val
        set_val(&new_val, set_extra);
    }

    error = 0;

cleanup:
    my_free(new_val.data, MYF(MY_ALLOW_ZERO_PTR));        

    return error;
}

749
// Expand an int field in a old row given the expand message in the extra.
750
static int tokudb_expand_int_field(
751 752 753 754 755 756 757 758 759
    DB* db,
    const DBT *key,
    const DBT *old_val, 
    const DBT *extra,
    void (*set_val)(const DBT *new_val, void *set_extra),
    void *set_extra
    ) 
{
    int error = 0;
760
    tokudb::buffer extra_val(extra->data, 0, extra->size);
761

762 763
    uchar operation;
    extra_val.consume(&operation, sizeof operation);
764
    assert(operation == UPDATE_OP_EXPAND_INT || operation == UPDATE_OP_EXPAND_UINT);
765
    uint32_t the_offset;
766
    extra_val.consume(&the_offset, sizeof the_offset);
767
    uint32_t old_length;
768
    extra_val.consume(&old_length, sizeof old_length);
769
    uint32_t new_length;
770 771
    extra_val.consume(&new_length, sizeof new_length);
    assert(extra_val.size() == extra_val.limit());
772 773 774

    assert(new_length >= old_length); // expand only

775
    DBT new_val; memset(&new_val, 0, sizeof new_val);
776 777

    if (old_val != NULL) {
778 779
        assert(the_offset + old_length <= old_val->size); // old field within the old val

780 781 782 783 784 785 786 787 788 789 790 791
        // compute the new val from the old val
        uchar *old_val_ptr = (uchar *)old_val->data;

        // allocate space for the new val's data
        uchar *new_val_ptr = (uchar *)my_malloc(old_val->size + (new_length - old_length), MYF(MY_FAE));
        if (!new_val_ptr) {
            error = ENOMEM;
            goto cleanup;
        }
        new_val.data = new_val_ptr;
        
        // copy up to the old offset
792 793 794
        memcpy(new_val_ptr, old_val_ptr, the_offset);
        new_val_ptr += the_offset;
        old_val_ptr += the_offset;
795 796
        
        switch (operation) {
797
        case UPDATE_OP_EXPAND_INT:
798 799 800 801
            // fill the entire new value with ones or zeros depending on the sign bit
            // the encoding is little endian
            memset(new_val_ptr, (old_val_ptr[old_length-1] & 0x80) ? 0xff : 0x00, new_length);
            memcpy(new_val_ptr, old_val_ptr, old_length);  // overlay the low bytes of the new value with the old value
802 803 804 805
            new_val_ptr += new_length;
            old_val_ptr += old_length;
            break;
        case UPDATE_OP_EXPAND_UINT:
806 807
            memset(new_val_ptr, 0, new_length);            // fill the entire new value with zeros
            memcpy(new_val_ptr, old_val_ptr, old_length);  // overlay the low bytes of the new value with the old value
808 809 810
            new_val_ptr += new_length;
            old_val_ptr += old_length;
            break;
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
        default:
            assert(0);
        }
        
        // copy the rest
        size_t n = old_val->size - (old_val_ptr - (uchar *)old_val->data);
        memcpy(new_val_ptr, old_val_ptr, n);
        new_val_ptr += n;
        old_val_ptr += n;
        new_val.size = new_val_ptr - (uchar *)new_val.data;

        assert(new_val_ptr == (uchar *)new_val.data + new_val.size);
        assert(old_val_ptr == (uchar *)old_val->data + old_val->size);

        // set the new val
        set_val(&new_val, set_extra);
    }

    error = 0;

cleanup:
    my_free(new_val.data, MYF(MY_ALLOW_ZERO_PTR));        

    return error;
}

// Expand a char field in a old row given the expand message in the extra.
838
static int tokudb_expand_char_field(
839 840 841 842 843 844 845 846 847
    DB* db,
    const DBT *key,
    const DBT *old_val, 
    const DBT *extra,
    void (*set_val)(const DBT *new_val, void *set_extra),
    void *set_extra
    ) 
{
    int error = 0;
848
    tokudb::buffer extra_val(extra->data, 0, extra->size);
849

850 851
    uchar operation;
    extra_val.consume(&operation, sizeof operation);
852 853
    assert(operation == UPDATE_OP_EXPAND_CHAR || operation == UPDATE_OP_EXPAND_BINARY);
    uint32_t the_offset;
854
    extra_val.consume(&the_offset, sizeof the_offset);
855
    uint32_t old_length;
856
    extra_val.consume(&old_length, sizeof old_length);
857
    uint32_t new_length;
858 859 860 861
    extra_val.consume(&new_length, sizeof new_length);
    uchar pad_char;
    extra_val.consume(&pad_char, sizeof pad_char);
    assert(extra_val.size() == extra_val.limit());
862 863 864 865 866 867

    assert(new_length >= old_length); // expand only

    DBT new_val; memset(&new_val, 0, sizeof new_val);

    if (old_val != NULL) {
868 869
        assert(the_offset + old_length <= old_val->size); // old field within the old val

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
        // compute the new val from the old val
        uchar *old_val_ptr = (uchar *)old_val->data;

        // allocate space for the new val's data
        uchar *new_val_ptr = (uchar *)my_malloc(old_val->size + (new_length - old_length), MYF(MY_FAE));
        if (!new_val_ptr) {
            error = ENOMEM;
            goto cleanup;
        }
        new_val.data = new_val_ptr;
        
        // copy up to the old offset
        memcpy(new_val_ptr, old_val_ptr, the_offset);
        new_val_ptr += the_offset;
        old_val_ptr += the_offset;
        
        switch (operation) {
887 888
        case UPDATE_OP_EXPAND_CHAR:
        case UPDATE_OP_EXPAND_BINARY:
889 890
            memset(new_val_ptr, pad_char, new_length);     // fill the entire new value with the pad char
            memcpy(new_val_ptr, old_val_ptr, old_length);  // overlay the low bytes of the new value with the old value
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
            new_val_ptr += new_length;
            old_val_ptr += old_length;
            break;
        default:
            assert(0);
        }
        
        // copy the rest
        size_t n = old_val->size - (old_val_ptr - (uchar *)old_val->data);
        memcpy(new_val_ptr, old_val_ptr, n);
        new_val_ptr += n;
        old_val_ptr += n;
        new_val.size = new_val_ptr - (uchar *)new_val.data;

        assert(new_val_ptr == (uchar *)new_val.data + new_val.size);
        assert(old_val_ptr == (uchar *)old_val->data + old_val->size);

        // set the new val
        set_val(&new_val, set_extra);
    }

    error = 0;

cleanup:
    my_free(new_val.data, MYF(MY_ALLOW_ZERO_PTR));        

    return error;
}

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
class Simple_row_descriptor {
public:
    Simple_row_descriptor() : m_fixed_field_offset(0), m_var_field_offset(0), m_var_offset_bytes(0), m_num_var_fields(0) {
    }
    ~Simple_row_descriptor() {
    }
    void consume(tokudb::buffer &b) {
        b.consume(&m_fixed_field_offset, sizeof m_fixed_field_offset);
        b.consume(&m_var_field_offset, sizeof m_var_field_offset);
        b.consume(&m_var_offset_bytes, sizeof m_var_offset_bytes);
        b.consume(&m_num_var_fields, sizeof m_num_var_fields);
    }
    void append(tokudb::buffer &b) {
        b.append(&m_fixed_field_offset, sizeof m_fixed_field_offset);
        b.append(&m_var_field_offset, sizeof m_var_field_offset);
        b.append(&m_var_offset_bytes, sizeof m_var_offset_bytes);
        b.append(&m_num_var_fields, sizeof m_num_var_fields);
    }
public:
    uint32_t m_fixed_field_offset;
    uint32_t m_var_field_offset;
    uint8_t  m_var_offset_bytes;
    uint32_t m_num_var_fields;
};

// Update a fixed field: new_val@offset = extra_val
static void set_fixed_field(uint32_t the_offset, uint32_t length, uint32_t field_num, uint32_t field_null_num,
                            tokudb::buffer &new_val, void *extra_val) {
    assert(the_offset + length <= new_val.size());
    new_val.replace(the_offset, length, extra_val, length);
    if (field_null_num)
        set_overall_null_position((uchar *) new_val.data(), field_null_num & ~(1<<31), false);
}

// Update an int field: signed newval@offset = old_val@offset OP extra_val
955
static void int_op(uint32_t operation, uint32_t the_offset, uint32_t length, uint32_t field_num, uint32_t field_null_num,
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
                   tokudb::buffer &new_val, tokudb::buffer &old_val, void *extra_val) {
    assert(the_offset + length <= new_val.size());
    assert(the_offset + length <= old_val.size());
    assert(length == 1 || length == 2 || length == 3 || length == 4 || length == 8);

    uchar *old_val_ptr = (uchar *) old_val.data();
    bool field_is_null = false;
    if (field_null_num)
        field_is_null = is_overall_null_position_set(old_val_ptr, field_null_num & ~(1<<31));
    int64_t v = 0;
    memcpy(&v, old_val_ptr + the_offset, length);
    v = tokudb::int_sign_extend(v, 8*length);
    int64_t extra_v = 0;
    memcpy(&extra_v, extra_val, length);
    extra_v = tokudb::int_sign_extend(extra_v, 8*length);
    switch (operation) {
    case '+':
        if (!field_is_null) {
            bool over;
975
            v = tokudb::int_add(v, extra_v, 8*length, &over);
976 977 978 979 980 981
            if (over) {
                if (extra_v > 0)
                    v = tokudb::int_high_endpoint(8*length);
                else
                    v = tokudb::int_low_endpoint(8*length);
            }
982
            new_val.replace(the_offset, length, &v, length);
983 984 985 986 987
        }
        break;
    case '-':
        if (!field_is_null) {
            bool over;
988
            v = tokudb::int_sub(v, extra_v, 8*length, &over);
989 990 991 992 993 994
            if (over) {
                if (extra_v > 0)
                    v = tokudb::int_low_endpoint(8*length);
                else
                    v = tokudb::int_high_endpoint(8*length);
            }
995
            new_val.replace(the_offset, length, &v, length);
996 997 998 999 1000 1001 1002 1003
        }
        break;
    default:
        assert(0);
    }
}

// Update an unsigned field: unsigned newval@offset = old_val@offset OP extra_val
1004
static void uint_op(uint32_t operation, uint32_t the_offset, uint32_t length, uint32_t field_num, uint32_t field_null_num,
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
                    tokudb::buffer &new_val, tokudb::buffer &old_val, void *extra_val) {
    assert(the_offset + length <= new_val.size());
    assert(the_offset + length <= old_val.size());
    assert(length == 1 || length == 2 || length == 3 || length == 4 || length == 8);

    uchar *old_val_ptr = (uchar *) old_val.data();
    bool field_is_null = false;
    if (field_null_num)
        field_is_null = is_overall_null_position_set(old_val_ptr, field_null_num & ~(1<<31));
    uint64_t v = 0;
    memcpy(&v, old_val_ptr + the_offset, length);
    uint64_t extra_v = 0;
    memcpy(&extra_v, extra_val, length);
    switch (operation) {
    case '+':
        if (!field_is_null) {
            bool over;
1022
            v = tokudb::uint_add(v, extra_v, 8*length, &over);
1023 1024 1025
            if (over) {
                v = tokudb::uint_high_endpoint(8*length);
            }
1026
            new_val.replace(the_offset, length, &v, length);
1027 1028 1029 1030 1031
        }
        break;
    case '-':
        if (!field_is_null) {
            bool over;
1032
            v = tokudb::uint_sub(v, extra_v, 8*length, &over);
1033 1034 1035
            if (over) {
                v = tokudb::uint_low_endpoint(8*length);
            }
1036
            new_val.replace(the_offset, length, &v, length);
1037 1038 1039 1040 1041 1042 1043 1044 1045
        }
        break;
    default:
        assert(0);
    }
}

// Decode and apply a sequence of update operations defined in the extra to the old value and put the result
// in the new value.
1046 1047 1048 1049
static void apply_updates(tokudb::buffer &new_val, tokudb::buffer &old_val, tokudb::buffer &extra_val, const Simple_row_descriptor &sd) {
    uint32_t num_updates;
    extra_val.consume(&num_updates, sizeof num_updates);
    for ( ; num_updates > 0; num_updates--) {
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
        // get the update operation
        uint32_t update_operation;
        extra_val.consume(&update_operation, sizeof update_operation);
        uint32_t field_type;
        extra_val.consume(&field_type, sizeof field_type);
        uint32_t field_num;
        extra_val.consume(&field_num, sizeof field_num);
        uint32_t field_null_num;
        extra_val.consume(&field_null_num, sizeof field_null_num);
        uint32_t the_offset;
        extra_val.consume(&the_offset, sizeof the_offset);
        uint32_t length;
        extra_val.consume(&length, sizeof length);
        void *extra_val_ptr = extra_val.consume_ptr(length);

        // apply the update
        switch (field_type) {
        case UPDATE_TYPE_INT:
            if (update_operation == '=')
                set_fixed_field(the_offset, length, field_num, field_null_num, new_val, extra_val_ptr);
            else
1071
                int_op(update_operation, the_offset, length, field_num, field_null_num, new_val, old_val, extra_val_ptr);
1072 1073 1074 1075 1076
            break;
        case UPDATE_TYPE_UINT:
            if (update_operation == '=')
                set_fixed_field(the_offset, length, field_num, field_null_num, new_val, extra_val_ptr);
            else
1077
                uint_op(update_operation, the_offset, length, field_num, field_null_num, new_val, old_val, extra_val_ptr);
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
            break;
        case UPDATE_TYPE_CHAR:
        case UPDATE_TYPE_BINARY:
            if (update_operation == '=')
                set_fixed_field(the_offset, length, field_num, field_null_num, new_val, extra_val_ptr);
            else 
                assert(0);
            break;
        default:
            assert(0);
            break;
        }
    }
1091
    assert(extra_val.size() == extra_val.limit());
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
}

// Simple update handler. Decode the update message, apply the update operations to the old value, and set
// the new value.
static int tokudb_simple_update_fun(
    DB* db,
    const DBT *key_dbt,
    const DBT *old_val_dbt,
    const DBT *extra,
    void (*set_val)(const DBT *new_val_dbt, void *set_extra),
    void *set_extra
    )
{
    tokudb::buffer extra_val(extra->data, 0, extra->size);
    
    uchar operation;
    extra_val.consume(&operation, sizeof operation);
    assert(operation == UPDATE_OP_SIMPLE_UPDATE);

    if (old_val_dbt != NULL) {
        // get the simple descriptor
        Simple_row_descriptor sd;
        sd.consume(extra_val);

        tokudb::buffer old_val(old_val_dbt->data, old_val_dbt->size, old_val_dbt->size);

        // new val = old val
        tokudb::buffer new_val;
        new_val.append(old_val_dbt->data, old_val_dbt->size);

        // apply updates to new val
1123
        apply_updates(new_val, old_val, extra_val, sd);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

        // set the new val
        DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
        new_val_dbt.data = new_val.data();
        new_val_dbt.size = new_val.size();
        set_val(&new_val_dbt, set_extra);
    }

    return 0;
}

// Simple upsert handler. Decode the upsert message. If the key does not exist, then insert a new value from the extra.
// Otherwise, apply the update operations to the old value, and then set the new value.
static int tokudb_simple_upsert_fun(
    DB* db,
    const DBT *key_dbt,
    const DBT *old_val_dbt, 
    const DBT *extra,
    void (*set_val)(const DBT *new_val_dbt, void *set_extra),
    void *set_extra
    )
{
    tokudb::buffer extra_val(extra->data, 0, extra->size);

    uchar operation;
    extra_val.consume(&operation, sizeof operation);
    assert(operation == UPDATE_OP_SIMPLE_UPSERT);

    uint32_t insert_length;
    extra_val.consume(&insert_length, sizeof insert_length);
    void *insert_row = extra_val.consume_ptr(insert_length);

    if (old_val_dbt == NULL) {
        // insert a new row
        DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
        new_val_dbt.size = insert_length;
        new_val_dbt.data = insert_row;
        set_val(&new_val_dbt, set_extra);
    } else {
        // decode the simple descriptor
        Simple_row_descriptor sd;
        sd.consume(extra_val);

        tokudb::buffer old_val(old_val_dbt->data, old_val_dbt->size, old_val_dbt->size);

        // new val = old val
        tokudb::buffer new_val;
        new_val.append(old_val_dbt->data, old_val_dbt->size);

        // apply updates to new val
1174
        apply_updates(new_val, old_val, extra_val, sd);
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

        // set the new val
        DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
        new_val_dbt.data = new_val.data();
        new_val_dbt.size = new_val.size();
        set_val(&new_val_dbt, set_extra);
    }

    return 0;
}

// This function is the update callback function that is registered with the YDB environment.
// It uses the first byte in the update message to identify the update message type and call
// the handler for that message.
int tokudb_update_fun(
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    DB* db,
    const DBT *key,
    const DBT *old_val, 
    const DBT *extra,
    void (*set_val)(const DBT *new_val, void *set_extra),
    void *set_extra
    ) 
{
    uchar *extra_pos = (uchar *)extra->data;
    uchar operation = extra_pos[0];
1200
    int error;
1201
    switch (operation) {
1202
    case UPDATE_OP_COL_ADD_OR_DROP:
1203 1204
        error = tokudb_hcad_update_fun(db, key, old_val, extra, set_val, set_extra);
        break;
1205 1206
    case UPDATE_OP_EXPAND_VARIABLE_OFFSETS:
        error = tokudb_expand_variable_offsets(db, key, old_val, extra, set_val, set_extra);
1207
        break;
1208
    case UPDATE_OP_EXPAND_INT:
1209
    case UPDATE_OP_EXPAND_UINT:
1210 1211
        error = tokudb_expand_int_field(db, key, old_val, extra, set_val, set_extra);
        break;
1212 1213
    case UPDATE_OP_EXPAND_CHAR:
    case UPDATE_OP_EXPAND_BINARY:
1214
        error = tokudb_expand_char_field(db, key, old_val, extra, set_val, set_extra);
1215
        break;
1216 1217 1218 1219 1220 1221
    case UPDATE_OP_SIMPLE_UPDATE:
        error = tokudb_simple_update_fun(db, key, old_val, extra, set_val, set_extra);
        break;
    case UPDATE_OP_SIMPLE_UPSERT:
        error = tokudb_simple_upsert_fun(db, key, old_val, extra, set_val, set_extra);
        break;
1222 1223 1224 1225 1226 1227
    default:
        error = EINVAL;
        break;
    }
    return error;
}