File: tcfdb.c

package info (click to toggle)
tokyocabinet 1.4.48-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,436 kB
  • ctags: 4,658
  • sloc: ansic: 58,929; makefile: 793; cpp: 601; perl: 410; ruby: 264; awk: 95; sh: 92
file content (2748 lines) | stat: -rw-r--r-- 82,867 bytes parent folder | download | duplicates (6)
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
/*************************************************************************************************
 * The fixed-length database API of Tokyo Cabinet
 *                                                               Copyright (C) 2006-2012 FAL Labs
 * This file is part of Tokyo Cabinet.
 * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software Foundation; either
 * version 2.1 of the License or any later version.  Tokyo Cabinet is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with Tokyo
 * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA.
 *************************************************************************************************/


#include "tcutil.h"
#include "tcfdb.h"
#include "myconf.h"

#define FDBFILEMODE    00644             // permission of created files
#define FDBIOBUFSIZ    8192              // size of an I/O buffer

#define FDBMAGICDATA   "ToKyO CaBiNeT"   // magic data for identification
#define FDBHEADSIZ     256               // size of the reagion of the header
#define FDBTYPEOFF     32                // offset of the region for the database type
#define FDBFLAGSOFF    33                // offset of the region for the additional flags
#define FDBRNUMOFF     48                // offset of the region for the record number
#define FDBFSIZOFF     56                // offset of the region for the file size
#define FDBWIDTHOFF    64                // offset of the region for the record width
#define FDBLIMSIZOFF   72                // offset of the region for the limit size
#define FDBMINOFF      80                // offset of the region for the minimum ID offset
#define FDBMAXOFF      88                // offset of the region for the maximum ID offset
#define FDBOPAQUEOFF   128               // offset of the region for the opaque field

#define FDBDEFWIDTH    255               // default value width
#define FDBDEFLIMSIZ   (256LL<<20)       // default limit size
#define FDBRMTXNUM     127               // number of record mutexes
#define FDBTRUNCALW    256               // number of record for truncate allowance
#define FDBIDARYUNIT   2048              // size of ID array allocation unit
#define FDBWALSUFFIX   "wal"             // suffix of write ahead logging file

enum {                                   // enumeration for duplication behavior
  FDBPDOVER,                             // overwrite an existing value
  FDBPDKEEP,                             // keep the existing value
  FDBPDCAT,                              // concatenate values
  FDBPDADDINT,                           // add an integer
  FDBPDADDDBL,                           // add a real number
  FDBPDPROC                              // process by a callback function
};

typedef struct {                         // type of structure for a duplication callback
  TCPDPROC proc;                         // function pointer
  void *op;                              // opaque pointer
} FDBPDPROCOP;


/* private macros */
#define FDBLOCKMETHOD(TC_fdb, TC_wr)                            \
  ((TC_fdb)->mmtx ? tcfdblockmethod((TC_fdb), (TC_wr)) : true)
#define FDBUNLOCKMETHOD(TC_fdb)                         \
  ((TC_fdb)->mmtx ? tcfdbunlockmethod(TC_fdb) : true)
#define FDBLOCKATTR(TC_fdb)                             \
  ((TC_fdb)->mmtx ? tcfdblockattr(TC_fdb) : true)
#define FDBUNLOCKATTR(TC_fdb)                           \
  ((TC_fdb)->mmtx ? tcfdbunlockattr(TC_fdb) : true)
#define FDBLOCKRECORD(TC_fdb, TC_wr, TC_id)                             \
  ((TC_fdb)->mmtx ? tcfdblockrecord((TC_fdb), (TC_wr), (TC_id)) : true)
#define FDBUNLOCKRECORD(TC_fdb, TC_id)                                  \
  ((TC_fdb)->mmtx ? tcfdbunlockrecord((TC_fdb), (TC_id)) : true)
#define FDBLOCKALLRECORDS(TC_fdb, TC_wr)                                \
  ((TC_fdb)->mmtx ? tcfdblockallrecords((TC_fdb), (TC_wr)) : true)
#define FDBUNLOCKALLRECORDS(TC_fdb)                             \
  ((TC_fdb)->mmtx ? tcfdbunlockallrecords(TC_fdb) : true)
#define FDBLOCKWAL(TC_fdb)                              \
  ((TC_fdb)->mmtx ? tcfdblockwal(TC_fdb) : true)
#define FDBUNLOCKWAL(TC_fdb)                            \
  ((TC_fdb)->mmtx ? tcfdbunlockwal(TC_fdb) : true)
#define FDBTHREADYIELD(TC_fdb)                          \
  do { if((TC_fdb)->mmtx) sched_yield(); } while(false)


/* private function prototypes */
static void tcfdbdumpmeta(TCFDB *fdb, char *hbuf);
static void tcfdbloadmeta(TCFDB *fdb, const char *hbuf);
static void tcfdbclear(TCFDB *fdb);
static void tcfdbsetflag(TCFDB *fdb, int flag, bool sign);
static bool tcfdbwalinit(TCFDB *fdb);
static bool tcfdbwalwrite(TCFDB *fdb, uint64_t off, int64_t size);
static int tcfdbwalrestore(TCFDB *fdb, const char *path);
static bool tcfdbwalremove(TCFDB *fdb, const char *path);
static bool tcfdbopenimpl(TCFDB *fdb, const char *path, int omode);
static bool tcfdbcloseimpl(TCFDB *fdb);
static int64_t tcfdbprevid(TCFDB *fdb, int64_t id);
static int64_t tcfdbnextid(TCFDB *fdb, int64_t id);
static bool tcfdbputimpl(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz, int dmode);
static bool tcfdboutimpl(TCFDB *fdb, int64_t id);
static const void *tcfdbgetimpl(TCFDB *fdb, int64_t id, int *sp);
static bool tcfdbiterinitimpl(TCFDB *fdb);
static uint64_t tcfdbiternextimpl(TCFDB *fdb);
static uint64_t *tcfdbrangeimpl(TCFDB *fdb, int64_t lower, int64_t upper, int max, int *np);
static bool tcfdboptimizeimpl(TCFDB *fdb, int32_t width, int64_t limsiz);
static bool tcfdbvanishimpl(TCFDB *fdb);
static bool tcfdbcopyimpl(TCFDB *fdb, const char *path);
static bool tcfdbiterjumpimpl(TCFDB *fdb, int64_t id);
static bool tcfdbforeachimpl(TCFDB *fdb, TCITER iter, void *op);
static bool tcfdblockmethod(TCFDB *fdb, bool wr);
static bool tcfdbunlockmethod(TCFDB *fdb);
static bool tcfdblockattr(TCFDB *fdb);
static bool tcfdbunlockattr(TCFDB *fdb);
static bool tcfdblockrecord(TCFDB *fdb, bool wr, uint64_t id);
static bool tcfdbunlockrecord(TCFDB *fdb, uint64_t id);
static bool tcfdblockallrecords(TCFDB *fdb, bool wr);
static bool tcfdbunlockallrecords(TCFDB *fdb);
static bool tcfdblockwal(TCFDB *fdb);
static bool tcfdbunlockwal(TCFDB *fdb);


/* debugging function prototypes */
void tcfdbprintmeta(TCFDB *fdb);



/*************************************************************************************************
 * API
 *************************************************************************************************/


/* Get the message string corresponding to an error code. */
const char *tcfdberrmsg(int ecode){
  return tcerrmsg(ecode);
}


/* Create a fixed-length database object. */
TCFDB *tcfdbnew(void){
  TCFDB *fdb;
  TCMALLOC(fdb, sizeof(*fdb));
  tcfdbclear(fdb);
  return fdb;
}


/* Delete a fixed-length database object. */
void tcfdbdel(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd >= 0) tcfdbclose(fdb);
  if(fdb->mmtx){
    pthread_key_delete(*(pthread_key_t *)fdb->eckey);
    pthread_mutex_destroy(fdb->wmtx);
    pthread_mutex_destroy(fdb->tmtx);
    for(int i = FDBRMTXNUM - 1; i >= 0; i--){
      pthread_rwlock_destroy((pthread_rwlock_t *)fdb->rmtxs + i);
    }
    pthread_mutex_destroy(fdb->amtx);
    pthread_rwlock_destroy(fdb->mmtx);
    TCFREE(fdb->eckey);
    TCFREE(fdb->wmtx);
    TCFREE(fdb->tmtx);
    TCFREE(fdb->rmtxs);
    TCFREE(fdb->amtx);
    TCFREE(fdb->mmtx);
  }
  TCFREE(fdb);
}


/* Get the last happened error code of a fixed-length database object. */
int tcfdbecode(TCFDB *fdb){
  assert(fdb);
  return fdb->mmtx ?
    (int)(intptr_t)pthread_getspecific(*(pthread_key_t *)fdb->eckey) : fdb->ecode;
}


/* Set mutual exclusion control of a fixed-length database object for threading. */
bool tcfdbsetmutex(TCFDB *fdb){
  assert(fdb);
  if(!TCUSEPTHREAD) return true;
  if(fdb->mmtx || fdb->fd >= 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return false;
  }
  TCMALLOC(fdb->mmtx, sizeof(pthread_rwlock_t));
  TCMALLOC(fdb->amtx, sizeof(pthread_mutex_t));
  TCMALLOC(fdb->rmtxs, sizeof(pthread_rwlock_t) * FDBRMTXNUM);
  TCMALLOC(fdb->tmtx, sizeof(pthread_mutex_t));
  TCMALLOC(fdb->wmtx, sizeof(pthread_mutex_t));
  TCMALLOC(fdb->eckey, sizeof(pthread_key_t));
  bool err = false;
  if(pthread_rwlock_init(fdb->mmtx, NULL) != 0) err = true;
  if(pthread_mutex_init(fdb->amtx, NULL) != 0) err = true;
  for(int i = 0; i < FDBRMTXNUM; i++){
    if(pthread_rwlock_init((pthread_rwlock_t *)fdb->rmtxs + i, NULL) != 0) err = true;
  }
  if(pthread_mutex_init(fdb->tmtx, NULL) != 0) err = true;
  if(pthread_mutex_init(fdb->wmtx, NULL) != 0) err = true;
  if(pthread_key_create(fdb->eckey, NULL) != 0) err = true;
  if(err){
    TCFREE(fdb->eckey);
    TCFREE(fdb->wmtx);
    TCFREE(fdb->tmtx);
    TCFREE(fdb->rmtxs);
    TCFREE(fdb->amtx);
    TCFREE(fdb->mmtx);
    fdb->eckey = NULL;
    fdb->wmtx = NULL;
    fdb->tmtx = NULL;
    fdb->rmtxs = NULL;
    fdb->amtx = NULL;
    fdb->mmtx = NULL;
    return false;
  }
  return true;
}


/* Set the tuning parameters of a fixed-length database object. */
bool tcfdbtune(TCFDB *fdb, int32_t width, int64_t limsiz){
  assert(fdb);
  if(fdb->fd >= 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return false;
  }
  fdb->width = (width > 0) ? width : FDBDEFWIDTH;
  fdb->limsiz = (limsiz > 0) ? limsiz : FDBDEFLIMSIZ;
  if(fdb->limsiz < FDBHEADSIZ + fdb->width + sizeof(uint32_t))
    fdb->limsiz = FDBHEADSIZ + fdb->width + sizeof(uint32_t);
  fdb->limsiz = tcpagealign(fdb->limsiz);
  return true;
}


/* Open a database file and connect a fixed-length database object. */
bool tcfdbopen(TCFDB *fdb, const char *path, int omode){
  assert(fdb && path);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd >= 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  char *rpath = tcrealpath(path);
  if(!rpath){
    int ecode = TCEOPEN;
    switch(errno){
      case EACCES: ecode = TCENOPERM; break;
      case ENOENT: ecode = TCENOFILE; break;
      case ENOTDIR: ecode = TCENOFILE; break;
    }
    tcfdbsetecode(fdb, ecode, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!tcpathlock(rpath)){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    TCFREE(rpath);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbopenimpl(fdb, path, omode);
  if(rv){
    fdb->rpath = rpath;
  } else {
    tcpathunlock(rpath);
    TCFREE(rpath);
  }
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Close a fixed-length database object. */
bool tcfdbclose(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbcloseimpl(fdb);
  tcpathunlock(fdb->rpath);
  TCFREE(fdb->rpath);
  fdb->rpath = NULL;
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Store a record into a fixed-length database object. */
bool tcfdbput(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz){
  assert(fdb && vbuf && vsiz >= 0);
  if(!FDBLOCKMETHOD(fdb, id < 1)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDPREV){
    id = fdb->min - 1;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  } else if(id == FDBIDNEXT){
    id = fdb->max + 1;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, true, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbputimpl(fdb, id, vbuf, vsiz, FDBPDOVER);
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Store a record with a decimal key into a fixed-length database object. */
bool tcfdbput2(TCFDB *fdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
  assert(fdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
  return tcfdbput(fdb, tcfdbkeytoid(kbuf, ksiz), vbuf, vsiz);
}


/* Store a string record with a decimal key into a fixed-length database object. */
bool tcfdbput3(TCFDB *fdb, const char *kstr, const void *vstr){
  assert(fdb && kstr && vstr);
  return tcfdbput(fdb, tcfdbkeytoid(kstr, strlen(kstr)), vstr, strlen(vstr));
}


/* Store a new record into a fixed-length database object. */
bool tcfdbputkeep(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz){
  assert(fdb && vbuf && vsiz >= 0);
  if(!FDBLOCKMETHOD(fdb, id < 1)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDPREV){
    id = fdb->min - 1;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  } else if(id == FDBIDNEXT){
    id = fdb->max + 1;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, true, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbputimpl(fdb, id, vbuf, vsiz, FDBPDKEEP);
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Store a new record with a decimal key into a fixed-length database object. */
bool tcfdbputkeep2(TCFDB *fdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
  assert(fdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
  return tcfdbputkeep(fdb, tcfdbkeytoid(kbuf, ksiz), vbuf, vsiz);
}


/* Store a new string record with a decimal key into a fixed-length database object. */
bool tcfdbputkeep3(TCFDB *fdb, const char *kstr, const void *vstr){
  assert(fdb && kstr && vstr);
  return tcfdbputkeep(fdb, tcfdbkeytoid(kstr, strlen(kstr)), vstr, strlen(vstr));
}


/* Concatenate a value at the end of the existing record in a fixed-length database object. */
bool tcfdbputcat(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz){
  assert(fdb && vbuf && vsiz >= 0);
  if(!FDBLOCKMETHOD(fdb, id < 1)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDPREV){
    id = fdb->min - 1;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  } else if(id == FDBIDNEXT){
    id = fdb->max + 1;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, true, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbputimpl(fdb, id, vbuf, vsiz, FDBPDCAT);
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Concatenate a value with a decimal key in a fixed-length database object. */
bool tcfdbputcat2(TCFDB *fdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
  assert(fdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
  return tcfdbputcat(fdb, tcfdbkeytoid(kbuf, ksiz), vbuf, vsiz);
}


/* Concatenate a string value with a decimal key in a fixed-length database object. */
bool tcfdbputcat3(TCFDB *fdb, const char *kstr, const void *vstr){
  assert(fdb && kstr && vstr);
  return tcfdbputcat(fdb, tcfdbkeytoid(kstr, strlen(kstr)), vstr, strlen(vstr));
}


/* Remove a record of a fixed-length database object. */
bool tcfdbout(TCFDB *fdb, int64_t id){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, true, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdboutimpl(fdb, id);
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Remove a record with a decimal key of a fixed-length database object. */
bool tcfdbout2(TCFDB *fdb, const void *kbuf, int ksiz){
  assert(fdb && kbuf && ksiz >= 0);
  return tcfdbout(fdb, tcfdbkeytoid(kbuf, ksiz));
}


/* Remove a string record with a decimal key of a fixed-length database object. */
bool tcfdbout3(TCFDB *fdb, const char *kstr){
  assert(fdb && kstr);
  return tcfdbout(fdb, tcfdbkeytoid(kstr, strlen(kstr)));
}


/* Retrieve a record in a fixed-length database object. */
void *tcfdbget(TCFDB *fdb, int64_t id, int *sp){
  assert(fdb && sp);
  if(!FDBLOCKMETHOD(fdb, false)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, false, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  const void *vbuf = tcfdbgetimpl(fdb, id, sp);
  char *rv = vbuf ? tcmemdup(vbuf, *sp) : NULL;
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Retrieve a record with a decimal key in a fixed-length database object. */
void *tcfdbget2(TCFDB *fdb, const void *kbuf, int ksiz, int *sp){
  assert(fdb && kbuf && ksiz >= 0 && sp);
  return tcfdbget(fdb, tcfdbkeytoid(kbuf, ksiz), sp);
}


/* Retrieve a string record with a decimal key in a fixed-length database object. */
char *tcfdbget3(TCFDB *fdb, const char *kstr){
  assert(fdb && kstr);
  int vsiz;
  return tcfdbget(fdb, tcfdbkeytoid(kstr, strlen(kstr)), &vsiz);
}


/* Retrieve a record in a fixed-length database object and write the value into a buffer. */
int tcfdbget4(TCFDB *fdb, int64_t id, void *vbuf, int max){
  assert(fdb && vbuf && max >= 0);
  if(!FDBLOCKMETHOD(fdb, false)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, false, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  int vsiz;
  const void *rbuf = tcfdbgetimpl(fdb, id, &vsiz);
  if(rbuf){
    if(vsiz > max) vsiz = max;
    memcpy(vbuf, rbuf, vsiz);
  } else {
    vsiz = -1;
  }
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return vsiz;
}


/* Get the size of the value of a record in a fixed-length database object. */
int tcfdbvsiz(TCFDB *fdb, int64_t id){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, false)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, false, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  int vsiz;
  const void *vbuf = tcfdbgetimpl(fdb, id, &vsiz);
  if(!vbuf) vsiz = -1;
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return vsiz;
}


/* Get the size of the value with a decimal key in a fixed-length database object. */
int tcfdbvsiz2(TCFDB *fdb, const void *kbuf, int ksiz){
  assert(fdb && kbuf && ksiz >= 0);
  return tcfdbvsiz(fdb, tcfdbkeytoid(kbuf, ksiz));
}


/* Get the size of the string value with a decimal key in a fixed-length database object. */
int tcfdbvsiz3(TCFDB *fdb, const char *kstr){
  assert(fdb && kstr);
  return tcfdbvsiz(fdb, tcfdbkeytoid(kstr, strlen(kstr)));
}


/* Initialize the iterator of a fixed-length database object. */
bool tcfdbiterinit(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbiterinitimpl(fdb);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Get the next ID number of the iterator of a fixed-length database object. */
uint64_t tcfdbiternext(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  uint64_t rv = tcfdbiternextimpl(fdb);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Get the next decimay key of the iterator of a fixed-length database object. */
void *tcfdbiternext2(TCFDB *fdb, int *sp){
  assert(fdb && sp);
  uint64_t id = tcfdbiternextimpl(fdb);
  if(id < 1) return NULL;
  char kbuf[TCNUMBUFSIZ];
  int ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
  *sp = ksiz;
  return tcmemdup(kbuf, ksiz);
}


/* Get the next decimay key string of the iterator of a fixed-length database object. */
char *tcfdbiternext3(TCFDB *fdb){
  assert(fdb);
  int ksiz;
  return tcfdbiternext2(fdb, &ksiz);
}


/* Get range matching decimal keys in a fixed-length database object. */
uint64_t *tcfdbrange(TCFDB *fdb, int64_t lower, int64_t upper, int max, int *np){
  assert(fdb && np);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    *np = 0;
    return tcmalloc(1);
  }
  if(lower == FDBIDMIN) lower = fdb->min;
  if(upper == FDBIDMAX) upper = fdb->max;
  if(lower < 1 || lower > fdb->limid || upper < 1 || upper > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    *np = 0;
    return tcmalloc(1);
  }
  uint64_t *rv = tcfdbrangeimpl(fdb, lower, upper, max, np);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Get range matching decimal keys in a fixed-length database object. */
TCLIST *tcfdbrange2(TCFDB *fdb, const void *lbuf, int lsiz, const void *ubuf, int usiz, int max){
  assert(fdb && lbuf && lsiz >= 0 && ubuf && usiz >= 0);
  int num;
  uint64_t *ids = tcfdbrange(fdb, tcfdbkeytoid(lbuf, lsiz), tcfdbkeytoid(ubuf, usiz), max, &num);
  TCLIST *keys = tclistnew2(num);
  for(int i = 0; i < num; i++){
    char kbuf[TCNUMBUFSIZ];
    int ksiz = sprintf(kbuf, "%llu", (unsigned long long)ids[i]);
    TCLISTPUSH(keys, kbuf, ksiz);
  }
  TCFREE(ids);
  return keys;
}


/* Get range matching decimal keys with strings in a fixed-length database object. */
TCLIST *tcfdbrange3(TCFDB *fdb, const char *lstr, const char *ustr, int max){
  assert(fdb && lstr && ustr);
  return tcfdbrange2(fdb, lstr, strlen(lstr), ustr, strlen(ustr), max);
}


/* Get keys with an interval notation in a fixed-length database object. */
TCLIST *tcfdbrange4(TCFDB *fdb, const void *ibuf, int isiz, int max){
  assert(fdb && ibuf && isiz >= 0);
  char *expr;
  TCMEMDUP(expr, ibuf, isiz);
  char *pv = expr;
  while(*pv > '\0' && *pv <= ' '){
    pv++;
  }
  bool linc = false;
  if(*pv == '['){
    linc = true;
  } else if(*pv != '('){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    TCFREE(expr);
    return tclistnew();
  }
  pv++;
  char *sep = strchr(pv, ',');
  if(!sep){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    TCFREE(expr);
    return tclistnew();
  }
  *sep = '\0';
  tcstrtrim(pv);
  int64_t lower = tcfdbkeytoid(pv, strlen(pv));
  pv = sep + 1;
  bool uinc = false;
  if((sep = strchr(pv, ']')) != NULL){
    uinc = true;
    *sep = '\0';
  } else if((sep = strchr(pv, ')')) != NULL){
    *sep = '\0';
  } else {
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    TCFREE(expr);
    return tclistnew();
  }
  tcstrtrim(pv);
  int64_t upper = tcfdbkeytoid(pv, strlen(pv));
  if(lower == FDBIDMIN){
    lower = fdb->min;
  } else if(lower == FDBIDPREV){
    lower = fdb->min - 1;
  } else if(lower == FDBIDMAX){
    lower = fdb->max;
  } else if(lower == FDBIDNEXT){
    lower = fdb->max + 1;
  }
  if(!linc) lower++;
  if(upper == FDBIDMIN){
    upper = fdb->min;
  } else if(upper == FDBIDPREV){
    upper = fdb->min - 1;
  } else if(upper == FDBIDMAX){
    upper = fdb->max;
  } else if(upper == FDBIDNEXT){
    upper = fdb->max + 1;
  }
  if(!uinc) upper--;
  TCFREE(expr);
  int num;
  uint64_t *ids = tcfdbrange(fdb, lower, upper, max, &num);
  TCLIST *keys = tclistnew2(num);
  for(int i = 0; i < num; i++){
    char kbuf[TCNUMBUFSIZ];
    int ksiz = sprintf(kbuf, "%llu", (unsigned long long)ids[i]);
    TCLISTPUSH(keys, kbuf, ksiz);
  }
  TCFREE(ids);
  return keys;
}


/* Get keys with an interval notation string in a fixed-length database object. */
TCLIST *tcfdbrange5(TCFDB *fdb, const void *istr, int max){
  assert(fdb && istr);
  return tcfdbrange4(fdb, istr, strlen(istr), max);
}


/* Add an integer to a record in a fixed-length database object. */
int tcfdbaddint(TCFDB *fdb, int64_t id, int num){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, id < 1)) return INT_MIN;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return INT_MIN;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDPREV){
    id = fdb->min - 1;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  } else if(id == FDBIDNEXT){
    id = fdb->max + 1;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return INT_MIN;
  }
  if(!FDBLOCKRECORD(fdb, true, id)){
    FDBUNLOCKMETHOD(fdb);
    return INT_MIN;
  }
  bool rv = tcfdbputimpl(fdb, id, (char *)&num, sizeof(num), FDBPDADDINT);
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv ? num : INT_MIN;
}


/* Add a real number to a record in a fixed-length database object. */
double tcfdbadddouble(TCFDB *fdb, int64_t id, double num){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, id < 1)) return nan("");
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return nan("");
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDPREV){
    id = fdb->min - 1;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  } else if(id == FDBIDNEXT){
    id = fdb->max + 1;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return nan("");
  }
  if(!FDBLOCKRECORD(fdb, true, id)){
    FDBUNLOCKMETHOD(fdb);
    return nan("");
  }
  bool rv = tcfdbputimpl(fdb, id, (char *)&num, sizeof(num), FDBPDADDDBL);
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv ? num : nan("");
}


/* Synchronize updated contents of a fixed-length database object with the file and the device. */
bool tcfdbsync(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER) || fdb->tran){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbmemsync(fdb, true);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Optimize the file of a fixed-length database object. */
bool tcfdboptimize(TCFDB *fdb, int32_t width, int64_t limsiz){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER) || fdb->tran){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  FDBTHREADYIELD(fdb);
  bool rv = tcfdboptimizeimpl(fdb, width, limsiz);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Remove all records of a fixed-length database object. */
bool tcfdbvanish(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER) || fdb->tran){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  FDBTHREADYIELD(fdb);
  bool rv = tcfdbvanishimpl(fdb);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Copy the database file of a fixed-length database object. */
bool tcfdbcopy(TCFDB *fdb, const char *path){
  assert(fdb && path);
  if(!FDBLOCKMETHOD(fdb, false)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKALLRECORDS(fdb, false)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  FDBTHREADYIELD(fdb);
  bool rv = tcfdbcopyimpl(fdb, path);
  FDBUNLOCKALLRECORDS(fdb);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Begin the transaction of a fixed-length database object. */
bool tcfdbtranbegin(TCFDB *fdb){
  assert(fdb);
  for(double wsec = 1.0 / sysconf(_SC_CLK_TCK); true; wsec *= 2){
    if(!FDBLOCKMETHOD(fdb, true)) return false;
    if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER) || fdb->fatal){
      tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
      FDBUNLOCKMETHOD(fdb);
      return false;
    }
    if(!fdb->tran) break;
    FDBUNLOCKMETHOD(fdb);
    if(wsec > 1.0) wsec = 1.0;
    tcsleep(wsec);
  }
  if(!tcfdbmemsync(fdb, false)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if((fdb->omode & FDBOTSYNC) && fsync(fdb->fd) == -1){
    tcfdbsetecode(fdb, TCESYNC, __FILE__, __LINE__, __func__);
    return false;
  }
  if(fdb->walfd < 0){
    char *tpath = tcsprintf("%s%c%s", fdb->path, MYEXTCHR, FDBWALSUFFIX);
    int walfd = open(tpath, O_RDWR | O_CREAT | O_TRUNC, FDBFILEMODE);
    TCFREE(tpath);
    if(walfd < 0){
      int ecode = TCEOPEN;
      switch(errno){
        case EACCES: ecode = TCENOPERM; break;
        case ENOENT: ecode = TCENOFILE; break;
        case ENOTDIR: ecode = TCENOFILE; break;
      }
      tcfdbsetecode(fdb, ecode, __FILE__, __LINE__, __func__);
      FDBUNLOCKMETHOD(fdb);
      return false;
    }
    fdb->walfd = walfd;
  }
  tcfdbsetflag(fdb, FDBFOPEN, false);
  if(!tcfdbwalinit(fdb)){
    tcfdbsetflag(fdb, FDBFOPEN, true);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  tcfdbsetflag(fdb, FDBFOPEN, true);
  fdb->tran = true;
  FDBUNLOCKMETHOD(fdb);
  return true;
}


/* Commit the transaction of a fixed-length database object. */
bool tcfdbtrancommit(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER) || fdb->fatal || !fdb->tran){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool err = false;
  if(!tcfdbmemsync(fdb, fdb->omode & FDBOTSYNC)) err = true;
  if(!err && ftruncate(fdb->walfd, 0) == -1){
    tcfdbsetecode(fdb, TCETRUNC, __FILE__, __LINE__, __func__);
    err = true;
  }
  fdb->tran = false;
  FDBUNLOCKMETHOD(fdb);
  return !err;
}


/* Abort the transaction of a fixed-length database object. */
bool tcfdbtranabort(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER) || !fdb->tran){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool err = false;
  if(!tcfdbmemsync(fdb, false)) err = true;
  if(!tcfdbwalrestore(fdb, fdb->path)) err = true;
  char hbuf[FDBHEADSIZ];
  if(lseek(fdb->fd, 0, SEEK_SET) == -1){
    tcfdbsetecode(fdb, TCESEEK, __FILE__, __LINE__, __func__);
    err = false;
  } else if(!tcread(fdb->fd, hbuf, FDBHEADSIZ)){
    tcfdbsetecode(fdb, TCEREAD, __FILE__, __LINE__, __func__);
    err = false;
  } else {
    tcfdbloadmeta(fdb, hbuf);
  }
  fdb->tran = false;
  FDBUNLOCKMETHOD(fdb);
  return !err;
}


/* Get the file path of a fixed-length database object. */
const char *tcfdbpath(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, false)) return NULL;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return NULL;
  }
  const char *rv = fdb->path;
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Get the number of records of a fixed-length database object. */
uint64_t tcfdbrnum(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, false)) return 0;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return 0;
  }
  uint64_t rv = fdb->rnum;
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Get the size of the database file of a fixed-length database object. */
uint64_t tcfdbfsiz(TCFDB *fdb){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, false)) return 0;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return 0;
  }
  uint64_t rv = fdb->fsiz;
  FDBUNLOCKMETHOD(fdb);
  return rv;
}



/*************************************************************************************************
 * features for experts
 *************************************************************************************************/


/* Set the error code of a fixed-length database object. */
void tcfdbsetecode(TCFDB *fdb, int ecode, const char *filename, int line, const char *func){
  assert(fdb && filename && line >= 1 && func);
  int myerrno = errno;
  if(!fdb->fatal){
    fdb->ecode = ecode;
    if(fdb->mmtx) pthread_setspecific(*(pthread_key_t *)fdb->eckey, (void *)(intptr_t)ecode);
  }
  if(ecode != TCEINVALID && ecode != TCEKEEP && ecode != TCENOREC){
    fdb->fatal = true;
    if(fdb->fd >= 0 && (fdb->omode & FDBOWRITER)) tcfdbsetflag(fdb, FDBFFATAL, true);
  }
  if(fdb->dbgfd >= 0 && (fdb->dbgfd != UINT16_MAX || fdb->fatal)){
    int dbgfd = (fdb->dbgfd == UINT16_MAX) ? 1 : fdb->dbgfd;
    char obuf[FDBIOBUFSIZ];
    int osiz = sprintf(obuf, "ERROR:%s:%d:%s:%s:%d:%s:%d:%s\n", filename, line, func,
                       fdb->path ? fdb->path : "-", ecode, tcfdberrmsg(ecode),
                       myerrno, strerror(myerrno));
    tcwrite(dbgfd, obuf, osiz);
  }
}


/* Set the file descriptor for debugging output. */
void tcfdbsetdbgfd(TCFDB *fdb, int fd){
  assert(fdb && fd >= 0);
  fdb->dbgfd = fd;
}


/* Get the file descriptor for debugging output. */
int tcfdbdbgfd(TCFDB *fdb){
  assert(fdb);
  return fdb->dbgfd;
}


/* Check whether mutual exclusion control is set to a fixed-length database object. */
bool tcfdbhasmutex(TCFDB *fdb){
  assert(fdb);
  return fdb->mmtx != NULL;
}


/* Synchronize updating contents on memory of a fixed-length database object. */
bool tcfdbmemsync(TCFDB *fdb, bool phys){
  assert(fdb);
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return false;
  }
  bool err = false;
  char hbuf[FDBHEADSIZ];
  tcfdbdumpmeta(fdb, hbuf);
  memcpy(fdb->map, hbuf, FDBOPAQUEOFF);
  if(phys){
#ifndef __GNU__
    if(msync(fdb->map, fdb->limsiz, MS_SYNC) == -1){
      tcfdbsetecode(fdb, TCEMMAP, __FILE__, __LINE__, __func__);
      err = true;
    }
#endif
    if(fsync(fdb->fd) == -1){
      tcfdbsetecode(fdb, TCESYNC, __FILE__, __LINE__, __func__);
      err = true;
    }
  }
  return !err;
}


/* Get the minimum ID number of records of a fixed-length database object. */
uint64_t tcfdbmin(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->min;
}


/* Get the maximum ID number of records of a fixed-length database object. */
uint64_t tcfdbmax(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->max;
}


/* Get the width of the value of each record of a fixed-length database object. */
uint32_t tcfdbwidth(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->width;
}


/* Get the limit file size of a fixed-length database object. */
uint64_t tcfdblimsiz(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->limsiz;
}


/* Get the limit ID number of a fixed-length database object. */
uint64_t tcfdblimid(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->limid;
}


/* Get the inode number of the database file of a fixed-length database object. */
uint64_t tcfdbinode(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->inode;
}


/* Get the modification time of the database file of a fixed-length database object. */
time_t tcfdbmtime(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->mtime;
}


/* Get the connection mode of a fixed-length database object. */
int tcfdbomode(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->omode;
}


/* Get the database type of a fixed-length database object. */
uint8_t tcfdbtype(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->type;
}


/* Get the additional flags of a fixed-length database object. */
uint8_t tcfdbflags(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return 0;
  }
  return fdb->flags;
}


/* Get the pointer to the opaque field of a fixed-length database object. */
char *tcfdbopaque(TCFDB *fdb){
  assert(fdb);
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    return NULL;
  }
  return fdb->map + FDBOPAQUEOFF;
}


/* Store a record into a fixed-length database object with a duplication handler. */
bool tcfdbputproc(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz, TCPDPROC proc, void *op){
  assert(fdb && proc);
  if(!FDBLOCKMETHOD(fdb, id < 1)) return false;
  if(fdb->fd < 0 || !(fdb->omode & FDBOWRITER)){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDPREV){
    id = fdb->min - 1;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  } else if(id == FDBIDNEXT){
    id = fdb->max + 1;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKRECORD(fdb, true, id)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  FDBPDPROCOP procop;
  procop.proc = proc;
  procop.op = op;
  FDBPDPROCOP *procptr = &procop;
  tcgeneric_t stack[(FDBDEFWIDTH+TCNUMBUFSIZ)/sizeof(tcgeneric_t)+1];
  char *rbuf;
  if(vbuf){
    if(vsiz <= sizeof(stack) - sizeof(procptr)){
      rbuf = (char *)stack;
    } else {
      TCMALLOC(rbuf, vsiz + sizeof(procptr));
    }
    char *wp = rbuf;
    memcpy(wp, &procptr, sizeof(procptr));
    wp += sizeof(procptr);
    memcpy(wp, vbuf, vsiz);
    vbuf = rbuf + sizeof(procptr);
  } else {
    rbuf = (char *)stack;
    memcpy(rbuf, &procptr, sizeof(procptr));
    vbuf = rbuf + sizeof(procptr);
    vsiz = -1;
  }
  bool rv = tcfdbputimpl(fdb, id, vbuf, vsiz, FDBPDPROC);
  if(rbuf != (char *)stack) TCFREE(rbuf);
  FDBUNLOCKRECORD(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Move the iterator to the record corresponding a key of a fixed-length database object. */
bool tcfdbiterinit2(TCFDB *fdb, int64_t id){
  assert(fdb);
  if(!FDBLOCKMETHOD(fdb, true)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(id == FDBIDMIN){
    id = fdb->min;
  } else if(id == FDBIDMAX){
    id = fdb->max;
  }
  if(id < 1 || id > fdb->limid){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  bool rv = tcfdbiterjumpimpl(fdb, id);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Move the iterator to the decimal record of a fixed-length database object. */
bool tcfdbiterinit3(TCFDB *fdb, const void *kbuf, int ksiz){
  assert(fdb && kbuf && ksiz >= 0);
  return tcfdbiterinit2(fdb, tcfdbkeytoid(kbuf, ksiz));
}


/* Move the iterator to the decimal string record of a fixed-length database object. */
bool tcfdbiterinit4(TCFDB *fdb, const char *kstr){
  assert(fdb && kstr);
  return tcfdbiterinit2(fdb, tcfdbkeytoid(kstr, strlen(kstr)));
}


/* Process each record atomically of a fixed-length database object. */
bool tcfdbforeach(TCFDB *fdb, TCITER iter, void *op){
  assert(fdb && iter);
  if(!FDBLOCKMETHOD(fdb, false)) return false;
  if(fdb->fd < 0){
    tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  if(!FDBLOCKALLRECORDS(fdb, false)){
    FDBUNLOCKMETHOD(fdb);
    return false;
  }
  FDBTHREADYIELD(fdb);
  bool rv = tcfdbforeachimpl(fdb, iter, op);
  FDBUNLOCKALLRECORDS(fdb);
  FDBUNLOCKMETHOD(fdb);
  return rv;
}


/* Generate the ID number from arbitrary binary data. */
int64_t tcfdbkeytoid(const char *kbuf, int ksiz){
  assert(kbuf && ksiz >= 0);
  if(ksiz == 3 && !memcmp(kbuf, "min", 3)){
    return FDBIDMIN;
  } else if(ksiz == 4 && !memcmp(kbuf, "prev", 4)){
    return FDBIDPREV;
  } else if(ksiz == 3 && !memcmp(kbuf, "max", 3)){
    return FDBIDMAX;
  } else if(ksiz == 4 && !memcmp(kbuf, "next", 4)){
    return FDBIDNEXT;
  }
  int64_t id = 0;
  const char *end = kbuf + ksiz;
  while(kbuf < end){
    int c = *(unsigned char *)(kbuf++);
    if(c >= '0' && c <= '9') id = id * 10 + c - '0';
  }
  return id;
}



/*************************************************************************************************
 * private features
 *************************************************************************************************/


/* Serialize meta data into a buffer.
   `fdb' specifies the fixed-length database object.
   `hbuf' specifies the buffer. */
static void tcfdbdumpmeta(TCFDB *fdb, char *hbuf){
  memset(hbuf, 0, FDBHEADSIZ);
  sprintf(hbuf, "%s\n%s:%d\n", FDBMAGICDATA, _TC_FORMATVER, _TC_LIBVER);
  memcpy(hbuf + FDBTYPEOFF, &(fdb->type), sizeof(fdb->type));
  memcpy(hbuf + FDBFLAGSOFF, &(fdb->flags), sizeof(fdb->flags));
  uint64_t llnum;
  llnum = fdb->rnum;
  llnum = TCHTOILL(llnum);
  memcpy(hbuf + FDBRNUMOFF, &llnum, sizeof(llnum));
  llnum = fdb->fsiz;
  llnum = TCHTOILL(llnum);
  memcpy(hbuf + FDBFSIZOFF, &llnum, sizeof(llnum));
  llnum = fdb->width;
  llnum = TCHTOILL(llnum);
  memcpy(hbuf + FDBWIDTHOFF, &llnum, sizeof(llnum));
  llnum = fdb->limsiz;
  llnum = TCHTOILL(llnum);
  memcpy(hbuf + FDBLIMSIZOFF, &llnum, sizeof(llnum));
  llnum = fdb->min;
  llnum = TCHTOILL(llnum);
  memcpy(hbuf + FDBMINOFF, &llnum, sizeof(llnum));
  llnum = fdb->max;
  llnum = TCHTOILL(llnum);
  memcpy(hbuf + FDBMAXOFF, &llnum, sizeof(llnum));
}


/* Deserialize meta data from a buffer.
   `fdb' specifies the fixed-length database object.
   `hbuf' specifies the buffer. */
static void tcfdbloadmeta(TCFDB *fdb, const char *hbuf){
  memcpy(&(fdb->type), hbuf + FDBTYPEOFF, sizeof(fdb->type));
  memcpy(&(fdb->flags), hbuf + FDBFLAGSOFF, sizeof(fdb->flags));
  uint64_t llnum;
  memcpy(&llnum, hbuf + FDBRNUMOFF, sizeof(llnum));
  fdb->rnum = TCITOHLL(llnum);
  memcpy(&llnum, hbuf + FDBFSIZOFF, sizeof(llnum));
  fdb->fsiz = TCITOHLL(llnum);
  memcpy(&llnum, hbuf + FDBWIDTHOFF, sizeof(llnum));
  fdb->width = TCITOHLL(llnum);
  memcpy(&llnum, hbuf + FDBLIMSIZOFF, sizeof(llnum));
  fdb->limsiz = TCITOHLL(llnum);
  memcpy(&llnum, hbuf + FDBMINOFF, sizeof(llnum));
  fdb->min = TCITOHLL(llnum);
  memcpy(&llnum, hbuf + FDBMAXOFF, sizeof(llnum));
  fdb->max = TCITOHLL(llnum);
}


/* Clear all members.
   `fdb' specifies the fixed-length database object. */
static void tcfdbclear(TCFDB *fdb){
  assert(fdb);
  fdb->mmtx = NULL;
  fdb->amtx = NULL;
  fdb->rmtxs = NULL;
  fdb->tmtx = NULL;
  fdb->wmtx = NULL;
  fdb->eckey = NULL;
  fdb->rpath = NULL;
  fdb->type = TCDBTFIXED;
  fdb->flags = 0;
  fdb->width = FDBDEFWIDTH;
  fdb->limsiz = FDBDEFLIMSIZ;
  fdb->wsiz = 0;
  fdb->rsiz = 0;
  fdb->limid = 0;
  fdb->path = NULL;
  fdb->fd = -1;
  fdb->omode = 0;
  fdb->rnum = 0;
  fdb->fsiz = 0;
  fdb->min = 0;
  fdb->max = 0;
  fdb->iter = 0;
  fdb->map = NULL;
  fdb->array = NULL;
  fdb->ecode = TCESUCCESS;
  fdb->fatal = false;
  fdb->inode = 0;
  fdb->mtime = 0;
  fdb->tran = false;
  fdb->walfd = -1;
  fdb->walend = 0;
  fdb->dbgfd = -1;
  fdb->cnt_writerec = -1;
  fdb->cnt_readrec = -1;
  fdb->cnt_truncfile = -1;
  TCDODEBUG(fdb->cnt_writerec = 0);
  TCDODEBUG(fdb->cnt_readrec = 0);
  TCDODEBUG(fdb->cnt_truncfile = 0);
}


/* Set the open flag.
   `fdb' specifies the fixed-length database object.
   `flag' specifies the flag value.
   `sign' specifies the sign. */
static void tcfdbsetflag(TCFDB *fdb, int flag, bool sign){
  assert(fdb);
  char *fp = (char *)fdb->map + FDBFLAGSOFF;
  if(sign){
    *fp |= (uint8_t)flag;
  } else {
    *fp &= ~(uint8_t)flag;
  }
  fdb->flags = *fp;
}


/* Initialize the write ahead logging file.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbwalinit(TCFDB *fdb){
  assert(fdb);
  if(lseek(fdb->walfd, 0, SEEK_SET) == -1){
    tcfdbsetecode(fdb, TCESEEK, __FILE__, __LINE__, __func__);
    return false;
  }
  if(ftruncate(fdb->walfd, 0) == -1){
    tcfdbsetecode(fdb, TCETRUNC, __FILE__, __LINE__, __func__);
    return false;
  }
  uint64_t llnum = fdb->fsiz;
  llnum = TCHTOILL(llnum);
  if(!tcwrite(fdb->walfd, &llnum, sizeof(llnum))){
    tcfdbsetecode(fdb, TCEWRITE, __FILE__, __LINE__, __func__);
    return false;
  }
  fdb->walend = fdb->fsiz;
  if(!tcfdbwalwrite(fdb, 0, FDBHEADSIZ)) return false;
  return true;
}


/* Write an event into the write ahead logging file.
   `fdb' specifies the fixed-length database object.
   `off' specifies the offset of the region to be updated.
   `size' specifies the size of the region.
   If successful, the return value is true, else, it is false. */
static bool tcfdbwalwrite(TCFDB *fdb, uint64_t off, int64_t size){
  assert(fdb && off >= 0 && size >= 0);
  if(off + size > fdb->walend) size = fdb->walend - off;
  if(size < 1) return true;
  char stack[FDBIOBUFSIZ];
  char *buf;
  if(size + sizeof(off) + sizeof(size) <= FDBIOBUFSIZ){
    buf = stack;
  } else {
    TCMALLOC(buf, size + sizeof(off) + sizeof(size));
  }
  char *wp = buf;
  uint64_t llnum = TCHTOILL(off);
  memcpy(wp, &llnum, sizeof(llnum));
  wp += sizeof(llnum);
  uint32_t lnum = TCHTOIL(size);
  memcpy(wp, &lnum, sizeof(lnum));
  wp += sizeof(lnum);
  memcpy(wp, fdb->map + off, size);
  wp += size;
  if(!FDBLOCKWAL(fdb)) return false;
  if(!tcwrite(fdb->walfd, buf, wp - buf)){
    tcfdbsetecode(fdb, TCEWRITE, __FILE__, __LINE__, __func__);
    if(buf != stack) TCFREE(buf);
    FDBUNLOCKWAL(fdb);
    return false;
  }
  if(buf != stack) TCFREE(buf);
  if((fdb->omode & FDBOTSYNC) && fsync(fdb->walfd) == -1){
    tcfdbsetecode(fdb, TCESYNC, __FILE__, __LINE__, __func__);
    FDBUNLOCKWAL(fdb);
    return false;
  }
  FDBUNLOCKWAL(fdb);
  return true;
}


/* Restore the database from the write ahead logging file.
   `fdb' specifies the fixed-length database object.
   `path' specifies the path of the database file.
   If successful, the return value is true, else, it is false. */
static int tcfdbwalrestore(TCFDB *fdb, const char *path){
  assert(fdb && path);
  char *tpath = tcsprintf("%s%c%s", path, MYEXTCHR, FDBWALSUFFIX);
  int walfd = open(tpath, O_RDONLY, FDBFILEMODE);
  TCFREE(tpath);
  if(walfd < 0) return false;
  bool err = false;
  uint64_t walsiz = 0;
  struct stat sbuf;
  if(fstat(walfd, &sbuf) == 0){
    walsiz = sbuf.st_size;
  } else {
    tcfdbsetecode(fdb, TCESTAT, __FILE__, __LINE__, __func__);
    err = true;
  }
  if(walsiz >= sizeof(walsiz) + FDBHEADSIZ){
    int dbfd = fdb->fd;
    int tfd = -1;
    if(!(fdb->omode & FDBOWRITER)){
      tfd = open(path, O_WRONLY, FDBFILEMODE);
      if(tfd >= 0){
        dbfd = tfd;
      } else {
        int ecode = TCEOPEN;
        switch(errno){
          case EACCES: ecode = TCENOPERM; break;
          case ENOENT: ecode = TCENOFILE; break;
          case ENOTDIR: ecode = TCENOFILE; break;
        }
        tcfdbsetecode(fdb, ecode, __FILE__, __LINE__, __func__);
        err = true;
      }
    }
    uint64_t fsiz = 0;
    if(tcread(walfd, &fsiz, sizeof(fsiz))){
      fsiz = TCITOHLL(fsiz);
    } else {
      tcfdbsetecode(fdb, TCEREAD, __FILE__, __LINE__, __func__);
      err = true;
    }
    TCLIST *list = tclistnew();
    uint64_t waloff = sizeof(fsiz);
    char stack[FDBIOBUFSIZ];
    while(waloff < walsiz){
      uint64_t off;
      uint32_t size;
      if(!tcread(walfd, stack, sizeof(off) + sizeof(size))){
        tcfdbsetecode(fdb, TCEREAD, __FILE__, __LINE__, __func__);
        err = true;
        break;
      }
      memcpy(&off, stack, sizeof(off));
      off = TCITOHLL(off);
      memcpy(&size, stack + sizeof(off), sizeof(size));
      size = TCITOHL(size);
      char *buf;
      if(sizeof(off) + size <= FDBIOBUFSIZ){
        buf = stack;
      } else {
        TCMALLOC(buf, sizeof(off) + size);
      }
      *(uint64_t *)buf = off;
      if(!tcread(walfd, buf + sizeof(off), size)){
        tcfdbsetecode(fdb, TCEREAD, __FILE__, __LINE__, __func__);
        err = true;
        if(buf != stack) TCFREE(buf);
        break;
      }
      TCLISTPUSH(list, buf, sizeof(off) + size);
      if(buf != stack) TCFREE(buf);
      waloff += sizeof(off) + sizeof(size) + size;
    }
    for(int i = TCLISTNUM(list) - 1; i >= 0; i--){
      const char *rec;
      int size;
      TCLISTVAL(rec, list, i, size);
      uint64_t off = *(uint64_t *)rec;
      rec += sizeof(off);
      size -= sizeof(off);
      if(lseek(dbfd, off, SEEK_SET) == -1){
        tcfdbsetecode(fdb, TCESEEK, __FILE__, __LINE__, __func__);
        err = true;
        break;
      }
      if(!tcwrite(dbfd, rec, size)){
        tcfdbsetecode(fdb, TCEWRITE, __FILE__, __LINE__, __func__);
        err = true;
        break;
      }
    }
    tclistdel(list);
    if(ftruncate(dbfd, fsiz) == -1){
      tcfdbsetecode(fdb, TCETRUNC, __FILE__, __LINE__, __func__);
      err = true;
    }
    if((fdb->omode & FDBOTSYNC) && fsync(dbfd) == -1){
      tcfdbsetecode(fdb, TCESYNC, __FILE__, __LINE__, __func__);
      err = true;
    }
    if(tfd >= 0 && close(tfd) == -1){
      tcfdbsetecode(fdb, TCECLOSE, __FILE__, __LINE__, __func__);
      err = true;
    }
  } else {
    err = true;
  }
  if(close(walfd) == -1){
    tcfdbsetecode(fdb, TCECLOSE, __FILE__, __LINE__, __func__);
    err = true;
  }
  return !err;
}


/* Remove the write ahead logging file.
   `fdb' specifies the fixed-length database object.
   `path' specifies the path of the database file.
   If successful, the return value is true, else, it is false. */
static bool tcfdbwalremove(TCFDB *fdb, const char *path){
  assert(fdb && path);
  char *tpath = tcsprintf("%s%c%s", path, MYEXTCHR, FDBWALSUFFIX);
  bool err = false;
  if(unlink(tpath) == -1 && errno != ENOENT){
    tcfdbsetecode(fdb, TCEUNLINK, __FILE__, __LINE__, __func__);
    err = true;
  }
  TCFREE(tpath);
  return !err;
}


/* Open a database file and connect a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `path' specifies the path of the database file.
   `omode' specifies the connection mode.
   If successful, the return value is true, else, it is false. */
static bool tcfdbopenimpl(TCFDB *fdb, const char *path, int omode){
  assert(fdb && path);
  int mode = O_RDONLY;
  if(omode & FDBOWRITER){
    mode = O_RDWR;
    if(omode & FDBOCREAT) mode |= O_CREAT;
  }
  int fd = open(path, mode, FDBFILEMODE);
  if(fd < 0){
    int ecode = TCEOPEN;
    switch(errno){
      case EACCES: ecode = TCENOPERM; break;
      case ENOENT: ecode = TCENOFILE; break;
      case ENOTDIR: ecode = TCENOFILE; break;
    }
    tcfdbsetecode(fdb, ecode, __FILE__, __LINE__, __func__);
    return false;
  }
  if(!(omode & FDBONOLCK)){
    if(!tclock(fd, omode & FDBOWRITER, omode & FDBOLCKNB)){
      tcfdbsetecode(fdb, TCELOCK, __FILE__, __LINE__, __func__);
      close(fd);
      return false;
    }
  }
  if((omode & FDBOWRITER) && (omode & FDBOTRUNC)){
    if(ftruncate(fd, 0) == -1){
      tcfdbsetecode(fdb, TCETRUNC, __FILE__, __LINE__, __func__);
      close(fd);
      return false;
    }
    if(!tcfdbwalremove(fdb, path)){
      close(fd);
      return false;
    }
  }
  struct stat sbuf;
  if(fstat(fd, &sbuf) == -1 || !S_ISREG(sbuf.st_mode)){
    tcfdbsetecode(fdb, TCESTAT, __FILE__, __LINE__, __func__);
    close(fd);
    return false;
  }
  char hbuf[FDBHEADSIZ];
  if((omode & FDBOWRITER) && sbuf.st_size < 1){
    fdb->flags = 0;
    fdb->rnum = 0;
    fdb->fsiz = FDBHEADSIZ;
    fdb->min = 0;
    fdb->max = 0;
    tcfdbdumpmeta(fdb, hbuf);
    if(!tcwrite(fd, hbuf, FDBHEADSIZ)){
      tcfdbsetecode(fdb, TCEWRITE, __FILE__, __LINE__, __func__);
      close(fd);
      return false;
    }
    sbuf.st_size = fdb->fsiz;
  }
  if(lseek(fd, 0, SEEK_SET) == -1){
    tcfdbsetecode(fdb, TCESEEK, __FILE__, __LINE__, __func__);
    close(fd);
    return false;
  }
  if(!tcread(fd, hbuf, FDBHEADSIZ)){
    tcfdbsetecode(fdb, TCEREAD, __FILE__, __LINE__, __func__);
    close(fd);
    return false;
  }
  int type = fdb->type;
  tcfdbloadmeta(fdb, hbuf);
  if((fdb->flags & FDBFOPEN) && tcfdbwalrestore(fdb, path)){
    if(lseek(fd, 0, SEEK_SET) == -1){
      tcfdbsetecode(fdb, TCESEEK, __FILE__, __LINE__, __func__);
      close(fd);
      return false;
    }
    if(!tcread(fd, hbuf, FDBHEADSIZ)){
      tcfdbsetecode(fdb, TCEREAD, __FILE__, __LINE__, __func__);
      close(fd);
      return false;
    }
    tcfdbloadmeta(fdb, hbuf);
    if(!tcfdbwalremove(fdb, path)){
      close(fd);
      return false;
    }
  }
  if(!(omode & FDBONOLCK)){
    if(memcmp(hbuf, FDBMAGICDATA, strlen(FDBMAGICDATA)) || fdb->type != type ||
       fdb->width < 1 || sbuf.st_size < fdb->fsiz || fdb->limsiz < FDBHEADSIZ ||
       fdb->fsiz > fdb->limsiz){
      tcfdbsetecode(fdb, TCEMETA, __FILE__, __LINE__, __func__);
      close(fd);
      return false;
    }
    if(sbuf.st_size > fdb->fsiz) fdb->fsiz = sbuf.st_size;
  }
  void *map = mmap(0, fdb->limsiz, PROT_READ | ((omode & FDBOWRITER) ? PROT_WRITE : 0),
                   MAP_SHARED, fd, 0);
  if(map == MAP_FAILED){
    tcfdbsetecode(fdb, TCEMMAP, __FILE__, __LINE__, __func__);
    close(fd);
    return false;
  }
  if(fdb->width <= UINT8_MAX){
    fdb->wsiz = sizeof(uint8_t);
  } else if(fdb->width <= UINT16_MAX){
    fdb->wsiz = sizeof(uint16_t);
  } else {
    fdb->wsiz = sizeof(uint32_t);
  }
  fdb->rsiz = fdb->width + fdb->wsiz;
  fdb->limid = (fdb->limsiz - FDBHEADSIZ) / fdb->rsiz;
  fdb->path = tcstrdup(path);
  fdb->fd = fd;
  fdb->omode = omode;
  fdb->iter = 0;
  fdb->map = map;
  fdb->array = (unsigned char *)map + FDBHEADSIZ;
  fdb->ecode = TCESUCCESS;
  fdb->fatal = false;
  fdb->inode = (uint64_t)sbuf.st_ino;
  fdb->mtime = sbuf.st_mtime;
  fdb->tran = false;
  fdb->walfd = -1;
  fdb->walend = 0;
  if(fdb->omode & FDBOWRITER) tcfdbsetflag(fdb, FDBFOPEN, true);
  return true;
}


/* Close a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbcloseimpl(TCFDB *fdb){
  assert(fdb);
  bool err = false;
  if(fdb->omode & FDBOWRITER) tcfdbsetflag(fdb, FDBFOPEN, false);
  if((fdb->omode & FDBOWRITER) && !tcfdbmemsync(fdb, false)) err = true;
  if(munmap(fdb->map, fdb->limsiz) == -1){
    tcfdbsetecode(fdb, TCEMMAP, __FILE__, __LINE__, __func__);
    err = true;
  }
  if(fdb->tran){
    if(!tcfdbwalrestore(fdb, fdb->path)) err = true;
    fdb->tran = false;
  }
  if(fdb->walfd >= 0){
    if(close(fdb->walfd) == -1){
      tcfdbsetecode(fdb, TCECLOSE, __FILE__, __LINE__, __func__);
      err = true;
    }
    if(!fdb->fatal && !tcfdbwalremove(fdb, fdb->path)) err = true;
  }
  if(close(fdb->fd) == -1){
    tcfdbsetecode(fdb, TCECLOSE, __FILE__, __LINE__, __func__);
    err = true;
  }
  TCFREE(fdb->path);
  fdb->path = NULL;
  fdb->fd = -1;
  return !err;
}


/* Get the previous record of a record.
   `fdb' specifies the fixed-length database object.
   `id' specifies the ID number.
   The return value is the ID number of the previous record or 0 if no record corresponds. */
static int64_t tcfdbprevid(TCFDB *fdb, int64_t id){
  assert(fdb && id >= 0);
  id--;
  while(id >= fdb->min){
    TCDODEBUG(fdb->cnt_readrec++);
    unsigned char *rec = fdb->array + (id - 1) * (fdb->rsiz);
    unsigned char *rp = rec;
    uint32_t osiz;
    uint16_t snum;
    uint32_t lnum;
    switch(fdb->wsiz){
      case 1:
        osiz = *(rp++);
        break;
      case 2:
        memcpy(&snum, rp, sizeof(snum));
        osiz = TCITOHS(snum);
        rp += sizeof(snum);
        break;
      default:
        memcpy(&lnum, rp, sizeof(lnum));
        osiz = TCITOHL(lnum);
        rp += sizeof(lnum);
        break;
    }
    if(osiz > 0 || *rp != 0) return id;
    id--;
  }
  return 0;
}


/* Get the next record of a record.
   `fdb' specifies the fixed-length database object.
   `id' specifies the ID number.
   The return value is the ID number of the next record or 0 if no record corresponds. */
static int64_t tcfdbnextid(TCFDB *fdb, int64_t id){
  assert(fdb && id >= 0);
  id++;
  while(id <= fdb->max){
    TCDODEBUG(fdb->cnt_readrec++);
    unsigned char *rec = fdb->array + (id - 1) * (fdb->rsiz);
    unsigned char *rp = rec;
    uint32_t osiz;
    uint16_t snum;
    uint32_t lnum;
    switch(fdb->wsiz){
      case 1:
        osiz = *(rp++);
        break;
      case 2:
        memcpy(&snum, rp, sizeof(snum));
        osiz = TCITOHS(snum);
        rp += sizeof(snum);
        break;
      default:
        memcpy(&lnum, rp, sizeof(lnum));
        osiz = TCITOHL(lnum);
        rp += sizeof(lnum);
        break;
    }
    if(osiz > 0 || *rp != 0) return id;
    id++;
  }
  return 0;
}


/* Store a record.
   `fdb' specifies the fixed-length database object.
   `id' specifies the ID number.
   `vbuf' specifies the pointer to the region of the value.
   `vsiz' specifies the size of the region of the value.
   `dmode' specifies behavior when the key overlaps.
   If successful, the return value is true, else, it is false. */
static bool tcfdbputimpl(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz, int dmode){
  assert(fdb && id > 0);
  if(vsiz > (int64_t)fdb->width) vsiz = fdb->width;
  TCDODEBUG(fdb->cnt_readrec++);
  unsigned char *rec = fdb->array + (id - 1) * (fdb->rsiz);
  uint64_t nsiz = FDBHEADSIZ + id * fdb->rsiz;
  if(nsiz > fdb->fsiz){
    if(nsiz > fdb->limsiz){
      tcfdbsetecode(fdb, TCEINVALID, __FILE__, __LINE__, __func__);
      return false;
    }
    if(!FDBLOCKATTR(fdb)) return false;
    if(nsiz > fdb->fsiz){
      if(vsiz < 0){
        tcfdbsetecode(fdb, TCENOREC, __FILE__, __LINE__, __func__);
        FDBUNLOCKATTR(fdb);
        return false;
      }
      if(nsiz + fdb->rsiz * FDBTRUNCALW < fdb->limsiz) nsiz += fdb->rsiz * FDBTRUNCALW;
      if(ftruncate(fdb->fd, nsiz) == -1){
        tcfdbsetecode(fdb, TCETRUNC, __FILE__, __LINE__, __func__);
        FDBUNLOCKATTR(fdb);
        return false;
      }
      TCDODEBUG(fdb->cnt_truncfile++);
      fdb->fsiz = nsiz;
      unsigned char *wp = rec;
      uint16_t snum;
      uint32_t lnum;
      switch(fdb->wsiz){
        case 1:
          *(wp++) = vsiz;
          break;
        case 2:
          snum = TCHTOIS(vsiz);
          memcpy(wp, &snum, sizeof(snum));
          wp += sizeof(snum);
          break;
        default:
          lnum = TCHTOIL(vsiz);
          memcpy(wp, &lnum, sizeof(lnum));
          wp += sizeof(lnum);
          break;
      }
      if(vsiz > 0){
        memcpy(wp, vbuf, vsiz);
      } else {
        *wp = 1;
      }
      TCDODEBUG(fdb->cnt_writerec++);
      fdb->rnum++;
      if(fdb->min < 1 || id < fdb->min) fdb->min = id;
      if(fdb->max < 1 || id > fdb->max) fdb->max = id;
      FDBUNLOCKATTR(fdb);
      return true;
    }
    FDBUNLOCKATTR(fdb);
  }
  unsigned char *rp = rec;
  uint32_t osiz;
  uint16_t snum;
  uint32_t lnum;
  switch(fdb->wsiz){
    case 1:
      osiz = *(rp++);
      break;
    case 2:
      memcpy(&snum, rp, sizeof(snum));
      osiz = TCITOHS(snum);
      rp += sizeof(snum);
      break;
    default:
      memcpy(&lnum, rp, sizeof(lnum));
      osiz = TCITOHL(lnum);
      rp += sizeof(lnum);
      break;
  }
  bool miss = osiz == 0 && *rp == 0;
  if(dmode != FDBPDOVER && !miss){
    if(dmode == FDBPDKEEP){
      tcfdbsetecode(fdb, TCEKEEP, __FILE__, __LINE__, __func__);
      return false;
    }
    if(dmode == FDBPDCAT){
      if(fdb->tran && !tcfdbwalwrite(fdb, (char *)rec - fdb->map, fdb->width)) return false;
      vsiz = tclmin(vsiz, fdb->width - osiz);
      unsigned char *wp = rec;
      int usiz = osiz + vsiz;
      switch(fdb->wsiz){
        case 1:
          *(wp++) = usiz;
          break;
        case 2:
          snum = TCHTOIS(usiz);
          memcpy(wp, &snum, sizeof(snum));
          wp += sizeof(snum);
          break;
        default:
          lnum = TCHTOIL(usiz);
          memcpy(wp, &lnum, sizeof(lnum));
          wp += sizeof(lnum);
          break;
      }
      if(usiz > 0){
        memcpy(wp + osiz, vbuf, vsiz);
      } else {
        *wp = 1;
      }
      TCDODEBUG(fdb->cnt_writerec++);
      return true;
    }
    if(dmode == FDBPDADDINT){
      if(osiz != sizeof(int)){
        tcfdbsetecode(fdb, TCEKEEP, __FILE__, __LINE__, __func__);
        return false;
      }
      int lnum;
      memcpy(&lnum, rp, sizeof(lnum));
      if(*(int *)vbuf == 0){
        *(int *)vbuf = lnum;
        return true;
      }
      if(fdb->tran && !tcfdbwalwrite(fdb, (char *)rec - fdb->map, fdb->width)) return false;
      lnum += *(int *)vbuf;
      *(int *)vbuf = lnum;
      memcpy(rp, &lnum, sizeof(lnum));
      TCDODEBUG(fdb->cnt_writerec++);
      return true;
    }
    if(dmode == FDBPDADDDBL){
      if(osiz != sizeof(double)){
        tcfdbsetecode(fdb, TCEKEEP, __FILE__, __LINE__, __func__);
        return false;
      }
      double dnum;
      memcpy(&dnum, rp, sizeof(dnum));
      if(*(double *)vbuf == 0.0){
        *(double *)vbuf = dnum;
        return true;
      }
      if(fdb->tran && !tcfdbwalwrite(fdb, (char *)rec - fdb->map, fdb->width)) return false;
      dnum += *(double *)vbuf;
      *(double *)vbuf = dnum;
      memcpy(rp, &dnum, sizeof(dnum));
      TCDODEBUG(fdb->cnt_writerec++);
      return true;
    }
    if(dmode == FDBPDPROC){
      FDBPDPROCOP *procptr = *(FDBPDPROCOP **)((char *)vbuf - sizeof(procptr));
      int nvsiz;
      char *nvbuf = procptr->proc(rp, osiz, &nvsiz, procptr->op);
      if(nvbuf == (void *)-1){
        if(fdb->tran && !tcfdbwalwrite(fdb, (char *)rec - fdb->map, fdb->width)) return false;
        memset(rec, 0, fdb->wsiz + 1);
        TCDODEBUG(fdb->cnt_writerec++);
        if(!FDBLOCKATTR(fdb)) return false;
        fdb->rnum--;
        if(fdb->rnum < 1){
          fdb->min = 0;
          fdb->max = 0;
        } else if(fdb->rnum < 2){
          if(fdb->min == id){
            fdb->min = fdb->max;
          } else if(fdb->max == id){
            fdb->max = fdb->min;
          }
        } else {
          if(id == fdb->min) fdb->min = tcfdbnextid(fdb, id);
          if(id == fdb->max) fdb->max = tcfdbprevid(fdb, id);
        }
        FDBUNLOCKATTR(fdb);
        return true;
      }
      if(!nvbuf){
        tcfdbsetecode(fdb, TCEKEEP, __FILE__, __LINE__, __func__);
        return false;
      }
      if(fdb->tran && !tcfdbwalwrite(fdb, (char *)rec - fdb->map, fdb->width)) return false;
      if(nvsiz > fdb->width) nvsiz = fdb->width;
      unsigned char *wp = rec;
      switch(fdb->wsiz){
        case 1:
          *(wp++) = nvsiz;
          break;
        case 2:
          snum = TCHTOIS(nvsiz);
          memcpy(wp, &snum, sizeof(snum));
          wp += sizeof(snum);
          break;
        default:
          lnum = TCHTOIL(nvsiz);
          memcpy(wp, &lnum, sizeof(lnum));
          wp += sizeof(lnum);
          break;
      }
      if(nvsiz > 0){
        memcpy(wp, nvbuf, nvsiz);
      } else {
        *wp = 1;
      }
      TCFREE(nvbuf);
      TCDODEBUG(fdb->cnt_writerec++);
      return true;
    }
  }
  if(vsiz < 0){
    tcfdbsetecode(fdb, TCENOREC, __FILE__, __LINE__, __func__);
    return false;
  }
  if(fdb->tran && !tcfdbwalwrite(fdb, (char *)rec - fdb->map, fdb->width)) return false;
  unsigned char *wp = rec;
  switch(fdb->wsiz){
    case 1:
      *(wp++) = vsiz;
      break;
    case 2:
      snum = TCHTOIS(vsiz);
      memcpy(wp, &snum, sizeof(snum));
      wp += sizeof(snum);
      break;
    default:
      lnum = TCHTOIL(vsiz);
      memcpy(wp, &lnum, sizeof(lnum));
      wp += sizeof(lnum);
      break;
  }
  if(vsiz > 0){
    memcpy(wp, vbuf, vsiz);
  } else {
    *wp = 1;
  }
  TCDODEBUG(fdb->cnt_writerec++);
  if(miss){
    if(!FDBLOCKATTR(fdb)) return false;
    fdb->rnum++;
    if(fdb->min < 1 || id < fdb->min) fdb->min = id;
    if(fdb->max < 1 || id > fdb->max) fdb->max = id;
    FDBUNLOCKATTR(fdb);
  }
  return true;
}


/* Remove a record of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `id' specifies the ID number.
   If successful, the return value is true, else, it is false. */
static bool tcfdboutimpl(TCFDB *fdb, int64_t id){
  assert(fdb && id >= 0);
  TCDODEBUG(fdb->cnt_readrec++);
  unsigned char *rec = fdb->array + (id - 1) * (fdb->rsiz);
  uint64_t nsiz = FDBHEADSIZ + id * fdb->rsiz;
  if(nsiz > fdb->fsiz){
    tcfdbsetecode(fdb, TCENOREC, __FILE__, __LINE__, __func__);
    return false;
  }
  unsigned char *rp = rec;
  uint32_t osiz;
  uint16_t snum;
  uint32_t lnum;
  switch(fdb->wsiz){
    case 1:
      osiz = *(rp++);
      break;
    case 2:
      memcpy(&snum, rp, sizeof(snum));
      osiz = TCITOHS(snum);
      rp += sizeof(snum);
      break;
    default:
      memcpy(&lnum, rp, sizeof(lnum));
      osiz = TCITOHL(lnum);
      rp += sizeof(lnum);
      break;
  }
  if(osiz == 0 && *rp == 0){
    tcfdbsetecode(fdb, TCENOREC, __FILE__, __LINE__, __func__);
    return false;
  }
  if(fdb->tran && !tcfdbwalwrite(fdb, (char *)rec - fdb->map, fdb->width)) return false;
  memset(rec, 0, fdb->wsiz + 1);
  TCDODEBUG(fdb->cnt_writerec++);
  if(!FDBLOCKATTR(fdb)) return false;
  fdb->rnum--;
  if(fdb->rnum < 1){
    fdb->min = 0;
    fdb->max = 0;
  } else if(fdb->rnum < 2){
    if(fdb->min == id){
      fdb->min = fdb->max;
    } else if(fdb->max == id){
      fdb->max = fdb->min;
    }
  } else {
    if(id == fdb->min) fdb->min = tcfdbnextid(fdb, id);
    if(id == fdb->max) fdb->max = tcfdbprevid(fdb, id);
  }
  FDBUNLOCKATTR(fdb);
  return true;
}


/* Retrieve a record.
   `fdb' specifies the fixed-length database object.
   `id' specifies the ID number.
   `sp' specifies the pointer to the variable into which the size of the region of the return
   value is assigned.
   If successful, the return value is the pointer to the region of the value of the corresponding
   record. */
static const void *tcfdbgetimpl(TCFDB *fdb, int64_t id, int *sp){
  assert(fdb && id >= 0 && sp);
  TCDODEBUG(fdb->cnt_readrec++);
  unsigned char *rec = fdb->array + (id - 1) * (fdb->rsiz);
  uint64_t nsiz = FDBHEADSIZ + id * fdb->rsiz;
  if(nsiz > fdb->fsiz){
    tcfdbsetecode(fdb, TCENOREC, __FILE__, __LINE__, __func__);
    return false;
  }
  unsigned char *rp = rec;
  uint32_t osiz;
  uint16_t snum;
  uint32_t lnum;
  switch(fdb->wsiz){
    case 1:
      osiz = *(rp++);
      break;
    case 2:
      memcpy(&snum, rp, sizeof(snum));
      osiz = TCITOHS(snum);
      rp += sizeof(snum);
      break;
    default:
      memcpy(&lnum, rp, sizeof(lnum));
      osiz = TCITOHL(lnum);
      rp += sizeof(lnum);
      break;
  }
  if(osiz == 0 && *rp == 0){
    tcfdbsetecode(fdb, TCENOREC, __FILE__, __LINE__, __func__);
    return false;
  }
  *sp = osiz;
  return rp;
}


/* Initialize the iterator of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbiterinitimpl(TCFDB *fdb){
  assert(fdb);
  fdb->iter = fdb->min;
  return true;
}


/* Get the next key of the iterator of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is the next ID number of the iterator, else, it is 0. */
static uint64_t tcfdbiternextimpl(TCFDB *fdb){
  assert(fdb);
  if(fdb->iter < 1){
    tcfdbsetecode(fdb, TCENOREC, __FILE__, __LINE__, __func__);
    return 0;
  }
  uint64_t cur = fdb->iter;
  fdb->iter = tcfdbnextid(fdb, fdb->iter);
  return cur;
}


/* Get range matching ID numbers in a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `lower' specifies the lower limit of the range.
   `upper' specifies the upper limit of the range.
   `max' specifies the maximum number of keys to be fetched.
   `np' specifies the pointer to the variable into which the number of elements of the return
   value is assigned.
   If successful, the return value is the pointer to an array of ID numbers of the corresponding
   records. */
static uint64_t *tcfdbrangeimpl(TCFDB *fdb, int64_t lower, int64_t upper, int max, int *np){
  assert(fdb && lower > 0 && upper > 0 && np);
  if(lower < fdb->min) lower = fdb->min;
  if(upper > fdb->max) upper = fdb->max;
  if(max < 0) max = INT_MAX;
  int anum = FDBIDARYUNIT;
  uint64_t *ids;
  TCMALLOC(ids, anum * sizeof(*ids));
  int num = 0;
  for(int64_t i = lower; i <= upper && num < max; i++){
    int vsiz;
    const void *vbuf = tcfdbgetimpl(fdb, i, &vsiz);
    if(vbuf){
      if(num >= anum){
        anum *= 2;
        TCREALLOC(ids, ids, anum * sizeof(*ids));
      }
      ids[num++] = i;
    }
  }
  *np = num;
  return ids;
}


/* Optimize the file of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `width' specifies the width of the value of each record.
   `limsiz' specifies the limit size of the database file.
   If successful, the return value is true, else, it is false. */
static bool tcfdboptimizeimpl(TCFDB *fdb, int32_t width, int64_t limsiz){
  assert(fdb);
  char *tpath = tcsprintf("%s%ctmp%c%llu", fdb->path, MYEXTCHR, MYEXTCHR, fdb->inode);
  TCFDB *tfdb = tcfdbnew();
  tfdb->dbgfd = fdb->dbgfd;
  if(width < 1) width = fdb->width;
  if(limsiz < 1) limsiz = fdb->limsiz;
  tcfdbtune(tfdb, width, limsiz);
  if(!tcfdbopen(tfdb, tpath, FDBOWRITER | FDBOCREAT | FDBOTRUNC)){
    tcfdbsetecode(fdb, tfdb->ecode, __FILE__, __LINE__, __func__);
    tcfdbdel(tfdb);
    TCFREE(tpath);
    return false;
  }
  bool err = false;
  int64_t max = fdb->max;
  for(int i = fdb->min; !err && i <= max; i++){
    int vsiz;
    const void *vbuf = tcfdbgetimpl(fdb, i, &vsiz);
    if(vbuf && !tcfdbput(tfdb, i, vbuf, vsiz)){
      tcfdbsetecode(fdb, tfdb->ecode, __FILE__, __LINE__, __func__);
      err = true;
    }
  }
  if(!tcfdbclose(tfdb)){
    tcfdbsetecode(fdb, tfdb->ecode, __FILE__, __LINE__, __func__);
    err = true;
  }
  tcfdbdel(tfdb);
  if(unlink(fdb->path) == -1){
    tcfdbsetecode(fdb, TCEUNLINK, __FILE__, __LINE__, __func__);
    err = true;
  }
  if(rename(tpath, fdb->path) == -1){
    tcfdbsetecode(fdb, TCERENAME, __FILE__, __LINE__, __func__);
    err = true;
  }
  TCFREE(tpath);
  if(err) return false;
  tpath = tcstrdup(fdb->path);
  int omode = (fdb->omode & ~FDBOCREAT) & ~FDBOTRUNC;
  if(!tcfdbcloseimpl(fdb)){
    TCFREE(tpath);
    return false;
  }
  bool rv = tcfdbopenimpl(fdb, tpath, omode);
  TCFREE(tpath);
  return rv;
}


/* Remove all records of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbvanishimpl(TCFDB *fdb){
  assert(fdb);
  char *path = tcstrdup(fdb->path);
  int omode = fdb->omode;
  bool err = false;
  if(!tcfdbcloseimpl(fdb)) err = true;
  if(!tcfdbopenimpl(fdb, path, FDBOTRUNC | omode)){
    tcpathunlock(fdb->rpath);
    TCFREE(fdb->rpath);
    err = true;
  }
  TCFREE(path);
  return !err;
}


/* Copy the database file of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `path' specifies the path of the destination file.
   If successful, the return value is true, else, it is false. */
static bool tcfdbcopyimpl(TCFDB *fdb, const char *path){
  assert(fdb && path);
  bool err = false;
  if(fdb->omode & FDBOWRITER){
    if(!tcfdbmemsync(fdb, false)) err = true;
    tcfdbsetflag(fdb, FDBFOPEN, false);
  }
  if(*path == '@'){
    char tsbuf[TCNUMBUFSIZ];
    sprintf(tsbuf, "%llu", (unsigned long long)(tctime() * 1000000));
    const char *args[3];
    args[0] = path + 1;
    args[1] = fdb->path;
    args[2] = tsbuf;
    if(tcsystem(args, sizeof(args) / sizeof(*args)) != 0) err = true;
  } else {
    if(!tccopyfile(fdb->path, path)){
      tcfdbsetecode(fdb, TCEMISC, __FILE__, __LINE__, __func__);
      err = true;
    }
  }
  if(fdb->omode & FDBOWRITER) tcfdbsetflag(fdb, FDBFOPEN, true);
  return !err;
}


/* Move the iterator to the record corresponding a key of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `id' specifies the ID number.
   If successful, the return value is true, else, it is false. */
static bool tcfdbiterjumpimpl(TCFDB *fdb, int64_t id){
  assert(fdb && id >= 0);
  if(id <= fdb->min){
    fdb->iter = fdb->min;
  } else {
    int vsiz;
    if(tcfdbgetimpl(fdb, id, &vsiz)){
      fdb->iter = id;
    } else {
      uint64_t iter = tcfdbnextid(fdb, id);
      if(iter > 0){
        fdb->iter = iter;
      } else {
        return false;
      }
    }
  }
  return true;
}


/* Process each record atomically of a fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `iter' specifies the pointer to the iterator function called for each record.
   `op' specifies an arbitrary pointer to be given as a parameter of the iterator function.
   If successful, the return value is true, else, it is false. */
static bool tcfdbforeachimpl(TCFDB *fdb, TCITER iter, void *op){
  bool err = false;
  uint64_t id = fdb->min;
  while(id > 0){
    int vsiz;
    const void *vbuf = tcfdbgetimpl(fdb, id, &vsiz);
    if(vbuf){
      char kbuf[TCNUMBUFSIZ];
      int ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
      if(!iter(kbuf, ksiz, vbuf, vsiz, op)) break;
    } else {
      tcfdbsetecode(fdb, TCEMISC, __FILE__, __LINE__, __func__);
      err = true;
    }
    id = tcfdbnextid(fdb, id);
  }
  return !err;
}


/* Lock a method of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `wr' specifies whether the lock is writer or not.
   If successful, the return value is true, else, it is false. */
static bool tcfdblockmethod(TCFDB *fdb, bool wr){
  assert(fdb);
  if(wr ? pthread_rwlock_wrlock(fdb->mmtx) != 0 : pthread_rwlock_rdlock(fdb->mmtx) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}


/* Unlock a method of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbunlockmethod(TCFDB *fdb){
  assert(fdb);
  if(pthread_rwlock_unlock(fdb->mmtx) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}


/* Lock the attributes of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdblockattr(TCFDB *fdb){
  assert(fdb);
  if(pthread_mutex_lock(fdb->amtx) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}


/* Unlock the attributes of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbunlockattr(TCFDB *fdb){
  assert(fdb);
  if(pthread_mutex_unlock(fdb->amtx) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}


/* Lock a record of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `wr' specifies whether the lock is writer or not.
   If successful, the return value is true, else, it is false. */
static bool tcfdblockrecord(TCFDB *fdb, bool wr, uint64_t id){
  assert(fdb && id > 0);
  if(wr ? pthread_rwlock_wrlock((pthread_rwlock_t *)fdb->rmtxs + id % FDBRMTXNUM) != 0 :
     pthread_rwlock_rdlock((pthread_rwlock_t *)fdb->rmtxs + id % FDBRMTXNUM) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}


/* Unlock a record of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbunlockrecord(TCFDB *fdb, uint64_t id){
  assert(fdb);
  if(pthread_rwlock_unlock((pthread_rwlock_t *)fdb->rmtxs + id % FDBRMTXNUM) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}


/* Lock all records of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   `wr' specifies whether the lock is writer or not.
   If successful, the return value is true, else, it is false. */
static bool tcfdblockallrecords(TCFDB *fdb, bool wr){
  assert(fdb);
  for(int i = 0; i < FDBRMTXNUM; i++){
    if(wr ? pthread_rwlock_wrlock((pthread_rwlock_t *)fdb->rmtxs + i) != 0 :
       pthread_rwlock_rdlock((pthread_rwlock_t *)fdb->rmtxs + i) != 0){
      tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
      while(--i >= 0){
        pthread_rwlock_unlock((pthread_rwlock_t *)fdb->rmtxs + i);
      }
      return false;
    }
  }
  TCTESTYIELD();
  return true;
}


/* Unlock all records of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbunlockallrecords(TCFDB *fdb){
  assert(fdb);
  bool err = false;
  for(int i = FDBRMTXNUM - 1; i >= 0; i--){
    if(pthread_rwlock_unlock((pthread_rwlock_t *)fdb->rmtxs + i)) err = true;
  }
  TCTESTYIELD();
  if(err){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  return true;
}


/* Lock the write ahead logging file of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdblockwal(TCFDB *fdb){
  assert(fdb);
  if(pthread_mutex_lock(fdb->wmtx) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}


/* Unlock the write ahead logging file of the fixed-length database object.
   `fdb' specifies the fixed-length database object.
   If successful, the return value is true, else, it is false. */
static bool tcfdbunlockwal(TCFDB *fdb){
  assert(fdb);
  if(pthread_mutex_unlock(fdb->wmtx) != 0){
    tcfdbsetecode(fdb, TCETHREAD, __FILE__, __LINE__, __func__);
    return false;
  }
  TCTESTYIELD();
  return true;
}



/*************************************************************************************************
 * debugging functions
 *************************************************************************************************/


/* Print meta data of the header into the debugging output.
   `fdb' specifies the fixed-length database object. */
void tcfdbprintmeta(TCFDB *fdb){
  assert(fdb);
  if(fdb->dbgfd < 0) return;
  int dbgfd = (fdb->dbgfd == UINT16_MAX) ? 1 : fdb->dbgfd;
  char buf[FDBIOBUFSIZ];
  char *wp = buf;
  wp += sprintf(wp, "META:");
  wp += sprintf(wp, " mmtx=%p", (void *)fdb->mmtx);
  wp += sprintf(wp, " amtx=%p", (void *)fdb->amtx);
  wp += sprintf(wp, " rmtxs=%p", (void *)fdb->rmtxs);
  wp += sprintf(wp, " tmtx=%p", (void *)fdb->tmtx);
  wp += sprintf(wp, " wmtx=%p", (void *)fdb->wmtx);
  wp += sprintf(wp, " eckey=%p", (void *)fdb->eckey);
  wp += sprintf(wp, " rpath=%s", fdb->rpath ? fdb->rpath : "-");
  wp += sprintf(wp, " type=%02X", fdb->type);
  wp += sprintf(wp, " flags=%02X", fdb->flags);
  wp += sprintf(wp, " width=%u", fdb->width);
  wp += sprintf(wp, " limsiz=%llu", (unsigned long long)fdb->limsiz);
  wp += sprintf(wp, " wsiz=%u", fdb->wsiz);
  wp += sprintf(wp, " rsiz=%u", fdb->rsiz);
  wp += sprintf(wp, " limid=%llu", (unsigned long long)fdb->limid);
  wp += sprintf(wp, " path=%s", fdb->path ? fdb->path : "-");
  wp += sprintf(wp, " fd=%d", fdb->fd);
  wp += sprintf(wp, " omode=%u", fdb->omode);
  wp += sprintf(wp, " rnum=%llu", (unsigned long long)fdb->rnum);
  wp += sprintf(wp, " fsiz=%llu", (unsigned long long)fdb->fsiz);
  wp += sprintf(wp, " min=%llu", (unsigned long long)fdb->min);
  wp += sprintf(wp, " max=%llu", (unsigned long long)fdb->max);
  wp += sprintf(wp, " iter=%llu", (unsigned long long)fdb->iter);
  wp += sprintf(wp, " map=%p", (void *)fdb->map);
  wp += sprintf(wp, " array=%p", (void *)fdb->array);
  wp += sprintf(wp, " ecode=%d", fdb->ecode);
  wp += sprintf(wp, " fatal=%u", fdb->fatal);
  wp += sprintf(wp, " inode=%llu", (unsigned long long)fdb->inode);
  wp += sprintf(wp, " mtime=%llu", (unsigned long long)fdb->mtime);
  wp += sprintf(wp, " tran=%d", fdb->tran);
  wp += sprintf(wp, " walfd=%d", fdb->walfd);
  wp += sprintf(wp, " walend=%llu", (unsigned long long)fdb->walend);
  wp += sprintf(wp, " dbgfd=%d", fdb->dbgfd);
  wp += sprintf(wp, " cnt_writerec=%lld", (long long)fdb->cnt_writerec);
  wp += sprintf(wp, " cnt_readrec=%lld", (long long)fdb->cnt_readrec);
  wp += sprintf(wp, " cnt_truncfile=%lld", (long long)fdb->cnt_truncfile);
  *(wp++) = '\n';
  tcwrite(dbgfd, buf, wp - buf);
}



// END OF FILE