summaryrefslogtreecommitdiff
path: root/storage/tokudb/tokudb_update_fun.cc
blob: 05149d17317b45760cae34b68ab2feb0da5a841e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
423
424
425
426
427
428
429
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
467
468
469
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
509
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
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
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
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
1123
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
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*======
This file is part of TokuDB


Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.

    TokuDBis is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2,
    as published by the Free Software Foundation.

    TokuDB is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with TokuDB.  If not, see <http://www.gnu.org/licenses/>.

======= */

#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."

// 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.
enum {
    UPDATE_OP_COL_ADD_OR_DROP = 0,

    UPDATE_OP_EXPAND_VARIABLE_OFFSETS = 1,
    UPDATE_OP_EXPAND_INT = 2,
    UPDATE_OP_EXPAND_UINT = 3,
    UPDATE_OP_EXPAND_CHAR = 4,
    UPDATE_OP_EXPAND_BINARY = 5,
    UPDATE_OP_EXPAND_BLOB = 6,

    UPDATE_OP_UPDATE_1 = 10,
    UPDATE_OP_UPSERT_1 = 11,
    UPDATE_OP_UPDATE_2 = 12,
    UPDATE_OP_UPSERT_2 = 13,
};

// 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,
    UPDATE_TYPE_TEXT = 7,
    UPDATE_TYPE_BLOB = 8,
};

#define UP_COL_ADD_OR_DROP UPDATE_OP_COL_ADD_OR_DROP

// add or drop column sub-operations
#define COL_DROP 0xaa
#define COL_ADD 0xbb

// add or drop column types
#define COL_FIXED 0xcc
#define COL_VAR 0xdd
#define COL_BLOB 0xee

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

// 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

// The expand varchar offsets message is used to expand the size of an offset
// from 1 to 2 bytes.  Not VLQ coded.
//     uint8  operation          = UPDATE_OP_EXPAND_VARIABLE_OFFSETS
//     uint32 number of offsets
//     uint32 starting offset of the variable length field offsets 

// Expand the size of a fixed length column message. Not VLQ coded.
// The field type is encoded in the operation code.
//     uint8  operation          = UPDATE_OP_EXPAND_INT/UINT/CHAR/BINARY
//     uint32 offset             offset of the field
//     uint32 old length         the old length of the field's value
//     uint32 new length         the new length of the field's value

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

// Expand blobs message. VLQ coded.
//     uint8  operation = UPDATE_OP_EXPAND_BLOB
//     uint32 start variable offset
//     uint32 variable offset bytes
//     uint32 bytes per offset
//     uint32 num blobs = N
//     uint8  old lengths[N]
//     uint8  new lengths[N]

// Update and Upsert version 1 messages. Not VLQ coded. Not used anymore, but
// may be in the fractal tree from a previous build.
//
// Field descriptor:
// Operations:
//     update operation   4 == { '=', '+', '-' }
//         x = k
//         x = x + k
//         x = x - k
//     field type         4 see field types above
//     unused             4 unused
//     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
//     value:
//         value length   4 == N, length of the value
//         value          N value to add or subtract
//
// Update_1 message:
//     Operation          1 == UPDATE_OP_UPDATE_1
//     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 length of offsets (Note: not big enough)
//     bytes_per_offset   4 number of bytes per offset
//     Number of update ops 4 == N
//     Update ops [N]
//
// Upsert_1 message:
//     Operation          1 == UPDATE_OP_UPSERT_1
//     Insert row:
//         length         4 == N
//         data           N
//     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 length of offsets (Note: not big enough)
//     bytes_per_offset   4 number of bytes per offset
//     Number of update ops 4 == N
//     Update ops [N] 

// Update and Upserver version 2 messages. VLQ coded.
// Update version 2
//     uint8  operation = UPDATE_OP_UPDATE_2
//     uint32 number of update ops = N
//     uint8  update ops [ N ] 
//
// Upsert version 2
//     uint8 operation = UPDATE_OP_UPSERT_2
//     uint32 insert length = N
//     uint8 insert data [ N ]
//     uint32 number of update ops = M
//     update ops [ M ]
// 
// Variable fields info
//     uint32 update operation = 'v'
//     uint32 start offset
//     uint32 num varchars
//     uint32 bytes per offset
//
// Blobs info
//     uint32 update operation = 'b'
//     uint32 num blobs = N
//     uint8  blob lengths [ N ]
//
// Update operation on fixed length fields
//     uint32 update operation = '=', '+', '-'
//     uint32 field type
//     uint32 null num 0 => not nullable, otherwise encoded as field_null_num + 1
//     uint32 offset
//     uint32 value length = N
//     uint8  value [ N ] 
//
// Update operation on varchar fields
//     uint32 update operation = '='
//     uint32 field type
//     uint32 null num
//     uint32 var index
//     uint32 value length = N
//     uint8  value [ N ] 
//
// Update operation on blob fields
//     uint32 update operation = '='
//     uint32 field type
//     uint32 null num
//     uint32 blob index
//     uint32 value length = N
//     uint8  value [ N ] 

#include "tokudb_buffer.h"
#include "tokudb_math.h"

//
// checks whether the bit at index pos in data is set or not
//
static inline bool is_overall_null_position_set(uchar* data, uint32_t pos) {
    uint32_t offset = pos/8;
    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
// 
static inline void set_overall_null_position(
    uchar* data,
    uint32_t pos,
    bool is_null) {

    uint32_t offset = pos/8;
    uchar remainder = pos%8;
    uchar null_bit = 1<<remainder;
    if (is_null) {
        data[offset] |= null_bit;
    }
    else {
        data[offset] &= ~null_bit;
    }
}

static inline void copy_null_bits(
    uint32_t start_old_pos,
    uint32_t start_new_pos,
    uint32_t num_bits,
    uchar* old_null_bytes,
    uchar* new_null_bytes) {
    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;
        // 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);
        }
    }
}

static inline void copy_var_fields(
    //index of var fields that we should start writing
    uint32_t start_old_num_var_field,
    // number of var fields to copy
    uint32_t num_var_fields,
    //static ptr to where offset bytes begin in old row
    uchar* old_var_field_offset_ptr,
    //number of offset bytes used in old row
    uchar old_num_offset_bytes,
    // where the new var data should be written
    uchar* start_new_var_field_data_ptr,
    // where the new var offsets should be written
    uchar* start_new_var_field_offset_ptr,
    // pointer to beginning of var fields in new row
    uchar* new_var_field_data_ptr,
    // pointer to beginning of var fields in old row
    uchar* old_var_field_data_ptr,
    // number of offset bytes used in new row
    uint32_t new_num_offset_bytes,
    uint32_t* num_data_bytes_written,
    uint32_t* num_offset_bytes_written) {

    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;
    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;
        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;
    }
    *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);
}

static inline uint32_t copy_toku_blob(
    uchar* to_ptr,
    uchar* from_ptr,
    uint32_t len_bytes,
    bool skip) {

    uint32_t length = 0;
    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);
}

static int tokudb_hcad_update_fun(
    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) {

    uint32_t max_num_bytes;
    uint32_t num_columns;
    DBT new_val;
    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;
    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
    //
    uint32_t old_num_null_bytes;
    uint32_t new_num_null_bytes;
    uchar old_num_offset_bytes;
    uchar new_num_offset_bytes;
    uint32_t old_fixed_field_size;
    uint32_t new_fixed_field_size;
    uint32_t old_len_of_offsets;
    uint32_t new_len_of_offsets;

    uchar* old_fixed_field_ptr = NULL;
    uchar* new_fixed_field_ptr = NULL;
    uint32_t curr_old_fixed_offset;
    uint32_t curr_new_fixed_offset;

    uchar* old_null_bytes = NULL;
    uchar* new_null_bytes = NULL;
    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;
    // uint32_t new_num_var_fields;
    uint32_t curr_old_num_var_field;
    uint32_t curr_new_num_var_field;
    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;

    uint32_t start_blob_offset;
    uchar* start_blob_ptr;
    uint32_t num_blob_bytes;

    // 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_always(operation == UP_COL_ADD_OR_DROP);

    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);

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

    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);

    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);

    max_num_bytes =
        old_val->size + extra->size + new_len_of_offsets + new_fixed_field_size;
    new_val_data = (uchar *)tokudb::memory::malloc(
        max_num_bytes,
        MYF(MY_FAE));
    if (new_val_data == NULL) {
        error = ENOMEM;
        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;
    // new_num_var_fields = new_len_of_offsets/new_num_offset_bytes;
    // 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;
    
    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);

    memcpy(&num_columns, extra_pos, sizeof(num_columns));
    extra_pos += sizeof(num_columns);

    memset(new_null_bytes, 0, new_num_null_bytes); // shut valgrind up
    
    //
    // now go through and apply the change into new_val_data
    //
    for (uint32_t i = 0; i < num_columns; i++) {
        uchar op_type = extra_pos[0];
        bool is_null_default = false;
        extra_pos++;

        assert_always(op_type == COL_DROP || op_type == COL_ADD);
        bool nullable = (extra_pos[0] != 0);
        extra_pos++;
        if (nullable) {
            uint32_t null_bit_position;
            memcpy(&null_bit_position, extra_pos, sizeof(uint32_t));
            extra_pos += sizeof(uint32_t);
            uint32_t num_bits;
            if (op_type == COL_DROP) {
                assert_always(curr_old_null_pos <= null_bit_position);
                num_bits = null_bit_position - curr_old_null_pos;
            } else {
                assert_always(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) {
            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);

            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) {
            uint32_t var_col_index;
            memcpy(&var_col_index, extra_pos, sizeof(uint32_t));
            extra_pos += sizeof(uint32_t);
            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,
                // pointer to beginning of var fields in new row
                new_var_field_data_ptr,
                // pointer to beginning of var fields in old row
                old_var_field_data_ptr,
                // number of offset bytes used in new row
                new_num_offset_bytes,
                &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 {
                    uint32_t data_length;
                    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_unreachable();
        }
    }
    // 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_always(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,
        // pointer to beginning of var fields in new row
        new_var_field_data_ptr,
        // pointer to beginning of var fields in old row
        old_var_field_data_ptr,
        // number of offset bytes used in new row
        new_num_offset_bytes,
        &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_always(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 {
        // else, there is blob information to process
        uchar* len_bytes = NULL;
        uint32_t curr_old_blob = 0;
        uint32_t curr_new_blob = 0;
        uint32_t num_old_blobs = 0;
        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++;
            uint32_t num_blobs_to_copy = 0;
            uint32_t blob_index;
            memcpy(&blob_index, extra_pos, sizeof(blob_index));
            extra_pos += sizeof(blob_index);
            assert_always (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;
            }
            for (uint32_t i = 0; i < num_blobs_to_copy; i++) {
                uint32_t num_bytes_written = copy_toku_blob(
                    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
                uint32_t num_bytes = copy_toku_blob(
                    NULL,
                    curr_old_blob_ptr,
                    len_bytes[curr_old_blob],
                    true);
                curr_old_blob++;
                curr_old_blob_ptr += num_bytes;
            } else {
                // copy new data
                uint32_t new_len_bytes = extra_pos[0];
                extra_pos++;
                uint32_t num_bytes = copy_toku_blob(
                    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:
    tokudb::memory::free(new_val_data);
    return error;    
}

// Expand the variable offset array in the old row given the update mesage
// in the extra.
static int tokudb_expand_variable_offsets(
    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;
    tokudb::buffer extra_val(extra->data, 0, extra->size);

    // decode the operation
    uint8_t operation;
    extra_val.consume(&operation, sizeof operation);
    assert_always(operation == UPDATE_OP_EXPAND_VARIABLE_OFFSETS);

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

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

    assert_always(extra_val.size() == extra_val.limit());

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

    if (old_val != NULL) {
        assert_always(offset_start + number_of_offsets <= old_val->size);
    
        // 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*)tokudb::memory::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;
        
        // expand each offset from 1 to 2 bytes
        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_always(new_val_ptr == (uchar *)new_val.data + new_val.size);
        assert_always(old_val_ptr == (uchar *)old_val->data + old_val->size);

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

    error = 0;

cleanup:
    tokudb::memory::free(new_val.data);
    return error;
}

// Expand an int field in a old row given the expand message in the extra.
static int tokudb_expand_int_field(
    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;
    tokudb::buffer extra_val(extra->data, 0, extra->size);

    uint8_t operation;
    extra_val.consume(&operation, sizeof operation);
    assert_always(
        operation == UPDATE_OP_EXPAND_INT ||
        operation == UPDATE_OP_EXPAND_UINT);
    uint32_t the_offset;
    extra_val.consume(&the_offset, sizeof the_offset);
    uint32_t old_length;
    extra_val.consume(&old_length, sizeof old_length);
    uint32_t new_length;
    extra_val.consume(&new_length, sizeof new_length);
    assert_always(extra_val.size() == extra_val.limit());

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

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

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

        // 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*)tokudb::memory::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) {
        case UPDATE_OP_EXPAND_INT:
            // 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);
            // overlay the low bytes of the new value with the old value
            memcpy(new_val_ptr, old_val_ptr, old_length);
            new_val_ptr += new_length;
            old_val_ptr += old_length;
            break;
        case UPDATE_OP_EXPAND_UINT:
            // fill the entire new value with zeros
            memset(new_val_ptr, 0, new_length);
            // overlay the low bytes of the new value with the old value
            memcpy(new_val_ptr, old_val_ptr, old_length);
            new_val_ptr += new_length;
            old_val_ptr += old_length;
            break;
        default:
            assert_unreachable();
        }
        
        // 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_always(new_val_ptr == (uchar *)new_val.data + new_val.size);
        assert_always(old_val_ptr == (uchar *)old_val->data + old_val->size);

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

    error = 0;

cleanup:
    tokudb::memory::free(new_val.data);
    return error;
}

// Expand a char field in a old row given the expand message in the extra.
static int tokudb_expand_char_field(
    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;
    tokudb::buffer extra_val(extra->data, 0, extra->size);

    uint8_t operation;
    extra_val.consume(&operation, sizeof operation);
    assert_always(
        operation == UPDATE_OP_EXPAND_CHAR ||
        operation == UPDATE_OP_EXPAND_BINARY);
    uint32_t the_offset;
    extra_val.consume(&the_offset, sizeof the_offset);
    uint32_t old_length;
    extra_val.consume(&old_length, sizeof old_length);
    uint32_t new_length;
    extra_val.consume(&new_length, sizeof new_length);
    uchar pad_char;
    extra_val.consume(&pad_char, sizeof pad_char);
    assert_always(extra_val.size() == extra_val.limit());

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

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

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

        // 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*)tokudb::memory::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) {
        case UPDATE_OP_EXPAND_CHAR:
        case UPDATE_OP_EXPAND_BINARY:
            // fill the entire new value with the pad char
            memset(new_val_ptr, pad_char, new_length);
            // overlay the low bytes of the new value with the old value
            memcpy(new_val_ptr, old_val_ptr, old_length);
            new_val_ptr += new_length;
            old_val_ptr += old_length;
            break;
        default:
            assert_unreachable();
        }
        
        // 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_always(new_val_ptr == (uchar *)new_val.data + new_val.size);
        assert_always(old_val_ptr == (uchar *)old_val->data + old_val->size);

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

    error = 0;

cleanup:
    tokudb::memory::free(new_val.data);
    return error;
}

namespace tokudb {

class var_fields {
public:
    inline var_fields() {
    }
    inline void init_var_fields(
        uint32_t var_offset,
        uint32_t offset_bytes,
        uint32_t bytes_per_offset,
        tokudb::buffer* val_buffer) {

        assert_always(
            bytes_per_offset == 0 ||
            bytes_per_offset == 1 ||
            bytes_per_offset == 2);
        m_var_offset = var_offset;
        m_val_offset = m_var_offset + offset_bytes;
        m_bytes_per_offset = bytes_per_offset;
        if (bytes_per_offset > 0) {
            m_num_fields = offset_bytes / bytes_per_offset;
        } else {
            assert_always(offset_bytes == 0);
            m_num_fields = 0;
        }
        m_val_buffer = val_buffer;
    }
    uint32_t value_offset(uint32_t var_index);
    uint32_t value_length(uint32_t var_index);
    void update_offsets(uint32_t var_index, uint32_t old_s, uint32_t new_s);
    uint32_t end_offset();
    void replace(
        uint32_t var_index,
        void* new_val_ptr,
        uint32_t new_val_length);
private:
    uint32_t read_offset(uint32_t var_index);
    void write_offset(uint32_t var_index, uint32_t v);
private:
    uint32_t m_var_offset;
    uint32_t m_val_offset;
    uint32_t m_bytes_per_offset;
    uint32_t m_num_fields;
    tokudb::buffer* m_val_buffer;
};

// Return the ith variable length offset
uint32_t var_fields::read_offset(uint32_t var_index) {
    uint32_t offset = 0;
    m_val_buffer->read(
    &offset, m_bytes_per_offset, m_var_offset + var_index * m_bytes_per_offset);
    return offset;
}

// Write the ith variable length offset with a new offset.
void var_fields::write_offset(uint32_t var_index, uint32_t new_offset) {
    m_val_buffer->write(
        &new_offset,
        m_bytes_per_offset,
        m_var_offset + var_index * m_bytes_per_offset);
}

// Return the offset of the ith variable length field
uint32_t var_fields::value_offset(uint32_t var_index) {
    assert_always(var_index < m_num_fields);
    if (var_index == 0)
        return m_val_offset;
    else
        return m_val_offset + read_offset(var_index-1);
}

// Return the length of the ith variable length field
uint32_t var_fields::value_length(uint32_t var_index) {
    assert_always(var_index < m_num_fields);
    if (var_index == 0)
        return read_offset(0);
    else
        return read_offset(var_index) - read_offset(var_index-1);
}

// The length of the ith variable length fields changed.
// Update all of the subsequent offsets.
void var_fields::update_offsets(
    uint32_t var_index,
    uint32_t old_s,
    uint32_t new_s) {

    assert_always(var_index < m_num_fields);
    if (old_s == new_s)
        return;
    for (uint i = var_index; i < m_num_fields; i++) {
        uint32_t v = read_offset(i);
        if (new_s > old_s)
            write_offset(i, v + (new_s - old_s));
        else
            write_offset(i, v - (old_s - new_s));
    }
}

uint32_t var_fields::end_offset() {
    if (m_num_fields == 0)
        return m_val_offset;
    else
        return m_val_offset + read_offset(m_num_fields-1);
}

void var_fields::replace(
    uint32_t var_index,
    void* new_val_ptr,
    uint32_t new_val_length) {

    // replace the new val with the extra val
    uint32_t the_offset = value_offset(var_index);
    uint32_t old_s = value_length(var_index);
    uint32_t new_s = new_val_length;
    m_val_buffer->replace(the_offset, old_s, new_val_ptr, new_s);

    // update the var offsets
    update_offsets(var_index, old_s, new_s);
}

class blob_fields {
public:
    blob_fields() {
    }
    void init_blob_fields(
        uint32_t num_blobs,
        const uint8_t* blob_lengths,
        tokudb::buffer* val_buffer) {
        m_num_blobs = num_blobs;
        m_blob_lengths = blob_lengths;
        m_val_buffer = val_buffer;
    }
    void start_blobs(uint32_t offset) {
        m_blob_offset = offset;
    }
    void replace(uint32_t blob_index, uint32_t length, void *p);
    
    void expand_length(
        uint32_t blob_index,
        uint8_t old_length_length,
        uint8_t new_length_length);
private:
    uint32_t read_length(uint32_t offset, size_t size);
    void write_length(uint32_t offset, size_t size, uint32_t new_length);
    uint32_t blob_offset(uint32_t blob_index);
private:
    uint32_t m_blob_offset;
    uint32_t m_num_blobs;
    const uint8_t *m_blob_lengths;
    tokudb::buffer *m_val_buffer;
};

uint32_t blob_fields::read_length(uint32_t offset, size_t blob_length) {
    uint32_t length = 0;
    m_val_buffer->read(&length, blob_length, offset);
    return length;
}

void blob_fields::write_length(
    uint32_t offset,
    size_t size,
    uint32_t new_length) {
    m_val_buffer->write(&new_length, size, offset);
}

uint32_t blob_fields::blob_offset(uint32_t blob_index) {
    assert_always(blob_index < m_num_blobs);
    uint32_t offset = m_blob_offset;
    for (uint i = 0; i < blob_index; i++) {
        uint32_t blob_length = m_blob_lengths[i];
        uint32_t length = read_length(offset, blob_length);
        offset += blob_length + length;
    }
    return offset;
}

void blob_fields::replace(
    uint32_t blob_index,
    uint32_t new_length,
    void* new_value) {

    assert_always(blob_index < m_num_blobs);

    // compute the ith blob offset
    uint32_t offset = blob_offset(blob_index);
    uint8_t blob_length = m_blob_lengths[blob_index];

    // read the old length
    uint32_t old_length = read_length(offset, blob_length);

    // replace the data
    m_val_buffer->replace(
        offset + blob_length,
        old_length,
        new_value,
        new_length);

    // write the new length
    write_length(offset, blob_length, new_length);
}

void blob_fields::expand_length(
    uint32_t blob_index,
    uint8_t old_length_length,
    uint8_t new_length_length) {

    assert_always(blob_index < m_num_blobs);
    assert_always(old_length_length == m_blob_lengths[blob_index]);

    // compute the ith blob offset
    uint32_t offset = blob_offset(blob_index);

    // read the blob length
    uint32_t blob_length = read_length(offset, old_length_length);

    // expand the length
    m_val_buffer->replace(
        offset,
        old_length_length,
        &blob_length,
        new_length_length);
}

class value_map {
public:
    value_map(tokudb::buffer *val_buffer) : m_val_buffer(val_buffer) {
    }

    void init_var_fields(
        uint32_t var_offset,
        uint32_t offset_bytes,
        uint32_t bytes_per_offset) {

        m_var_fields.init_var_fields(
            var_offset,
            offset_bytes,
            bytes_per_offset,
            m_val_buffer);
    }

    void init_blob_fields(uint32_t num_blobs, const uint8_t *blob_lengths) {
        m_blob_fields.init_blob_fields(num_blobs, blob_lengths, m_val_buffer);
    }

    // Replace the value of a fixed length field
    void replace_fixed(
        uint32_t the_offset,
        uint32_t field_null_num,
        void* new_val_ptr,
        uint32_t new_val_length) {

        m_val_buffer->replace(
            the_offset,
            new_val_length,
            new_val_ptr,
            new_val_length);
        maybe_clear_null(field_null_num);
    }

    // Replace the value of a variable length field
    void replace_varchar(
        uint32_t var_index,
        uint32_t field_null_num,
        void* new_val_ptr,
        uint32_t new_val_length) {

        m_var_fields.replace(var_index, new_val_ptr, new_val_length);
        maybe_clear_null(field_null_num);
    }

    // Replace the value of a blob field
    void replace_blob(
        uint32_t blob_index,
        uint32_t field_null_num,
        void* new_val_ptr,
        uint32_t new_val_length) {

        m_blob_fields.start_blobs(m_var_fields.end_offset());
        m_blob_fields.replace(blob_index, new_val_length, new_val_ptr);
        maybe_clear_null(field_null_num);
    }

    void expand_blob_lengths(
        uint32_t num_blob,
        const uint8_t* old_length,
        const uint8_t* new_length);

    void int_op(
        uint32_t operation,
        uint32_t the_offset,
        uint32_t length,
        uint32_t field_null_num,
        tokudb::buffer& old_val,
        void* extra_val);

    void uint_op(
        uint32_t operation,
        uint32_t the_offset,
        uint32_t length,
        uint32_t field_null_num,
        tokudb::buffer& old_val,
        void* extra_val);

private:
    bool is_null(uint32_t null_num, uchar *null_bytes) {
        bool field_is_null = false;
        if (null_num) {
            if (null_num & (1<<31))
                null_num &= ~(1<<31);
            else
                null_num -= 1;
            field_is_null = is_overall_null_position_set(null_bytes, null_num);
        }
        return field_is_null;
    }

    void maybe_clear_null(uint32_t null_num) {
        if (null_num) {
            if (null_num & (1<<31))
                null_num &= ~(1<<31);
            else
                null_num -= 1;            
            set_overall_null_position(
                (uchar*)m_val_buffer->data(),
                null_num,
                false);
        }
    }

private:
    var_fields m_var_fields;
    blob_fields m_blob_fields;
    tokudb::buffer *m_val_buffer;
};

// Update an int field: signed newval@offset = old_val@offset OP extra_val
void value_map::int_op(
    uint32_t operation,
    uint32_t the_offset,
    uint32_t length,
    uint32_t field_null_num,
    tokudb::buffer &old_val,
    void* extra_val) {

    assert_always(the_offset + length <= m_val_buffer->size());
    assert_always(the_offset + length <= old_val.size());
    assert_always(
        length == 1 || length == 2 || length == 3 ||
        length == 4 || length == 8);

    uchar *old_val_ptr = (uchar *) old_val.data();
    bool field_is_null = is_null(field_null_num, old_val_ptr);
    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;
            v = tokudb::int_add(v, extra_v, 8*length, &over);
            if (over) {
                if (extra_v > 0)
                    v = tokudb::int_high_endpoint(8*length);
                else
                    v = tokudb::int_low_endpoint(8*length);
            }
            m_val_buffer->replace(the_offset, length, &v, length);
        }
        break;
    case '-':
        if (!field_is_null) {
            bool over;
            v = tokudb::int_sub(v, extra_v, 8*length, &over);
            if (over) {
                if (extra_v > 0)
                    v = tokudb::int_low_endpoint(8*length);
                else
                    v = tokudb::int_high_endpoint(8*length);
            }
            m_val_buffer->replace(the_offset, length, &v, length);
        }
        break;
    default:
        assert_unreachable();
    }
}

// Update an unsigned field: unsigned newval@offset = old_val@offset OP extra_val
void value_map::uint_op(
    uint32_t operation,
    uint32_t the_offset,
    uint32_t length,
    uint32_t field_null_num,
    tokudb::buffer& old_val,
    void* extra_val) {

    assert_always(the_offset + length <= m_val_buffer->size());
    assert_always(the_offset + length <= old_val.size());
    assert_always(
        length == 1 || length == 2 || length == 3 ||
        length == 4 || length == 8);

    uchar *old_val_ptr = (uchar *) old_val.data();
    bool field_is_null = is_null(field_null_num, old_val_ptr);
    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;
            v = tokudb::uint_add(v, extra_v, 8*length, &over);
            if (over) {
                v = tokudb::uint_high_endpoint(8*length);
            }
            m_val_buffer->replace(the_offset, length, &v, length);
        }
        break;
    case '-':
        if (!field_is_null) {
            bool over;
            v = tokudb::uint_sub(v, extra_v, 8*length, &over);
            if (over) {
                v = tokudb::uint_low_endpoint(8*length);
            }
            m_val_buffer->replace(the_offset, length, &v, length);
        }
        break;
    default:
        assert_unreachable();
    }
}

void value_map::expand_blob_lengths(
    uint32_t num_blob,
    const uint8_t* old_length,
    const uint8_t* new_length) {

    uint8_t current_length[num_blob];
    memcpy(current_length, old_length, num_blob);
    for (uint32_t i = 0; i < num_blob; i++) {
        if (new_length[i] > current_length[i]) {
            m_blob_fields.init_blob_fields(
                num_blob,
                current_length,
                m_val_buffer);
            m_blob_fields.start_blobs(m_var_fields.end_offset());
            m_blob_fields.expand_length(i, current_length[i], new_length[i]);
            current_length[i] = new_length[i];
        }
    }
}

}

static uint32_t consume_uint32(tokudb::buffer &b) {
    uint32_t n;
    size_t s = b.consume_ui<uint32_t>(&n);
    assert_always(s > 0);
    return n;
}

static uint8_t *consume_uint8_array(tokudb::buffer &b, uint32_t array_size) {
    uint8_t *p = (uint8_t *) b.consume_ptr(array_size);
    assert_always(p);
    return p;
}

static int tokudb_expand_blobs(
    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);
    
    uint8_t operation;
    extra_val.consume(&operation, sizeof operation);
    assert_always(operation == UPDATE_OP_EXPAND_BLOB);

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

        tokudb::value_map vd(&new_val);

        // decode variable field info
        uint32_t var_field_offset = consume_uint32(extra_val);
        uint32_t var_offset_bytes = consume_uint32(extra_val);
        uint32_t bytes_per_offset = consume_uint32(extra_val);
        vd.init_var_fields(
            var_field_offset,
            var_offset_bytes,
            bytes_per_offset);

        // decode blob info
        uint32_t num_blob = consume_uint32(extra_val);
        const uint8_t* old_blob_length =
            consume_uint8_array(extra_val, num_blob);
        const uint8_t* new_blob_length =
            consume_uint8_array(extra_val, num_blob);
        assert_always(extra_val.size() == extra_val.limit());

        // expand blob lengths
        vd.expand_blob_lengths(num_blob, old_blob_length, new_blob_length);

        // 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;
}

// Decode and apply a sequence of update operations defined in the extra to
// the old value and put the result in the new value.
static void apply_1_updates(
    tokudb::value_map& vd,
    tokudb::buffer& new_val,
    tokudb::buffer& old_val,
    tokudb::buffer& extra_val) {

    uint32_t num_updates;
    extra_val.consume(&num_updates, sizeof num_updates);
    for ( ; num_updates > 0; num_updates--) {
        // 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 unused;
        extra_val.consume(&unused, sizeof unused);
        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 extra_val_length;
        extra_val.consume(&extra_val_length, sizeof extra_val_length);
        void *extra_val_ptr = extra_val.consume_ptr(extra_val_length);

        // apply the update
        switch (field_type) {
        case UPDATE_TYPE_INT:
            if (update_operation == '=')
                vd.replace_fixed(
                    the_offset,
                    field_null_num,
                    extra_val_ptr,
                    extra_val_length);
            else
                vd.int_op(
                    update_operation,
                    the_offset,
                    extra_val_length,
                    field_null_num,
                    old_val,
                    extra_val_ptr);
            break;
        case UPDATE_TYPE_UINT:
            if (update_operation == '=')
                vd.replace_fixed(
                    the_offset,
                    field_null_num,
                    extra_val_ptr,
                    extra_val_length);
            else
                vd.uint_op(
                    update_operation,
                    the_offset,
                    extra_val_length,
                    field_null_num,
                    old_val,
                    extra_val_ptr);
            break;
        case UPDATE_TYPE_CHAR:
        case UPDATE_TYPE_BINARY:
            if (update_operation == '=')
                vd.replace_fixed(
                    the_offset,
                    field_null_num,
                    extra_val_ptr,
                    extra_val_length);
            else 
                assert_unreachable();
            break;
        default:
            assert_unreachable();
            break;
        }
    }
    assert_always(extra_val.size() == extra_val.limit());
}

// Simple update handler. Decode the update message, apply the update operations
// to the old value, and set the new value.
static int tokudb_update_1_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);
    
    uint8_t operation;
    extra_val.consume(&operation, sizeof operation);
    assert_always(operation == UPDATE_OP_UPDATE_1);

    if (old_val_dbt != NULL) {
        // get the simple descriptor
        uint32_t m_fixed_field_offset;
        extra_val.consume(&m_fixed_field_offset, sizeof m_fixed_field_offset);
        uint32_t m_var_field_offset;
        extra_val.consume(&m_var_field_offset, sizeof m_var_field_offset);
        uint32_t m_var_offset_bytes;
        extra_val.consume(&m_var_offset_bytes, sizeof m_var_offset_bytes);
        uint32_t m_bytes_per_offset;
        extra_val.consume(&m_bytes_per_offset, sizeof m_bytes_per_offset);

        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);

        tokudb::value_map vd(&new_val);
        vd.init_var_fields(
            m_var_field_offset,
            m_var_offset_bytes,
            m_bytes_per_offset);

        // apply updates to new val
        apply_1_updates(vd, new_val, old_val, extra_val);

        // 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_upsert_1_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);

    uint8_t operation;
    extra_val.consume(&operation, sizeof operation);
    assert_always(operation == UPDATE_OP_UPSERT_1);

    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
        uint32_t m_fixed_field_offset;
        extra_val.consume(&m_fixed_field_offset, sizeof m_fixed_field_offset);
        uint32_t m_var_field_offset;
        extra_val.consume(&m_var_field_offset, sizeof m_var_field_offset);
        uint32_t m_var_offset_bytes;
        extra_val.consume(&m_var_offset_bytes, sizeof m_var_offset_bytes);
        uint32_t m_bytes_per_offset;
        extra_val.consume(&m_bytes_per_offset, sizeof m_bytes_per_offset);

        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);

        tokudb::value_map vd(&new_val);
        vd.init_var_fields(
            m_var_field_offset,
            m_var_offset_bytes,
            m_bytes_per_offset);

        // apply updates to new val
        apply_1_updates(vd, new_val, old_val, extra_val);

        // 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;
}

// Decode and apply a sequence of update operations defined in the extra to the
// old value and put the result in the new value.
static void apply_2_updates(
    tokudb::value_map& vd,
    tokudb::buffer& new_val,
    tokudb::buffer& old_val,
    tokudb::buffer& extra_val) {

    uint32_t num_updates = consume_uint32(extra_val);
    for (uint32_t i = 0; i < num_updates; i++) {
        uint32_t update_operation = consume_uint32(extra_val);
        if (update_operation == 'v') {
            uint32_t var_field_offset = consume_uint32(extra_val);
            uint32_t var_offset_bytes = consume_uint32(extra_val);
            uint32_t bytes_per_offset = consume_uint32(extra_val);
            vd.init_var_fields(
                var_field_offset,
                var_offset_bytes,
                bytes_per_offset);
        } else if (update_operation == 'b') {
            uint32_t num_blobs = consume_uint32(extra_val);
            const uint8_t* blob_lengths =
                consume_uint8_array(extra_val, num_blobs);
            vd.init_blob_fields(num_blobs, blob_lengths);
        } else {
            uint32_t field_type = consume_uint32(extra_val);
            uint32_t field_null_num = consume_uint32(extra_val);
            uint32_t the_offset = consume_uint32(extra_val);
            uint32_t extra_val_length = consume_uint32(extra_val);
            void* extra_val_ptr = extra_val.consume_ptr(extra_val_length);
            assert_always(extra_val_ptr);

            switch (field_type) {
            case UPDATE_TYPE_INT:
                if (update_operation == '=')
                    vd.replace_fixed(
                        the_offset,
                        field_null_num,
                        extra_val_ptr,
                        extra_val_length);
                else
                    vd.int_op(
                        update_operation,
                        the_offset,
                        extra_val_length,
                        field_null_num,
                        old_val,
                        extra_val_ptr);
                break;
            case UPDATE_TYPE_UINT:
                if (update_operation == '=')
                    vd.replace_fixed(
                        the_offset,
                        field_null_num,
                        extra_val_ptr,
                        extra_val_length);
                else
                    vd.uint_op(
                        update_operation,
                        the_offset,
                        extra_val_length,
                        field_null_num,
                        old_val,
                        extra_val_ptr);
                break;
            case UPDATE_TYPE_CHAR:
            case UPDATE_TYPE_BINARY:
                if (update_operation == '=')
                    vd.replace_fixed(
                        the_offset,
                        field_null_num,
                        extra_val_ptr,
                        extra_val_length);
                else 
                    assert_unreachable();
                break;
            case UPDATE_TYPE_VARBINARY:
            case UPDATE_TYPE_VARCHAR:
                if (update_operation == '=') 
                    vd.replace_varchar(
                        the_offset,
                        field_null_num,
                        extra_val_ptr,
                        extra_val_length);
                else
                    assert_unreachable();
                break;
            case UPDATE_TYPE_TEXT:
            case UPDATE_TYPE_BLOB: 
                if (update_operation == '=')
                    vd.replace_blob(
                        the_offset,
                        field_null_num,
                        extra_val_ptr,
                        extra_val_length);
                else
                    assert_unreachable();
                break;
            default:
                assert_unreachable();
            }
        }
    }
    assert_always(extra_val.size() == extra_val.limit());
}

// Simple update handler. Decode the update message, apply the update
// operations to the old value, and set the new value.
static int tokudb_update_2_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);
    
    uint8_t op;
    extra_val.consume(&op, sizeof op);
    assert_always(op == UPDATE_OP_UPDATE_2);

    if (old_val_dbt != NULL) {
        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);

        tokudb::value_map vd(&new_val);

        // apply updates to new val
        apply_2_updates(vd, new_val, old_val, extra_val);

        // 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_upsert_2_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);

    uint8_t op;
    extra_val.consume(&op, sizeof op);
    assert_always(op == UPDATE_OP_UPSERT_2);

    uint32_t insert_length = consume_uint32(extra_val);
    assert_always(insert_length < extra_val.limit());
    void* insert_row = extra_val.consume_ptr(insert_length);
    assert_always(insert_row);

    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 {
        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);

        tokudb::value_map vd(&new_val);

        // apply updates to new val
        apply_2_updates(vd, new_val, old_val, extra_val);

        // 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(
    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) {

    assert_always(extra->size > 0);
    uint8_t* extra_pos = (uchar*)extra->data;
    uint8_t operation = extra_pos[0];
    int error;
    switch (operation) {
    case UPDATE_OP_COL_ADD_OR_DROP:
        error = tokudb_hcad_update_fun(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_EXPAND_VARIABLE_OFFSETS:
        error = tokudb_expand_variable_offsets(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_EXPAND_INT:
    case UPDATE_OP_EXPAND_UINT:
        error = tokudb_expand_int_field(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_EXPAND_CHAR:
    case UPDATE_OP_EXPAND_BINARY:
        error = tokudb_expand_char_field(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_EXPAND_BLOB:
        error = tokudb_expand_blobs(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_UPDATE_1:
        error = tokudb_update_1_fun(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_UPSERT_1:
        error = tokudb_upsert_1_fun(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_UPDATE_2:
        error = tokudb_update_2_fun(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    case UPDATE_OP_UPSERT_2:
        error = tokudb_upsert_2_fun(
            db,
            key,
            old_val,
            extra,
            set_val,
            set_extra);
        break;
    default:
        assert_unreachable();
    }
    return error;
}