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
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
// Copyright 2014 Tyler Neely
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![feature(c_variadic)]

extern crate bzip2_sys;
extern crate libc;
#[cfg(test)]
extern crate tempfile;

use std::ffi::CStr;
use std::fmt;

use libc::{c_char, c_double, c_int, c_uchar, c_void, size_t};

// FFI-safe opaque types.
//
// These represent opaque RocksDB types. They are used behind pointers, but are
// also wrapped in other types in the higher-level bindings.
//
// These use the strategy for opaque C types described in the nomicon [1]:
// but with the exception that they contain c_void instead of [u8; 0], thus
// making them uninstantiable sized types instead of ZSTs.
//
// The c_void documentation publicly recommends using the ZST pattern from the
// nomicon, but in private documentation [2] warns about UB from dereferencing
// pointers to uninhabited types, which these bindings do.
//
// Additionally, these bindings wrap some these types directly (not through
// pointers) and it's impossible to repr(transparent) a ZST, without which the
// unsafe casts within are dubious.
//
// [1]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
// [2]: https://doc.rust-lang.org/nightly/src/core/ffi.rs.html#28

#[repr(C)]
pub struct Options(c_void);
#[repr(C)]
pub struct ColumnFamilyDescriptor(c_void);
#[repr(C)]
pub struct DBInstance(c_void);
#[repr(C)]
pub struct DBWriteOptions(c_void);
#[repr(C)]
pub struct DBReadOptions(c_void);
#[repr(C)]
pub struct DBMergeOperator(c_void);
#[repr(C)]
pub struct DBBlockBasedTableOptions(c_void);
#[repr(C)]
pub struct DBMemoryAllocator(c_void);
#[repr(C)]
pub struct DBLRUCacheOptions(c_void);
#[repr(C)]
pub struct DBCache(c_void);
#[repr(C)]
pub struct DBFilterPolicy(c_void);
#[repr(C)]
pub struct DBSnapshot(c_void);
#[repr(C)]
pub struct DBIterator(c_void);
#[repr(C)]
pub struct DBCFHandle(c_void);
#[repr(C)]
pub struct DBWriteBatch(c_void);
#[repr(C)]
pub struct DBComparator(c_void);
#[repr(C)]
pub struct DBFlushOptions(c_void);
#[repr(C)]
pub struct DBCompactionFilter(c_void);
#[repr(C)]
pub struct DBCompactionFilterFactory(c_void);
#[repr(C)]
pub struct DBCompactionFilterContext(c_void);
#[repr(C)]
pub struct EnvOptions(c_void);
#[repr(C)]
pub struct SstFileReader(c_void);
#[repr(C)]
pub struct SstFileWriter(c_void);
#[repr(C)]
pub struct ExternalSstFileInfo(c_void);
#[repr(C)]
pub struct IngestExternalFileOptions(c_void);
#[repr(C)]
pub struct DBBackupEngine(c_void);
#[repr(C)]
pub struct DBRestoreOptions(c_void);
#[repr(C)]
pub struct DBSliceTransform(c_void);
#[repr(C)]
pub struct DBRateLimiter(c_void);
#[repr(C)]
pub struct DBLogger(c_void);
#[repr(C)]
pub struct DBCompactOptions(c_void);
#[repr(C)]
pub struct DBFifoCompactionOptions(c_void);
#[repr(C)]
pub struct DBPinnableSlice(c_void);
#[repr(C)]
pub struct DBUserCollectedProperties(c_void);
#[repr(C)]
pub struct DBUserCollectedPropertiesIterator(c_void);
#[repr(C)]
pub struct DBTableProperties(c_void);
#[repr(C)]
pub struct DBTablePropertiesCollection(c_void);
#[repr(C)]
pub struct DBTablePropertiesCollectionIterator(c_void);
#[repr(C)]
pub struct DBTablePropertiesCollector(c_void);
#[repr(C)]
pub struct DBTablePropertiesCollectorFactory(c_void);
#[repr(C)]
pub struct DBFlushJobInfo(c_void);
#[repr(C)]
pub struct DBCompactionJobInfo(c_void);
#[repr(C)]
pub struct DBSubcompactionJobInfo(c_void);
#[repr(C)]
pub struct DBIngestionInfo(c_void);
#[repr(C)]
pub struct DBEventListener(c_void);
#[repr(C)]
pub struct DBKeyVersions(c_void);
#[repr(C)]
pub struct DBEnv(c_void);
#[repr(C)]
pub struct DBSequentialFile(c_void);
#[repr(C)]
pub struct DBColumnFamilyMetaData(c_void);
#[repr(C)]
pub struct DBLevelMetaData(c_void);
#[repr(C)]
pub struct DBSstFileMetaData(c_void);
#[repr(C)]
pub struct DBCompactionOptions(c_void);
#[repr(C)]
pub struct DBPerfContext(c_void);
#[repr(C)]
pub struct DBIOStatsContext(c_void);
#[repr(C)]
pub struct DBWriteStallInfo(c_void);
#[repr(C)]
pub struct DBStatusPtr(c_void);
#[repr(C)]
pub struct DBMapProperty(c_void);
#[cfg(feature = "encryption")]
#[repr(C)]
pub struct DBFileEncryptionInfo(c_void);
#[cfg(feature = "encryption")]
#[repr(C)]
pub struct DBEncryptionKeyManagerInstance(c_void);
#[repr(C)]
pub struct DBSstPartitioner(c_void);
#[repr(C)]
pub struct DBSstPartitionerRequest(c_void);
#[repr(C)]
pub struct DBSstPartitionerContext(c_void);
#[repr(C)]
pub struct DBSstPartitionerFactory(c_void);
#[repr(C)]
pub struct DBWriteBatchIterator(c_void);
#[repr(C)]
pub struct DBFileSystemInspectorInstance(c_void);

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum WriteStallCondition {
    Normal = 0,
    Delayed = 1,
    Stopped = 2,
}

mod generated;
pub use generated::*;

#[repr(C)]
pub struct DBTitanDBOptions(c_void);
#[repr(C)]
pub struct DBTitanReadOptions(c_void);

#[derive(Clone, Debug, Default)]
#[repr(C)]
pub struct DBTitanBlobIndex {
    pub file_number: u64,
    pub blob_offset: u64,
    pub blob_size: u64,
}

pub fn new_bloom_filter(bits: c_int) -> *mut DBFilterPolicy {
    unsafe { crocksdb_filterpolicy_create_bloom(bits) }
}

/// # Safety
///
/// `DBLRUCacheOptions` should pointer to a valid cache options
pub unsafe fn new_lru_cache(opt: *mut DBLRUCacheOptions) -> *mut DBCache {
    crocksdb_cache_create_lru(opt)
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBEntryType {
    Put = 0,
    Delete = 1,
    SingleDelete = 2,
    Merge = 3,
    RangeDeletion = 4,
    BlobIndex = 5,
    Other = 6,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBCompressionType {
    No = 0,
    Snappy = 1,
    Zlib = 2,
    Bz2 = 3,
    Lz4 = 4,
    Lz4hc = 5,
    // DBXpress = 6, not support currently.
    Zstd = 7,
    ZstdNotFinal = 0x40,
    Disable = 0xff,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBCompactionStyle {
    Level = 0,
    Universal = 1,
    Fifo = 2,
    None = 3,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBUniversalCompactionStyle {
    SimilarSize = 0,
    TotalSize = 1,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBRecoveryMode {
    TolerateCorruptedTailRecords = 0,
    AbsoluteConsistency = 1,
    PointInTime = 2,
    SkipAnyCorruptedRecords = 3,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum CompactionPriority {
    // In Level-based compaction, it Determines which file from a level to be
    // picked to merge to the next level. We suggest people try
    // kMinOverlappingRatio first when you tune your database.
    ByCompensatedSize = 0,
    // First compact files whose data's latest update time is oldest.
    // Try this if you only update some hot keys in small ranges.
    OldestLargestSeqFirst = 1,
    // First compact files whose range hasn't been compacted to the next level
    // for the longest. If your updates are random across the key space,
    // write amplification is slightly better with this option.
    OldestSmallestSeqFirst = 2,
    // First compact files whose ratio between overlapping size in next level
    // and its size is the smallest. It in many cases can optimize write
    // amplification.
    MinOverlappingRatio = 3,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum CompactionReason {
    Unknown,
    // [Level] number of L0 files > level0_file_num_compaction_trigger
    LevelL0FilesNum,
    // [Level] total size of level > MaxBytesForLevel()
    LevelMaxLevelSize,
    // [Universal] Compacting for size amplification
    UniversalSizeAmplification,
    // [Universal] Compacting for size ratio
    UniversalSizeRatio,
    // [Universal] number of sorted runs > level0_file_num_compaction_trigger
    UniversalSortedRunNum,
    // [FIFO] total size > max_table_files_size
    FIFOMaxSize,
    // [FIFO] reduce number of files.
    FIFOReduceNumFiles,
    // [FIFO] files with creation time < (current_time - interval)
    FIFOTtl,
    // Manual compaction
    ManualCompaction,
    // DB::SuggestCompactRange() marked files for compaction
    FilesMarkedForCompaction,
    // [Level] Automatic compaction within bottommost level to cleanup duplicate
    // versions of same user key, usually due to a released snapshot.
    BottommostFiles,
    // Compaction based on TTL
    Ttl,
    // According to the comments in flush_job.cc, RocksDB treats flush as
    // a level 0 compaction in internal stats.
    Flush,
    // Compaction caused by external sst file ingestion
    ExternalSstIngestion,
    // total number of compaction reasons, new reasons must be added above this.
    NumOfReasons,
}

impl fmt::Display for CompactionReason {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBInfoLogLevel {
    Debug = 0,
    Info = 1,
    Warn = 2,
    Error = 3,
    Fatal = 4,
    Header = 5,
    NumInfoLog = 6,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBTableProperty {
    DataSize = 1,
    IndexSize = 2,
    FilterSize = 3,
    RawKeySize = 4,
    RawValueSize = 5,
    NumDataBlocks = 6,
    NumEntries = 7,
    FormatVersion = 8,
    FixedKeyLen = 9,
    ColumnFamilyId = 10,
    ColumnFamilyName = 11,
    FilterPolicyName = 12,
    ComparatorName = 13,
    MergeOperatorName = 14,
    PrefixExtractorName = 15,
    PropertyCollectorsNames = 16,
    CompressionName = 17,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBBottommostLevelCompaction {
    // Skip bottommost level compaction
    Skip = 0,
    // Compact bottommost level if there is a compaction filter
    // This is the default option
    IfHaveCompactionFilter = 1,
    // Force bottommost level compaction
    Force = 2,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBRateLimiterMode {
    ReadOnly = 1,
    WriteOnly = 2,
    AllIo = 3,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBTitanDBBlobRunMode {
    Normal = 0,
    ReadOnly = 1,
    Fallback = 2,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum IndexType {
    BinarySearch = 0,
    HashSearch = 1,
    TwoLevelIndexSearch = 2,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBBackgroundErrorReason {
    Flush = 1,
    Compaction = 2,
    WriteCallback = 3,
    MemTable = 4,
}

#[cfg(feature = "encryption")]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBEncryptionMethod {
    Unknown = 0,
    Plaintext = 1,
    Aes128Ctr = 2,
    Aes192Ctr = 3,
    Aes256Ctr = 4,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBValueType {
    TypeDeletion = 0x0,
    TypeValue = 0x1,
    TypeMerge = 0x2,
    TypeColumnFamilyDeletion = 0x4, // WAL only.
    TypeColumnFamilyValue = 0x5,    // WAL only.
    TypeColumnFamilyMerge = 0x6,    // WAL only.
    TypeSingleDeletion = 0x7,
    TypeColumnFamilyRangeDeletion = 0xE, // WAL only.
    TypeRangeDeletion = 0xF,             // meta block
    TypeColumnFamilyBlobIndex = 0x10,    // Blob DB only
    TypeBlobIndex = 0x11,                // Blob DB only
    MaxValue = 0x7F,
}

#[cfg(feature = "encryption")]
impl fmt::Display for DBEncryptionMethod {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum DBSstPartitionerResult {
    NotRequired = 0,
    Required = 1,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum CompactionFilterValueType {
    Value = 0,
    MergeOperand = 1,
    BlobIndex = 2,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub enum CompactionFilterDecision {
    Keep = 0,
    Remove = 1,
    ChangeValue = 2,
    RemoveAndSkipUntil = 3,
}

/// # Safety
///
/// ptr must point to a valid CStr value
pub unsafe fn error_message(ptr: *mut c_char) -> String {
    let c_str = CStr::from_ptr(ptr);
    let s = format!("{}", c_str.to_string_lossy());
    libc::free(ptr as *mut c_void);
    s
}

#[macro_export]
macro_rules! ffi_try {
    ($func:ident($($arg:expr),+)) => ({
        use std::ptr;
        let mut err = ptr::null_mut();
        let res = $crate::$func($($arg),+, &mut err);
        if !err.is_null() {
            return Err($crate::error_message(err));
        }
        res
    });
    ($func:ident()) => ({
        use std::ptr;
        let mut err = ptr::null_mut();
        let res = $crate::$func(&mut err);
        if !err.is_null() {
            return Err($crate::error_message(err));
        }
        res
    })
}

// TODO audit the use of boolean arguments, b/c I think they need to be u8
// instead...
extern "C" {
    pub fn crocksdb_status_ptr_get_error(status: *mut DBStatusPtr, err: *mut *mut c_char);
    pub fn crocksdb_get_db_options(db: *mut DBInstance) -> *mut Options;
    pub fn crocksdb_set_db_options(
        db: *mut DBInstance,
        names: *const *const c_char,
        values: *const *const c_char,
        num_options: size_t,
        errptr: *mut *mut c_char,
    );
    pub fn crocksdb_get_options_cf(db: *mut DBInstance, cf: *mut DBCFHandle) -> *mut Options;
    pub fn crocksdb_set_options_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        names: *const *const c_char,
        values: *const *const c_char,
        num_options: size_t,
        errptr: *mut *mut c_char,
    );
    pub fn crocksdb_options_create() -> *mut Options;
    pub fn crocksdb_options_copy(opts: *const Options) -> *mut Options;
    pub fn crocksdb_options_destroy(opts: *mut Options);
    pub fn crocksdb_options_set_paranoid_checks(opts: *mut Options, _: u8);
    pub fn crocksdb_column_family_descriptor_destroy(cf_desc: *mut ColumnFamilyDescriptor);
    pub fn crocksdb_name_from_column_family_descriptor(
        cf_descs: *const ColumnFamilyDescriptor,
    ) -> *const c_char;
    pub fn crocksdb_options_from_column_family_descriptor(
        cf_descs: *const ColumnFamilyDescriptor,
    ) -> *mut Options;

    // Memory Allocator
    pub fn crocksdb_jemalloc_nodump_allocator_create(
        err: *mut *mut c_char,
    ) -> *mut DBMemoryAllocator;
    pub fn crocksdb_memory_allocator_destroy(allocator: *mut DBMemoryAllocator);

    // Cache
    pub fn crocksdb_lru_cache_options_create() -> *mut DBLRUCacheOptions;
    pub fn crocksdb_lru_cache_options_destroy(opt: *mut DBLRUCacheOptions);
    pub fn crocksdb_lru_cache_options_set_capacity(opt: *mut DBLRUCacheOptions, capacity: size_t);
    pub fn crocksdb_lru_cache_options_set_num_shard_bits(
        opt: *mut DBLRUCacheOptions,
        num_shard_bits: c_int,
    );
    pub fn crocksdb_lru_cache_options_set_strict_capacity_limit(
        opt: *mut DBLRUCacheOptions,
        strict_capacity_limit: bool,
    );
    pub fn crocksdb_lru_cache_options_set_high_pri_pool_ratio(
        opt: *mut DBLRUCacheOptions,
        high_pri_pool_ratio: c_double,
    );
    pub fn crocksdb_lru_cache_options_set_memory_allocator(
        opt: *mut DBLRUCacheOptions,
        allocator: *mut DBMemoryAllocator,
    );
    pub fn crocksdb_cache_create_lru(opt: *mut DBLRUCacheOptions) -> *mut DBCache;
    pub fn crocksdb_cache_destroy(cache: *mut DBCache);

    pub fn crocksdb_block_based_options_create() -> *mut DBBlockBasedTableOptions;
    pub fn crocksdb_block_based_options_destroy(opts: *mut DBBlockBasedTableOptions);
    pub fn crocksdb_block_based_options_set_metadata_block_size(
        block_options: *mut DBBlockBasedTableOptions,
        block_size: size_t,
    );
    pub fn crocksdb_block_based_options_set_block_size(
        block_options: *mut DBBlockBasedTableOptions,
        block_size: size_t,
    );
    pub fn crocksdb_block_based_options_set_block_size_deviation(
        block_options: *mut DBBlockBasedTableOptions,
        block_size_deviation: c_int,
    );
    pub fn crocksdb_block_based_options_set_block_restart_interval(
        block_options: *mut DBBlockBasedTableOptions,
        block_restart_interval: c_int,
    );
    pub fn crocksdb_block_based_options_set_index_type(
        block_options: *mut DBBlockBasedTableOptions,
        v: IndexType,
    );
    pub fn crocksdb_block_based_options_set_hash_index_allow_collision(
        block_options: *mut DBBlockBasedTableOptions,
        v: c_uchar,
    );
    pub fn crocksdb_block_based_options_set_partition_filters(
        block_options: *mut DBBlockBasedTableOptions,
        v: c_uchar,
    );
    pub fn crocksdb_block_based_options_set_cache_index_and_filter_blocks(
        block_options: *mut DBBlockBasedTableOptions,
        v: c_uchar,
    );
    pub fn crocksdb_block_based_options_set_pin_top_level_index_and_filter(
        block_options: *mut DBBlockBasedTableOptions,
        v: c_uchar,
    );
    pub fn crocksdb_block_based_options_set_cache_index_and_filter_blocks_with_high_priority(
        block_options: *mut DBBlockBasedTableOptions,
        v: c_uchar,
    );
    pub fn crocksdb_block_based_options_set_filter_policy(
        block_options: *mut DBBlockBasedTableOptions,
        filter_policy: *mut DBFilterPolicy,
    );
    pub fn crocksdb_block_based_options_set_no_block_cache(
        block_options: *mut DBBlockBasedTableOptions,
        no_block_cache: bool,
    );
    pub fn crocksdb_block_based_options_set_block_cache(
        block_options: *mut DBBlockBasedTableOptions,
        block_cache: *mut DBCache,
    );
    pub fn crocksdb_block_based_options_set_block_cache_compressed(
        block_options: *mut DBBlockBasedTableOptions,
        block_cache_compressed: *mut DBCache,
    );
    pub fn crocksdb_block_based_options_set_whole_key_filtering(
        ck_options: *mut DBBlockBasedTableOptions,
        doit: bool,
    );
    pub fn crocksdb_options_set_block_based_table_factory(
        options: *mut Options,
        block_options: *mut DBBlockBasedTableOptions,
    );
    pub fn crocksdb_block_based_options_set_pin_l0_filter_and_index_blocks_in_cache(
        block_options: *mut DBBlockBasedTableOptions,
        v: c_uchar,
    );
    pub fn crocksdb_block_based_options_set_read_amp_bytes_per_bit(
        block_options: *mut DBBlockBasedTableOptions,
        v: c_int,
    );
    pub fn crocksdb_options_increase_parallelism(options: *mut Options, threads: c_int);
    pub fn crocksdb_options_optimize_level_style_compaction(
        options: *mut Options,
        memtable_memory_budget: c_int,
    );
    pub fn crocksdb_options_set_env(options: *mut Options, env: *mut DBEnv);
    pub fn crocksdb_options_set_compaction_filter(
        options: *mut Options,
        filter: *mut DBCompactionFilter,
    );
    pub fn crocksdb_options_set_compaction_filter_factory(
        options: *mut Options,
        filter: *mut DBCompactionFilterFactory,
    );
    pub fn crocksdb_options_set_create_if_missing(options: *mut Options, v: bool);
    pub fn crocksdb_options_set_create_missing_column_families(options: *mut Options, v: bool);
    pub fn crocksdb_options_set_max_open_files(options: *mut Options, files: c_int);
    pub fn crocksdb_options_set_max_total_wal_size(options: *mut Options, size: u64);
    pub fn crocksdb_options_set_use_fsync(options: *mut Options, v: c_int);
    pub fn crocksdb_options_set_bytes_per_sync(options: *mut Options, bytes: u64);
    pub fn crocksdb_options_set_enable_pipelined_write(options: *mut Options, v: bool);
    pub fn crocksdb_options_set_enable_multi_batch_write(options: *mut Options, v: bool);
    pub fn crocksdb_options_is_enable_multi_batch_write(options: *mut Options) -> bool;
    pub fn crocksdb_options_set_unordered_write(options: *mut Options, v: bool);
    pub fn crocksdb_options_set_allow_concurrent_memtable_write(options: *mut Options, v: bool);
    pub fn crocksdb_options_set_manual_wal_flush(options: *mut Options, v: bool);
    pub fn crocksdb_options_optimize_for_point_lookup(
        options: *mut Options,
        block_cache_size_mb: u64,
    );
    pub fn crocksdb_options_set_table_cache_numshardbits(options: *mut Options, bits: c_int);
    pub fn crocksdb_options_set_writable_file_max_buffer_size(options: *mut Options, nbytes: c_int);
    pub fn crocksdb_options_set_max_write_buffer_number(options: *mut Options, bufno: c_int);
    pub fn crocksdb_options_get_max_write_buffer_number(options: *mut Options) -> c_int;
    pub fn crocksdb_options_set_min_write_buffer_number_to_merge(
        options: *mut Options,
        bufno: c_int,
    );
    pub fn crocksdb_options_get_min_write_buffer_number_to_merge(options: *mut Options) -> c_int;
    pub fn crocksdb_options_set_level0_file_num_compaction_trigger(
        options: *mut Options,
        no: c_int,
    );
    pub fn crocksdb_options_get_level0_file_num_compaction_trigger(options: *mut Options) -> c_int;
    pub fn crocksdb_options_set_level0_slowdown_writes_trigger(options: *mut Options, no: c_int);
    pub fn crocksdb_options_get_level0_slowdown_writes_trigger(options: *mut Options) -> c_int;
    pub fn crocksdb_options_set_level0_stop_writes_trigger(options: *mut Options, no: c_int);
    pub fn crocksdb_options_get_level0_stop_writes_trigger(options: *mut Options) -> c_int;
    pub fn crocksdb_options_set_write_buffer_size(options: *mut Options, bytes: u64);
    pub fn crocksdb_options_get_write_buffer_size(options: *mut Options) -> u64;
    pub fn crocksdb_options_set_target_file_size_base(options: *mut Options, bytes: u64);
    pub fn crocksdb_options_get_target_file_size_base(options: *const Options) -> u64;
    pub fn crocksdb_options_set_target_file_size_multiplier(options: *mut Options, mul: c_int);
    pub fn crocksdb_options_set_max_bytes_for_level_base(options: *mut Options, bytes: u64);
    pub fn crocksdb_options_get_max_bytes_for_level_base(options: *mut Options) -> u64;
    pub fn crocksdb_options_set_max_bytes_for_level_multiplier(options: *mut Options, mul: f64);
    pub fn crocksdb_options_get_max_bytes_for_level_multiplier(options: *mut Options) -> f64;
    pub fn crocksdb_options_set_max_compaction_bytes(options: *mut Options, bytes: u64);
    pub fn crocksdb_options_get_max_compaction_bytes(options: *mut Options) -> u64;
    pub fn crocksdb_options_set_max_log_file_size(options: *mut Options, bytes: size_t);
    pub fn crocksdb_options_set_log_file_time_to_roll(options: *mut Options, bytes: size_t);
    pub fn crocksdb_options_set_info_log_level(options: *mut Options, level: DBInfoLogLevel);
    pub fn crocksdb_options_set_keep_log_file_num(options: *mut Options, num: size_t);
    pub fn crocksdb_options_set_recycle_log_file_num(options: *mut Options, num: size_t);
    pub fn crocksdb_options_set_max_manifest_file_size(options: *mut Options, bytes: u64);
    pub fn crocksdb_options_get_memtable_factory_name(options: *mut Options) -> *const c_char;
    pub fn crocksdb_options_set_hash_skip_list_rep(
        options: *mut Options,
        bytes: u64,
        a1: i32,
        a2: i32,
    );
    pub fn crocksdb_options_set_doubly_skip_list_rep(options: *mut Options);
    pub fn crocksdb_options_set_compaction_style(options: *mut Options, cs: DBCompactionStyle);
    pub fn crocksdb_options_set_fifo_compaction_options(
        options: *mut Options,
        fifo_opts: *mut DBFifoCompactionOptions,
    );
    pub fn crocksdb_options_set_compression(
        options: *mut Options,
        compression_style_no: DBCompressionType,
    );
    pub fn crocksdb_options_get_compression(options: *mut Options) -> DBCompressionType;
    pub fn crocksdb_options_set_compression_options(
        options: *mut Options,
        window_bits: c_int,
        level: c_int,
        strategy: c_int,
        max_dict_bytes: c_int,
        zstd_max_train_bytes: c_int,
    );
    pub fn crocksdb_options_set_bottommost_compression_options(
        options: *mut Options,
        window_bits: c_int,
        level: c_int,
        strategy: c_int,
        max_dict_bytes: c_int,
        zstd_max_train_bytes: c_int,
    );
    pub fn crocksdb_options_set_compression_per_level(
        options: *mut Options,
        level_values: *const DBCompressionType,
        num_levels: size_t,
    );
    pub fn crocksdb_options_get_compression_level_number(options: *mut Options) -> size_t;
    pub fn crocksdb_options_get_compression_per_level(
        options: *mut Options,
        level_values: *mut DBCompressionType,
    );
    pub fn crocksdb_set_bottommost_compression(options: *mut Options, c: DBCompressionType);
    pub fn crocksdb_options_set_max_background_jobs(options: *mut Options, max_bg_jobs: c_int);
    pub fn crocksdb_options_get_max_background_jobs(options: *const Options) -> c_int;
    pub fn crocksdb_options_set_max_background_compactions(
        options: *mut Options,
        max_bg_compactions: c_int,
    );
    pub fn crocksdb_options_get_max_background_compactions(options: *const Options) -> c_int;
    pub fn crocksdb_options_set_max_background_flushes(
        options: *mut Options,
        max_bg_flushes: c_int,
    );
    pub fn crocksdb_options_get_max_background_flushes(options: *const Options) -> c_int;
    pub fn crocksdb_options_set_disable_auto_compactions(options: *mut Options, v: c_int);
    pub fn crocksdb_options_get_disable_auto_compactions(options: *const Options) -> c_int;
    pub fn crocksdb_options_set_report_bg_io_stats(options: *mut Options, v: c_int);
    pub fn crocksdb_options_set_compaction_readahead_size(options: *mut Options, v: size_t);
    pub fn crocksdb_options_set_wal_recovery_mode(options: *mut Options, mode: DBRecoveryMode);
    pub fn crocksdb_options_set_max_subcompactions(options: *mut Options, v: u32);
    pub fn crocksdb_options_set_wal_bytes_per_sync(options: *mut Options, v: u64);
    pub fn crocksdb_options_enable_statistics(options: *mut Options, v: bool);
    pub fn crocksdb_options_reset_statistics(options: *mut Options);
    pub fn crocksdb_options_statistics_get_string(options: *mut Options) -> *const c_char;
    pub fn crocksdb_options_statistics_get_ticker_count(
        options: *mut Options,
        ticker_type: DBStatisticsTickerType,
    ) -> u64;
    pub fn crocksdb_options_statistics_get_and_reset_ticker_count(
        options: *mut Options,
        ticker_type: DBStatisticsTickerType,
    ) -> u64;
    pub fn crocksdb_options_statistics_get_histogram_string(
        options: *mut Options,
        hist_type: DBStatisticsHistogramType,
    ) -> *const c_char;
    pub fn crocksdb_options_statistics_get_histogram(
        options: *mut Options,
        hist_type: DBStatisticsHistogramType,
        median: *mut c_double,
        percentile95: *mut c_double,
        percentile99: *mut c_double,
        average: *mut c_double,
        standard_deviation: *mut c_double,
        max: *mut c_double,
    ) -> bool;
    pub fn crocksdb_options_set_stats_dump_period_sec(options: *mut Options, v: usize);
    pub fn crocksdb_options_set_num_levels(options: *mut Options, v: c_int);
    pub fn crocksdb_options_get_num_levels(options: *mut Options) -> c_int;
    pub fn crocksdb_options_set_db_log_dir(options: *mut Options, path: *const c_char);
    pub fn crocksdb_options_set_wal_dir(options: *mut Options, path: *const c_char);
    pub fn crocksdb_options_set_wal_ttl_seconds(options: *mut Options, ttl: u64);
    pub fn crocksdb_options_set_wal_size_limit_mb(options: *mut Options, limit: u64);
    pub fn crocksdb_options_set_use_direct_reads(options: *mut Options, v: bool);
    pub fn crocksdb_options_set_use_direct_io_for_flush_and_compaction(
        options: *mut Options,
        v: bool,
    );
    pub fn crocksdb_options_set_prefix_extractor(
        options: *mut Options,
        prefix_extractor: *mut DBSliceTransform,
    );
    pub fn crocksdb_options_set_optimize_filters_for_hits(options: *mut Options, v: bool);
    pub fn crocksdb_options_set_level_compaction_dynamic_level_bytes(
        options: *mut Options,
        v: bool,
    );
    pub fn crocksdb_options_get_level_compaction_dynamic_level_bytes(
        options: *const Options,
    ) -> bool;
    pub fn crocksdb_options_set_memtable_insert_with_hint_prefix_extractor(
        options: *mut Options,
        prefix_extractor: *mut DBSliceTransform,
    );
    pub fn crocksdb_options_set_memtable_prefix_bloom_size_ratio(
        options: *mut Options,
        ratio: c_double,
    );
    pub fn crocksdb_options_set_delayed_write_rate(options: *mut Options, rate: u64);
    pub fn crocksdb_options_set_force_consistency_checks(options: *mut Options, v: bool);
    pub fn crocksdb_options_get_force_consistency_checks(options: *mut Options) -> bool;
    pub fn crocksdb_options_set_ratelimiter(options: *mut Options, limiter: *mut DBRateLimiter);
    pub fn crocksdb_options_get_ratelimiter(options: *mut Options) -> *mut DBRateLimiter;
    pub fn crocksdb_options_set_info_log(options: *mut Options, logger: *mut DBLogger);
    pub fn crocksdb_options_get_block_cache_usage(options: *const Options) -> usize;
    pub fn crocksdb_options_set_block_cache_capacity(
        options: *const Options,
        capacity: usize,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_options_get_block_cache_capacity(options: *const Options) -> usize;
    pub fn crocksdb_load_latest_options(
        dbpath: *const c_char,
        env: *mut DBEnv,
        db_options: *const Options,
        cf_descs: *const *mut *mut ColumnFamilyDescriptor,
        cf_descs_len: *mut size_t,
        ignore_unknown_options: bool,
        errptr: *mut *mut c_char,
    ) -> bool;
    pub fn crocksdb_ratelimiter_create(
        rate_bytes_per_sec: i64,
        refill_period_us: i64,
        fairness: i32,
    ) -> *mut DBRateLimiter;
    pub fn crocksdb_ratelimiter_create_with_auto_tuned(
        rate_bytes_per_sec: i64,
        refill_period_us: i64,
        fairness: i32,
        mode: DBRateLimiterMode,
        auto_tuned: bool,
    ) -> *mut DBRateLimiter;
    pub fn crocksdb_writeampbasedratelimiter_create_with_auto_tuned(
        rate_bytes_per_sec: i64,
        refill_period_us: i64,
        fairness: i32,
        mode: DBRateLimiterMode,
        auto_tuned: bool,
    ) -> *mut DBRateLimiter;
    pub fn crocksdb_ratelimiter_destroy(limiter: *mut DBRateLimiter);
    pub fn crocksdb_ratelimiter_set_bytes_per_second(
        limiter: *mut DBRateLimiter,
        bytes_per_sec: i64,
    );
    pub fn crocksdb_ratelimiter_set_auto_tuned(limiter: *mut DBRateLimiter, auto_tuned: bool);
    pub fn crocksdb_ratelimiter_get_singleburst_bytes(limiter: *mut DBRateLimiter) -> i64;
    pub fn crocksdb_ratelimiter_request(limiter: *mut DBRateLimiter, bytes: i64, pri: c_uchar);
    pub fn crocksdb_ratelimiter_get_total_bytes_through(
        limiter: *mut DBRateLimiter,
        pri: c_uchar,
    ) -> i64;
    pub fn crocksdb_ratelimiter_get_bytes_per_second(limiter: *mut DBRateLimiter) -> i64;
    pub fn crocksdb_ratelimiter_get_auto_tuned(limiter: *mut DBRateLimiter) -> bool;
    pub fn crocksdb_ratelimiter_get_total_requests(
        limiter: *mut DBRateLimiter,
        pri: c_uchar,
    ) -> i64;
    pub fn crocksdb_options_set_soft_pending_compaction_bytes_limit(options: *mut Options, v: u64);
    pub fn crocksdb_options_get_soft_pending_compaction_bytes_limit(options: *mut Options) -> u64;
    pub fn crocksdb_options_set_hard_pending_compaction_bytes_limit(options: *mut Options, v: u64);
    pub fn crocksdb_options_get_hard_pending_compaction_bytes_limit(options: *mut Options) -> u64;
    pub fn crocksdb_options_set_compaction_priority(options: *mut Options, v: CompactionPriority);
    pub fn crocksdb_options_set_db_paths(
        options: *mut Options,
        db_paths: *const *const c_char,
        path_lens: *const usize,
        target_size: *const u64,
        num_paths: c_int,
    );
    pub fn crocksdb_options_get_db_paths_num(options: *mut Options) -> usize;
    pub fn crocksdb_options_get_db_path(options: *mut Options, idx: size_t) -> *const c_char;
    pub fn crocksdb_options_get_path_target_size(options: *mut Options, idx: size_t) -> u64;
    pub fn crocksdb_options_set_vector_memtable_factory(options: *mut Options, reserved_bytes: u64);
    pub fn crocksdb_options_set_atomic_flush(option: *mut Options, enable: bool);
    pub fn crocksdb_options_get_sst_partitioner_factory(
        option: *mut Options,
    ) -> *mut DBSstPartitionerFactory;
    pub fn crocksdb_options_set_sst_partitioner_factory(
        option: *mut Options,
        factory: *mut DBSstPartitionerFactory,
    );
    pub fn crocksdb_filterpolicy_create_bloom_full(bits_per_key: c_int) -> *mut DBFilterPolicy;
    pub fn crocksdb_filterpolicy_create_bloom(bits_per_key: c_int) -> *mut DBFilterPolicy;
    pub fn crocksdb_open(
        options: *mut Options,
        path: *const c_char,
        err: *mut *mut c_char,
    ) -> *mut DBInstance;
    pub fn crocksdb_open_with_ttl(
        options: *mut Options,
        path: *const c_char,
        ttl: c_int,
        err: *mut *mut c_char,
    ) -> *mut DBInstance;
    pub fn crocksdb_open_for_read_only(
        options: *mut Options,
        path: *const c_char,
        error_if_log_file_exist: bool,
        err: *mut *mut c_char,
    ) -> *mut DBInstance;
    pub fn crocksdb_writeoptions_create() -> *mut DBWriteOptions;
    pub fn crocksdb_writeoptions_destroy(writeopts: *mut DBWriteOptions);
    pub fn crocksdb_writeoptions_set_sync(writeopts: *mut DBWriteOptions, v: bool);
    pub fn crocksdb_writeoptions_disable_wal(writeopts: *mut DBWriteOptions, v: c_int);
    pub fn crocksdb_writeoptions_set_ignore_missing_column_families(
        writeopts: *mut DBWriteOptions,
        v: bool,
    );
    pub fn crocksdb_writeoptions_set_no_slowdown(writeopts: *mut DBWriteOptions, v: bool);
    pub fn crocksdb_writeoptions_set_low_pri(writeopts: *mut DBWriteOptions, v: bool);
    pub fn crocksdb_put(
        db: *mut DBInstance,
        writeopts: *mut DBWriteOptions,
        k: *const u8,
        kLen: size_t,
        v: *const u8,
        vLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_put_cf(
        db: *mut DBInstance,
        writeopts: *mut DBWriteOptions,
        cf: *mut DBCFHandle,
        k: *const u8,
        kLen: size_t,
        v: *const u8,
        vLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_readoptions_create() -> *mut DBReadOptions;
    pub fn crocksdb_readoptions_destroy(readopts: *mut DBReadOptions);
    pub fn crocksdb_readoptions_set_verify_checksums(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_fill_cache(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_snapshot(
        readopts: *mut DBReadOptions,
        snapshot: *const DBSnapshot,
    );
    pub fn crocksdb_readoptions_set_iterate_lower_bound(
        readopts: *mut DBReadOptions,
        k: *const u8,
        kLen: size_t,
    );
    pub fn crocksdb_readoptions_set_iterate_upper_bound(
        readopts: *mut DBReadOptions,
        k: *const u8,
        kLen: size_t,
    );
    pub fn crocksdb_readoptions_set_read_tier(readopts: *mut DBReadOptions, tier: c_int);
    pub fn crocksdb_readoptions_set_tailing(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_managed(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_readahead_size(readopts: *mut DBReadOptions, size: size_t);
    pub fn crocksdb_readoptions_set_max_skippable_internal_keys(
        readopts: *mut DBReadOptions,
        n: u64,
    );
    pub fn crocksdb_readoptions_set_total_order_seek(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_prefix_same_as_start(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_pin_data(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_background_purge_on_iterator_cleanup(
        readopts: *mut DBReadOptions,
        v: bool,
    );
    pub fn crocksdb_readoptions_set_ignore_range_deletions(readopts: *mut DBReadOptions, v: bool);
    pub fn crocksdb_readoptions_set_table_filter(
        readopts: *mut DBReadOptions,
        ctx: *mut c_void,
        filter: extern "C" fn(*mut c_void, *const DBTableProperties) -> c_int,
        destroy: extern "C" fn(*mut c_void),
    );

    pub fn crocksdb_get(
        db: *const DBInstance,
        readopts: *const DBReadOptions,
        k: *const u8,
        kLen: size_t,
        valLen: *const size_t,
        err: *mut *mut c_char,
    ) -> *mut u8;
    pub fn crocksdb_get_cf(
        db: *const DBInstance,
        readopts: *const DBReadOptions,
        cf_handle: *mut DBCFHandle,
        k: *const u8,
        kLen: size_t,
        valLen: *const size_t,
        err: *mut *mut c_char,
    ) -> *mut u8;
    pub fn crocksdb_create_iterator(
        db: *mut DBInstance,
        readopts: *const DBReadOptions,
    ) -> *mut DBIterator;
    pub fn crocksdb_create_iterator_cf(
        db: *mut DBInstance,
        readopts: *const DBReadOptions,
        cf_handle: *mut DBCFHandle,
    ) -> *mut DBIterator;
    pub fn crocksdb_create_snapshot(db: *mut DBInstance) -> *const DBSnapshot;
    pub fn crocksdb_release_snapshot(db: *mut DBInstance, snapshot: *const DBSnapshot);
    pub fn crocksdb_get_snapshot_sequence_number(snapshot: *const DBSnapshot) -> u64;

    pub fn crocksdb_delete(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        k: *const u8,
        kLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_delete_cf(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        cf: *mut DBCFHandle,
        k: *const u8,
        kLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_single_delete(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        k: *const u8,
        kLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_single_delete_cf(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        cf: *mut DBCFHandle,
        k: *const u8,
        kLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_delete_range_cf(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        cf: *mut DBCFHandle,
        begin_key: *const u8,
        begin_keylen: size_t,
        end_key: *const u8,
        end_keylen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_close(db: *mut DBInstance);
    pub fn crocksdb_pause_bg_work(db: *mut DBInstance);
    pub fn crocksdb_continue_bg_work(db: *mut DBInstance);
    pub fn crocksdb_destroy_db(options: *const Options, path: *const c_char, err: *mut *mut c_char);
    pub fn crocksdb_repair_db(options: *const Options, path: *const c_char, err: *mut *mut c_char);
    // Merge
    pub fn crocksdb_merge(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        k: *const u8,
        kLen: size_t,
        v: *const u8,
        vLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_merge_cf(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        cf: *mut DBCFHandle,
        k: *const u8,
        kLen: size_t,
        v: *const u8,
        vLen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_mergeoperator_create(
        state: *mut c_void,
        destroy: unsafe extern "C" fn(*mut c_void) -> (),
        full_merge: unsafe extern "C" fn(
            arg: *mut c_void,
            key: *const c_char,
            key_len: size_t,
            existing_value: *const c_char,
            existing_value_len: size_t,
            operands_list: *const *const c_char,
            operands_list_len: *const size_t,
            num_operands: c_int,
            success: *mut u8,
            new_value_length: *mut size_t,
        ) -> *const c_char,
        partial_merge: unsafe extern "C" fn(
            arg: *mut c_void,
            key: *const c_char,
            key_len: size_t,
            operands_list: *const *const c_char,
            operands_list_len: *const size_t,
            num_operands: c_int,
            success: *mut u8,
            new_value_length: *mut size_t,
        ) -> *const c_char,
        delete_value: Option<
            unsafe extern "C" fn(*mut c_void, value: *const c_char, value_len: *mut size_t) -> (),
        >,
        name_fn: unsafe extern "C" fn(*mut c_void) -> *const c_char,
    ) -> *mut DBMergeOperator;
    pub fn crocksdb_mergeoperator_destroy(mo: *mut DBMergeOperator);
    pub fn crocksdb_options_set_merge_operator(options: *mut Options, mo: *mut DBMergeOperator);
    // Iterator
    pub fn crocksdb_iter_destroy(iter: *mut DBIterator);
    pub fn crocksdb_iter_valid(iter: *const DBIterator) -> bool;
    pub fn crocksdb_iter_seek_to_first(iter: *mut DBIterator);
    pub fn crocksdb_iter_seek_to_last(iter: *mut DBIterator);
    pub fn crocksdb_iter_seek(iter: *mut DBIterator, key: *const u8, klen: size_t);
    pub fn crocksdb_iter_seek_for_prev(iter: *mut DBIterator, key: *const u8, klen: size_t);
    pub fn crocksdb_iter_next(iter: *mut DBIterator);
    pub fn crocksdb_iter_prev(iter: *mut DBIterator);
    pub fn crocksdb_iter_key(iter: *const DBIterator, klen: *mut size_t) -> *mut u8;
    pub fn crocksdb_iter_value(iter: *const DBIterator, vlen: *mut size_t) -> *mut u8;
    pub fn crocksdb_iter_seqno(iter: *const DBIterator, seqno: *mut u64) -> bool;
    pub fn crocksdb_iter_get_error(iter: *const DBIterator, err: *mut *mut c_char);
    // Write batch
    pub fn crocksdb_write(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        batch: *mut DBWriteBatch,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_write_multi_batch(
        db: *mut DBInstance,
        writeopts: *const DBWriteOptions,
        batch: *const *mut DBWriteBatch,
        batchlen: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_writebatch_create() -> *mut DBWriteBatch;
    pub fn crocksdb_writebatch_create_with_capacity(cap: size_t) -> *mut DBWriteBatch;
    pub fn crocksdb_writebatch_create_from(rep: *const u8, size: size_t) -> *mut DBWriteBatch;
    pub fn crocksdb_writebatch_destroy(batch: *mut DBWriteBatch);
    pub fn crocksdb_writebatch_clear(batch: *mut DBWriteBatch);
    pub fn crocksdb_writebatch_count(batch: *mut DBWriteBatch) -> c_int;
    pub fn crocksdb_writebatch_put(
        batch: *mut DBWriteBatch,
        key: *const u8,
        klen: size_t,
        val: *const u8,
        vlen: size_t,
    );
    pub fn crocksdb_writebatch_put_cf(
        batch: *mut DBWriteBatch,
        cf: *mut DBCFHandle,
        key: *const u8,
        klen: size_t,
        val: *const u8,
        vlen: size_t,
    );
    pub fn crocksdb_writebatch_merge(
        batch: *mut DBWriteBatch,
        key: *const u8,
        klen: size_t,
        val: *const u8,
        vlen: size_t,
    );
    pub fn crocksdb_writebatch_merge_cf(
        batch: *mut DBWriteBatch,
        cf: *mut DBCFHandle,
        key: *const u8,
        klen: size_t,
        val: *const u8,
        vlen: size_t,
    );
    pub fn crocksdb_writebatch_delete(batch: *mut DBWriteBatch, key: *const u8, klen: size_t);
    pub fn crocksdb_writebatch_delete_cf(
        batch: *mut DBWriteBatch,
        cf: *mut DBCFHandle,
        key: *const u8,
        klen: size_t,
    );
    pub fn crocksdb_writebatch_single_delete(
        batch: *mut DBWriteBatch,
        key: *const u8,
        klen: size_t,
    );
    pub fn crocksdb_writebatch_single_delete_cf(
        batch: *mut DBWriteBatch,
        cf: *mut DBCFHandle,
        key: *const u8,
        klen: size_t,
    );
    pub fn crocksdb_writebatch_delete_range(
        batch: *mut DBWriteBatch,
        begin_key: *const u8,
        begin_keylen: size_t,
        end_key: *const u8,
        end_keylen: size_t,
    );
    pub fn crocksdb_writebatch_delete_range_cf(
        batch: *mut DBWriteBatch,
        cf: *mut DBCFHandle,
        begin_key: *const u8,
        begin_keylen: size_t,
        end_key: *const u8,
        end_keylen: size_t,
    );
    pub fn crocksdb_writebatch_iterate(
        batch: *mut DBWriteBatch,
        state: *mut c_void,
        put_fn: extern "C" fn(
            state: *mut c_void,
            k: *const u8,
            klen: size_t,
            v: *const u8,
            vlen: size_t,
        ),
        deleted_fn: extern "C" fn(state: *mut c_void, k: *const u8, klen: size_t),
    );
    pub fn crocksdb_writebatch_iterate_cf(
        batch: *mut DBWriteBatch,
        state: *mut c_void,
        put_fn: unsafe extern "C" fn(
            state: *mut c_void,
            k: *const u8,
            klen: size_t,
            v: *const u8,
            vlen: size_t,
        ) -> (),
        put_cf_fn: unsafe extern "C" fn(
            state: *mut c_void,
            cf: u32,
            k: *const u8,
            klen: size_t,
            v: *const u8,
            vlen: size_t,
        ) -> (),
        delete_fn: unsafe extern "C" fn(state: *mut c_void, k: *const u8, klen: size_t) -> (),
        delete_cf_fn: unsafe extern "C" fn(
            state: *mut c_void,
            cf: u32,
            k: *const u8,
            klen: size_t,
        ) -> (),
    );
    pub fn crocksdb_writebatch_data(batch: *mut DBWriteBatch, size: *mut size_t) -> *const u8;
    pub fn crocksdb_writebatch_set_save_point(batch: *mut DBWriteBatch);
    pub fn crocksdb_writebatch_pop_save_point(batch: *mut DBWriteBatch, err: *mut *mut c_char);
    pub fn crocksdb_writebatch_rollback_to_save_point(
        batch: *mut DBWriteBatch,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_writebatch_set_content(batch: *mut DBWriteBatch, data: *const u8, dlen: size_t);

    pub fn crocksdb_writebatch_append_content(
        dest: *mut DBWriteBatch,
        data: *const u8,
        dlen: size_t,
    );
    pub fn crocksdb_writebatch_ref_count(data: *const u8, dlen: size_t) -> c_int;
    pub fn crocksdb_writebatch_ref_iterator_create(
        data: *const u8,
        dlen: size_t,
    ) -> *mut DBWriteBatchIterator;
    pub fn crocksdb_writebatch_iterator_create(
        dest: *mut DBWriteBatch,
    ) -> *mut DBWriteBatchIterator;
    pub fn crocksdb_writebatch_iterator_destroy(it: *mut DBWriteBatchIterator);
    pub fn crocksdb_writebatch_iterator_valid(it: *mut DBWriteBatchIterator) -> bool;
    pub fn crocksdb_writebatch_iterator_next(it: *mut DBWriteBatchIterator);

    pub fn crocksdb_writebatch_iterator_key(
        it: *mut DBWriteBatchIterator,
        klen: *mut size_t,
    ) -> *mut u8;
    pub fn crocksdb_writebatch_iterator_value(
        it: *mut DBWriteBatchIterator,
        vlen: *mut size_t,
    ) -> *mut u8;
    pub fn crocksdb_writebatch_iterator_value_type(it: *mut DBWriteBatchIterator) -> DBValueType;
    pub fn crocksdb_writebatch_iterator_column_family_id(it: *mut DBWriteBatchIterator) -> u32;
    // Comparator
    pub fn crocksdb_options_set_comparator(options: *mut Options, cb: *mut DBComparator);
    pub fn crocksdb_comparator_create(
        state: *mut c_void,
        destroy: unsafe extern "C" fn(*mut c_void) -> (),
        compare: unsafe extern "C" fn(
            arg: *mut c_void,
            a: *const c_char,
            alen: size_t,
            b: *const c_char,
            blen: size_t,
        ) -> c_int,
        name_fn: unsafe extern "C" fn(*mut c_void) -> *const c_char,
    ) -> *mut DBComparator;
    pub fn crocksdb_comparator_destroy(cmp: *mut DBComparator);

    // Column Family
    pub fn crocksdb_open_column_families(
        options: *const Options,
        path: *const c_char,
        num_column_families: c_int,
        column_family_names: *const *const c_char,
        column_family_options: *const *const Options,
        column_family_handles: *const *mut DBCFHandle,
        err: *mut *mut c_char,
    ) -> *mut DBInstance;
    pub fn crocksdb_open_column_families_with_ttl(
        options: *const Options,
        path: *const c_char,
        num_column_families: c_int,
        column_family_names: *const *const c_char,
        column_family_options: *const *const Options,
        ttl_array: *const c_int,
        read_only: bool,
        column_family_handles: *const *mut DBCFHandle,
        err: *mut *mut c_char,
    ) -> *mut DBInstance;
    pub fn crocksdb_open_for_read_only_column_families(
        options: *const Options,
        path: *const c_char,
        num_column_families: c_int,
        column_family_names: *const *const c_char,
        column_family_options: *const *const Options,
        column_family_handles: *const *mut DBCFHandle,
        error_if_log_file_exist: bool,
        err: *mut *mut c_char,
    ) -> *mut DBInstance;
    pub fn crocksdb_create_column_family(
        db: *mut DBInstance,
        column_family_options: *const Options,
        column_family_name: *const c_char,
        err: *mut *mut c_char,
    ) -> *mut DBCFHandle;
    pub fn crocksdb_drop_column_family(
        db: *mut DBInstance,
        column_family_handle: *mut DBCFHandle,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_column_family_handle_id(column_family_handle: *mut DBCFHandle) -> u32;
    pub fn crocksdb_column_family_handle_destroy(column_family_handle: *mut DBCFHandle);
    pub fn crocksdb_list_column_families(
        db: *const Options,
        path: *const c_char,
        lencf: *mut size_t,
        err: *mut *mut c_char,
    ) -> *mut *mut c_char;
    pub fn crocksdb_list_column_families_destroy(list: *mut *mut c_char, len: size_t);

    // Flush options
    pub fn crocksdb_flushoptions_create() -> *mut DBFlushOptions;
    pub fn crocksdb_flushoptions_destroy(opt: *mut DBFlushOptions);
    pub fn crocksdb_flushoptions_set_wait(opt: *mut DBFlushOptions, whether_wait: bool);
    pub fn crocksdb_flushoptions_set_allow_write_stall(opt: *mut DBFlushOptions, allow: bool);

    pub fn crocksdb_flush(
        db: *mut DBInstance,
        options: *const DBFlushOptions,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_flush_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        options: *const DBFlushOptions,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_flush_cfs(
        db: *mut DBInstance,
        cfs: *const *mut DBCFHandle,
        num_cfs: size_t,
        options: *const DBFlushOptions,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_flush_wal(db: *mut DBInstance, sync: bool, err: *mut *mut c_char);
    pub fn crocksdb_sync_wal(db: *mut DBInstance, err: *mut *mut c_char);

    pub fn crocksdb_get_latest_sequence_number(db: *mut DBInstance) -> u64;

    pub fn crocksdb_approximate_sizes(
        db: *mut DBInstance,
        num_ranges: c_int,
        range_start_key: *const *const u8,
        range_start_key_len: *const size_t,
        range_limit_key: *const *const u8,
        range_limit_key_len: *const size_t,
        sizes: *mut u64,
    );
    pub fn crocksdb_approximate_sizes_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        num_ranges: c_int,
        range_start_key: *const *const u8,
        range_start_key_len: *const size_t,
        range_limit_key: *const *const u8,
        range_limit_key_len: *const size_t,
        sizes: *mut u64,
    );
    pub fn crocksdb_approximate_memtable_stats(
        db: *const DBInstance,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        count: *mut u64,
        size: *mut u64,
    );
    pub fn crocksdb_approximate_memtable_stats_cf(
        db: *const DBInstance,
        cf: *const DBCFHandle,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        count: *mut u64,
        size: *mut u64,
    );
    pub fn crocksdb_compactoptions_create() -> *mut DBCompactOptions;
    pub fn crocksdb_compactoptions_destroy(opt: *mut DBCompactOptions);
    pub fn crocksdb_compactoptions_set_exclusive_manual_compaction(
        opt: *mut DBCompactOptions,
        v: bool,
    );
    pub fn crocksdb_compactoptions_set_change_level(opt: *mut DBCompactOptions, v: bool);
    pub fn crocksdb_compactoptions_set_target_level(opt: *mut DBCompactOptions, v: i32);
    pub fn crocksdb_compactoptions_set_target_path_id(opt: *mut DBCompactOptions, v: i32);
    pub fn crocksdb_compactoptions_set_max_subcompactions(opt: *mut DBCompactOptions, v: i32);
    pub fn crocksdb_compactoptions_set_bottommost_level_compaction(
        opt: *mut DBCompactOptions,
        v: DBBottommostLevelCompaction,
    );

    pub fn crocksdb_fifo_compaction_options_create() -> *mut DBFifoCompactionOptions;
    pub fn crocksdb_fifo_compaction_options_set_max_table_files_size(
        fifo_opts: *mut DBFifoCompactionOptions,
        size: u64,
    );
    pub fn crocksdb_fifo_compaction_options_set_allow_compaction(
        fifo_opts: *mut DBFifoCompactionOptions,
        allow_compaction: bool,
    );
    pub fn crocksdb_fifo_compaction_options_destroy(fifo_opts: *mut DBFifoCompactionOptions);

    pub fn crocksdb_compact_range(
        db: *mut DBInstance,
        start_key: *const u8,
        start_key_len: size_t,
        limit_key: *const u8,
        limit_key_len: size_t,
    );
    pub fn crocksdb_compact_range_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        start_key: *const u8,
        start_key_len: size_t,
        limit_key: *const u8,
        limit_key_len: size_t,
    );
    pub fn crocksdb_compact_range_cf_opt(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        compact_options: *mut DBCompactOptions,
        start_key: *const u8,
        start_key_len: size_t,
        limit_key: *const u8,
        limit_key_len: size_t,
    );
    pub fn crocksdb_delete_files_in_range(
        db: *mut DBInstance,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        include_end: bool,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_delete_files_in_range_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        include_end: bool,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_delete_files_in_ranges_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        start_keys: *const *const u8,
        start_keys_lens: *const size_t,
        limit_keys: *const *const u8,
        limit_keys_lens: *const size_t,
        num_ranges: size_t,
        include_end: bool,
        errptr: *mut *mut c_char,
    );
    pub fn crocksdb_create_map_property() -> *mut DBMapProperty;
    pub fn crocksdb_destroy_map_property(info: *mut DBMapProperty);
    pub fn crocksdb_get_map_property_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        name: *const c_char,
        info: *mut DBMapProperty,
    ) -> bool;

    pub fn crocksdb_map_property_value(
        info: *const DBMapProperty,
        propname: *const c_char,
    ) -> *mut c_char;

    pub fn crocksdb_map_property_int_value(
        info: *const DBMapProperty,
        propname: *const c_char,
    ) -> u64;

    pub fn crocksdb_property_value(db: *mut DBInstance, propname: *const c_char) -> *mut c_char;
    pub fn crocksdb_property_value_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        propname: *const c_char,
    ) -> *mut c_char;
    // Compaction filter
    pub fn crocksdb_compactionfilter_create(
        state: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        filter: extern "C" fn(
            *mut c_void,
            c_int,
            *const u8,
            size_t,
            u64,
            CompactionFilterValueType,
            *const u8,
            size_t,
            *mut *mut u8,
            *mut size_t,
            *mut *mut u8,
            *mut size_t,
        ) -> CompactionFilterDecision,
        name: extern "C" fn(*mut c_void) -> *const c_char,
    ) -> *mut DBCompactionFilter;
    pub fn crocksdb_compactionfilter_destroy(filter: *mut DBCompactionFilter);

    // Compaction filter context
    pub fn crocksdb_compactionfiltercontext_is_full_compaction(
        context: *const DBCompactionFilterContext,
    ) -> bool;
    pub fn crocksdb_compactionfiltercontext_is_manual_compaction(
        context: *const DBCompactionFilterContext,
    ) -> bool;
    pub fn crocksdb_compactionfiltercontext_is_bottommost_level(
        context: *const DBCompactionFilterContext,
    ) -> bool;
    pub fn crocksdb_compactionfiltercontext_file_numbers(
        context: *const DBCompactionFilterContext,
        buffer: *mut *const u64,
        len: *mut usize,
    );
    pub fn crocksdb_compactionfiltercontext_table_properties(
        context: *const DBCompactionFilterContext,
        offset: usize,
    ) -> *const DBTableProperties;

    // Compaction filter factory
    pub fn crocksdb_compactionfilterfactory_create(
        state: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        create_compaction_filter: extern "C" fn(
            *mut c_void,
            *const DBCompactionFilterContext,
        ) -> *mut DBCompactionFilter,
        name: extern "C" fn(*mut c_void) -> *const c_char,
    ) -> *mut DBCompactionFilterFactory;
    pub fn crocksdb_compactionfilterfactory_destroy(factory: *mut DBCompactionFilterFactory);

    // Env
    pub fn crocksdb_default_env_create() -> *mut DBEnv;
    pub fn crocksdb_mem_env_create() -> *mut DBEnv;
    pub fn crocksdb_ctr_encrypted_env_create(
        base_env: *mut DBEnv,
        ciphertext: *const c_char,
        ciphertext_len: size_t,
    ) -> *mut DBEnv;
    pub fn crocksdb_env_file_exists(env: *mut DBEnv, path: *const c_char, err: *mut *mut c_char);
    pub fn crocksdb_env_delete_file(env: *mut DBEnv, path: *const c_char, err: *mut *mut c_char);
    pub fn crocksdb_env_destroy(env: *mut DBEnv);

    // EnvOptions
    pub fn crocksdb_envoptions_create() -> *mut EnvOptions;
    pub fn crocksdb_envoptions_destroy(opt: *mut EnvOptions);

    // SequentialFile
    pub fn crocksdb_sequential_file_create(
        env: *mut DBEnv,
        path: *const c_char,
        opts: *mut EnvOptions,
        err: *mut *mut c_char,
    ) -> *mut DBSequentialFile;
    pub fn crocksdb_sequential_file_read(
        file: *mut DBSequentialFile,
        n: size_t,
        buf: *mut u8,
        err: *mut *mut c_char,
    ) -> size_t;
    pub fn crocksdb_sequential_file_skip(
        file: *mut DBSequentialFile,
        n: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_sequential_file_destroy(file: *mut DBSequentialFile);

    // IngestExternalFileOptions
    pub fn crocksdb_ingestexternalfileoptions_create() -> *mut IngestExternalFileOptions;
    pub fn crocksdb_ingestexternalfileoptions_set_move_files(
        opt: *mut IngestExternalFileOptions,
        move_files: bool,
    );
    pub fn crocksdb_ingestexternalfileoptions_set_snapshot_consistency(
        opt: *mut IngestExternalFileOptions,
        snapshot_consistency: bool,
    );
    pub fn crocksdb_ingestexternalfileoptions_set_allow_global_seqno(
        opt: *mut IngestExternalFileOptions,
        allow_global_seqno: bool,
    );
    pub fn crocksdb_ingestexternalfileoptions_set_allow_blocking_flush(
        opt: *mut IngestExternalFileOptions,
        allow_blocking_flush: bool,
    );
    pub fn crocksdb_ingestexternalfileoptions_destroy(opt: *mut IngestExternalFileOptions);

    // KeyManagedEncryptedEnv
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_create() -> *mut DBFileEncryptionInfo;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_destroy(file_info: *mut DBFileEncryptionInfo);
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_method(
        file_info: *mut DBFileEncryptionInfo,
    ) -> DBEncryptionMethod;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_key(
        file_info: *mut DBFileEncryptionInfo,
        key_len: *mut size_t,
    ) -> *const c_char;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_iv(
        file_info: *mut DBFileEncryptionInfo,
        iv_len: *mut size_t,
    ) -> *const c_char;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_set_method(
        file_info: *mut DBFileEncryptionInfo,
        method: DBEncryptionMethod,
    );
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_set_key(
        file_info: *mut DBFileEncryptionInfo,
        key: *const c_char,
        key_len: size_t,
    );
    #[cfg(feature = "encryption")]
    pub fn crocksdb_file_encryption_info_set_iv(
        file_info: *mut DBFileEncryptionInfo,
        iv: *const c_char,
        iv_len: size_t,
    );

    #[cfg(feature = "encryption")]
    pub fn crocksdb_encryption_key_manager_create(
        state: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        get_file: extern "C" fn(
            *mut c_void,
            *const c_char,
            *mut DBFileEncryptionInfo,
        ) -> *const c_char,
        new_file: extern "C" fn(
            *mut c_void,
            *const c_char,
            *mut DBFileEncryptionInfo,
        ) -> *const c_char,
        delete_file: extern "C" fn(*mut c_void, *const c_char) -> *const c_char,
        link_file: extern "C" fn(*mut c_void, *const c_char, *const c_char) -> *const c_char,
    ) -> *mut DBEncryptionKeyManagerInstance;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_encryption_key_manager_destroy(
        key_manager: *mut DBEncryptionKeyManagerInstance,
    );
    #[cfg(feature = "encryption")]
    pub fn crocksdb_encryption_key_manager_get_file(
        key_manager: *mut DBEncryptionKeyManagerInstance,
        fname: *const c_char,
        file_info: *mut DBFileEncryptionInfo,
    ) -> *const c_char;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_encryption_key_manager_new_file(
        key_manager: *mut DBEncryptionKeyManagerInstance,
        fname: *const c_char,
        file_info: *mut DBFileEncryptionInfo,
    ) -> *const c_char;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_encryption_key_manager_delete_file(
        key_manager: *mut DBEncryptionKeyManagerInstance,
        fname: *const c_char,
    ) -> *const c_char;
    #[cfg(feature = "encryption")]
    pub fn crocksdb_encryption_key_manager_link_file(
        key_manager: *mut DBEncryptionKeyManagerInstance,
        src_fname: *const c_char,
        dst_fname: *const c_char,
    ) -> *const c_char;

    #[cfg(feature = "encryption")]
    pub fn crocksdb_key_managed_encrypted_env_create(
        base_env: *mut DBEnv,
        key_manager: *mut DBEncryptionKeyManagerInstance,
    ) -> *mut DBEnv;

    // FileSystemInspectedEnv
    pub fn crocksdb_file_system_inspector_create(
        state: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        read: extern "C" fn(*mut c_void, size_t, *mut *mut c_char) -> size_t,
        write: extern "C" fn(*mut c_void, size_t, *mut *mut c_char) -> size_t,
    ) -> *mut DBFileSystemInspectorInstance;
    pub fn crocksdb_file_system_inspector_destroy(inspector: *mut DBFileSystemInspectorInstance);
    pub fn crocksdb_file_system_inspector_read(
        inspector: *mut DBFileSystemInspectorInstance,
        len: size_t,
        errptr: *mut *mut c_char,
    ) -> size_t;
    pub fn crocksdb_file_system_inspector_write(
        inspector: *mut DBFileSystemInspectorInstance,
        len: size_t,
        errptr: *mut *mut c_char,
    ) -> size_t;

    pub fn crocksdb_file_system_inspected_env_create(
        base_env: *mut DBEnv,
        inspector: *mut DBFileSystemInspectorInstance,
    ) -> *mut DBEnv;

    // SstFileReader
    pub fn crocksdb_sstfilereader_create(io_options: *const Options) -> *mut SstFileReader;

    pub fn crocksdb_sstfilereader_open(
        reader: *mut SstFileReader,
        name: *const c_char,
        err: *mut *mut c_char,
    );

    pub fn crocksdb_sstfilereader_new_iterator(
        reader: *mut SstFileReader,
        options: *const DBReadOptions,
    ) -> *mut DBIterator;

    pub fn crocksdb_sstfilereader_read_table_properties(
        reader: *const SstFileReader,
        ctx: *mut c_void,
        callback: extern "C" fn(*mut c_void, *const DBTableProperties),
    );

    pub fn crocksdb_sstfilereader_verify_checksum(
        reader: *mut SstFileReader,
        errptr: *mut *mut c_char,
    );

    pub fn crocksdb_sstfilereader_destroy(reader: *mut SstFileReader);

    // SstFileWriter
    pub fn crocksdb_sstfilewriter_create(
        env: *mut EnvOptions,
        io_options: *const Options,
    ) -> *mut SstFileWriter;
    pub fn crocksdb_sstfilewriter_create_cf(
        env: *mut EnvOptions,
        io_options: *const Options,
        cf: *mut DBCFHandle,
    ) -> *mut SstFileWriter;
    pub fn crocksdb_sstfilewriter_open(
        writer: *mut SstFileWriter,
        name: *const c_char,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_sstfilewriter_put(
        writer: *mut SstFileWriter,
        key: *const u8,
        key_len: size_t,
        val: *const u8,
        val_len: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_sstfilewriter_merge(
        writer: *mut SstFileWriter,
        key: *const u8,
        key_len: size_t,
        val: *const u8,
        val_len: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_sstfilewriter_delete(
        writer: *mut SstFileWriter,
        key: *const u8,
        key_len: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_sstfilewriter_delete_range(
        writer: *mut SstFileWriter,
        begin_key: *const u8,
        begin_key_len: size_t,
        end_key: *const u8,
        end_key_len: size_t,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_sstfilewriter_finish(
        writer: *mut SstFileWriter,
        info: *mut ExternalSstFileInfo,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_sstfilewriter_file_size(writer: *mut SstFileWriter) -> u64;
    pub fn crocksdb_sstfilewriter_destroy(writer: *mut SstFileWriter);

    // ExternalSstFileInfo
    pub fn crocksdb_externalsstfileinfo_create() -> *mut ExternalSstFileInfo;
    pub fn crocksdb_externalsstfileinfo_destroy(info: *mut ExternalSstFileInfo);
    pub fn crocksdb_externalsstfileinfo_file_path(
        info: *mut ExternalSstFileInfo,
        size: *mut size_t,
    ) -> *const u8;
    pub fn crocksdb_externalsstfileinfo_smallest_key(
        info: *mut ExternalSstFileInfo,
        size: *mut size_t,
    ) -> *const u8;
    pub fn crocksdb_externalsstfileinfo_largest_key(
        info: *mut ExternalSstFileInfo,
        size: *mut size_t,
    ) -> *const u8;
    pub fn crocksdb_externalsstfileinfo_sequence_number(info: *mut ExternalSstFileInfo) -> u64;
    pub fn crocksdb_externalsstfileinfo_file_size(info: *mut ExternalSstFileInfo) -> u64;
    pub fn crocksdb_externalsstfileinfo_num_entries(info: *mut ExternalSstFileInfo) -> u64;

    pub fn crocksdb_ingest_external_file(
        db: *mut DBInstance,
        file_list: *const *const c_char,
        list_len: size_t,
        opt: *const IngestExternalFileOptions,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_ingest_external_file_cf(
        db: *mut DBInstance,
        handle: *const DBCFHandle,
        file_list: *const *const c_char,
        list_len: size_t,
        opt: *const IngestExternalFileOptions,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_ingest_external_file_optimized(
        db: *mut DBInstance,
        handle: *const DBCFHandle,
        file_list: *const *const c_char,
        list_len: size_t,
        opt: *const IngestExternalFileOptions,
        err: *mut *mut c_char,
    ) -> bool;

    // Restore Option
    pub fn crocksdb_restore_options_create() -> *mut DBRestoreOptions;
    pub fn crocksdb_restore_options_destroy(ropts: *mut DBRestoreOptions);
    pub fn crocksdb_restore_options_set_keep_log_files(ropts: *mut DBRestoreOptions, v: c_int);

    // Backup engine
    // TODO: add more ffis about backup engine.
    pub fn crocksdb_backup_engine_open(
        options: *const Options,
        path: *const c_char,
        err: *mut *mut c_char,
    ) -> *mut DBBackupEngine;
    pub fn crocksdb_backup_engine_create_new_backup(
        be: *mut DBBackupEngine,
        db: *mut DBInstance,
        err: *mut *mut c_char,
    );
    pub fn crocksdb_backup_engine_close(be: *mut DBBackupEngine);
    pub fn crocksdb_backup_engine_restore_db_from_latest_backup(
        be: *mut DBBackupEngine,
        db_path: *const c_char,
        wal_path: *const c_char,
        ropts: *const DBRestoreOptions,
        err: *mut *mut c_char,
    );
    // SliceTransform
    pub fn crocksdb_slicetransform_create(
        state: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        transform: extern "C" fn(*mut c_void, *const u8, size_t, *mut size_t) -> *const u8,
        in_domain: extern "C" fn(*mut c_void, *const u8, size_t) -> u8,
        in_range: extern "C" fn(*mut c_void, *const u8, size_t) -> u8,
        name: extern "C" fn(*mut c_void) -> *const c_char,
    ) -> *mut DBSliceTransform;
    pub fn crocksdb_slicetransform_destroy(transform: *mut DBSliceTransform);
    pub fn crocksdb_logger_create(
        state: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        logv: extern "C" fn(ctx: *mut c_void, log_level: DBInfoLogLevel, log: *const c_char),
    ) -> *mut DBLogger;
    pub fn crocksdb_create_env_logger(fname: *const libc::c_char, env: *mut DBEnv)
        -> *mut DBLogger;
    pub fn crocksdb_create_log_from_options(
        path: *const c_char,
        options: *mut Options,
        err: *mut *mut c_char,
    ) -> *mut DBLogger;
    pub fn crocksdb_log_destroy(logger: *mut DBLogger);
    pub fn crocksdb_get_pinned(
        db: *mut DBInstance,
        readopts: *const DBReadOptions,
        k: *const u8,
        kLen: size_t,
        err: *mut *mut c_char,
    ) -> *mut DBPinnableSlice;
    pub fn crocksdb_get_pinned_cf(
        db: *mut DBInstance,
        readopts: *const DBReadOptions,
        cf_handle: *mut DBCFHandle,
        k: *const u8,
        kLen: size_t,
        err: *mut *mut c_char,
    ) -> *mut DBPinnableSlice;
    pub fn crocksdb_pinnableslice_value(
        s: *const DBPinnableSlice,
        valLen: *mut size_t,
    ) -> *const u8;
    pub fn crocksdb_pinnableslice_destroy(v: *mut DBPinnableSlice);
    pub fn crocksdb_get_supported_compression_number() -> size_t;
    pub fn crocksdb_get_supported_compression(v: *mut DBCompressionType, l: size_t);

    pub fn crocksdb_user_collected_properties_add(
        props: *mut DBUserCollectedProperties,
        key: *const u8,
        key_len: size_t,
        value: *const u8,
        value_len: size_t,
    );

    pub fn crocksdb_user_collected_properties_iter_create(
        props: *const DBUserCollectedProperties,
    ) -> *mut DBUserCollectedPropertiesIterator;

    pub fn crocksdb_user_collected_properties_iter_destroy(
        it: *mut DBUserCollectedPropertiesIterator,
    );

    pub fn crocksdb_user_collected_properties_iter_valid(
        it: *const DBUserCollectedPropertiesIterator,
    ) -> bool;

    pub fn crocksdb_user_collected_properties_iter_next(it: *mut DBUserCollectedPropertiesIterator);

    pub fn crocksdb_user_collected_properties_iter_key(
        it: *const DBUserCollectedPropertiesIterator,
        klen: *mut size_t,
    ) -> *const u8;

    pub fn crocksdb_user_collected_properties_iter_value(
        it: *const DBUserCollectedPropertiesIterator,
        vlen: *mut size_t,
    ) -> *const u8;

    pub fn crocksdb_table_properties_get_u64(
        props: *const DBTableProperties,
        prop: DBTableProperty,
    ) -> u64;

    pub fn crocksdb_table_properties_get_str(
        props: *const DBTableProperties,
        prop: DBTableProperty,
        slen: *mut size_t,
    ) -> *const u8;

    pub fn crocksdb_table_properties_get_user_properties(
        props: *const DBTableProperties,
    ) -> *const DBUserCollectedProperties;

    pub fn crocksdb_user_collected_properties_get(
        props: *const DBUserCollectedProperties,
        key: *const u8,
        klen: size_t,
        vlen: *mut size_t,
    ) -> *const u8;

    pub fn crocksdb_user_collected_properties_len(
        props: *const DBUserCollectedProperties,
    ) -> size_t;

    pub fn crocksdb_table_properties_collection_len(
        props: *const DBTablePropertiesCollection,
    ) -> size_t;

    pub fn crocksdb_table_properties_collection_destroy(props: *mut DBTablePropertiesCollection);

    pub fn crocksdb_table_properties_collection_iter_create(
        props: *const DBTablePropertiesCollection,
    ) -> *mut DBTablePropertiesCollectionIterator;

    pub fn crocksdb_table_properties_collection_iter_destroy(
        it: *mut DBTablePropertiesCollectionIterator,
    );

    pub fn crocksdb_table_properties_collection_iter_valid(
        it: *const DBTablePropertiesCollectionIterator,
    ) -> bool;

    pub fn crocksdb_table_properties_collection_iter_next(
        it: *mut DBTablePropertiesCollectionIterator,
    );

    pub fn crocksdb_table_properties_collection_iter_key(
        it: *const DBTablePropertiesCollectionIterator,
        klen: *mut size_t,
    ) -> *const u8;

    pub fn crocksdb_table_properties_collection_iter_value(
        it: *const DBTablePropertiesCollectionIterator,
    ) -> *const DBTableProperties;

    pub fn crocksdb_table_properties_collector_create(
        state: *mut c_void,
        name: extern "C" fn(*mut c_void) -> *const c_char,
        destruct: extern "C" fn(*mut c_void),
        add_userkey: extern "C" fn(
            *mut c_void,
            *const u8,
            size_t,
            *const u8,
            size_t,
            c_int,
            u64,
            u64,
        ),
        finish: extern "C" fn(*mut c_void, *mut DBUserCollectedProperties),
    ) -> *mut DBTablePropertiesCollector;

    pub fn crocksdb_table_properties_collector_destroy(c: *mut DBTablePropertiesCollector);

    pub fn crocksdb_table_properties_collector_factory_create(
        state: *mut c_void,
        name: extern "C" fn(*mut c_void) -> *const c_char,
        destruct: extern "C" fn(*mut c_void),
        create_table_properties_collector: extern "C" fn(
            *mut c_void,
            u32,
        )
            -> *mut DBTablePropertiesCollector,
    ) -> *mut DBTablePropertiesCollectorFactory;

    pub fn crocksdb_table_properties_collector_factory_destroy(
        f: *mut DBTablePropertiesCollectorFactory,
    );

    pub fn crocksdb_options_add_table_properties_collector_factory(
        options: *mut Options,
        f: *mut DBTablePropertiesCollectorFactory,
    );

    pub fn crocksdb_options_set_compact_on_deletion(
        options: *mut Options,
        sliding_window_size: size_t,
        deletion_trigger: size_t,
    );

    pub fn crocksdb_get_properties_of_all_tables(
        db: *mut DBInstance,
        errptr: *mut *mut c_char,
    ) -> *mut DBTablePropertiesCollection;

    pub fn crocksdb_get_properties_of_all_tables_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        errptr: *mut *mut c_char,
    ) -> *mut DBTablePropertiesCollection;

    pub fn crocksdb_get_properties_of_tables_in_range(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        num_ranges: c_int,
        start_keys: *const *const u8,
        start_keys_lens: *const size_t,
        limit_keys: *const *const u8,
        limit_keys_lens: *const size_t,
        errptr: *mut *mut c_char,
    ) -> *mut DBTablePropertiesCollection;

    pub fn crocksdb_flushjobinfo_cf_name(
        info: *const DBFlushJobInfo,
        size: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_flushjobinfo_file_path(
        info: *const DBFlushJobInfo,
        size: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_flushjobinfo_table_properties(
        info: *const DBFlushJobInfo,
    ) -> *const DBTableProperties;
    pub fn crocksdb_flushjobinfo_triggered_writes_slowdown(info: *const DBFlushJobInfo) -> bool;
    pub fn crocksdb_flushjobinfo_triggered_writes_stop(info: *const DBFlushJobInfo) -> bool;

    pub fn crocksdb_compactionjobinfo_status(
        info: *const DBCompactionJobInfo,
        errptr: *mut *mut c_char,
    );
    pub fn crocksdb_compactionjobinfo_cf_name(
        info: *const DBCompactionJobInfo,
        size: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_compactionjobinfo_input_files_count(info: *const DBCompactionJobInfo)
        -> size_t;
    pub fn crocksdb_compactionjobinfo_input_file_at(
        info: *const DBCompactionJobInfo,
        pos: size_t,
        len: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_compactionjobinfo_output_files_count(
        info: *const DBCompactionJobInfo,
    ) -> size_t;
    pub fn crocksdb_compactionjobinfo_output_file_at(
        info: *const DBCompactionJobInfo,
        pos: size_t,
        len: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_compactionjobinfo_table_properties(
        info: *const DBCompactionJobInfo,
    ) -> *const DBTablePropertiesCollection;
    pub fn crocksdb_compactionjobinfo_elapsed_micros(info: *const DBCompactionJobInfo) -> u64;
    pub fn crocksdb_compactionjobinfo_num_corrupt_keys(info: *const DBCompactionJobInfo) -> u64;
    pub fn crocksdb_compactionjobinfo_base_input_level(info: *const DBCompactionJobInfo) -> c_int;
    pub fn crocksdb_compactionjobinfo_output_level(info: *const DBCompactionJobInfo) -> c_int;
    pub fn crocksdb_compactionjobinfo_input_records(info: *const DBCompactionJobInfo) -> u64;
    pub fn crocksdb_compactionjobinfo_output_records(info: *const DBCompactionJobInfo) -> u64;
    pub fn crocksdb_compactionjobinfo_total_input_bytes(info: *const DBCompactionJobInfo) -> u64;
    pub fn crocksdb_compactionjobinfo_total_output_bytes(info: *const DBCompactionJobInfo) -> u64;
    pub fn crocksdb_compactionjobinfo_compaction_reason(
        info: *const DBCompactionJobInfo,
    ) -> CompactionReason;

    pub fn crocksdb_subcompactionjobinfo_status(
        info: *const DBSubcompactionJobInfo,
        errptr: *mut *mut c_char,
    );
    pub fn crocksdb_subcompactionjobinfo_cf_name(
        info: *const DBSubcompactionJobInfo,
        size: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_subcompactionjobinfo_thread_id(info: *const DBSubcompactionJobInfo) -> u64;
    pub fn crocksdb_subcompactionjobinfo_base_input_level(
        info: *const DBSubcompactionJobInfo,
    ) -> c_int;
    pub fn crocksdb_subcompactionjobinfo_output_level(info: *const DBSubcompactionJobInfo)
        -> c_int;

    pub fn crocksdb_externalfileingestioninfo_cf_name(
        info: *const DBIngestionInfo,
        size: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_externalfileingestioninfo_internal_file_path(
        info: *const DBIngestionInfo,
        size: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_externalfileingestioninfo_table_properties(
        info: *const DBIngestionInfo,
    ) -> *const DBTableProperties;

    pub fn crocksdb_writestallinfo_cf_name(
        info: *const DBWriteStallInfo,
        size: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_writestallinfo_prev(
        info: *const DBWriteStallInfo,
    ) -> *const WriteStallCondition;
    pub fn crocksdb_writestallinfo_cur(info: *const DBWriteStallInfo)
        -> *const WriteStallCondition;

    pub fn crocksdb_eventlistener_create(
        state: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        flush_begin: extern "C" fn(*mut c_void, *mut DBInstance, *const DBFlushJobInfo),
        flush_completed: extern "C" fn(*mut c_void, *mut DBInstance, *const DBFlushJobInfo),
        compact_begin: extern "C" fn(*mut c_void, *mut DBInstance, *const DBCompactionJobInfo),
        compact_completed: extern "C" fn(*mut c_void, *mut DBInstance, *const DBCompactionJobInfo),
        subcompact_begin: extern "C" fn(*mut c_void, *const DBSubcompactionJobInfo),
        subcompact_completed: extern "C" fn(*mut c_void, *const DBSubcompactionJobInfo),
        ingest: extern "C" fn(*mut c_void, *mut DBInstance, *const DBIngestionInfo),
        bg_error: extern "C" fn(*mut c_void, DBBackgroundErrorReason, *mut DBStatusPtr),
        stall_conditions: extern "C" fn(*mut c_void, *const DBWriteStallInfo),
    ) -> *mut DBEventListener;
    pub fn crocksdb_eventlistener_destroy(et: *mut DBEventListener);
    pub fn crocksdb_options_add_eventlistener(opt: *mut Options, et: *mut DBEventListener);
    // Get All Key Versions
    pub fn crocksdb_keyversions_destroy(kvs: *mut DBKeyVersions);

    pub fn crocksdb_get_all_key_versions(
        db: *mut DBInstance,
        begin_key: *const u8,
        begin_keylen: size_t,
        end_key: *const u8,
        end_keylen: size_t,
        errptr: *mut *mut c_char,
    ) -> *mut DBKeyVersions;

    pub fn crocksdb_keyversions_count(kvs: *mut DBKeyVersions) -> size_t;

    pub fn crocksdb_keyversions_key(kvs: *mut DBKeyVersions, index: usize) -> *const c_char;

    pub fn crocksdb_keyversions_value(kvs: *mut DBKeyVersions, index: usize) -> *const c_char;

    pub fn crocksdb_keyversions_seq(kvs: *mut DBKeyVersions, index: usize) -> u64;

    pub fn crocksdb_keyversions_type(kvs: *mut DBKeyVersions, index: usize) -> c_int;

    pub fn crocksdb_set_external_sst_file_global_seq_no(
        db: *mut DBInstance,
        handle: *mut DBCFHandle,
        file: *const c_char,
        seq_no: u64,
        err: *mut *mut c_char,
    ) -> u64;

    pub fn crocksdb_get_column_family_meta_data(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        meta: *mut DBColumnFamilyMetaData,
    );

    pub fn crocksdb_column_family_meta_data_create() -> *mut DBColumnFamilyMetaData;
    pub fn crocksdb_column_family_meta_data_destroy(meta: *mut DBColumnFamilyMetaData);
    pub fn crocksdb_column_family_meta_data_level_count(
        meta: *const DBColumnFamilyMetaData,
    ) -> size_t;
    pub fn crocksdb_column_family_meta_data_level_data(
        meta: *const DBColumnFamilyMetaData,
        n: size_t,
    ) -> *const DBLevelMetaData;

    pub fn crocksdb_level_meta_data_file_count(meta: *const DBLevelMetaData) -> size_t;
    pub fn crocksdb_level_meta_data_file_data(
        meta: *const DBLevelMetaData,
        n: size_t,
    ) -> *const DBSstFileMetaData;

    pub fn crocksdb_sst_file_meta_data_size(meta: *const DBSstFileMetaData) -> size_t;
    pub fn crocksdb_sst_file_meta_data_name(meta: *const DBSstFileMetaData) -> *const c_char;
    pub fn crocksdb_sst_file_meta_data_smallestkey(
        meta: *const DBSstFileMetaData,
        len: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_sst_file_meta_data_largestkey(
        meta: *const DBSstFileMetaData,
        len: *mut size_t,
    ) -> *const c_char;

    pub fn crocksdb_compaction_options_create() -> *mut DBCompactionOptions;
    pub fn crocksdb_compaction_options_destroy(opts: *mut DBCompactionOptions);
    pub fn crocksdb_compaction_options_set_compression(
        opts: *mut DBCompactionOptions,
        compression: DBCompressionType,
    );
    pub fn crocksdb_compaction_options_set_output_file_size_limit(
        opts: *mut DBCompactionOptions,
        size: size_t,
    );
    pub fn crocksdb_compaction_options_set_max_subcompactions(
        opts: *mut DBCompactionOptions,
        v: i32,
    );

    pub fn crocksdb_compact_files_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        opts: *const DBCompactionOptions,
        input_file_names: *const *const c_char,
        input_file_count: size_t,
        output_level: c_int,
        errptr: *mut *mut c_char,
    );

    pub fn crocksdb_get_perf_level() -> c_int;
    pub fn crocksdb_set_perf_level(level: c_int);
    pub fn crocksdb_get_perf_context() -> *mut DBPerfContext;
    pub fn crocksdb_perf_context_reset(ctx: *mut DBPerfContext);
    pub fn crocksdb_perf_context_user_key_comparison_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_cache_hit_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_read_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_read_byte(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_read_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_cache_index_hit_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_index_block_read_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_cache_filter_hit_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_filter_block_read_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_checksum_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_decompress_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_get_read_bytes(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_multiget_read_bytes(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_iter_read_bytes(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_internal_key_skipped_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_internal_delete_skipped_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_internal_recent_skipped_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_internal_merge_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_get_snapshot_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_get_from_memtable_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_get_from_memtable_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_get_post_process_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_get_from_output_files_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_seek_on_memtable_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_seek_on_memtable_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_next_on_memtable_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_prev_on_memtable_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_seek_child_seek_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_seek_child_seek_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_seek_min_heap_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_seek_max_heap_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_seek_internal_seek_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_find_next_user_entry_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_write_wal_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_write_memtable_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_write_delay_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_write_pre_and_post_process_time(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_db_mutex_lock_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_write_thread_wait_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_write_scheduling_flushes_compactions_time(
        ctx: *mut DBPerfContext,
    ) -> u64;
    pub fn crocksdb_perf_context_db_condition_wait_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_merge_operator_time_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_read_index_block_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_read_filter_block_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_new_table_block_iter_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_new_table_iterator_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_block_seek_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_find_table_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_bloom_memtable_hit_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_bloom_memtable_miss_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_bloom_sst_hit_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_bloom_sst_miss_count(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_new_sequential_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_new_random_access_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_new_writable_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_reuse_writable_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_new_random_rw_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_new_directory_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_file_exists_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_get_children_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_get_children_file_attributes_nanos(
        ctx: *mut DBPerfContext,
    ) -> u64;
    pub fn crocksdb_perf_context_env_delete_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_create_dir_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_create_dir_if_missing_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_delete_dir_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_get_file_size_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_get_file_modification_time_nanos(
        ctx: *mut DBPerfContext,
    ) -> u64;
    pub fn crocksdb_perf_context_env_rename_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_link_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_lock_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_unlock_file_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_env_new_logger_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_get_cpu_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_iter_next_cpu_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_iter_prev_cpu_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_iter_seek_cpu_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_encrypt_data_nanos(ctx: *mut DBPerfContext) -> u64;
    pub fn crocksdb_perf_context_decrypt_data_nanos(ctx: *mut DBPerfContext) -> u64;

    pub fn crocksdb_get_iostats_context() -> *mut DBIOStatsContext;
    pub fn crocksdb_iostats_context_reset(ctx: *mut DBIOStatsContext);
    pub fn crocksdb_iostats_context_bytes_written(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_bytes_read(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_open_nanos(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_allocate_nanos(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_write_nanos(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_read_nanos(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_range_sync_nanos(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_fsync_nanos(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_prepare_write_nanos(ctx: *mut DBIOStatsContext) -> u64;
    pub fn crocksdb_iostats_context_logger_nanos(ctx: *mut DBIOStatsContext) -> u64;

    pub fn crocksdb_sst_partitioner_request_create() -> *mut DBSstPartitionerRequest;
    pub fn crocksdb_sst_partitioner_request_destroy(state: *mut DBSstPartitionerRequest);
    pub fn crocksdb_sst_partitioner_request_prev_user_key(
        state: *mut DBSstPartitionerRequest,
        len: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_sst_partitioner_request_current_user_key(
        state: *mut DBSstPartitionerRequest,
        len: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_sst_partitioner_request_current_output_file_size(
        state: *mut DBSstPartitionerRequest,
    ) -> u64;
    pub fn crocksdb_sst_partitioner_request_set_prev_user_key(
        state: *mut DBSstPartitionerRequest,
        key: *const c_char,
        len: size_t,
    );
    pub fn crocksdb_sst_partitioner_request_set_current_user_key(
        state: *mut DBSstPartitionerRequest,
        key: *const c_char,
        len: size_t,
    );
    pub fn crocksdb_sst_partitioner_request_set_current_output_file_size(
        state: *mut DBSstPartitionerRequest,
        current_output_file_size: u64,
    );

    pub fn crocksdb_sst_partitioner_create(
        underlying: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        should_partition_cb: extern "C" fn(
            *mut c_void,
            *mut DBSstPartitionerRequest,
        ) -> DBSstPartitionerResult,
        can_do_trivial_move_cb: extern "C" fn(
            *mut c_void,
            *const c_char,
            size_t,
            *const c_char,
            size_t,
        ) -> c_uchar,
    ) -> *mut DBSstPartitioner;
    pub fn crocksdb_sst_partitioner_destroy(partitioner: *mut DBSstPartitioner);
    pub fn crocksdb_sst_partitioner_should_partition(
        partitioner: *mut DBSstPartitioner,
        req: *mut DBSstPartitionerRequest,
    ) -> DBSstPartitionerResult;
    pub fn crocksdb_sst_partitioner_can_do_trivial_move(
        partitioner: *mut DBSstPartitioner,
        smallest_key: *const c_char,
        smallest_key_len: size_t,
        largest_key: *const c_char,
        largest_key_len: size_t,
    ) -> bool;

    pub fn crocksdb_sst_partitioner_context_create() -> *mut DBSstPartitionerContext;
    pub fn crocksdb_sst_partitioner_context_destroy(context: *mut DBSstPartitionerContext);
    pub fn crocksdb_sst_partitioner_context_is_full_compaction(
        context: *mut DBSstPartitionerContext,
    ) -> c_uchar;
    pub fn crocksdb_sst_partitioner_context_is_manual_compaction(
        context: *mut DBSstPartitionerContext,
    ) -> c_uchar;
    pub fn crocksdb_sst_partitioner_context_output_level(
        context: *mut DBSstPartitionerContext,
    ) -> c_int;
    pub fn crocksdb_sst_partitioner_context_smallest_key(
        context: *mut DBSstPartitionerContext,
        key_len: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_sst_partitioner_context_largest_key(
        context: *mut DBSstPartitionerContext,
        key_len: *mut size_t,
    ) -> *const c_char;
    pub fn crocksdb_sst_partitioner_context_set_is_full_compaction(
        context: *mut DBSstPartitionerContext,
        is_full_compaction: c_uchar,
    );
    pub fn crocksdb_sst_partitioner_context_set_is_manual_compaction(
        context: *mut DBSstPartitionerContext,
        is_manual_compaction: c_uchar,
    );
    pub fn crocksdb_sst_partitioner_context_set_output_level(
        context: *mut DBSstPartitionerContext,
        output_level: c_int,
    );
    pub fn crocksdb_sst_partitioner_context_set_smallest_key(
        context: *mut DBSstPartitionerContext,
        smallest_key: *const c_char,
        key_len: size_t,
    );
    pub fn crocksdb_sst_partitioner_context_set_largest_key(
        context: *mut DBSstPartitionerContext,
        largest_key: *const c_char,
        key_len: size_t,
    );

    pub fn crocksdb_sst_partitioner_factory_create(
        underlying: *mut c_void,
        destructor: extern "C" fn(*mut c_void),
        name_cb: extern "C" fn(*mut c_void) -> *const c_char,
        create_partitioner_cb: extern "C" fn(
            *mut c_void,
            *mut DBSstPartitionerContext,
        ) -> *mut DBSstPartitioner,
    ) -> *mut DBSstPartitionerFactory;
    pub fn crocksdb_sst_partitioner_factory_destroy(factory: *mut DBSstPartitionerFactory);
    pub fn crocksdb_sst_partitioner_factory_name(
        factory: *mut DBSstPartitionerFactory,
    ) -> *const c_char;
    pub fn crocksdb_sst_partitioner_factory_create_partitioner(
        factory: *mut DBSstPartitionerFactory,
        context: *mut DBSstPartitionerContext,
    ) -> *mut DBSstPartitioner;

    pub fn crocksdb_run_ldb_tool(argc: c_int, argv: *const *const c_char, opts: *const Options);
    pub fn crocksdb_run_sst_dump_tool(
        argc: c_int,
        argv: *const *const c_char,
        opts: *const Options,
    );
}

// Titan
extern "C" {
    pub fn ctitandb_open_column_families(
        path: *const c_char,
        titan_options: *const DBTitanDBOptions,
        num_column_families: c_int,
        column_family_names: *const *const c_char,
        titan_column_family_options: *const *const DBTitanDBOptions,
        column_family_handles: *const *mut DBCFHandle,
        err: *mut *mut c_char,
    ) -> *mut DBInstance;

    pub fn ctitandb_create_column_family(
        db: *mut DBInstance,
        titan_column_family_options: *const DBTitanDBOptions,
        column_family_name: *const c_char,
        err: *mut *mut c_char,
    ) -> *mut DBCFHandle;

    pub fn ctitandb_options_create() -> *mut DBTitanDBOptions;
    pub fn ctitandb_options_destroy(opts: *mut DBTitanDBOptions);
    pub fn ctitandb_options_copy(opts: *mut DBTitanDBOptions) -> *mut DBTitanDBOptions;
    pub fn ctitandb_options_set_rocksdb_options(
        opts: *mut DBTitanDBOptions,
        rocksdb_opts: *const Options,
    );
    pub fn ctitandb_get_titan_options_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
    ) -> *mut DBTitanDBOptions;
    pub fn ctitandb_get_titan_db_options(db: *mut DBInstance) -> *mut DBTitanDBOptions;
    pub fn ctitandb_options_dirname(opts: *mut DBTitanDBOptions) -> *const c_char;
    pub fn ctitandb_options_set_dirname(opts: *mut DBTitanDBOptions, name: *const c_char);
    pub fn ctitandb_options_min_blob_size(opts: *mut DBTitanDBOptions) -> u64;
    pub fn ctitandb_options_set_min_blob_size(opts: *mut DBTitanDBOptions, size: u64);
    pub fn ctitandb_options_blob_file_compression(opts: *mut DBTitanDBOptions)
        -> DBCompressionType;
    pub fn ctitandb_options_set_gc_merge_rewrite(opts: *mut DBTitanDBOptions, enable: bool);
    pub fn ctitandb_options_set_blob_file_compression(
        opts: *mut DBTitanDBOptions,
        t: DBCompressionType,
    );
    pub fn ctitandb_options_set_compression_options(
        options: *mut DBTitanDBOptions,
        window_bits: c_int,
        level: c_int,
        strategy: c_int,
        max_dict_bytes: c_int,
        zstd_max_train_bytes: c_int,
    );

    pub fn ctitandb_decode_blob_index(
        value: *const u8,
        value_size: u64,
        index: *mut DBTitanBlobIndex,
        errptr: *mut *mut c_char,
    );
    pub fn ctitandb_encode_blob_index(
        index: &DBTitanBlobIndex,
        value: *mut *mut u8,
        value_size: *mut u64,
    );

    pub fn ctitandb_options_set_disable_background_gc(opts: *mut DBTitanDBOptions, disable: bool);
    pub fn ctitandb_options_set_level_merge(opts: *mut DBTitanDBOptions, enable: bool);
    pub fn ctitandb_options_set_range_merge(opts: *mut DBTitanDBOptions, enable: bool);
    pub fn ctitandb_options_set_max_sorted_runs(opts: *mut DBTitanDBOptions, size: i32);
    pub fn ctitandb_options_set_max_background_gc(opts: *mut DBTitanDBOptions, size: i32);
    pub fn ctitandb_options_set_purge_obsolete_files_period_sec(
        opts: *mut DBTitanDBOptions,
        period: usize,
    );
    pub fn ctitandb_options_set_min_gc_batch_size(opts: *mut DBTitanDBOptions, size: u64);
    pub fn ctitandb_options_set_max_gc_batch_size(opts: *mut DBTitanDBOptions, size: u64);
    pub fn ctitandb_options_set_blob_cache(opts: *mut DBTitanDBOptions, cache: *mut DBCache);
    pub fn ctitandb_options_get_blob_cache_usage(options: *const DBTitanDBOptions) -> usize;
    pub fn ctitandb_options_set_blob_cache_capacity(
        options: *const DBTitanDBOptions,
        capacity: usize,
        err: *mut *mut c_char,
    );
    pub fn ctitandb_options_get_blob_cache_capacity(options: *const DBTitanDBOptions) -> usize;

    pub fn ctitandb_options_set_discardable_ratio(opts: *mut DBTitanDBOptions, ratio: f64);
    pub fn ctitandb_options_set_sample_ratio(opts: *mut DBTitanDBOptions, ratio: f64);
    pub fn ctitandb_options_set_merge_small_file_threshold(opts: *mut DBTitanDBOptions, size: u64);
    pub fn ctitandb_options_set_blob_run_mode(opts: *mut DBTitanDBOptions, t: DBTitanDBBlobRunMode);

    pub fn ctitandb_readoptions_set_key_only(opts: *mut DBTitanReadOptions, v: bool);

    pub fn ctitandb_readoptions_create() -> *mut DBTitanReadOptions;
    pub fn ctitandb_readoptions_destroy(readopts: *mut DBTitanReadOptions);

    pub fn ctitandb_create_iterator(
        db: *mut DBInstance,
        readopts: *const DBReadOptions,
        titan_readopts: *const DBTitanReadOptions,
    ) -> *mut DBIterator;
    pub fn ctitandb_create_iterator_cf(
        db: *mut DBInstance,
        readopts: *const DBReadOptions,
        titan_readopts: *const DBTitanReadOptions,
        cf_handle: *mut DBCFHandle,
    ) -> *mut DBIterator;
    pub fn ctitandb_delete_files_in_range(
        db: *mut DBInstance,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        include_end: bool,
        err: *mut *mut c_char,
    );
    pub fn ctitandb_delete_files_in_range_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        include_end: bool,
        err: *mut *mut c_char,
    );
    pub fn ctitandb_delete_files_in_ranges_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        start_keys: *const *const u8,
        start_keys_lens: *const size_t,
        limit_keys: *const *const u8,
        limit_keys_lens: *const size_t,
        num_ranges: size_t,
        include_end: bool,
        errptr: *mut *mut c_char,
    );
    pub fn ctitandb_delete_blob_files_in_range(
        db: *mut DBInstance,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        include_end: bool,
        err: *mut *mut c_char,
    );
    pub fn ctitandb_delete_blob_files_in_range_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        range_start_key: *const u8,
        range_start_key_len: size_t,
        range_limit_key: *const u8,
        range_limit_key_len: size_t,
        include_end: bool,
        err: *mut *mut c_char,
    );
    pub fn ctitandb_delete_blob_files_in_ranges_cf(
        db: *mut DBInstance,
        cf: *mut DBCFHandle,
        start_keys: *const *const u8,
        start_keys_lens: *const size_t,
        limit_keys: *const *const u8,
        limit_keys_lens: *const size_t,
        num_ranges: size_t,
        include_end: bool,
        errptr: *mut *mut c_char,
    );
}

#[cfg(test)]
mod test {
    use super::*;
    use libc::{self, c_void};
    use std::ffi::{CStr, CString};
    use std::{fs, ptr, slice};

    fn tempdir_with_prefix(prefix: &str) -> tempfile::TempDir {
        tempfile::Builder::new().prefix(prefix).tempdir().expect()
    }

    #[test]
    fn internal() {
        unsafe {
            let opts = crocksdb_options_create();
            assert!(!opts.is_null());

            crocksdb_options_increase_parallelism(opts, 0);
            crocksdb_options_optimize_level_style_compaction(opts, 0);
            crocksdb_options_set_create_if_missing(opts, true);
            let rustpath = tempdir_with_prefix("_rust_rocksdb_internaltest");
            let cpath = CString::new(rustpath.path().to_str().unwrap()).unwrap();
            let cpath_ptr = cpath.as_ptr();

            let mut err = ptr::null_mut();
            let db = crocksdb_open(opts, cpath_ptr, &mut err);
            assert!(err.is_null(), error_message(err));

            let writeopts = crocksdb_writeoptions_create();
            assert!(!writeopts.is_null());

            let key = b"name\x00";
            let val = b"spacejam\x00";
            crocksdb_put(db, writeopts, key.as_ptr(), 4, val.as_ptr(), 8, &mut err);
            crocksdb_writeoptions_destroy(writeopts);
            assert!(err.is_null(), error_message(err));

            let readopts = crocksdb_readoptions_create();
            assert!(!readopts.is_null());

            let mut val_len = 0;
            crocksdb_get(db, readopts, key.as_ptr(), 4, &mut val_len, &mut err);
            crocksdb_readoptions_destroy(readopts);
            assert!(err.is_null(), error_message(err));

            // flush first to get approximate size later.
            let flush_opt = crocksdb_flushoptions_create();
            crocksdb_flushoptions_set_wait(flush_opt, true);
            crocksdb_flush(db, flush_opt, &mut err);
            crocksdb_flushoptions_destroy(flush_opt);
            assert!(err.is_null(), error_message(err));

            let mut sizes = vec![0; 1];
            crocksdb_approximate_sizes(
                db,
                1,
                vec![b"\x00\x00".as_ptr()].as_ptr(),
                vec![1].as_ptr(),
                vec![b"\xff\x00".as_ptr()].as_ptr(),
                vec![1].as_ptr(),
                sizes.as_mut_ptr(),
            );
            assert_eq!(sizes.len(), 1);
            assert!(sizes[0] > 0);

            crocksdb_delete_files_in_range(
                db,
                b"\x00\x00".as_ptr(),
                2,
                b"\xff\x00".as_ptr(),
                2,
                true,
                &mut err,
            );
            assert!(err.is_null(), error_message(err));

            let propname = CString::new("rocksdb.total-sst-files-size").unwrap();
            let value = crocksdb_property_value(db, propname.as_ptr());
            assert!(!value.is_null());

            let sst_size = CStr::from_ptr(value)
                .to_str()
                .unwrap()
                .parse::<u64>()
                .unwrap();
            assert!(sst_size > 0);
            libc::free(value as *mut c_void);

            let propname = CString::new("fake_key").unwrap();
            let value = crocksdb_property_value(db, propname.as_ptr());
            assert!(value.is_null());
            libc::free(value as *mut c_void);

            crocksdb_close(db);
            crocksdb_destroy_db(opts, cpath_ptr, &mut err);
            assert!(err.is_null());
        }
    }

    unsafe fn check_get(
        db: *mut DBInstance,
        opt: *const DBReadOptions,
        key: &[u8],
        val: Option<&[u8]>,
    ) {
        let mut val_len = 0;
        let mut err = ptr::null_mut();
        let res_ptr = crocksdb_get(db, opt, key.as_ptr(), key.len(), &mut val_len, &mut err);
        assert!(err.is_null());
        let res = if res_ptr.is_null() {
            None
        } else {
            Some(slice::from_raw_parts(res_ptr, val_len))
        };
        assert_eq!(res, val);
        if !res_ptr.is_null() {
            libc::free(res_ptr as *mut libc::c_void);
        }
    }

    #[test]
    fn test_ingest_external_file() {
        unsafe {
            let opts = crocksdb_options_create();
            crocksdb_options_set_create_if_missing(opts, true);

            let rustpath = tempdir_with_prefix("_rust_rocksdb_internaltest");
            let cpath = CString::new(rustpath.path().to_str().unwrap()).unwrap();
            let cpath_ptr = cpath.as_ptr();

            let mut err = ptr::null_mut();
            let db = crocksdb_open(opts, cpath_ptr, &mut err);
            assert!(err.is_null(), error_message(err));

            let env_opt = crocksdb_envoptions_create();
            let io_options = crocksdb_options_create();
            let writer = crocksdb_sstfilewriter_create(env_opt, io_options);

            let sst_dir = tempdir_with_prefix("_rust_rocksdb_internaltest");
            let sst_path = sst_dir.path().join("sstfilename");
            let c_sst_path = CString::new(sst_path.to_str().unwrap()).unwrap();
            let c_sst_path_ptr = c_sst_path.as_ptr();

            crocksdb_sstfilewriter_open(writer, c_sst_path_ptr, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_put(writer, b"sstk1".as_ptr(), 5, b"v1".as_ptr(), 2, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_put(writer, b"sstk2".as_ptr(), 5, b"v2".as_ptr(), 2, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_put(writer, b"sstk3".as_ptr(), 5, b"v3".as_ptr(), 2, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_finish(writer, ptr::null_mut(), &mut err);
            assert!(err.is_null(), error_message(err));
            assert!(crocksdb_sstfilewriter_file_size(writer) > 0);

            let ing_opt = crocksdb_ingestexternalfileoptions_create();
            let file_list = &[c_sst_path_ptr];
            crocksdb_ingest_external_file(db, file_list.as_ptr(), 1, ing_opt, &mut err);
            assert!(err.is_null(), error_message(err));
            let roptions = crocksdb_readoptions_create();
            check_get(db, roptions, b"sstk1", Some(b"v1"));
            check_get(db, roptions, b"sstk2", Some(b"v2"));
            check_get(db, roptions, b"sstk3", Some(b"v3"));

            let snap = crocksdb_create_snapshot(db);

            fs::remove_file(sst_path).unwrap();
            crocksdb_sstfilewriter_open(writer, c_sst_path_ptr, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_put(writer, "sstk2".as_ptr(), 5, "v4".as_ptr(), 2, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_put(writer, "sstk22".as_ptr(), 6, "v5".as_ptr(), 2, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_put(writer, "sstk3".as_ptr(), 5, "v6".as_ptr(), 2, &mut err);
            assert!(err.is_null(), error_message(err));
            crocksdb_sstfilewriter_finish(writer, ptr::null_mut(), &mut err);
            assert!(err.is_null(), error_message(err));
            assert!(crocksdb_sstfilewriter_file_size(writer) > 0);

            crocksdb_ingest_external_file(db, file_list.as_ptr(), 1, ing_opt, &mut err);
            assert!(err.is_null(), error_message(err));
            check_get(db, roptions, b"sstk1", Some(b"v1"));
            check_get(db, roptions, b"sstk2", Some(b"v4"));
            check_get(db, roptions, b"sstk22", Some(b"v5"));
            check_get(db, roptions, b"sstk3", Some(b"v6"));

            let roptions2 = crocksdb_readoptions_create();
            crocksdb_readoptions_set_snapshot(roptions2, snap);
            check_get(db, roptions2, b"sstk1", Some(b"v1"));
            check_get(db, roptions2, b"sstk2", Some(b"v2"));
            check_get(db, roptions2, b"sstk22", None);
            check_get(db, roptions2, b"sstk3", Some(b"v3"));
            crocksdb_readoptions_destroy(roptions2);

            crocksdb_readoptions_destroy(roptions);
            crocksdb_release_snapshot(db, snap);
            crocksdb_ingestexternalfileoptions_destroy(ing_opt);
            crocksdb_sstfilewriter_destroy(writer);
            crocksdb_options_destroy(io_options);
            crocksdb_envoptions_destroy(env_opt);
        }
    }
}