File: btr.c

package info (click to toggle)
egenix-mx-base 3.2.1-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,496 kB
  • sloc: ansic: 21,976; python: 17,192; sh: 137; makefile: 116
file content (1488 lines) | stat: -rw-r--r-- 42,656 bytes parent folder | download | duplicates (5)
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
/* Source file extracted from btr.c -- an ANSI C implementation
   included in the source code distribution of 

   SORTING AND SEARCHING ALGORITHMS: A COOKBOOK

   by THOMAS NIEMANN Portland, Oregon 
   email: thomasn@jps.net 
   home: http://members.xoom.com/thomasn/s_man.htm

   From the cookbook:

   Permission to reproduce this document, in whole or in part, is
   given provided the original web site listed below is referenced,
   and no additional restrictions apply. Source code, when part of a
   software project, may be used freely without reference to the
   author.

   Includes modifications by Marc-Andre Lemburg, 1998,
   mal@lemburg.com. See btr.h for details.

*/

/* Logging file used by debugging facility */
#ifndef MAL_DEBUG_OUTPUTFILE
# define MAL_DEBUG_OUTPUTFILE "mxBeeBase.log"
#endif

#include "mxstdlib.h"
#include "btr.h"

/* --- Globals ------------------------------------------------------------ */

/* line number for last IO or memory error */
int bErrLineNo;

/* --- Internal APIs ------------------------------------------------------ */

/*
 *  algorithm:
 *    A B+tree implementation, with keys stored in internal nodes,
 *    and keys/record addresses stored in leaf nodes.  Each node is
 *    one sector in length, except the root node whose length is
 *    3 sectors.  When traversing the tree to insert a key, full
 *    children are adjusted to make room for possible new entries.
 *    Similarly, on deletion, half-full nodes are adjusted to allow for
 *    possible deleted entries.  Adjustments are first done by 
 *    examining 2 nearest neighbors at the same level, and redistibuting
 *    the keys if possible.  If redistribution won't solve the problem,
 *    nodes are split/joined as needed.  Typically, a node is 3/4 full.
 *    On insertion, if 3 nodes are full, they are split into 4 nodes,
 *    each 3/4 full.  On deletion, if 3 nodes are 1/2 full, they are
 *    joined to create 2 nodes 3/4 full.
 *
 *    A LRR (least-recently-read) buffering scheme for nodes is used to
 *    simplify storage management, and, assuming some locality of reference,
 *    improve performance.
 *
 *    To simplify matters, both internal nodes and leafs contain the
 *    same fields.
 *   
 */

/* macros for addressing fields */

/* primitives */
#define bAdr(p) *(bIdxAddr *)(p)
#define eAdr(p) *(bRecAddr *)(p)

/* based on k = &[key,rec,childGE] */
#define childLT(k) bAdr((char *)k - sizeof(bIdxAddr))
#define key(k) (k)
#define rec(k) eAdr((char *)(k) + h->keySize)
#define childGE(k) bAdr((char *)(k) + h->keySize + sizeof(bRecAddr))

/* based on b = &bBuffer */
#define leaf(b) b->p->leaf
#define ct(b) b->p->ct
#define next(b) b->p->next
#define prev(b) b->p->prev
#define fkey(b) &b->p->fkey
#define lkey(b) (fkey(b) + ks((ct(b) - 1)))
#define p(b) (char *)(b->p)

/* shortcuts */
#define ks(ct) ((ct) * h->ks)
#define error(rc) lineError(__LINE__, rc)

static 
bError lineError(int lineno, 
		 bError rc) 
{
    if (rc == bErrIO || rc == bErrMemory)
        if (!bErrLineNo) 
            bErrLineNo = lineno;
    return rc;
}

static 
bIdxAddr allocAdr(bHandle *h) 
{
    bIdxAddr adr;
    adr = h->nextFreeAdr;
    h->nextFreeAdr += h->sectorSize;
    return adr;
}

static 
bError flush(bHandle *h,
	     bBuffer *buf) 
{
    int len;            /* number of bytes to write */

    /* flush buffer to disk */
    len = h->sectorSize;
    if (buf->adr == 0) len *= 3;        /* root */
    if (fseek(h->fp, buf->adr, SEEK_SET)) return error(bErrIO);
    if (fwrite(buf->p, len, 1, h->fp) != 1) return error(bErrIO);
    buf->modified = false;
    h->nDiskWrites++;
    return bErrOk;
}

static 
bError flushAll(bHandle *h) 
{
    bError rc;                /* return code */
    bBuffer *buf;               /* buffer */

    if (h->root.modified)
        if ((rc = flush(h,&h->root)) != 0) return rc;

    buf = h->bufList.next;
    while (buf != &h->bufList) {
        if (buf->modified)
            if ((rc = flush(h,buf)) != 0) return rc;
        buf = buf->next;
    }

    /* Now make sure the data is really written to disk */
    fflush(h->fp);

    return bErrOk;
}

static 
bError assignBuf(bHandle *h,
		 bIdxAddr adr, 
		 bBuffer **b) 
{
    /* assign buf to adr */
    bBuffer *buf;               /* buffer */
    bError rc;                /* return code */

    if (adr == 0) {
        *b = &h->root;
        return bErrOk;
    }

    /* search for buf with matching adr */
    buf = h->bufList.next;
    while (buf->next != &h->bufList) {
        if (buf->valid && buf->adr == adr) break;
        buf = buf->next;
    }

    /* either buf points to a match, or it's last one in list (LRR) */
    if (buf->valid) {
        if (buf->adr != adr) {
            if (buf->modified) {
                if ((rc = flush(h,buf)) != 0) return rc;
            }
            buf->adr = adr;
            buf->valid = false;
        }
    } else {
        buf->adr = adr;
    }

    /* remove from current position and place at front of list */
    buf->next->prev = buf->prev;
    buf->prev->next = buf->next;
    buf->next = h->bufList.next;
    buf->prev = &h->bufList;
    buf->next->prev = buf;
    buf->prev->next = buf;
    *b = buf;
    return bErrOk;
}

static 
bError writeDisk(bHandle *h,
		 bBuffer *buf) 
{
    /* write buf to disk */
    buf->valid = true;
    buf->modified = true;
    return bErrOk;
}

static 
bError readDisk(bHandle *h,
		bIdxAddr adr, 
		bBuffer **b) 
{
    /* read data into buf */
    int len;
    bBuffer *buf;               /* buffer */
    bError rc;                /* return code */

    if ((rc = assignBuf(h, adr, &buf)) != 0) return rc;
    if (!buf->valid) {
        len = h->sectorSize;
        if (adr == 0) len *= 3;         /* root */
        if (fseek(h->fp, adr, SEEK_SET)) return error(bErrIO);
        if (fread(buf->p, len, 1, h->fp) != 1) return error(bErrIO);
        buf->modified = false;
        buf->valid = true;
        h->nDiskReads++;
    }
    *b = buf;
    return bErrOk;
}

static
void dumpBuf(bHandle *h,
	     char *msg,
	     bBuffer *buf) 
{
    unsigned int i;
    bKey *k;

    if (!buf) {
        DPRINTF("\n%s: buf empty\n", msg);
        return;
    }
    k = key(fkey(buf));
    DPRINTF("\n%s: buf[%04lx], ct=%d, leaf=%d", 
            msg, (unsigned long)buf->adr, ct(buf), leaf(buf));
    if (childLT(k)) 
	DPRINTF(", LT(%04lx)", childLT(k));
    if (leaf(buf)) 
	DPRINTF(", prev(%04lx), next(%04lx)", 
		(unsigned long)prev(buf), (unsigned long)next(buf));
    DPRINTF("\n");
    for (i = 0; i < ct(buf); i++) {
        DPRINTF("  key %3d: %08x rec(%08lx)",
		i, *(int *)key(k), rec(k));
        if (childGE(k)) 
	    DPRINTF(" GE(%04lx)", (unsigned long)childGE(k));
        DPRINTF("\n");
        k += ks(1);
    }
}

#define report(rc) reportErr(__LINE__, rc)

static
void reportErr(int lineno, bError rc) 
{
    if (rc == bErrIO || rc == bErrMemory) {
        perror("aborting");
        DPRINTF("line %d: error %d\n", bErrLineNo, rc);
    } else {
        DPRINTF("line %d: error %d\n", lineno, rc);
    }
}

static
int dumpNode(bHandle *h,
	     char *msg,
	     bIdxAddr adr) 
{
    bBuffer *buf;               /* buffer */
    bError rc;                /* return code */
    bKey *k;
    unsigned int i;

    if ((rc = readDisk(h, adr, &buf)) != 0) {
        report(rc);
        return -1;
    }
    dumpBuf(h, msg, buf);
    k = fkey(buf);
    for (i = 0; i < ct(buf); i++) {
        if (childLT(k)) dumpNode(h, msg, childLT(k));
        if (childGE(k)) dumpNode(h, msg, childGE(k));
        k += ks(1);
    }
    return 0;
}

#if 0
static
int dump(bHandle *h,
	 char *msg) 
{
    return dumpNode(h, msg, h->root.adr);
}
#endif

static
int _validateTree(bHandle *h,
		  bBuffer *b,
		  char *visited,
		  int level) 
{
    bKey *k;
    bError rc;
    unsigned int i;
    char p[3*MAX_SECTOR_SIZE];
    bBuffer *cbuf, bufx;
    bBuffer *buf = &bufx;

    if (h->sectorSize > MAX_SECTOR_SIZE) {
	DPRINTF("sectorSize exceeds MAX_SECTOR_SIZE; aborting check\n");
	return -1;
    }
    memcpy(buf, b, sizeof(bBuffer));
    memcpy(p, b->p, 3*h->sectorSize);
    buf->p = (bNode *)p;
    dumpBuf(h,"validate", buf);
    if (visited[buf->adr >> 8]) {
        DPRINTF("previous visit, buf[%04lx]\n", (unsigned long)buf->adr);
        return -1;
    }
    visited[buf->adr >> 8] = 1;
    DPRINTF("\n");
    if (ct(buf)) {
        if (!leaf(buf)) {
            DPRINTF("level %d: recursing on buf[%04lx] LT\n", 
		    level, childLT(fkey(buf)));
            if ((rc = readDisk(h, childLT(fkey(buf)), &cbuf)) != 0) {
                DPRINTF("unable to read buffer %04lx\n", 
			childLT(fkey(buf)));
                return -1;
            }
            if (*(unsigned int *)key(lkey(cbuf)) > *(unsigned int *)key(fkey(buf))) {
                DPRINTF("last element in child buf[%04lx] LT "
			"> first element of parent buf[%04lx]\n", 
			(unsigned long)cbuf->adr, 
			(unsigned long)buf->adr);
                return -1;
            }
            _validateTree(h, cbuf, visited, level+1);
            k = fkey(buf);
            for (i = 0; i < ct(buf); i++) {
                DPRINTF("level %d: recursing on buf[%04lx] GE[%d]\n", 
			level, key(childGE(k)), i);
                if ((rc = readDisk(h, childGE(k), &cbuf)) != 0) {
                    DPRINTF("unable to read buffer %04lx\n", childGE(k));
                    return -1;
                }
                if (*(unsigned int *)key(fkey(cbuf)) < *(unsigned int *)key(k)) {
                    DPRINTF("first element in child buf[%04lx] "
			    "< parent buf[%04lx] GE (%08x < %08x)\n", 
			    (unsigned long)cbuf->adr,
			    (unsigned long)buf->adr,
			    *(int *)key(fkey(cbuf)),
			    *(int *)key(k));

                    dumpBuf(h,"buf", buf);
                    dumpBuf(h,"cbuf", cbuf);
                    return -1;
                }
                if (!leaf(cbuf) && *(unsigned int *)key(fkey(cbuf)) ==  *(unsigned int *)key(k)) {
                    DPRINTF("first element in child buf[%04lx] "
			    "= parent buf[%04lx] GE (%08x < %08x)\n",
			    (unsigned long)cbuf->adr,
			    (unsigned long)buf->adr,
			    *(int *)key(fkey(cbuf)),
			    *(int *)key(k));
                    dumpBuf(h,"buf", buf);
                    dumpBuf(h,"cbuf", cbuf);
                    return -1;
                }
                _validateTree(h,cbuf,visited,level+1);
                k += ks(1);
            }
        }
    }
    return 0;
}

typedef enum { MODE_FIRST, MODE_MATCH } modeEnum;

static 
int search(bHandle *h,
	   bBuffer *buf,
	   void *key, 
	   bRecAddr rec, 
	   bKey **mkey,
	   modeEnum mode) 
{
    /*
     * input:
     *   p                      pointer to node
     *   key                    key to find
     *   rec                    record address (dupkey only)
     *   mode			MODE_FIRST (find first),
     *				MODE_MATCH (rec's must match too) [dupkey only]
     * output:
     *   mkey                   pointer to bKey info
     *
     * returns:
     *   CC_EQ                  key = mkey
     *   CC_LT                  key < mkey
     *   CC_GT                  key > mkey
     */
    int cc = CC_LT;             /* condition code */
    int m;                      /* midpoint of search */
    int lb;                     /* lower-bound of binary search */
    int ub;                     /* upper-bound of binary search */
    bool foundDup;              /* true if found a duplicate key */

    /* Test for empty buffer */
    if (ct(buf) == 0) {
        *mkey = fkey(buf);
        return cc;
    }

    /* Scan current node for key using binary search */
    foundDup = false;
    lb = 0; 
    ub = ct(buf) - 1;
    while (lb <= ub) {
        m = (lb + ub) / 2;
        *mkey = fkey(buf) + ks(m);
        cc = h->comp(h->keySize, key, key(*mkey));
        if (cc < 0)
            /* key less than key[m] */
            ub = m - 1;
        else if (cc > 0)
            /* key greater than key[m] */
            lb = m + 1;
        else {
            /* keys match */
            if (h->dupKeys) {
                switch (mode) {
                case MODE_FIRST:
                    /* backtrack to first key */
                    ub = m - 1;
                    foundDup = true;
                    break;
                case MODE_MATCH:
                    /* rec's must also match */
                    if (rec < rec(*mkey)) {
                        ub = m - 1;
                        cc = CC_LT;
                    } else if (rec > rec(*mkey)) {
                        lb = m + 1;
                        cc = CC_GT;
                    } else {
                        return CC_EQ;
                    }
                    break;
                }
            } else {
		return CC_EQ;
            }
        }
    }

    /* Handle set of duplicates */
    if (h->dupKeys && (mode == MODE_FIRST) && foundDup) {
#if 0
	DPRINTF("found dups: cc=%i, lb=%i, ub=%i, key(*mkey)=%i next=%i\n",
	       cc,lb,ub,*(int*)key(*mkey),*(int*)key((*mkey+ks(1))));
#endif
        if (cc == CC_GT)
	    /* next key is first key */
	    *mkey += ks(1);
        return CC_EQ;
    }

    /* didn't find key */
    return cc < 0 ? CC_LT : CC_GT;
}

static 
bError scatterRoot(bHandle *h)
{
    bBuffer *gbuf;
    bBuffer *root;

    /* scatter gbuf to root */

    root = &h->root;
    gbuf = &h->gbuf;
    memcpy(fkey(root), fkey(gbuf), ks(ct(gbuf)));
    childLT(fkey(root)) = childLT(fkey(gbuf));
    ct(root) = ct(gbuf);
    leaf(root) = leaf(gbuf);
    return bErrOk;
}

static 
bError scatter(bHandle *h,
	       bBuffer *pbuf, 
	       bKey *pkey, 
	       int is, 
	       bBuffer **tmp) 
{
    bBuffer *gbuf;              /* gather buf */
    bKey *gkey;              /* gather buf key */
    bError rc;                /* return code */
    int iu;                     /* number of tmp's used */
    int k0Min;                  /* min #keys that can be mapped to tmp[0] */
    int knMin;                  /* min #keys that can be mapped to tmp[1..3] */
    int k0Max;                  /* max #keys that can be mapped to tmp[0] */
    int knMax;                  /* max #keys that can be mapped to tmp[1..3] */
    int sw;                     /* shift width */
    int len;                    /* length of remainder of buf */
    int base;                   /* base count distributed to tmps */
    int extra;                  /* extra counts */
    int ct;
    int i;

    /*
     * input:
     *   pbuf                   parent buffer of gathered keys
     *   pkey                   where we insert a key if needed in parent
     *   is                     number of supplied tmps
     *   tmp                    array of tmp's to be used for scattering
     * output:
     *   tmp                    array of tmp's used for scattering
     */

    /* scatter gbuf to tmps, placing 3/4 max in each tmp */

    gbuf = &h->gbuf;
    gkey = fkey(gbuf);
    ct = ct(gbuf);

   /****************************************
    * determine number of tmps to use (iu) *
    ****************************************/
    iu = is;

    /* determine limits */
    if (leaf(gbuf)) {
        /* minus 1 to allow for insertion */
        k0Max= h->maxCt - 1;
        knMax= h->maxCt - 1;
        /* plus 1 to allow for deletion */
        k0Min= (h->maxCt / 2) + 1;
        knMin= (h->maxCt / 2) + 1;
    } else {
        /* can hold an extra gbuf key as it's translated to a LT pointer */
        k0Max = h->maxCt - 1;
        knMax = h->maxCt;
        k0Min = (h->maxCt / 2) + 1;
        knMin = ((h->maxCt+1) / 2) + 1;
    }

    /* calculate iu, number of tmps to use */
    while(1) {
        if (iu == 0 || ct > (k0Max + (iu-1)*knMax)) {
            /* add a buffer */
            if ((rc = assignBuf(h, allocAdr(h), &tmp[iu])) != 0) 
                return rc;
            /* update sequential links */
            if (leaf(gbuf)) {
                /* adjust sequential links */
                if (iu == 0) {
                    /* no tmps supplied when splitting root for first time */
                    prev(tmp[0]) = 0;
                    next(tmp[0]) = 0;
                } else {
                    prev(tmp[iu]) = tmp[iu-1]->adr;
                    next(tmp[iu]) = next(tmp[iu-1]);
                    next(tmp[iu-1]) = tmp[iu]->adr;
                }
            }
            iu++;
            h->nNodesIns++;
        } else if (iu > 1 && ct < (k0Min + (iu-1)*knMin)) {
            /* del a buffer */
            iu--;
            /* adjust sequential links */
            if (leaf(gbuf) && tmp[iu-1]->adr) {
                next(tmp[iu-1]) = next(tmp[iu]);
            }
            next(tmp[iu-1]) = next(tmp[iu]);
            h->nNodesDel++;
        } else {
            break;
        }
    }

    /* establish count for each tmp used */
    base = ct / iu;
    extra = ct % iu;
    for (i = 0; i < iu; i++) {
        int n;

        n = base;
        /* distribute extras, one at a time */
        /* don't do to 1st node, as it may be internal and can't hold it */
        if (i && extra) {
            n++;
            extra--;
        }
        ct(tmp[i]) = n;
    }


    /**************************************
     * update sequential links and parent *
     **************************************/
    if (iu != is) {
        /* link last node to next */
        if (leaf(gbuf) && next(tmp[iu-1])) {
            bBuffer *buf;
            if ((rc = readDisk(h, next(tmp[iu-1]), &buf)) != 0) return rc;
            prev(buf) = tmp[iu-1]->adr;
            if ((rc = writeDisk(h, buf)) != 0) return rc;
        }
        /* shift keys in parent */
        sw = ks(iu - is);
        if (sw < 0) {
            len = ks(ct(pbuf)) - (pkey - fkey(pbuf)) + sw;
            memmove(pkey, pkey - sw, len);
        } else {
            len = ks(ct(pbuf)) - (pkey - fkey(pbuf));
            memmove(pkey + sw, pkey, len);
        }
        /* don't count LT buffer for empty parent */
        if (ct(pbuf))
            ct(pbuf) += iu - is;
        else
            ct(pbuf) += iu - is - 1;
    }

   /*******************************
    * distribute keys to children *
    *******************************/
    for (i = 0; i < iu; i++) {

        /* update LT pointer and parent nodes */
        if (leaf(gbuf)) {
            /* update LT, tmp[i] */
            childLT(fkey(tmp[i])) = 0;

            /* update parent */
            if (i == 0) {
                childLT(pkey) = tmp[i]->adr;
            } else {
                memcpy(pkey, gkey, ks(1));
                childGE(pkey) = tmp[i]->adr;
                pkey += ks(1);
            }
        } else {
            if (i == 0) {
                /* update LT, tmp[0] */
                childLT(fkey(tmp[i])) = childLT(gkey);
                /* update LT, parent */
                childLT(pkey) = tmp[i]->adr;
            } else {
                /* update LT, tmp[i] */
                childLT(fkey(tmp[i])) = childGE(gkey);
                /* update parent key */
                memcpy(pkey, gkey, ks(1));
                childGE(pkey) = tmp[i]->adr;
                gkey += ks(1);
                pkey += ks(1);
                ct(tmp[i])--;
            }
        }

        /* install keys, tmp[i] */
        memcpy(fkey(tmp[i]), gkey, ks(ct(tmp[i])));
        leaf(tmp[i]) = leaf(gbuf);

        gkey += ks(ct(tmp[i]));
    }
    leaf(pbuf) = false;

   /************************
    * write modified nodes *
    ************************/
    if ((rc = writeDisk(h, pbuf)) != 0) return rc;
    for (i = 0; i < iu; i++)
        if ((rc = writeDisk(h, tmp[i])) != 0) return rc;

    return bErrOk;
}

static 
bError gatherRoot(bHandle *h)
{
    bBuffer *gbuf;
    bBuffer *root;

    /* gather root to gbuf */
    root = &h->root;
    gbuf = &h->gbuf;
    memcpy(p(gbuf), root->p, 3 * h->sectorSize);
    leaf(gbuf) = leaf(root);
    ct(root) = 0;
    return bErrOk;
}

static 
bError gather(bHandle *h, 
	      bBuffer *pbuf, 
	      bKey **pkey, 
	      bBuffer **tmp) 
{
    bError rc;                /* return code */
    bBuffer *gbuf;
    bKey *gkey;

    /*
     * input:
     *   pbuf                   parent buffer
     *   pkey                   pointer to match key in parent
     * output:
     *   tmp                    buffers to use for scatter
     *   pkey                   pointer to match key in parent
     * returns:
     *   bErrOk                 operation successful
     * notes:
     *   Gather 3 buffers to gbuf.  Setup for subsequent scatter by
     *   doing the following:
     *     - setup tmp buffer array for scattered buffers
     *     - adjust pkey to point to first key of 3 buffers
     */

    /* find 3 adjacent buffers */
    if (*pkey == lkey(pbuf))
        *pkey -= ks(1);
    if ((rc = readDisk(h, childLT(*pkey), &tmp[0])) != 0) return rc;
    if ((rc = readDisk(h, childGE(*pkey), &tmp[1])) != 0) return rc;
    if ((rc = readDisk(h, childGE(*pkey + ks(1)), &tmp[2])) != 0) return rc;

    /* gather nodes to gbuf */
    gbuf = &h->gbuf;
    gkey = fkey(gbuf);

    /* tmp[0] */
    childLT(gkey) = childLT(fkey(tmp[0]));
    memcpy(gkey, fkey(tmp[0]), ks(ct(tmp[0])));
    gkey += ks(ct(tmp[0]));
    ct(gbuf) = ct(tmp[0]);

    /* tmp[1] */
    if (!leaf(tmp[1])) {
        memcpy(gkey, *pkey, ks(1));
        childGE(gkey) = childLT(fkey(tmp[1]));
        ct(gbuf)++;
        gkey += ks(1);
    }
    memcpy(gkey, fkey(tmp[1]), ks(ct(tmp[1])));
    gkey += ks(ct(tmp[1]));
    ct(gbuf) += ct(tmp[1]);

    /* tmp[2] */
    if (!leaf(tmp[2])) {
        memcpy(gkey, *pkey+ks(1), ks(1));
        childGE(gkey) = childLT(fkey(tmp[2]));
        ct(gbuf)++;
        gkey += ks(1);
    }
    memcpy(gkey, fkey(tmp[2]), ks(ct(tmp[2])));
    ct(gbuf) += ct(tmp[2]);

    leaf(gbuf) = leaf(tmp[0]);

    return bErrOk;
}

/* --- Interface --------------------------------------------------------- */

bError bOpen(bDescription info,
	     bHandle **handle) 
{
    bError rc;                	/* return code */
    int bufCt;                  /* number of tmp buffers */
    bBuffer *buf;               /* buffer */
    int maxCt;                  /* maximum number of keys in a node */
    bBuffer *root;
    int i;
    bNode *p;
    bHandle *h;

    if ((info.sectorSize < sizeof(bNode)) 
	|| (info.sectorSize % 4)
	|| (info.sectorSize > MAX_SECTOR_SIZE))
        return bErrSectorSize;

    /* determine sizes and offsets */
    /* leaf/n, prev, next, [childLT,key,rec]... childGE */
    /* ensure that there are at least 3 children/parent for gather/scatter */
    maxCt = info.sectorSize - (sizeof(bNode) - sizeof(bKey));
    maxCt /= sizeof(bIdxAddr) + info.keySize + sizeof(bRecAddr);
    if (maxCt < 6) return bErrSectorSize;

    /* copy parms to bHandle */
    if ((h = calloc(sizeof(bHandle),1)) == NULL) return error(bErrMemory);
    h->keySize = info.keySize;
    h->dupKeys = info.dupKeys;
    h->sectorSize = info.sectorSize;
    h->comp = info.comp;

    /* childLT, key, rec */
    h->ks = sizeof(bIdxAddr) + h->keySize + sizeof(bRecAddr);
    h->maxCt = maxCt;

    /* Allocate buflist.
     * During insert/delete, need simultaneous access to 7 buffers:
     *  - 4 adjacent child bufs
     *  - 1 parent buf
     *  - 1 next sequential link
     *  - 1 lastGE
     */
    bufCt = 7 /*+ EXTRA_BUFFERS*/;
    if ((h->malloc1 = calloc(bufCt * sizeof(bBuffer),1)) == NULL) 
        return error(bErrMemory);
    buf = h->malloc1;

    /*
     * Allocate bufs.
     * We need space for the following:
     *  - bufCt buffers, of size sectorSize
     *  - 1 buffer for root, of size 3*sectorSize
     *  - 1 buffer for gbuf, size 3*sectorsize + 2 extra keys
     *    to allow for LT pointers in last 2 nodes when gathering 3 full nodes
     */
    if ((h->malloc2 = calloc((bufCt+6) * h->sectorSize + 2 * h->ks,1)) == NULL) 
        return error(bErrMemory);
    p = h->malloc2;

    /* initialize buflist */
    h->bufList.next = buf;
    h->bufList.prev = buf + (bufCt - 1);
    for (i = 0; i < bufCt; i++) {
        buf->next = buf + 1;
        buf->prev = buf - 1;
        buf->modified = false;
        buf->valid = false;
        buf->p = p;
        p = (bNode *)((char *)p + h->sectorSize);
        buf++;
    }
    h->bufList.next->prev = &h->bufList;
    h->bufList.prev->next = &h->bufList;

    /* initialize root */
    root = &h->root;
    root->p = p;
    p = (bNode *)((char *)p + 3*h->sectorSize);
    h->gbuf.p = p;      /* done last to include extra 2 keys */

    /* Open the file */
    switch (info.filemode) {

    case 1: /* Open in read-only mode */
	if ((h->fp = fopen(info.iName, "rb")) != NULL) {
	    /* open an existing database */
	    if ((rc = readDisk(h, 0, &root)) != 0) return rc;
	    if (fseek(h->fp, 0, SEEK_END)) return error(bErrIO);
	    if ((h->nextFreeAdr = ftell(h->fp)) == -1) return error(bErrIO);
	}
	else {
	    free(h);
	    return bErrFileNotOpen;
	}
	break;

    case 0: /* Open in update mode, revert to creating a new file */
    case 3: /* Open an existing file in update mode, fail if non-existing */
	if ((h->fp = fopen(info.iName, "r+b")) != NULL) {
	    /* open an existing database */
	    if ((rc = readDisk(h, 0, &root)) != 0) return rc;
	    if (fseek(h->fp, 0, SEEK_END)) return error(bErrIO);
	    if ((h->nextFreeAdr = ftell(h->fp)) == -1) return error(bErrIO);
	    break;
	}
	else if (info.filemode == 3) {
	    free(h);
	    return bErrFileNotOpen;
	}
	/* On error and filemode 0: fall through */

    case 2: /* Create a new file */
	if ((h->fp = fopen(info.iName, "w+b")) != NULL) {
	    /* initialize root */
	    memset(root->p, 0, 3*h->sectorSize);
	    leaf(root) = 1;
	    root->modified = true;
	    h->nextFreeAdr = 3 * h->sectorSize;
	    /* flush buffers to create a valid file stub */
	    flushAll(h);
	    break;
	}
	/* On error: fall through */
	
    default:
        /* Something's wrong */
        free(h);
        return bErrFileNotOpen;
    }

    *handle = h;
    return bErrOk;
}

bError bFlush(bHandle *h)
{
    if (h == NULL) return bErrOk;

    /* flush idx */
    if (h->fp) {
        flushAll(h);
    }
    return bErrOk;
}

bError bClose(bHandle *h) 
{
    if (h == NULL) return bErrOk;

    /* flush idx */
    if (h->fp) {
        flushAll(h);
        fclose(h->fp);
    }

    if (h->malloc2) free(h->malloc2);
    if (h->malloc1) free(h->malloc1);
    free(h);
    return bErrOk;
}

bError bFindKey(bHandle *h, 
		bCursor *c,
		void *key, 
		bRecAddr *rec) 
{
    bKey *mkey = 0;            	/* matched key */
    bBuffer *buf;               /* buffer */
    bError rc;                	/* return code */

    buf = &h->root;

    /* find key, and return address */
    while (1) {
        if (leaf(buf)) {
	    int cc;
	    
            if ((cc=search(h, buf, key, 0, &mkey, MODE_FIRST)) == CC_EQ) {
                if (rec) 
		    *rec = rec(mkey);
                c->buffer = buf; 
		c->key = mkey;
                return bErrOk;
            } else {
		DPRINTF("not found; cc=%i\n", cc);
		
                return bErrKeyNotFound;
            }
        } else {
            if (search(h, buf, key, 0, &mkey, MODE_FIRST) == CC_LT) {
                if ((rc = readDisk(h, childLT(mkey), &buf)) != 0) 
		    return rc;
            } else {
                if ((rc = readDisk(h, childGE(mkey), &buf)) != 0) 
		    return rc;
            }
        }
    }
}

bError bInsertKey(bHandle *h, 
		  void *key, 
		  bRecAddr rec) 
{
    int rc;                     /* return code */
    bKey *mkey;              	/* match key */
    int len;                    /* length to shift */
    int cc;                     /* condition code */
    bBuffer *buf, *root;
    bBuffer *tmp[4];
    unsigned int keyOff;
    bool lastGEvalid;           /* true if GE branch taken */
    bool lastLTvalid;           /* true if LT branch taken after GE branch */
    bIdxAddr lastGE = 0;        /* last childGE traversed */
    unsigned int lastGEkey = 0; /* last childGE key traversed */
    int height;                 /* height of tree */

    root = &h->root;
    lastGEvalid = false;
    lastLTvalid = false;

    /* check for full root */
    if (ct(root) == 3 * h->maxCt) {
        /* gather root and scatter to 4 bufs */
        /* this increases b-tree height by 1 */
        if ((rc = gatherRoot(h)) != 0) return rc;
        if ((rc = scatter(h, root, fkey(root), 0, tmp)) != 0) return rc;
    }
    buf = root;
    height = 0;
    while(1) {
        if (leaf(buf)) {
            /* in leaf, and there' room guaranteed */

            if (height > h->maxHeight) h->maxHeight = height;

            /* set mkey to point to insertion point */
            switch (search(h, buf, key, rec, &mkey, MODE_MATCH)) {
            case CC_LT:  /* key < mkey */
		if (ct(buf) == 0)
		    break;
                if (!h->dupKeys && 
		    h->comp(h->keySize, key, mkey) == CC_EQ)
                    return bErrDupKeys;
                break;
            case CC_EQ:  /* key = mkey */
                return bErrDupKeys;
                break;
            case CC_GT:  /* key > mkey */
                if (!h->dupKeys && 
		    h->comp(h->keySize, key, mkey) == CC_EQ)
                    return bErrDupKeys;
                mkey += ks(1);
                break;
            }

            /* shift items GE key to right */
            keyOff = mkey - fkey(buf);
            len = ks(ct(buf)) - keyOff;
            if (len) memmove(mkey + ks(1), mkey, len);

            /* insert new key */
            memcpy(key(mkey), key, h->keySize);
            rec(mkey) = rec;
            childGE(mkey) = 0;
            ct(buf)++;
            if ((rc = writeDisk(h, buf)) != 0) return rc;

            /* if new key is first key, then fixup lastGE key */
            if (!keyOff && lastLTvalid) {
                bBuffer *tbuf;
                bKey *tkey;
                if ((rc = readDisk(h, lastGE, &tbuf)) != 0) return rc;
                tkey = fkey(tbuf) + lastGEkey;
                memcpy(key(tkey), key, h->keySize);
                rec(tkey) = rec;
                if ((rc = writeDisk(h, tbuf)) != 0) return rc;
            }
            h->nKeysIns++;
            break;

        } else {
            /* internal node, descend to child */
            bBuffer *cbuf;      /* child buf */

            height++;
          
            /* read child */
            if ((cc = search(h, buf, key, rec, &mkey, MODE_MATCH)) == CC_LT) {
                if ((rc = readDisk(h, childLT(mkey), &cbuf)) != 0) return rc;
            } else {
                if ((rc = readDisk(h, childGE(mkey), &cbuf)) != 0) return rc;
            }

            /* check for room in child */
            if (ct(cbuf) == h->maxCt) {

                /* gather 3 bufs and scatter */
                if ((rc = gather(h, buf, &mkey, tmp)) != 0) return rc;
                if ((rc = scatter(h, buf, mkey, 3, tmp)) != 0) return rc;

                /* read child */
                if ((cc = search(h, buf, key, rec, &mkey, MODE_MATCH)) == CC_LT) {
                    if ((rc = readDisk(h, childLT(mkey), &cbuf)) != 0) 
			return rc;
                } else {
                    if ((rc = readDisk(h, childGE(mkey), &cbuf)) != 0) 
			return rc;
                }
            }
            if (cc >= 0 || mkey != fkey(buf)) {
                lastGEvalid = true;
                lastLTvalid = false;
                lastGE = buf->adr;
                lastGEkey = mkey - fkey(buf);
                if (cc < 0) lastGEkey -= ks(1);
            } else {
                if (lastGEvalid) lastLTvalid = true;
            }
            buf = cbuf;
        }
    }

#if BTREE_DEBUG
    DPRINTF("I %i = 0x%08x -> %i = 0x%08x\n",
	    *(long*)key,*(long*)key,(long)rec,(long)rec);
#endif

    return bErrOk;
}

bError bUpdateKey(bHandle *h, 
		  void *key, 
		  bRecAddr rec) 
{
    int rc;                     /* return code */
    bKey *mkey = 0;            	/* match key */
    int cc;                     /* condition code */
    bBuffer *buf, *root;

    if (h->dupKeys)
	return bErrNotWithDupKeys;
    
    root = &h->root;

    buf = root;
    while(1) {
        if (leaf(buf)) {
            if (search(h, buf, key, rec, &mkey, MODE_MATCH) != CC_EQ)
		return bErrKeyNotFound;

            /* update record */
            rec(mkey) = rec;
            if ((rc = writeDisk(h, buf)) != 0) return rc;

            h->nKeysUpd++;
            break;

        } else {
            bBuffer *cbuf;      /* child buf */

            /* read child */
            if ((cc = search(h, buf, key, rec, &mkey, MODE_MATCH)) == CC_LT) {
                if ((rc = readDisk(h, childLT(mkey), &cbuf)) != 0) return rc;
            } else {
                if ((rc = readDisk(h, childGE(mkey), &cbuf)) != 0) return rc;
            }

            if (cc == CC_EQ) {
		/* update internal key copy too */
		rec(mkey) = rec;
            }
            buf = cbuf;
        }
    }

#if BTREE_DEBUG
    DPRINTF("U %i = 0x%08x -> %i = 0x%08x\n",
	    *(long*)key,*(long*)key,(long)rec,(long)rec);
#endif

    return bErrOk;
}

bError bDeleteKey(bHandle *h, 
		  void *key, 
		  bRecAddr *rec) 
{
    int rc;                     /* return code */
    bKey *mkey;              /* match key */
    int len;                    /* length to shift */
    int cc;                     /* condition code */
    bBuffer *buf;               /* buffer */
    bBuffer *tmp[4];
    unsigned int keyOff;
    bool lastGEvalid = false;   /* true if GE branch taken */
    bool lastLTvalid = false;   /* true if LT branch taken after GE branch */
    bIdxAddr lastGE = 0;        /* last childGE traversed */
    unsigned int lastGEkey = 0; /* last childGE key traversed */
    bBuffer *root;
    bBuffer *gbuf;

    root = &h->root;
    gbuf = &h->gbuf;
    lastGEvalid = false;
    lastLTvalid = false;

    buf = root;
    while(1) {
        if (leaf(buf)) {

            /* set mkey to point to deletion point */
            if (search(h, buf, key, *rec, &mkey, MODE_MATCH) == CC_EQ)
                *rec = rec(mkey);
            else
                return bErrKeyNotFound;

            /* shift items GT key to left */
            keyOff = mkey - fkey(buf);
            len = ks(ct(buf)-1) - keyOff;
            if (len) memmove(mkey, mkey + ks(1), len);
            ct(buf)--;
            if ((rc = writeDisk(h, buf)) != 0) return rc;

            /* if deleted key is first key, then fixup lastGE key */
            if (!keyOff && lastLTvalid) {
                bBuffer *tbuf;
                bKey *tkey;
                if ((rc = readDisk(h, lastGE, &tbuf)) != 0) return rc;
                tkey = fkey(tbuf) + lastGEkey;
                memcpy(key(tkey), mkey, h->keySize);
                rec(tkey) = rec(mkey);
                if ((rc = writeDisk(h, tbuf)) != 0) return rc;
            }
            h->nKeysDel++;
            break;
        } else {
            /* internal node, descend to child */
            bBuffer *cbuf;      /* child buf */
          
            /* read child */
            if ((cc = search(h, buf, key, *rec, &mkey, MODE_MATCH)) == CC_LT) {
                if ((rc = readDisk(h, childLT(mkey), &cbuf)) != 0) return rc;
            } else {
                if ((rc = readDisk(h, childGE(mkey), &cbuf)) != 0) return rc;
            }

            /* check for room to delete */
            if (ct(cbuf) == h->maxCt/2) {

                /* gather 3 bufs and scatter */
                if ((rc = gather(h, buf, &mkey, tmp)) != 0) return rc;

                /* if last 3 bufs in root, and count is low enough... */
                if (buf == root
                && ct(root) == 2 
                && ct(gbuf) < (3*(3*h->maxCt))/4) {
                    /* collapse tree by one level */
                    scatterRoot(h);
                    h->nNodesDel += 3;
                    continue;
                }

                if ((rc = scatter(h, buf, mkey, 3, tmp)) != 0) return rc;

                /* read child */
                if ((cc = search(h, buf, key, *rec, &mkey, MODE_MATCH)) == CC_LT) {
                    if ((rc = readDisk(h, childLT(mkey), &cbuf)) != 0) 
			return rc;
                } else {
                    if ((rc = readDisk(h, childGE(mkey), &cbuf)) != 0) 
			return rc;
                }
            }
            if (cc >= 0 || mkey != fkey(buf)) {
                lastGEvalid = true;
                lastLTvalid = false;
                lastGE = buf->adr;
                lastGEkey = mkey - fkey(buf);
                if (cc < 0) lastGEkey -= ks(1);
            } else {
                if (lastGEvalid) lastLTvalid = true;
            }
            buf = cbuf;
        }
    }

#if BTREE_DEBUG
    DPRINTF("D %i = 0x%08x -> %i = 0x%08x\n",
	    *(long*)key,*(long*)key,(long)rec,(long)rec);
#endif

    return bErrOk;
}

bError bFindFirstKey(bHandle *h, 
		     bCursor *c,
		     void *key, 
		     bRecAddr *rec) 
{
    bError rc;                /* return code */
    bBuffer *buf;               /* buffer */

    buf = &h->root;
    while (!leaf(buf)) {
        if ((rc = readDisk(h, childLT(fkey(buf)), &buf)) != 0) return rc;
    }
    if (ct(buf) == 0) return bErrKeyNotFound;
    if (key) memcpy(key, key(fkey(buf)), h->keySize);
    if (rec) *rec = rec(fkey(buf));
    c->buffer = buf; c->key = fkey(buf);
    return bErrOk;
}

bError bFindLastKey(bHandle *h, 
		    bCursor *c,
		    void *key, 
		    bRecAddr *rec) 
{
    bError rc;                /* return code */
    bBuffer *buf;               /* buffer */

    buf = &h->root;
    while (!leaf(buf)) {
        if ((rc = readDisk(h, childGE(lkey(buf)), &buf)) != 0) return rc;
    }
    if (ct(buf) == 0) return bErrKeyNotFound;
    if (key) memcpy(key, key(lkey(buf)), h->keySize);
    if (rec) *rec = rec(lkey(buf));
    c->buffer = buf; c->key = lkey(buf);
    return bErrOk;
}

bError bFindNextKey(bHandle *h, 
		    bCursor *c,
		    void *key, 
		    bRecAddr *rec) 
{
    bError rc;                /* return code */
    bKey *nkey;              /* next key */
    bBuffer *buf;               /* buffer */

    if ((buf = c->buffer) == NULL) return bErrKeyNotFound;
    if (c->key == lkey(buf)) {
        /* current key is last key in leaf node */
        if (next(buf)) {
            /* fetch next set */
            if ((rc = readDisk(h, next(buf), &buf)) != 0) return rc;
            nkey = fkey(buf);
        } else {
            /* no more sets */
            return bErrKeyNotFound;
        }
    } else {
        /* bump to next key */
        nkey = c->key + ks(1);
    }
    if (key) memcpy(key, key(nkey), h->keySize);
    if (rec) *rec = rec(nkey);
    c->buffer = buf; c->key = nkey;
    return bErrOk;
}

bError bFindPrevKey(bHandle *h, 
		    bCursor *c,
		    void *key, 
		    bRecAddr *rec)
{
    bError rc;                /* return code */
    bKey *pkey;              /* previous key */
    bKey *fkey;              /* first key */
    bBuffer *buf;               /* buffer */

    if ((buf = c->buffer) == NULL) return bErrKeyNotFound;
    fkey = fkey(buf);
    if (c->key == fkey) {
        /* current key is first key in leaf node */
        if (prev(buf)) {
            /* fetch previous set */
            if ((rc = readDisk(h, prev(buf), &buf)) != 0) return rc;
            pkey = fkey(buf) + ks((ct(buf) - 1));
        } else {
            /* no more sets */
            return bErrKeyNotFound;
        }
    } else {
        /* bump to previous key */
        pkey = c->key - ks(1);
    }
    if (key) memcpy(key, key(pkey), h->keySize);
    if (rec) *rec = rec(pkey);
    c->buffer = buf; c->key = pkey;
    return bErrOk;
}

bError bCursorReadData(bHandle *h, 
		       bCursor *c,
		       void *key, 
		       bRecAddr *rec)
{
    if (c->buffer == NULL || !c->buffer->valid)
	return bErrBufferInvalid;
    if (key) memcpy(key, key(c->key), h->keySize);
    if (rec) *rec = rec(c->key);
    return bErrOk;
}

int bValidateTree(bHandle *h) 
{
    char *visited;

    visited = (char*)calloc(10240,1);
    if (!visited)
	return -1;
    flushAll(h);
    DPRINTF("Validating BTree with handle %0lx, root buffer at %0lx",
	    (long)h,(long)&h->root);
    return _validateTree(h,&h->root,visited,1);
}

#if 0

static
int comp(const void *key1, const void *key2) {
    unsigned int const *p1;
    unsigned int const *p2;
    p1 = key1; p2 = key2;
    return (*p1 == *p2) ? CC_EQ : (*p1 > *p2 ) ? CC_GT : CC_LT;
}

#define DO(xyz) \
    if ((rc = xyz) != bErrOk) {				\
        DPRINTF("Error in line %d: rc = %d\n", __LINE__, rc);	\
        exit(0);					\
    }

int main(void) {
    bDescription info;
    bHandle *handle;
    bCursor c;
    bError rc;
    long key;
    bRecAddr value;

    remove("t1.dat");

    info.iName = "t1.dat";
    info.keySize = sizeof(int);
    info.dupKeys = false;
    info.sectorSize = 256;
    info.comp = comp;

    DO (bOpen(info, &handle));

    key = 0x123;
    value = 0x456;
    DO (bInsertKey(handle, &key, value));

    DO (bFindKey(handle, &c, &key, &value));
    DPRINTF("Found key %x with value %x\n",key,value);

    bFlush(handle);
    DPRINTF("Buffers flushed.\n");

    key = 0x222;
    value = 0xbeef;
    DO (bInsertKey(handle, &key, value));

    key = 0x123;
    DO (bFindKey(handle, &c, &key, &value));
    DPRINTF("Found key %x with value %x\n",key,value);

    key = 0x222;
    DO (bFindKey(handle, &c, &key, &value));
    DPRINTF("Found key %x with value %x\n",key,value);

    DPRINTF("statistics:\n");
    DPRINTF("    maximum height: %8d\n", handle->maxHeight);
    DPRINTF("    nodes inserted: %8d\n", handle->nNodesIns);
    DPRINTF("    nodes deleted:  %8d\n", handle->nNodesDel);
    DPRINTF("    keys inserted:  %8d\n", handle->nKeysIns);
    DPRINTF("    keys deleted:   %8d\n", handle->nKeysDel);
    DPRINTF("    disk reads:     %8d\n", handle->nDiskReads);
    DPRINTF("    disk writes:    %8d\n", handle->nDiskWrites);

    bClose(handle);

    /* Second time... */
    DPRINTF("\nSecond time...\n\n");

    DO (bOpen(info, &handle));

    key = 0x123;
    DO (bFindKey(handle, &c, &key, &value));
    DPRINTF("Found key %x with value %x\n",key,value);

    key = 0x222;
    DO (bFindKey(handle, &c, &key, &value));
    DPRINTF("Found key %x with value %x\n",key,value);

    DPRINTF("statistics:\n");
    DPRINTF("    maximum height: %8d\n", handle->maxHeight);
    DPRINTF("    nodes inserted: %8d\n", handle->nNodesIns);
    DPRINTF("    nodes deleted:  %8d\n", handle->nNodesDel);
    DPRINTF("    keys inserted:  %8d\n", handle->nKeysIns);
    DPRINTF("    keys deleted:   %8d\n", handle->nKeysDel);
    DPRINTF("    disk reads:     %8d\n", handle->nDiskReads);
    DPRINTF("    disk writes:    %8d\n", handle->nDiskWrites);

    bClose(handle);

    return 0;
}

#endif