You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1833 lines
65 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. /*
  5. COPYING CONDITIONS NOTICE:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation, and provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain this COPYING
  11. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  12. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  13. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  14. GRANT (below).
  15. * Redistributions in binary form must reproduce this COPYING
  16. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  17. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  18. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  19. GRANT (below) in the documentation and/or other materials
  20. provided with the distribution.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. COPYRIGHT NOTICE:
  26. TokuDB, Tokutek Fractal Tree Indexing Library.
  27. Copyright (C) 2007-2013 Tokutek, Inc.
  28. DISCLAIMER:
  29. This program is distributed in the hope that it will be useful, but
  30. WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. General Public License for more details.
  33. UNIVERSITY PATENT NOTICE:
  34. The technology is licensed by the Massachusetts Institute of
  35. Technology, Rutgers State University of New Jersey, and the Research
  36. Foundation of State University of New York at Stony Brook under
  37. United States of America Serial No. 11/760379 and to the patents
  38. and/or patent applications resulting from it.
  39. PATENT MARKING NOTICE:
  40. This software is covered by US Patent No. 8,185,551.
  41. This software is covered by US Patent No. 8,489,638.
  42. PATENT RIGHTS GRANT:
  43. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  44. Tokutek as part of the Fractal Tree project.
  45. "PATENT CLAIMS" means the claims of patents that are owned or
  46. licensable by Tokutek, both currently or in the future; and that in
  47. the absence of this license would be infringed by THIS
  48. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  49. "PATENT CHALLENGE" shall mean a challenge to the validity,
  50. patentability, enforceability and/or non-infringement of any of the
  51. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  52. Tokutek hereby grants to you, for the term and geographical scope of
  53. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  54. irrevocable (except as stated in this section) patent license to
  55. make, have made, use, offer to sell, sell, import, transfer, and
  56. otherwise run, modify, and propagate the contents of THIS
  57. IMPLEMENTATION, where such license applies only to the PATENT
  58. CLAIMS. This grant does not include claims that would be infringed
  59. only as a consequence of further modifications of THIS
  60. IMPLEMENTATION. If you or your agent or licensee institute or order
  61. or agree to the institution of patent litigation against any entity
  62. (including a cross-claim or counterclaim in a lawsuit) alleging that
  63. THIS IMPLEMENTATION constitutes direct or contributory patent
  64. infringement, or inducement of patent infringement, then any rights
  65. granted to you under this License shall terminate as of the date
  66. such litigation is filed. If you or your agent or exclusive
  67. licensee institute or order or agree to the institution of a PATENT
  68. CHALLENGE, then Tokutek may terminate any rights granted to you
  69. under this License.
  70. */
  71. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  72. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  73. // Update operation codes. These codes get stuffed into update messages, so they can not change.
  74. // The operations are currently stored in a single byte in the update message, so only 256 operations
  75. // are supported. When we need more, we can use the last (255) code to indicate that the operation code
  76. // is expanded beyond 1 byte.
  77. enum {
  78. UPDATE_OP_COL_ADD_OR_DROP = 0,
  79. UPDATE_OP_EXPAND_VARIABLE_OFFSETS = 1,
  80. UPDATE_OP_EXPAND_INT = 2,
  81. UPDATE_OP_EXPAND_UINT = 3,
  82. UPDATE_OP_EXPAND_CHAR = 4,
  83. UPDATE_OP_EXPAND_BINARY = 5,
  84. UPDATE_OP_EXPAND_BLOB = 6,
  85. UPDATE_OP_UPDATE_1 = 10,
  86. UPDATE_OP_UPSERT_1 = 11,
  87. UPDATE_OP_UPDATE_2 = 12,
  88. UPDATE_OP_UPSERT_2 = 13,
  89. };
  90. // Field types used in the update messages
  91. enum {
  92. UPDATE_TYPE_UNKNOWN = 0,
  93. UPDATE_TYPE_INT = 1,
  94. UPDATE_TYPE_UINT = 2,
  95. UPDATE_TYPE_CHAR = 3,
  96. UPDATE_TYPE_BINARY = 4,
  97. UPDATE_TYPE_VARCHAR = 5,
  98. UPDATE_TYPE_VARBINARY = 6,
  99. UPDATE_TYPE_TEXT = 7,
  100. UPDATE_TYPE_BLOB = 8,
  101. };
  102. #define UP_COL_ADD_OR_DROP UPDATE_OP_COL_ADD_OR_DROP
  103. // add or drop column sub-operations
  104. #define COL_DROP 0xaa
  105. #define COL_ADD 0xbb
  106. // add or drop column types
  107. #define COL_FIXED 0xcc
  108. #define COL_VAR 0xdd
  109. #define COL_BLOB 0xee
  110. #define STATIC_ROW_MUTATOR_SIZE 1+8+2+8+8+8
  111. // how much space do I need for the mutators?
  112. // static stuff first:
  113. // operation 1 == UP_COL_ADD_OR_DROP
  114. // 8 - old null, new null
  115. // 2 - old num_offset, new num_offset
  116. // 8 - old fixed_field size, new fixed_field_size
  117. // 8 - old and new length of offsets
  118. // 8 - old and new starting null bit position
  119. // TOTAL: 27
  120. // dynamic stuff:
  121. // 4 - number of columns
  122. // for each column:
  123. // 1 - add or drop
  124. // 1 - is nullable
  125. // 4 - if nullable, position
  126. // 1 - if add, whether default is null or not
  127. // 1 - if fixed, var, or not
  128. // for fixed, entire default
  129. // for var, 4 bytes length, then entire default
  130. // for blob, nothing
  131. // So, an upperbound is 4 + num_fields(12) + all default stuff
  132. // static blob stuff:
  133. // 4 - num blobs
  134. // 1 byte for each num blobs in old table
  135. // So, an upperbound is 4 + kc_info->num_blobs
  136. // dynamic blob stuff:
  137. // for each blob added:
  138. // 1 - state if we are adding or dropping
  139. // 4 - blob index
  140. // if add, 1 len bytes
  141. // at most, 4 0's
  142. // So, upperbound is num_blobs(1+4+1+4) = num_columns*10
  143. // The expand varchar offsets message is used to expand the size of an offset from 1 to 2 bytes. Not VLQ coded.
  144. // uint8 operation = UPDATE_OP_EXPAND_VARIABLE_OFFSETS
  145. // uint32 number of offsets
  146. // uint32 starting offset of the variable length field offsets
  147. // Expand the size of a fixed length column message. Not VLQ coded.
  148. // The field type is encoded in the operation code.
  149. // uint8 operation = UPDATE_OP_EXPAND_INT/UINT/CHAR/BINARY
  150. // uint32 offset offset of the field
  151. // uint32 old length the old length of the field's value
  152. // uint32 new length the new length of the field's value
  153. // uint8 operation = UPDATE_OP_EXPAND_CHAR/BINARY
  154. // uint32 offset offset of the field
  155. // uint32 old length the old length of the field's value
  156. // uint32 new length the new length of the field's value
  157. // uint8 pad char
  158. // Expand blobs message. VLQ coded.
  159. // uint8 operation = UPDATE_OP_EXPAND_BLOB
  160. // uint32 start variable offset
  161. // uint32 variable offset bytes
  162. // uint32 bytes per offset
  163. // uint32 num blobs = N
  164. // uint8 old lengths[N]
  165. // uint8 new lengths[N]
  166. // Update and Upsert version 1 messages. Not VLQ coded. Not used anymore, but may be in the
  167. // fractal tree from a previous build.
  168. //
  169. // Field descriptor:
  170. // Operations:
  171. // update operation 4 == { '=', '+', '-' }
  172. // x = k
  173. // x = x + k
  174. // x = x - k
  175. // field type 4 see field types above
  176. // unused 4 unused
  177. // field null num 4 bit 31 is 1 if the field is nullible and the remaining bits contain the null bit number
  178. // field offset 4 for fixed fields, this is the offset from begining of the row of the field
  179. // value:
  180. // value length 4 == N, length of the value
  181. // value N value to add or subtract
  182. //
  183. // Update_1 message:
  184. // Operation 1 == UPDATE_OP_UPDATE_1
  185. // fixed field offset 4 offset of the beginning of the fixed fields
  186. // var field offset 4 offset of the variable length offsets
  187. // var_offset_bytes 1 length of offsets (Note: not big enough)
  188. // bytes_per_offset 4 number of bytes per offset
  189. // Number of update ops 4 == N
  190. // Update ops [N]
  191. //
  192. // Upsert_1 message:
  193. // Operation 1 == UPDATE_OP_UPSERT_1
  194. // Insert row:
  195. // length 4 == N
  196. // data N
  197. // fixed field offset 4 offset of the beginning of the fixed fields
  198. // var field offset 4 offset of the variable length offsets
  199. // var_offset_bytes 1 length of offsets (Note: not big enough)
  200. // bytes_per_offset 4 number of bytes per offset
  201. // Number of update ops 4 == N
  202. // Update ops [N]
  203. // Update and Upserver version 2 messages. VLQ coded.
  204. // Update version 2
  205. // uint8 operation = UPDATE_OP_UPDATE_2
  206. // uint32 number of update ops = N
  207. // uint8 update ops [ N ]
  208. //
  209. // Upsert version 2
  210. // uint8 operation = UPDATE_OP_UPSERT_2
  211. // uint32 insert length = N
  212. // uint8 insert data [ N ]
  213. // uint32 number of update ops = M
  214. // update ops [ M ]
  215. //
  216. // Variable fields info
  217. // uint32 update operation = 'v'
  218. // uint32 start offset
  219. // uint32 num varchars
  220. // uint32 bytes per offset
  221. //
  222. // Blobs info
  223. // uint32 update operation = 'b'
  224. // uint32 num blobs = N
  225. // uint8 blob lengths [ N ]
  226. //
  227. // Update operation on fixed length fields
  228. // uint32 update operation = '=', '+', '-'
  229. // uint32 field type
  230. // uint32 null num 0 => not nullable, otherwise encoded as field_null_num + 1
  231. // uint32 offset
  232. // uint32 value length = N
  233. // uint8 value [ N ]
  234. //
  235. // Update operation on varchar fields
  236. // uint32 update operation = '='
  237. // uint32 field type
  238. // uint32 null num
  239. // uint32 var index
  240. // uint32 value length = N
  241. // uint8 value [ N ]
  242. //
  243. // Update operation on blob fields
  244. // uint32 update operation = '='
  245. // uint32 field type
  246. // uint32 null num
  247. // uint32 blob index
  248. // uint32 value length = N
  249. // uint8 value [ N ]
  250. #include "tokudb_buffer.h"
  251. #include "tokudb_math.h"
  252. //
  253. // checks whether the bit at index pos in data is set or not
  254. //
  255. static inline bool is_overall_null_position_set(uchar* data, uint32_t pos) {
  256. uint32_t offset = pos/8;
  257. uchar remainder = pos%8;
  258. uchar null_bit = 1<<remainder;
  259. return ((data[offset] & null_bit) != 0);
  260. }
  261. //
  262. // sets the bit at index pos in data to 1 if is_null, 0 otherwise
  263. //
  264. static inline void set_overall_null_position(uchar* data, uint32_t pos, bool is_null) {
  265. uint32_t offset = pos/8;
  266. uchar remainder = pos%8;
  267. uchar null_bit = 1<<remainder;
  268. if (is_null) {
  269. data[offset] |= null_bit;
  270. }
  271. else {
  272. data[offset] &= ~null_bit;
  273. }
  274. }
  275. static inline void copy_null_bits(
  276. uint32_t start_old_pos,
  277. uint32_t start_new_pos,
  278. uint32_t num_bits,
  279. uchar* old_null_bytes,
  280. uchar* new_null_bytes
  281. )
  282. {
  283. for (uint32_t i = 0; i < num_bits; i++) {
  284. uint32_t curr_old_pos = i + start_old_pos;
  285. uint32_t curr_new_pos = i + start_new_pos;
  286. // copy over old null bytes
  287. if (is_overall_null_position_set(old_null_bytes,curr_old_pos)) {
  288. set_overall_null_position(new_null_bytes,curr_new_pos,true);
  289. }
  290. else {
  291. set_overall_null_position(new_null_bytes,curr_new_pos,false);
  292. }
  293. }
  294. }
  295. static inline void copy_var_fields(
  296. uint32_t start_old_num_var_field, //index of var fields that we should start writing
  297. uint32_t num_var_fields, // number of var fields to copy
  298. uchar* old_var_field_offset_ptr, //static ptr to where offset bytes begin in old row
  299. uchar old_num_offset_bytes, //number of offset bytes used in old row
  300. uchar* start_new_var_field_data_ptr, // where the new var data should be written
  301. uchar* start_new_var_field_offset_ptr, // where the new var offsets should be written
  302. uchar* new_var_field_data_ptr, // pointer to beginning of var fields in new row
  303. uchar* old_var_field_data_ptr, // pointer to beginning of var fields in old row
  304. uint32_t new_num_offset_bytes, // number of offset bytes used in new row
  305. uint32_t* num_data_bytes_written,
  306. uint32_t* num_offset_bytes_written
  307. )
  308. {
  309. uchar* curr_new_var_field_data_ptr = start_new_var_field_data_ptr;
  310. uchar* curr_new_var_field_offset_ptr = start_new_var_field_offset_ptr;
  311. for (uint32_t i = 0; i < num_var_fields; i++) {
  312. uint32_t field_len;
  313. uint32_t start_read_offset;
  314. uint32_t curr_old = i + start_old_num_var_field;
  315. uchar* data_to_copy = NULL;
  316. // get the length and pointer to data that needs to be copied
  317. get_var_field_info(
  318. &field_len,
  319. &start_read_offset,
  320. curr_old,
  321. old_var_field_offset_ptr,
  322. old_num_offset_bytes
  323. );
  324. data_to_copy = old_var_field_data_ptr + start_read_offset;
  325. // now need to copy field_len bytes starting from data_to_copy
  326. curr_new_var_field_data_ptr = write_var_field(
  327. curr_new_var_field_offset_ptr,
  328. curr_new_var_field_data_ptr,
  329. new_var_field_data_ptr,
  330. data_to_copy,
  331. field_len,
  332. new_num_offset_bytes
  333. );
  334. curr_new_var_field_offset_ptr += new_num_offset_bytes;
  335. }
  336. *num_data_bytes_written = (uint32_t)(curr_new_var_field_data_ptr - start_new_var_field_data_ptr);
  337. *num_offset_bytes_written = (uint32_t)(curr_new_var_field_offset_ptr - start_new_var_field_offset_ptr);
  338. }
  339. static inline uint32_t copy_toku_blob(uchar* to_ptr, uchar* from_ptr, uint32_t len_bytes, bool skip) {
  340. uint32_t length = 0;
  341. if (!skip) {
  342. memcpy(to_ptr, from_ptr, len_bytes);
  343. }
  344. length = get_blob_field_len(from_ptr,len_bytes);
  345. if (!skip) {
  346. memcpy(to_ptr + len_bytes, from_ptr + len_bytes, length);
  347. }
  348. return (length + len_bytes);
  349. }
  350. static int tokudb_hcad_update_fun(
  351. DB* db,
  352. const DBT *key,
  353. const DBT *old_val,
  354. const DBT *extra,
  355. void (*set_val)(const DBT *new_val, void *set_extra),
  356. void *set_extra
  357. )
  358. {
  359. uint32_t max_num_bytes;
  360. uint32_t num_columns;
  361. DBT new_val;
  362. uint32_t num_bytes_left;
  363. uint32_t num_var_fields_to_copy;
  364. uint32_t num_data_bytes_written = 0;
  365. uint32_t num_offset_bytes_written = 0;
  366. int error;
  367. memset(&new_val, 0, sizeof(DBT));
  368. uchar operation;
  369. uchar* new_val_data = NULL;
  370. uchar* extra_pos = NULL;
  371. uchar* extra_pos_start = NULL;
  372. //
  373. // info for pointers into rows
  374. //
  375. uint32_t old_num_null_bytes;
  376. uint32_t new_num_null_bytes;
  377. uchar old_num_offset_bytes;
  378. uchar new_num_offset_bytes;
  379. uint32_t old_fixed_field_size;
  380. uint32_t new_fixed_field_size;
  381. uint32_t old_len_of_offsets;
  382. uint32_t new_len_of_offsets;
  383. uchar* old_fixed_field_ptr = NULL;
  384. uchar* new_fixed_field_ptr = NULL;
  385. uint32_t curr_old_fixed_offset;
  386. uint32_t curr_new_fixed_offset;
  387. uchar* old_null_bytes = NULL;
  388. uchar* new_null_bytes = NULL;
  389. uint32_t curr_old_null_pos;
  390. uint32_t curr_new_null_pos;
  391. uint32_t old_null_bits_left;
  392. uint32_t new_null_bits_left;
  393. uint32_t overall_null_bits_left;
  394. uint32_t old_num_var_fields;
  395. // uint32_t new_num_var_fields;
  396. uint32_t curr_old_num_var_field;
  397. uint32_t curr_new_num_var_field;
  398. uchar* old_var_field_offset_ptr = NULL;
  399. uchar* new_var_field_offset_ptr = NULL;
  400. uchar* curr_new_var_field_offset_ptr = NULL;
  401. uchar* old_var_field_data_ptr = NULL;
  402. uchar* new_var_field_data_ptr = NULL;
  403. uchar* curr_new_var_field_data_ptr = NULL;
  404. uint32_t start_blob_offset;
  405. uchar* start_blob_ptr;
  406. uint32_t num_blob_bytes;
  407. // came across a delete, nothing to update
  408. if (old_val == NULL) {
  409. error = 0;
  410. goto cleanup;
  411. }
  412. extra_pos_start = (uchar *)extra->data;
  413. extra_pos = (uchar *)extra->data;
  414. operation = extra_pos[0];
  415. extra_pos++;
  416. assert(operation == UP_COL_ADD_OR_DROP);
  417. memcpy(&old_num_null_bytes, extra_pos, sizeof(uint32_t));
  418. extra_pos += sizeof(uint32_t);
  419. memcpy(&new_num_null_bytes, extra_pos, sizeof(uint32_t));
  420. extra_pos += sizeof(uint32_t);
  421. old_num_offset_bytes = extra_pos[0];
  422. extra_pos++;
  423. new_num_offset_bytes = extra_pos[0];
  424. extra_pos++;
  425. memcpy(&old_fixed_field_size, extra_pos, sizeof(uint32_t));
  426. extra_pos += sizeof(uint32_t);
  427. memcpy(&new_fixed_field_size, extra_pos, sizeof(uint32_t));
  428. extra_pos += sizeof(uint32_t);
  429. memcpy(&old_len_of_offsets, extra_pos, sizeof(uint32_t));
  430. extra_pos += sizeof(uint32_t);
  431. memcpy(&new_len_of_offsets, extra_pos, sizeof(uint32_t));
  432. extra_pos += sizeof(uint32_t);
  433. max_num_bytes = old_val->size + extra->size + new_len_of_offsets + new_fixed_field_size;
  434. new_val_data = (uchar *)tokudb_my_malloc(
  435. max_num_bytes,
  436. MYF(MY_FAE)
  437. );
  438. if (new_val_data == NULL) { goto cleanup; }
  439. old_fixed_field_ptr = (uchar *) old_val->data;
  440. old_fixed_field_ptr += old_num_null_bytes;
  441. new_fixed_field_ptr = new_val_data + new_num_null_bytes;
  442. curr_old_fixed_offset = 0;
  443. curr_new_fixed_offset = 0;
  444. old_num_var_fields = old_len_of_offsets/old_num_offset_bytes;
  445. // new_num_var_fields = new_len_of_offsets/new_num_offset_bytes;
  446. // following fields will change as we write the variable data
  447. old_var_field_offset_ptr = old_fixed_field_ptr + old_fixed_field_size;
  448. new_var_field_offset_ptr = new_fixed_field_ptr + new_fixed_field_size;
  449. old_var_field_data_ptr = old_var_field_offset_ptr + old_len_of_offsets;
  450. new_var_field_data_ptr = new_var_field_offset_ptr + new_len_of_offsets;
  451. curr_new_var_field_offset_ptr = new_var_field_offset_ptr;
  452. curr_new_var_field_data_ptr = new_var_field_data_ptr;
  453. curr_old_num_var_field = 0;
  454. curr_new_num_var_field = 0;
  455. old_null_bytes = (uchar *)old_val->data;
  456. new_null_bytes = new_val_data;
  457. memcpy(&curr_old_null_pos, extra_pos, sizeof(uint32_t));
  458. extra_pos += sizeof(uint32_t);
  459. memcpy(&curr_new_null_pos, extra_pos, sizeof(uint32_t));
  460. extra_pos += sizeof(uint32_t);
  461. memcpy(&num_columns, extra_pos, sizeof(num_columns));
  462. extra_pos += sizeof(num_columns);
  463. memset(new_null_bytes, 0, new_num_null_bytes); // shut valgrind up
  464. //
  465. // now go through and apply the change into new_val_data
  466. //
  467. for (uint32_t i = 0; i < num_columns; i++) {
  468. uchar op_type = extra_pos[0];
  469. bool is_null_default = false;
  470. extra_pos++;
  471. assert(op_type == COL_DROP || op_type == COL_ADD);
  472. bool nullable = (extra_pos[0] != 0);
  473. extra_pos++;
  474. if (nullable) {
  475. uint32_t null_bit_position;
  476. memcpy(&null_bit_position, extra_pos, sizeof(uint32_t));
  477. extra_pos += sizeof(uint32_t);
  478. uint32_t num_bits;
  479. if (op_type == COL_DROP) {
  480. assert(curr_old_null_pos <= null_bit_position);
  481. num_bits = null_bit_position - curr_old_null_pos;
  482. }
  483. else {
  484. assert(curr_new_null_pos <= null_bit_position);
  485. num_bits = null_bit_position - curr_new_null_pos;
  486. }
  487. copy_null_bits(
  488. curr_old_null_pos,
  489. curr_new_null_pos,
  490. num_bits,
  491. old_null_bytes,
  492. new_null_bytes
  493. );
  494. // update the positions
  495. curr_new_null_pos += num_bits;
  496. curr_old_null_pos += num_bits;
  497. if (op_type == COL_DROP) {
  498. curr_old_null_pos++; // account for dropped column
  499. }
  500. else {
  501. is_null_default = (extra_pos[0] != 0);
  502. extra_pos++;
  503. set_overall_null_position(
  504. new_null_bytes,
  505. null_bit_position,
  506. is_null_default
  507. );
  508. curr_new_null_pos++; //account for added column
  509. }
  510. }
  511. uchar col_type = extra_pos[0];
  512. extra_pos++;
  513. if (col_type == COL_FIXED) {
  514. uint32_t col_offset;
  515. uint32_t col_size;
  516. uint32_t num_bytes_to_copy;
  517. memcpy(&col_offset, extra_pos, sizeof(uint32_t));
  518. extra_pos += sizeof(uint32_t);
  519. memcpy(&col_size, extra_pos, sizeof(uint32_t));
  520. extra_pos += sizeof(uint32_t);
  521. if (op_type == COL_DROP) {
  522. num_bytes_to_copy = col_offset - curr_old_fixed_offset;
  523. }
  524. else {
  525. num_bytes_to_copy = col_offset - curr_new_fixed_offset;
  526. }
  527. memcpy(
  528. new_fixed_field_ptr + curr_new_fixed_offset,
  529. old_fixed_field_ptr + curr_old_fixed_offset,
  530. num_bytes_to_copy
  531. );
  532. curr_old_fixed_offset += num_bytes_to_copy;
  533. curr_new_fixed_offset += num_bytes_to_copy;
  534. if (op_type == COL_DROP) {
  535. // move old_fixed_offset val to skip OVER column that is being dropped
  536. curr_old_fixed_offset += col_size;
  537. }
  538. else {
  539. if (is_null_default) {
  540. // copy zeroes
  541. memset(new_fixed_field_ptr + curr_new_fixed_offset, 0, col_size);
  542. }
  543. else {
  544. // copy data from extra_pos into new row
  545. memcpy(
  546. new_fixed_field_ptr + curr_new_fixed_offset,
  547. extra_pos,
  548. col_size
  549. );
  550. extra_pos += col_size;
  551. }
  552. curr_new_fixed_offset += col_size;
  553. }
  554. }
  555. else if (col_type == COL_VAR) {
  556. uint32_t var_col_index;
  557. memcpy(&var_col_index, extra_pos, sizeof(uint32_t));
  558. extra_pos += sizeof(uint32_t);
  559. if (op_type == COL_DROP) {
  560. num_var_fields_to_copy = var_col_index - curr_old_num_var_field;
  561. }
  562. else {
  563. num_var_fields_to_copy = var_col_index - curr_new_num_var_field;
  564. }
  565. copy_var_fields(
  566. curr_old_num_var_field,
  567. num_var_fields_to_copy,
  568. old_var_field_offset_ptr,
  569. old_num_offset_bytes,
  570. curr_new_var_field_data_ptr,
  571. curr_new_var_field_offset_ptr,
  572. new_var_field_data_ptr, // pointer to beginning of var fields in new row
  573. old_var_field_data_ptr, // pointer to beginning of var fields in old row
  574. new_num_offset_bytes, // number of offset bytes used in new row
  575. &num_data_bytes_written,
  576. &num_offset_bytes_written
  577. );
  578. curr_new_var_field_data_ptr += num_data_bytes_written;
  579. curr_new_var_field_offset_ptr += num_offset_bytes_written;
  580. curr_new_num_var_field += num_var_fields_to_copy;
  581. curr_old_num_var_field += num_var_fields_to_copy;
  582. if (op_type == COL_DROP) {
  583. curr_old_num_var_field++; // skip over dropped field
  584. }
  585. else {
  586. if (is_null_default) {
  587. curr_new_var_field_data_ptr = write_var_field(
  588. curr_new_var_field_offset_ptr,
  589. curr_new_var_field_data_ptr,
  590. new_var_field_data_ptr,
  591. NULL, //copying no data
  592. 0, //copying 0 bytes
  593. new_num_offset_bytes
  594. );
  595. curr_new_var_field_offset_ptr += new_num_offset_bytes;
  596. }
  597. else {
  598. uint32_t data_length;
  599. memcpy(&data_length, extra_pos, sizeof(data_length));
  600. extra_pos += sizeof(data_length);
  601. curr_new_var_field_data_ptr = write_var_field(
  602. curr_new_var_field_offset_ptr,
  603. curr_new_var_field_data_ptr,
  604. new_var_field_data_ptr,
  605. extra_pos, //copying data from mutator
  606. data_length, //copying data_length bytes
  607. new_num_offset_bytes
  608. );
  609. extra_pos += data_length;
  610. curr_new_var_field_offset_ptr += new_num_offset_bytes;
  611. }
  612. curr_new_num_var_field++; //account for added column
  613. }
  614. }
  615. else if (col_type == COL_BLOB) {
  616. // handle blob data later
  617. continue;
  618. }
  619. else {
  620. assert(false);
  621. }
  622. }
  623. // finish copying the null stuff
  624. old_null_bits_left = 8*old_num_null_bytes - curr_old_null_pos;
  625. new_null_bits_left = 8*new_num_null_bytes - curr_new_null_pos;
  626. overall_null_bits_left = old_null_bits_left;
  627. set_if_smaller(overall_null_bits_left, new_null_bits_left);
  628. copy_null_bits(
  629. curr_old_null_pos,
  630. curr_new_null_pos,
  631. overall_null_bits_left,
  632. old_null_bytes,
  633. new_null_bytes
  634. );
  635. // finish copying fixed field stuff
  636. num_bytes_left = old_fixed_field_size - curr_old_fixed_offset;
  637. memcpy(
  638. new_fixed_field_ptr + curr_new_fixed_offset,
  639. old_fixed_field_ptr + curr_old_fixed_offset,
  640. num_bytes_left
  641. );
  642. curr_old_fixed_offset += num_bytes_left;
  643. curr_new_fixed_offset += num_bytes_left;
  644. // sanity check
  645. assert(curr_new_fixed_offset == new_fixed_field_size);
  646. // finish copying var field stuff
  647. num_var_fields_to_copy = old_num_var_fields - curr_old_num_var_field;
  648. copy_var_fields(
  649. curr_old_num_var_field,
  650. num_var_fields_to_copy,
  651. old_var_field_offset_ptr,
  652. old_num_offset_bytes,
  653. curr_new_var_field_data_ptr,
  654. curr_new_var_field_offset_ptr,
  655. new_var_field_data_ptr, // pointer to beginning of var fields in new row
  656. old_var_field_data_ptr, // pointer to beginning of var fields in old row
  657. new_num_offset_bytes, // number of offset bytes used in new row
  658. &num_data_bytes_written,
  659. &num_offset_bytes_written
  660. );
  661. curr_new_var_field_offset_ptr += num_offset_bytes_written;
  662. curr_new_var_field_data_ptr += num_data_bytes_written;
  663. // sanity check
  664. assert(curr_new_var_field_offset_ptr == new_var_field_data_ptr);
  665. // start handling blobs
  666. get_blob_field_info(
  667. &start_blob_offset,
  668. old_len_of_offsets,
  669. old_var_field_data_ptr,
  670. old_num_offset_bytes
  671. );
  672. start_blob_ptr = old_var_field_data_ptr + start_blob_offset;
  673. // if nothing else in extra, then there are no blobs to add or drop, so can copy blobs straight
  674. if ((extra_pos - extra_pos_start) == extra->size) {
  675. num_blob_bytes = old_val->size - (start_blob_ptr - old_null_bytes);
  676. memcpy(curr_new_var_field_data_ptr, start_blob_ptr, num_blob_bytes);
  677. curr_new_var_field_data_ptr += num_blob_bytes;
  678. }
  679. // else, there is blob information to process
  680. else {
  681. uchar* len_bytes = NULL;
  682. uint32_t curr_old_blob = 0;
  683. uint32_t curr_new_blob = 0;
  684. uint32_t num_old_blobs = 0;
  685. uchar* curr_old_blob_ptr = start_blob_ptr;
  686. memcpy(&num_old_blobs, extra_pos, sizeof(num_old_blobs));
  687. extra_pos += sizeof(num_old_blobs);
  688. len_bytes = extra_pos;
  689. extra_pos += num_old_blobs;
  690. // copy over blob fields one by one
  691. while ((extra_pos - extra_pos_start) < extra->size) {
  692. uchar op_type = extra_pos[0];
  693. extra_pos++;
  694. uint32_t num_blobs_to_copy = 0;
  695. uint32_t blob_index;
  696. memcpy(&blob_index, extra_pos, sizeof(blob_index));
  697. extra_pos += sizeof(blob_index);
  698. assert (op_type == COL_DROP || op_type == COL_ADD);
  699. if (op_type == COL_DROP) {
  700. num_blobs_to_copy = blob_index - curr_old_blob;
  701. }
  702. else {
  703. num_blobs_to_copy = blob_index - curr_new_blob;
  704. }
  705. for (uint32_t i = 0; i < num_blobs_to_copy; i++) {
  706. uint32_t num_bytes_written = copy_toku_blob(
  707. curr_new_var_field_data_ptr,
  708. curr_old_blob_ptr,
  709. len_bytes[curr_old_blob + i],
  710. false
  711. );
  712. curr_old_blob_ptr += num_bytes_written;
  713. curr_new_var_field_data_ptr += num_bytes_written;
  714. }
  715. curr_old_blob += num_blobs_to_copy;
  716. curr_new_blob += num_blobs_to_copy;
  717. if (op_type == COL_DROP) {
  718. // skip over blob in row
  719. uint32_t num_bytes = copy_toku_blob(
  720. NULL,
  721. curr_old_blob_ptr,
  722. len_bytes[curr_old_blob],
  723. true
  724. );
  725. curr_old_blob++;
  726. curr_old_blob_ptr += num_bytes;
  727. }
  728. else {
  729. // copy new data
  730. uint32_t new_len_bytes = extra_pos[0];
  731. extra_pos++;
  732. uint32_t num_bytes = copy_toku_blob(
  733. curr_new_var_field_data_ptr,
  734. extra_pos,
  735. new_len_bytes,
  736. false
  737. );
  738. curr_new_blob++;
  739. curr_new_var_field_data_ptr += num_bytes;
  740. extra_pos += num_bytes;
  741. }
  742. }
  743. num_blob_bytes = old_val->size - (curr_old_blob_ptr - old_null_bytes);
  744. memcpy(curr_new_var_field_data_ptr, curr_old_blob_ptr, num_blob_bytes);
  745. curr_new_var_field_data_ptr += num_blob_bytes;
  746. }
  747. new_val.data = new_val_data;
  748. new_val.size = curr_new_var_field_data_ptr - new_val_data;
  749. set_val(&new_val, set_extra);
  750. error = 0;
  751. cleanup:
  752. tokudb_my_free(new_val_data);
  753. return error;
  754. }
  755. // Expand the variable offset array in the old row given the update mesage in the extra.
  756. static int tokudb_expand_variable_offsets(
  757. DB* db,
  758. const DBT *key,
  759. const DBT *old_val,
  760. const DBT *extra,
  761. void (*set_val)(const DBT *new_val, void *set_extra),
  762. void *set_extra
  763. )
  764. {
  765. int error = 0;
  766. tokudb::buffer extra_val(extra->data, 0, extra->size);
  767. // decode the operation
  768. uint8_t operation;
  769. extra_val.consume(&operation, sizeof operation);
  770. assert(operation == UPDATE_OP_EXPAND_VARIABLE_OFFSETS);
  771. // decode number of offsets
  772. uint32_t number_of_offsets;
  773. extra_val.consume(&number_of_offsets, sizeof number_of_offsets);
  774. // decode the offset start
  775. uint32_t offset_start;
  776. extra_val.consume(&offset_start, sizeof offset_start);
  777. assert(extra_val.size() == extra_val.limit());
  778. DBT new_val; memset(&new_val, 0, sizeof new_val);
  779. if (old_val != NULL) {
  780. assert(offset_start + number_of_offsets < old_val->size);
  781. // compute the new val from the old val
  782. uchar *old_val_ptr = (uchar *)old_val->data;
  783. // allocate space for the new val's data
  784. uchar *new_val_ptr = (uchar *)tokudb_my_malloc(number_of_offsets + old_val->size, MYF(MY_FAE));
  785. if (!new_val_ptr) {
  786. error = ENOMEM;
  787. goto cleanup;
  788. }
  789. new_val.data = new_val_ptr;
  790. // copy up to the start of the varchar offset
  791. memcpy(new_val_ptr, old_val_ptr, offset_start);
  792. new_val_ptr += offset_start;
  793. old_val_ptr += offset_start;
  794. // expand each offset from 1 to 2 bytes
  795. for (uint32_t i = 0; i < number_of_offsets; i++) {
  796. uint16_t new_offset = *old_val_ptr;
  797. int2store(new_val_ptr, new_offset);
  798. new_val_ptr += 2;
  799. old_val_ptr += 1;
  800. }
  801. // copy the rest of the row
  802. size_t n = old_val->size - (old_val_ptr - (uchar *)old_val->data);
  803. memcpy(new_val_ptr, old_val_ptr, n);
  804. new_val_ptr += n;
  805. old_val_ptr += n;
  806. new_val.size = new_val_ptr - (uchar *)new_val.data;
  807. assert(new_val_ptr == (uchar *)new_val.data + new_val.size);
  808. assert(old_val_ptr == (uchar *)old_val->data + old_val->size);
  809. // set the new val
  810. set_val(&new_val, set_extra);
  811. }
  812. error = 0;
  813. cleanup:
  814. tokudb_my_free(new_val.data);
  815. return error;
  816. }
  817. // Expand an int field in a old row given the expand message in the extra.
  818. static int tokudb_expand_int_field(
  819. DB* db,
  820. const DBT *key,
  821. const DBT *old_val,
  822. const DBT *extra,
  823. void (*set_val)(const DBT *new_val, void *set_extra),
  824. void *set_extra
  825. )
  826. {
  827. int error = 0;
  828. tokudb::buffer extra_val(extra->data, 0, extra->size);
  829. uint8_t operation;
  830. extra_val.consume(&operation, sizeof operation);
  831. assert(operation == UPDATE_OP_EXPAND_INT || operation == UPDATE_OP_EXPAND_UINT);
  832. uint32_t the_offset;
  833. extra_val.consume(&the_offset, sizeof the_offset);
  834. uint32_t old_length;
  835. extra_val.consume(&old_length, sizeof old_length);
  836. uint32_t new_length;
  837. extra_val.consume(&new_length, sizeof new_length);
  838. assert(extra_val.size() == extra_val.limit());
  839. assert(new_length >= old_length); // expand only
  840. DBT new_val; memset(&new_val, 0, sizeof new_val);
  841. if (old_val != NULL) {
  842. assert(the_offset + old_length <= old_val->size); // old field within the old val
  843. // compute the new val from the old val
  844. uchar *old_val_ptr = (uchar *)old_val->data;
  845. // allocate space for the new val's data
  846. uchar *new_val_ptr = (uchar *)tokudb_my_malloc(old_val->size + (new_length - old_length), MYF(MY_FAE));
  847. if (!new_val_ptr) {
  848. error = ENOMEM;
  849. goto cleanup;
  850. }
  851. new_val.data = new_val_ptr;
  852. // copy up to the old offset
  853. memcpy(new_val_ptr, old_val_ptr, the_offset);
  854. new_val_ptr += the_offset;
  855. old_val_ptr += the_offset;
  856. switch (operation) {
  857. case UPDATE_OP_EXPAND_INT:
  858. // fill the entire new value with ones or zeros depending on the sign bit
  859. // the encoding is little endian
  860. memset(new_val_ptr, (old_val_ptr[old_length-1] & 0x80) ? 0xff : 0x00, new_length);
  861. memcpy(new_val_ptr, old_val_ptr, old_length); // overlay the low bytes of the new value with the old value
  862. new_val_ptr += new_length;
  863. old_val_ptr += old_length;
  864. break;
  865. case UPDATE_OP_EXPAND_UINT:
  866. memset(new_val_ptr, 0, new_length); // fill the entire new value with zeros
  867. memcpy(new_val_ptr, old_val_ptr, old_length); // overlay the low bytes of the new value with the old value
  868. new_val_ptr += new_length;
  869. old_val_ptr += old_length;
  870. break;
  871. default:
  872. assert(0);
  873. }
  874. // copy the rest
  875. size_t n = old_val->size - (old_val_ptr - (uchar *)old_val->data);
  876. memcpy(new_val_ptr, old_val_ptr, n);
  877. new_val_ptr += n;
  878. old_val_ptr += n;
  879. new_val.size = new_val_ptr - (uchar *)new_val.data;
  880. assert(new_val_ptr == (uchar *)new_val.data + new_val.size);
  881. assert(old_val_ptr == (uchar *)old_val->data + old_val->size);
  882. // set the new val
  883. set_val(&new_val, set_extra);
  884. }
  885. error = 0;
  886. cleanup:
  887. tokudb_my_free(new_val.data);
  888. return error;
  889. }
  890. // Expand a char field in a old row given the expand message in the extra.
  891. static int tokudb_expand_char_field(
  892. DB* db,
  893. const DBT *key,
  894. const DBT *old_val,
  895. const DBT *extra,
  896. void (*set_val)(const DBT *new_val, void *set_extra),
  897. void *set_extra
  898. )
  899. {
  900. int error = 0;
  901. tokudb::buffer extra_val(extra->data, 0, extra->size);
  902. uint8_t operation;
  903. extra_val.consume(&operation, sizeof operation);
  904. assert(operation == UPDATE_OP_EXPAND_CHAR || operation == UPDATE_OP_EXPAND_BINARY);
  905. uint32_t the_offset;
  906. extra_val.consume(&the_offset, sizeof the_offset);
  907. uint32_t old_length;
  908. extra_val.consume(&old_length, sizeof old_length);
  909. uint32_t new_length;
  910. extra_val.consume(&new_length, sizeof new_length);
  911. uchar pad_char;
  912. extra_val.consume(&pad_char, sizeof pad_char);
  913. assert(extra_val.size() == extra_val.limit());
  914. assert(new_length >= old_length); // expand only
  915. DBT new_val; memset(&new_val, 0, sizeof new_val);
  916. if (old_val != NULL) {
  917. assert(the_offset + old_length <= old_val->size); // old field within the old val
  918. // compute the new val from the old val
  919. uchar *old_val_ptr = (uchar *)old_val->data;
  920. // allocate space for the new val's data
  921. uchar *new_val_ptr = (uchar *)tokudb_my_malloc(old_val->size + (new_length - old_length), MYF(MY_FAE));
  922. if (!new_val_ptr) {
  923. error = ENOMEM;
  924. goto cleanup;
  925. }
  926. new_val.data = new_val_ptr;
  927. // copy up to the old offset
  928. memcpy(new_val_ptr, old_val_ptr, the_offset);
  929. new_val_ptr += the_offset;
  930. old_val_ptr += the_offset;
  931. switch (operation) {
  932. case UPDATE_OP_EXPAND_CHAR:
  933. case UPDATE_OP_EXPAND_BINARY:
  934. memset(new_val_ptr, pad_char, new_length); // fill the entire new value with the pad char
  935. memcpy(new_val_ptr, old_val_ptr, old_length); // overlay the low bytes of the new value with the old value
  936. new_val_ptr += new_length;
  937. old_val_ptr += old_length;
  938. break;
  939. default:
  940. assert(0);
  941. }
  942. // copy the rest
  943. size_t n = old_val->size - (old_val_ptr - (uchar *)old_val->data);
  944. memcpy(new_val_ptr, old_val_ptr, n);
  945. new_val_ptr += n;
  946. old_val_ptr += n;
  947. new_val.size = new_val_ptr - (uchar *)new_val.data;
  948. assert(new_val_ptr == (uchar *)new_val.data + new_val.size);
  949. assert(old_val_ptr == (uchar *)old_val->data + old_val->size);
  950. // set the new val
  951. set_val(&new_val, set_extra);
  952. }
  953. error = 0;
  954. cleanup:
  955. tokudb_my_free(new_val.data);
  956. return error;
  957. }
  958. namespace tokudb {
  959. class var_fields {
  960. public:
  961. var_fields() {
  962. }
  963. void init_var_fields(uint32_t var_offset, uint32_t offset_bytes, uint32_t bytes_per_offset, tokudb::buffer *val_buffer) {
  964. assert(bytes_per_offset == 0 || bytes_per_offset == 1 || bytes_per_offset == 2);
  965. m_var_offset = var_offset;
  966. m_val_offset = m_var_offset + offset_bytes;
  967. m_bytes_per_offset = bytes_per_offset;
  968. if (bytes_per_offset > 0) {
  969. m_num_fields = offset_bytes / bytes_per_offset;
  970. } else {
  971. assert(offset_bytes == 0);
  972. m_num_fields = 0;
  973. }
  974. m_val_buffer = val_buffer;
  975. }
  976. uint32_t value_offset(uint32_t var_index);
  977. uint32_t value_length(uint32_t var_index);
  978. void update_offsets(uint32_t var_index, uint32_t old_s, uint32_t new_s);
  979. uint32_t end_offset();
  980. void replace(uint32_t var_index, void *new_val_ptr, uint32_t new_val_length);
  981. private:
  982. uint32_t read_offset(uint32_t var_index);
  983. void write_offset(uint32_t var_index, uint32_t v);
  984. private:
  985. uint32_t m_var_offset;
  986. uint32_t m_val_offset;
  987. uint32_t m_bytes_per_offset;
  988. uint32_t m_num_fields;
  989. tokudb::buffer *m_val_buffer;
  990. };
  991. // Return the ith variable length offset
  992. uint32_t var_fields::read_offset(uint32_t var_index) {
  993. uint32_t offset = 0;
  994. m_val_buffer->read(&offset, m_bytes_per_offset, m_var_offset + var_index * m_bytes_per_offset);
  995. return offset;
  996. }
  997. // Write the ith variable length offset with a new offset.
  998. void var_fields::write_offset(uint32_t var_index, uint32_t new_offset) {
  999. m_val_buffer->write(&new_offset, m_bytes_per_offset, m_var_offset + var_index * m_bytes_per_offset);
  1000. }
  1001. // Return the offset of the ith variable length field
  1002. uint32_t var_fields::value_offset(uint32_t var_index) {
  1003. assert(var_index < m_num_fields);
  1004. if (var_index == 0)
  1005. return m_val_offset;
  1006. else
  1007. return m_val_offset + read_offset(var_index-1);
  1008. }
  1009. // Return the length of the ith variable length field
  1010. uint32_t var_fields::value_length(uint32_t var_index) {
  1011. assert(var_index < m_num_fields);
  1012. if (var_index == 0)
  1013. return read_offset(0);
  1014. else
  1015. return read_offset(var_index) - read_offset(var_index-1);
  1016. }
  1017. // The length of the ith variable length fields changed. Update all of the subsequent offsets.
  1018. void var_fields::update_offsets(uint32_t var_index, uint32_t old_s, uint32_t new_s) {
  1019. assert(var_index < m_num_fields);
  1020. if (old_s == new_s)
  1021. return;
  1022. for (uint i = var_index; i < m_num_fields; i++) {
  1023. uint32_t v = read_offset(i);
  1024. if (new_s > old_s)
  1025. write_offset(i, v + (new_s - old_s));
  1026. else
  1027. write_offset(i, v - (old_s - new_s));
  1028. }
  1029. }
  1030. uint32_t var_fields::end_offset() {
  1031. if (m_num_fields == 0)
  1032. return m_val_offset;
  1033. else
  1034. return m_val_offset + read_offset(m_num_fields-1);
  1035. }
  1036. void var_fields::replace(uint32_t var_index, void *new_val_ptr, uint32_t new_val_length) {
  1037. // replace the new val with the extra val
  1038. uint32_t the_offset = value_offset(var_index);
  1039. uint32_t old_s = value_length(var_index);
  1040. uint32_t new_s = new_val_length;
  1041. m_val_buffer->replace(the_offset, old_s, new_val_ptr, new_s);
  1042. // update the var offsets
  1043. update_offsets(var_index, old_s, new_s);
  1044. }
  1045. class blob_fields {
  1046. public:
  1047. blob_fields() {
  1048. }
  1049. void init_blob_fields(uint32_t num_blobs, const uint8_t *blob_lengths, tokudb::buffer *val_buffer) {
  1050. m_num_blobs = num_blobs; m_blob_lengths = blob_lengths; m_val_buffer = val_buffer;
  1051. }
  1052. void start_blobs(uint32_t offset) {
  1053. m_blob_offset = offset;
  1054. }
  1055. void replace(uint32_t blob_index, uint32_t length, void *p);
  1056. void expand_length(uint32_t blob_index, uint8_t old_length_length, uint8_t new_length_length);
  1057. private:
  1058. uint32_t read_length(uint32_t offset, size_t size);
  1059. void write_length(uint32_t offset, size_t size, uint32_t new_length);
  1060. uint32_t blob_offset(uint32_t blob_index);
  1061. private:
  1062. uint32_t m_blob_offset;
  1063. uint32_t m_num_blobs;
  1064. const uint8_t *m_blob_lengths;
  1065. tokudb::buffer *m_val_buffer;
  1066. };
  1067. uint32_t blob_fields::read_length(uint32_t offset, size_t blob_length) {
  1068. uint32_t length = 0;
  1069. m_val_buffer->read(&length, blob_length, offset);
  1070. return length;
  1071. }
  1072. void blob_fields::write_length(uint32_t offset, size_t size, uint32_t new_length) {
  1073. m_val_buffer->write(&new_length, size, offset);
  1074. }
  1075. uint32_t blob_fields::blob_offset(uint32_t blob_index) {
  1076. assert(blob_index < m_num_blobs);
  1077. uint32_t offset = m_blob_offset;
  1078. for (uint i = 0; i < blob_index; i++) {
  1079. uint32_t blob_length = m_blob_lengths[i];
  1080. uint32_t length = read_length(offset, blob_length);
  1081. offset += blob_length + length;
  1082. }
  1083. return offset;
  1084. }
  1085. void blob_fields::replace(uint32_t blob_index, uint32_t new_length, void *new_value) {
  1086. assert(blob_index < m_num_blobs);
  1087. // compute the ith blob offset
  1088. uint32_t offset = blob_offset(blob_index);
  1089. uint8_t blob_length = m_blob_lengths[blob_index];
  1090. // read the old length
  1091. uint32_t old_length = read_length(offset, blob_length);
  1092. // replace the data
  1093. m_val_buffer->replace(offset + blob_length, old_length, new_value, new_length);
  1094. // write the new length
  1095. write_length(offset, blob_length, new_length);
  1096. }
  1097. void blob_fields::expand_length(uint32_t blob_index, uint8_t old_length_length, uint8_t new_length_length) {
  1098. assert(blob_index < m_num_blobs);
  1099. assert(old_length_length == m_blob_lengths[blob_index]);
  1100. // compute the ith blob offset
  1101. uint32_t offset = blob_offset(blob_index);
  1102. // read the blob length
  1103. uint32_t blob_length = read_length(offset, old_length_length);
  1104. // expand the length
  1105. m_val_buffer->replace(offset, old_length_length, &blob_length, new_length_length);
  1106. }
  1107. class value_map {
  1108. public:
  1109. value_map(tokudb::buffer *val_buffer) : m_val_buffer(val_buffer) {
  1110. }
  1111. void init_var_fields(uint32_t var_offset, uint32_t offset_bytes, uint32_t bytes_per_offset) {
  1112. m_var_fields.init_var_fields(var_offset, offset_bytes, bytes_per_offset, m_val_buffer);
  1113. }
  1114. void init_blob_fields(uint32_t num_blobs, const uint8_t *blob_lengths) {
  1115. m_blob_fields.init_blob_fields(num_blobs, blob_lengths, m_val_buffer);
  1116. }
  1117. // Replace the value of a fixed length field
  1118. void replace_fixed(uint32_t the_offset, uint32_t field_null_num, void *new_val_ptr, uint32_t new_val_length) {
  1119. m_val_buffer->replace(the_offset, new_val_length, new_val_ptr, new_val_length);
  1120. maybe_clear_null(field_null_num);
  1121. }
  1122. // Replace the value of a variable length field
  1123. void replace_varchar(uint32_t var_index, uint32_t field_null_num, void *new_val_ptr, uint32_t new_val_length) {
  1124. m_var_fields.replace(var_index, new_val_ptr, new_val_length);
  1125. maybe_clear_null(field_null_num);
  1126. }
  1127. // Replace the value of a blob field
  1128. void replace_blob(uint32_t blob_index, uint32_t field_null_num, void *new_val_ptr, uint32_t new_val_length) {
  1129. m_blob_fields.start_blobs(m_var_fields.end_offset());
  1130. m_blob_fields.replace(blob_index, new_val_length, new_val_ptr);
  1131. maybe_clear_null(field_null_num);
  1132. }
  1133. void expand_blob_lengths(uint32_t num_blob, const uint8_t *old_length, const uint8_t *new_length);
  1134. void int_op(uint32_t operation, uint32_t the_offset, uint32_t length, uint32_t field_null_num,
  1135. tokudb::buffer &old_val, void *extra_val);
  1136. void uint_op(uint32_t operation, uint32_t the_offset, uint32_t length, uint32_t field_null_num,
  1137. tokudb::buffer &old_val, void *extra_val);
  1138. private:
  1139. bool is_null(uint32_t null_num, uchar *null_bytes) {
  1140. bool field_is_null = false;
  1141. if (null_num) {
  1142. if (null_num & (1<<31))
  1143. null_num &= ~(1<<31);
  1144. else
  1145. null_num -= 1;
  1146. field_is_null = is_overall_null_position_set(null_bytes, null_num);
  1147. }
  1148. return field_is_null;
  1149. }
  1150. void maybe_clear_null(uint32_t null_num) {
  1151. if (null_num) {
  1152. if (null_num & (1<<31))
  1153. null_num &= ~(1<<31);
  1154. else
  1155. null_num -= 1;
  1156. set_overall_null_position((uchar *) m_val_buffer->data(), null_num, false);
  1157. }
  1158. }
  1159. private:
  1160. var_fields m_var_fields;
  1161. blob_fields m_blob_fields;
  1162. tokudb::buffer *m_val_buffer;
  1163. };
  1164. // Update an int field: signed newval@offset = old_val@offset OP extra_val
  1165. void value_map::int_op(uint32_t operation, uint32_t the_offset, uint32_t length, uint32_t field_null_num,
  1166. tokudb::buffer &old_val, void *extra_val) {
  1167. assert(the_offset + length <= m_val_buffer->size());
  1168. assert(the_offset + length <= old_val.size());
  1169. assert(length == 1 || length == 2 || length == 3 || length == 4 || length == 8);
  1170. uchar *old_val_ptr = (uchar *) old_val.data();
  1171. bool field_is_null = is_null(field_null_num, old_val_ptr);
  1172. int64_t v = 0;
  1173. memcpy(&v, old_val_ptr + the_offset, length);
  1174. v = tokudb::int_sign_extend(v, 8*length);
  1175. int64_t extra_v = 0;
  1176. memcpy(&extra_v, extra_val, length);
  1177. extra_v = tokudb::int_sign_extend(extra_v, 8*length);
  1178. switch (operation) {
  1179. case '+':
  1180. if (!field_is_null) {
  1181. bool over;
  1182. v = tokudb::int_add(v, extra_v, 8*length, &over);
  1183. if (over) {
  1184. if (extra_v > 0)
  1185. v = tokudb::int_high_endpoint(8*length);
  1186. else
  1187. v = tokudb::int_low_endpoint(8*length);
  1188. }
  1189. m_val_buffer->replace(the_offset, length, &v, length);
  1190. }
  1191. break;
  1192. case '-':
  1193. if (!field_is_null) {
  1194. bool over;
  1195. v = tokudb::int_sub(v, extra_v, 8*length, &over);
  1196. if (over) {
  1197. if (extra_v > 0)
  1198. v = tokudb::int_low_endpoint(8*length);
  1199. else
  1200. v = tokudb::int_high_endpoint(8*length);
  1201. }
  1202. m_val_buffer->replace(the_offset, length, &v, length);
  1203. }
  1204. break;
  1205. default:
  1206. assert(0);
  1207. }
  1208. }
  1209. // Update an unsigned field: unsigned newval@offset = old_val@offset OP extra_val
  1210. void value_map::uint_op(uint32_t operation, uint32_t the_offset, uint32_t length, uint32_t field_null_num,
  1211. tokudb::buffer &old_val, void *extra_val) {
  1212. assert(the_offset + length <= m_val_buffer->size());
  1213. assert(the_offset + length <= old_val.size());
  1214. assert(length == 1 || length == 2 || length == 3 || length == 4 || length == 8);
  1215. uchar *old_val_ptr = (uchar *) old_val.data();
  1216. bool field_is_null = is_null(field_null_num, old_val_ptr);
  1217. uint64_t v = 0;
  1218. memcpy(&v, old_val_ptr + the_offset, length);
  1219. uint64_t extra_v = 0;
  1220. memcpy(&extra_v, extra_val, length);
  1221. switch (operation) {
  1222. case '+':
  1223. if (!field_is_null) {
  1224. bool over;
  1225. v = tokudb::uint_add(v, extra_v, 8*length, &over);
  1226. if (over) {
  1227. v = tokudb::uint_high_endpoint(8*length);
  1228. }
  1229. m_val_buffer->replace(the_offset, length, &v, length);
  1230. }
  1231. break;
  1232. case '-':
  1233. if (!field_is_null) {
  1234. bool over;
  1235. v = tokudb::uint_sub(v, extra_v, 8*length, &over);
  1236. if (over) {
  1237. v = tokudb::uint_low_endpoint(8*length);
  1238. }
  1239. m_val_buffer->replace(the_offset, length, &v, length);
  1240. }
  1241. break;
  1242. default:
  1243. assert(0);
  1244. }
  1245. }
  1246. void value_map::expand_blob_lengths(uint32_t num_blob, const uint8_t *old_length, const uint8_t *new_length) {
  1247. uint8_t current_length[num_blob];
  1248. memcpy(current_length, old_length, num_blob);
  1249. for (uint32_t i = 0; i < num_blob; i++) {
  1250. if (new_length[i] > current_length[i]) {
  1251. m_blob_fields.init_blob_fields(num_blob, current_length, m_val_buffer);
  1252. m_blob_fields.start_blobs(m_var_fields.end_offset());
  1253. m_blob_fields.expand_length(i, current_length[i], new_length[i]);
  1254. current_length[i] = new_length[i];
  1255. }
  1256. }
  1257. }
  1258. }
  1259. static uint32_t consume_uint32(tokudb::buffer &b) {
  1260. uint32_t n;
  1261. size_t s = b.consume_ui<uint32_t>(&n);
  1262. assert(s > 0);
  1263. return n;
  1264. }
  1265. static uint8_t *consume_uint8_array(tokudb::buffer &b, uint32_t array_size) {
  1266. uint8_t *p = (uint8_t *) b.consume_ptr(array_size);
  1267. assert(p);
  1268. return p;
  1269. }
  1270. static int tokudb_expand_blobs(
  1271. DB* db,
  1272. const DBT *key_dbt,
  1273. const DBT *old_val_dbt,
  1274. const DBT *extra,
  1275. void (*set_val)(const DBT *new_val_dbt, void *set_extra),
  1276. void *set_extra
  1277. )
  1278. {
  1279. tokudb::buffer extra_val(extra->data, 0, extra->size);
  1280. uint8_t operation;
  1281. extra_val.consume(&operation, sizeof operation);
  1282. assert(operation == UPDATE_OP_EXPAND_BLOB);
  1283. if (old_val_dbt != NULL) {
  1284. // new val = old val
  1285. tokudb::buffer new_val;
  1286. new_val.append(old_val_dbt->data, old_val_dbt->size);
  1287. tokudb::value_map vd(&new_val);
  1288. // decode variable field info
  1289. uint32_t var_field_offset = consume_uint32(extra_val);
  1290. uint32_t var_offset_bytes = consume_uint32(extra_val);
  1291. uint32_t bytes_per_offset = consume_uint32(extra_val);
  1292. vd.init_var_fields(var_field_offset, var_offset_bytes, bytes_per_offset);
  1293. // decode blob info
  1294. uint32_t num_blob = consume_uint32(extra_val);
  1295. const uint8_t *old_blob_length = consume_uint8_array(extra_val, num_blob);
  1296. const uint8_t *new_blob_length = consume_uint8_array(extra_val, num_blob);
  1297. assert(extra_val.size() == extra_val.limit());
  1298. // expand blob lengths
  1299. vd.expand_blob_lengths(num_blob, old_blob_length, new_blob_length);
  1300. // set the new val
  1301. DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
  1302. new_val_dbt.data = new_val.data();
  1303. new_val_dbt.size = new_val.size();
  1304. set_val(&new_val_dbt, set_extra);
  1305. }
  1306. return 0;
  1307. }
  1308. // Decode and apply a sequence of update operations defined in the extra to the old value and put the result in the new value.
  1309. static void apply_1_updates(tokudb::value_map &vd, tokudb::buffer &new_val, tokudb::buffer &old_val, tokudb::buffer &extra_val) {
  1310. uint32_t num_updates;
  1311. extra_val.consume(&num_updates, sizeof num_updates);
  1312. for ( ; num_updates > 0; num_updates--) {
  1313. // get the update operation
  1314. uint32_t update_operation;
  1315. extra_val.consume(&update_operation, sizeof update_operation);
  1316. uint32_t field_type;
  1317. extra_val.consume(&field_type, sizeof field_type);
  1318. uint32_t unused;
  1319. extra_val.consume(&unused, sizeof unused);
  1320. uint32_t field_null_num;
  1321. extra_val.consume(&field_null_num, sizeof field_null_num);
  1322. uint32_t the_offset;
  1323. extra_val.consume(&the_offset, sizeof the_offset);
  1324. uint32_t extra_val_length;
  1325. extra_val.consume(&extra_val_length, sizeof extra_val_length);
  1326. void *extra_val_ptr = extra_val.consume_ptr(extra_val_length);
  1327. // apply the update
  1328. switch (field_type) {
  1329. case UPDATE_TYPE_INT:
  1330. if (update_operation == '=')
  1331. vd.replace_fixed(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1332. else
  1333. vd.int_op(update_operation, the_offset, extra_val_length, field_null_num, old_val, extra_val_ptr);
  1334. break;
  1335. case UPDATE_TYPE_UINT:
  1336. if (update_operation == '=')
  1337. vd.replace_fixed(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1338. else
  1339. vd.uint_op(update_operation, the_offset, extra_val_length, field_null_num, old_val, extra_val_ptr);
  1340. break;
  1341. case UPDATE_TYPE_CHAR:
  1342. case UPDATE_TYPE_BINARY:
  1343. if (update_operation == '=')
  1344. vd.replace_fixed(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1345. else
  1346. assert(0);
  1347. break;
  1348. default:
  1349. assert(0);
  1350. break;
  1351. }
  1352. }
  1353. assert(extra_val.size() == extra_val.limit());
  1354. }
  1355. // Simple update handler. Decode the update message, apply the update operations to the old value, and set the new value.
  1356. static int tokudb_update_1_fun(
  1357. DB* db,
  1358. const DBT *key_dbt,
  1359. const DBT *old_val_dbt,
  1360. const DBT *extra,
  1361. void (*set_val)(const DBT *new_val_dbt, void *set_extra),
  1362. void *set_extra
  1363. )
  1364. {
  1365. tokudb::buffer extra_val(extra->data, 0, extra->size);
  1366. uint8_t operation;
  1367. extra_val.consume(&operation, sizeof operation);
  1368. assert(operation == UPDATE_OP_UPDATE_1);
  1369. if (old_val_dbt != NULL) {
  1370. // get the simple descriptor
  1371. uint32_t m_fixed_field_offset;
  1372. extra_val.consume(&m_fixed_field_offset, sizeof m_fixed_field_offset);
  1373. uint32_t m_var_field_offset;
  1374. extra_val.consume(&m_var_field_offset, sizeof m_var_field_offset);
  1375. uint32_t m_var_offset_bytes;
  1376. extra_val.consume(&m_var_offset_bytes, sizeof m_var_offset_bytes);
  1377. uint32_t m_bytes_per_offset;
  1378. extra_val.consume(&m_bytes_per_offset, sizeof m_bytes_per_offset);
  1379. tokudb::buffer old_val(old_val_dbt->data, old_val_dbt->size, old_val_dbt->size);
  1380. // new val = old val
  1381. tokudb::buffer new_val;
  1382. new_val.append(old_val_dbt->data, old_val_dbt->size);
  1383. tokudb::value_map vd(&new_val);
  1384. vd.init_var_fields(m_var_field_offset, m_var_offset_bytes, m_bytes_per_offset);
  1385. // apply updates to new val
  1386. apply_1_updates(vd, new_val, old_val, extra_val);
  1387. // set the new val
  1388. DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
  1389. new_val_dbt.data = new_val.data();
  1390. new_val_dbt.size = new_val.size();
  1391. set_val(&new_val_dbt, set_extra);
  1392. }
  1393. return 0;
  1394. }
  1395. // Simple upsert handler. Decode the upsert message. If the key does not exist, then insert a new value from the extra.
  1396. // Otherwise, apply the update operations to the old value, and then set the new value.
  1397. static int tokudb_upsert_1_fun(
  1398. DB* db,
  1399. const DBT *key_dbt,
  1400. const DBT *old_val_dbt,
  1401. const DBT *extra,
  1402. void (*set_val)(const DBT *new_val_dbt, void *set_extra),
  1403. void *set_extra
  1404. )
  1405. {
  1406. tokudb::buffer extra_val(extra->data, 0, extra->size);
  1407. uint8_t operation;
  1408. extra_val.consume(&operation, sizeof operation);
  1409. assert(operation == UPDATE_OP_UPSERT_1);
  1410. uint32_t insert_length;
  1411. extra_val.consume(&insert_length, sizeof insert_length);
  1412. void *insert_row = extra_val.consume_ptr(insert_length);
  1413. if (old_val_dbt == NULL) {
  1414. // insert a new row
  1415. DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
  1416. new_val_dbt.size = insert_length;
  1417. new_val_dbt.data = insert_row;
  1418. set_val(&new_val_dbt, set_extra);
  1419. } else {
  1420. // decode the simple descriptor
  1421. uint32_t m_fixed_field_offset;
  1422. extra_val.consume(&m_fixed_field_offset, sizeof m_fixed_field_offset);
  1423. uint32_t m_var_field_offset;
  1424. extra_val.consume(&m_var_field_offset, sizeof m_var_field_offset);
  1425. uint32_t m_var_offset_bytes;
  1426. extra_val.consume(&m_var_offset_bytes, sizeof m_var_offset_bytes);
  1427. uint32_t m_bytes_per_offset;
  1428. extra_val.consume(&m_bytes_per_offset, sizeof m_bytes_per_offset);
  1429. tokudb::buffer old_val(old_val_dbt->data, old_val_dbt->size, old_val_dbt->size);
  1430. // new val = old val
  1431. tokudb::buffer new_val;
  1432. new_val.append(old_val_dbt->data, old_val_dbt->size);
  1433. tokudb::value_map vd(&new_val);
  1434. vd.init_var_fields(m_var_field_offset, m_var_offset_bytes, m_bytes_per_offset);
  1435. // apply updates to new val
  1436. apply_1_updates(vd, new_val, old_val, extra_val);
  1437. // set the new val
  1438. DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
  1439. new_val_dbt.data = new_val.data();
  1440. new_val_dbt.size = new_val.size();
  1441. set_val(&new_val_dbt, set_extra);
  1442. }
  1443. return 0;
  1444. }
  1445. // Decode and apply a sequence of update operations defined in the extra to the old value and put the result in the new value.
  1446. static void apply_2_updates(tokudb::value_map &vd, tokudb::buffer &new_val, tokudb::buffer &old_val, tokudb::buffer &extra_val) {
  1447. uint32_t num_updates = consume_uint32(extra_val);
  1448. for (uint32_t i = 0; i < num_updates; i++) {
  1449. uint32_t update_operation = consume_uint32(extra_val);
  1450. if (update_operation == 'v') {
  1451. uint32_t var_field_offset = consume_uint32(extra_val);
  1452. uint32_t var_offset_bytes = consume_uint32(extra_val);
  1453. uint32_t bytes_per_offset = consume_uint32(extra_val);
  1454. vd.init_var_fields(var_field_offset, var_offset_bytes, bytes_per_offset);
  1455. } else if (update_operation == 'b') {
  1456. uint32_t num_blobs = consume_uint32(extra_val);
  1457. const uint8_t *blob_lengths = consume_uint8_array(extra_val, num_blobs);
  1458. vd.init_blob_fields(num_blobs, blob_lengths);
  1459. } else {
  1460. uint32_t field_type = consume_uint32(extra_val);
  1461. uint32_t field_null_num = consume_uint32(extra_val);
  1462. uint32_t the_offset = consume_uint32(extra_val);
  1463. uint32_t extra_val_length = consume_uint32(extra_val);
  1464. void *extra_val_ptr = extra_val.consume_ptr(extra_val_length); assert(extra_val_ptr);
  1465. switch (field_type) {
  1466. case UPDATE_TYPE_INT:
  1467. if (update_operation == '=')
  1468. vd.replace_fixed(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1469. else
  1470. vd.int_op(update_operation, the_offset, extra_val_length, field_null_num, old_val, extra_val_ptr);
  1471. break;
  1472. case UPDATE_TYPE_UINT:
  1473. if (update_operation == '=')
  1474. vd.replace_fixed(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1475. else
  1476. vd.uint_op(update_operation, the_offset, extra_val_length, field_null_num, old_val, extra_val_ptr);
  1477. break;
  1478. case UPDATE_TYPE_CHAR:
  1479. case UPDATE_TYPE_BINARY:
  1480. if (update_operation == '=')
  1481. vd.replace_fixed(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1482. else
  1483. assert(0);
  1484. break;
  1485. case UPDATE_TYPE_VARBINARY:
  1486. case UPDATE_TYPE_VARCHAR:
  1487. if (update_operation == '=')
  1488. vd.replace_varchar(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1489. else
  1490. assert(0);
  1491. break;
  1492. case UPDATE_TYPE_TEXT:
  1493. case UPDATE_TYPE_BLOB:
  1494. if (update_operation == '=')
  1495. vd.replace_blob(the_offset, field_null_num, extra_val_ptr, extra_val_length);
  1496. else
  1497. assert(0);
  1498. break;
  1499. default:
  1500. assert(0);
  1501. break;
  1502. }
  1503. }
  1504. }
  1505. assert(extra_val.size() == extra_val.limit());
  1506. }
  1507. // Simple update handler. Decode the update message, apply the update operations to the old value, and set the new value.
  1508. static int tokudb_update_2_fun(
  1509. DB* db,
  1510. const DBT *key_dbt,
  1511. const DBT *old_val_dbt,
  1512. const DBT *extra,
  1513. void (*set_val)(const DBT *new_val_dbt, void *set_extra),
  1514. void *set_extra
  1515. )
  1516. {
  1517. tokudb::buffer extra_val(extra->data, 0, extra->size);
  1518. uint8_t op;
  1519. extra_val.consume(&op, sizeof op);
  1520. assert(op == UPDATE_OP_UPDATE_2);
  1521. if (old_val_dbt != NULL) {
  1522. tokudb::buffer old_val(old_val_dbt->data, old_val_dbt->size, old_val_dbt->size);
  1523. // new val = old val
  1524. tokudb::buffer new_val;
  1525. new_val.append(old_val_dbt->data, old_val_dbt->size);
  1526. tokudb::value_map vd(&new_val);
  1527. // apply updates to new val
  1528. apply_2_updates(vd, new_val, old_val, extra_val);
  1529. // set the new val
  1530. DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
  1531. new_val_dbt.data = new_val.data();
  1532. new_val_dbt.size = new_val.size();
  1533. set_val(&new_val_dbt, set_extra);
  1534. }
  1535. return 0;
  1536. }
  1537. // Simple upsert handler. Decode the upsert message. If the key does not exist, then insert a new value from the extra.
  1538. // Otherwise, apply the update operations to the old value, and then set the new value.
  1539. static int tokudb_upsert_2_fun(
  1540. DB* db,
  1541. const DBT *key_dbt,
  1542. const DBT *old_val_dbt,
  1543. const DBT *extra,
  1544. void (*set_val)(const DBT *new_val_dbt, void *set_extra),
  1545. void *set_extra
  1546. )
  1547. {
  1548. tokudb::buffer extra_val(extra->data, 0, extra->size);
  1549. uint8_t op;
  1550. extra_val.consume(&op, sizeof op);
  1551. assert(op == UPDATE_OP_UPSERT_2);
  1552. uint32_t insert_length = consume_uint32(extra_val);
  1553. assert(insert_length < extra_val.limit());
  1554. void *insert_row = extra_val.consume_ptr(insert_length); assert(insert_row);
  1555. if (old_val_dbt == NULL) {
  1556. // insert a new row
  1557. DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
  1558. new_val_dbt.size = insert_length;
  1559. new_val_dbt.data = insert_row;
  1560. set_val(&new_val_dbt, set_extra);
  1561. } else {
  1562. tokudb::buffer old_val(old_val_dbt->data, old_val_dbt->size, old_val_dbt->size);
  1563. // new val = old val
  1564. tokudb::buffer new_val;
  1565. new_val.append(old_val_dbt->data, old_val_dbt->size);
  1566. tokudb::value_map vd(&new_val);
  1567. // apply updates to new val
  1568. apply_2_updates(vd, new_val, old_val, extra_val);
  1569. // set the new val
  1570. DBT new_val_dbt; memset(&new_val_dbt, 0, sizeof new_val_dbt);
  1571. new_val_dbt.data = new_val.data();
  1572. new_val_dbt.size = new_val.size();
  1573. set_val(&new_val_dbt, set_extra);
  1574. }
  1575. return 0;
  1576. }
  1577. // This function is the update callback function that is registered with the YDB environment.
  1578. // It uses the first byte in the update message to identify the update message type and call
  1579. // the handler for that message.
  1580. int tokudb_update_fun(
  1581. DB* db,
  1582. const DBT *key,
  1583. const DBT *old_val,
  1584. const DBT *extra,
  1585. void (*set_val)(const DBT *new_val, void *set_extra),
  1586. void *set_extra
  1587. )
  1588. {
  1589. assert(extra->size > 0);
  1590. uint8_t *extra_pos = (uchar *)extra->data;
  1591. uint8_t operation = extra_pos[0];
  1592. int error;
  1593. switch (operation) {
  1594. case UPDATE_OP_COL_ADD_OR_DROP:
  1595. error = tokudb_hcad_update_fun(db, key, old_val, extra, set_val, set_extra);
  1596. break;
  1597. case UPDATE_OP_EXPAND_VARIABLE_OFFSETS:
  1598. error = tokudb_expand_variable_offsets(db, key, old_val, extra, set_val, set_extra);
  1599. break;
  1600. case UPDATE_OP_EXPAND_INT:
  1601. case UPDATE_OP_EXPAND_UINT:
  1602. error = tokudb_expand_int_field(db, key, old_val, extra, set_val, set_extra);
  1603. break;
  1604. case UPDATE_OP_EXPAND_CHAR:
  1605. case UPDATE_OP_EXPAND_BINARY:
  1606. error = tokudb_expand_char_field(db, key, old_val, extra, set_val, set_extra);
  1607. break;
  1608. case UPDATE_OP_EXPAND_BLOB:
  1609. error = tokudb_expand_blobs(db, key, old_val, extra, set_val, set_extra);
  1610. break;
  1611. case UPDATE_OP_UPDATE_1:
  1612. error = tokudb_update_1_fun(db, key, old_val, extra, set_val, set_extra);
  1613. break;
  1614. case UPDATE_OP_UPSERT_1:
  1615. error = tokudb_upsert_1_fun(db, key, old_val, extra, set_val, set_extra);
  1616. break;
  1617. case UPDATE_OP_UPDATE_2:
  1618. error = tokudb_update_2_fun(db, key, old_val, extra, set_val, set_extra);
  1619. break;
  1620. case UPDATE_OP_UPSERT_2:
  1621. error = tokudb_upsert_2_fun(db, key, old_val, extra, set_val, set_extra);
  1622. break;
  1623. default:
  1624. assert(0);
  1625. error = EINVAL;
  1626. break;
  1627. }
  1628. return error;
  1629. }