about summary refs log tree commit diff
path: root/db2/hash/hash_page.c
blob: 5b3463947beb6dbba8f1c5628b493fc2d39224cf (plain) (blame)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998
 *	Sleepycat Software.  All rights reserved.
 */
/*
 * Copyright (c) 1990, 1993, 1994
 *	Margo Seltzer.  All rights reserved.
 */
/*
 * Copyright (c) 1990, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Margo Seltzer.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)hash_page.c	10.40 (Sleepycat) 6/2/98";
#endif /* not lint */

/*
 * PACKAGE:  hashing
 *
 * DESCRIPTION:
 *      Page manipulation for hashing package.
 *
 * ROUTINES:
 *
 * External
 *      __get_page
 *      __add_ovflpage
 *      __overflow_page
 * Internal
 *      open_temp
 */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <errno.h>
#include <string.h>
#endif

#include "db_int.h"
#include "db_page.h"
#include "hash.h"

static int __ham_lock_bucket __P((DB *, HASH_CURSOR *, db_lockmode_t));

#ifdef DEBUG_SLOW
static void  __account_page(HTAB *, db_pgno_t, int);
#endif

/*
 * PUBLIC: int __ham_item __P((HTAB *, HASH_CURSOR *, db_lockmode_t));
 */
int
__ham_item(hashp, cursorp, mode)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
	db_lockmode_t mode;
{
	db_pgno_t next_pgno;
	int ret;

	if (F_ISSET(cursorp, H_DELETED))
		return (EINVAL);
	F_CLR(cursorp, H_OK | H_NOMORE);

	/* Check if we need to get a page for this cursor. */
	if ((ret = __ham_get_cpage(hashp, cursorp, mode)) != 0)
		return (ret);

	/* Check if we are looking for space in which to insert an item. */
	if (cursorp->seek_size && cursorp->seek_found_page == PGNO_INVALID
	    && cursorp->seek_size < P_FREESPACE(cursorp->pagep))
		cursorp->seek_found_page = cursorp->pgno;

	/* Check if we need to go on to the next page. */
	if (F_ISSET(cursorp, H_ISDUP) && cursorp->dpgno == PGNO_INVALID)
		/*
		 * ISDUP is set, and offset is at the beginning of the datum.
		 * We need to grab the length of the datum, then set the datum
		 * pointer to be the beginning of the datum.
		 */
		memcpy(&cursorp->dup_len,
		    HKEYDATA_DATA(H_PAIRDATA(cursorp->pagep, cursorp->bndx)) +
		    cursorp->dup_off, sizeof(db_indx_t));
	else if (F_ISSET(cursorp, H_ISDUP)) {
		/* Make sure we're not about to run off the page. */
		if (cursorp->dpagep == NULL && (ret = __ham_get_page(hashp->dbp,
		    cursorp->dpgno, &cursorp->dpagep)) != 0)
			return (ret);

		if (cursorp->dndx >= NUM_ENT(cursorp->dpagep)) {
			if (NEXT_PGNO(cursorp->dpagep) == PGNO_INVALID) {
				if ((ret = __ham_put_page(hashp->dbp,
				    cursorp->dpagep, 0)) != 0)
					return (ret);
				F_CLR(cursorp, H_ISDUP);
				cursorp->dpagep = NULL;
				cursorp->dpgno = PGNO_INVALID;
				cursorp->dndx = NDX_INVALID;
				cursorp->bndx++;
			} else if ((ret = __ham_next_cpage(hashp, cursorp,
			    NEXT_PGNO(cursorp->dpagep), 0, H_ISDUP)) != 0)
				return (ret);
		}
	}

	if (cursorp->bndx >= (db_indx_t)H_NUMPAIRS(cursorp->pagep)) {
		/* Fetch next page. */
		if (NEXT_PGNO(cursorp->pagep) == PGNO_INVALID) {
			F_SET(cursorp, H_NOMORE);
			if (cursorp->dpagep != NULL &&
			    (ret = __ham_put_page(hashp->dbp,
			    cursorp->dpagep, 0)) != 0)
				return (ret);
			cursorp->dpgno = PGNO_INVALID;
			return (DB_NOTFOUND);
		}
		next_pgno = NEXT_PGNO(cursorp->pagep);
		cursorp->bndx = 0;
		if ((ret = __ham_next_cpage(hashp,
		    cursorp, next_pgno, 0, 0)) != 0)
			return (ret);
	}

	F_SET(cursorp, H_OK);
	return (0);
}

/*
 * PUBLIC: int __ham_item_reset __P((HTAB *, HASH_CURSOR *));
 */
int
__ham_item_reset(hashp, cursorp)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
{
	int ret;

	if (cursorp->pagep)
		ret = __ham_put_page(hashp->dbp, cursorp->pagep, 0);
	else
		ret = 0;

	__ham_item_init(cursorp);
	return (ret);
}

/*
 * PUBLIC: void __ham_item_init __P((HASH_CURSOR *));
 */
void
__ham_item_init(cursorp)
	HASH_CURSOR *cursorp;
{
	cursorp->pagep = NULL;
	cursorp->bucket = BUCKET_INVALID;
	cursorp->lock = 0;
	cursorp->bndx = NDX_INVALID;
	cursorp->pgno = PGNO_INVALID;
	cursorp->dpgno = PGNO_INVALID;
	cursorp->dndx = NDX_INVALID;
	cursorp->dpagep = NULL;
	cursorp->flags = 0;
	cursorp->seek_size = 0;
	cursorp->seek_found_page = PGNO_INVALID;
}

/*
 * PUBLIC: int __ham_item_done __P((HTAB *, HASH_CURSOR *, int));
 */
int
__ham_item_done(hashp, cursorp, dirty)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
	int dirty;
{
	int ret, t_ret;

	t_ret = ret = 0;

	if (cursorp->pagep)
		ret = __ham_put_page(hashp->dbp, cursorp->pagep,
		    dirty && cursorp->dpagep == NULL);
	cursorp->pagep = NULL;

	if (cursorp->dpagep)
		t_ret = __ham_put_page(hashp->dbp, cursorp->dpagep, dirty);
	cursorp->dpagep = NULL;

	if (ret == 0 && t_ret != 0)
		ret = t_ret;

	/*
	 * If we are running with transactions, then we must
	 * not relinquish locks explicitly.
	 */
	if (cursorp->lock && hashp->dbp->txn == NULL)
	    t_ret = lock_put(hashp->dbp->dbenv->lk_info, cursorp->lock);
	cursorp->lock = 0;


	/*
	 * We don't throw out the page number since we might want to
	 * continue getting on this page.
	 */
	return (ret != 0 ? ret : t_ret);
}

/*
 * Returns the last item in a bucket.
 *
 * PUBLIC: int __ham_item_last __P((HTAB *, HASH_CURSOR *, db_lockmode_t));
 */
int
__ham_item_last(hashp, cursorp, mode)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
	db_lockmode_t mode;
{
	int ret;

	if ((ret = __ham_item_reset(hashp, cursorp)) != 0)
		return (ret);

	cursorp->bucket = hashp->hdr->max_bucket;
	F_SET(cursorp, H_OK);
	return (__ham_item_prev(hashp, cursorp, mode));
}

/*
 * PUBLIC: int __ham_item_first __P((HTAB *, HASH_CURSOR *, db_lockmode_t));
 */
int
__ham_item_first(hashp, cursorp, mode)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
	db_lockmode_t mode;
{
	int ret;

	if ((ret = __ham_item_reset(hashp, cursorp)) != 0)
		return (ret);
	F_SET(cursorp, H_OK);
	cursorp->bucket = 0;
	return (__ham_item_next(hashp, cursorp, mode));
}

/*
 * __ham_item_prev --
 *	Returns a pointer to key/data pair on a page.  In the case of
 *	bigkeys, just returns the page number and index of the bigkey
 *	pointer pair.
 *
 * PUBLIC: int __ham_item_prev __P((HTAB *, HASH_CURSOR *, db_lockmode_t));
 */
int
__ham_item_prev(hashp, cursorp, mode)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
	db_lockmode_t mode;
{
	db_pgno_t next_pgno;
	int ret;

	/*
	 * There are N cases for backing up in a hash file.
	 * Case 1: In the middle of a page, no duplicates, just dec the index.
	 * Case 2: In the middle of a duplicate set, back up one.
	 * Case 3: At the beginning of a duplicate set, get out of set and
	 *	back up to next key.
	 * Case 4: At the beginning of a page; go to previous page.
	 * Case 5: At the beginning of a bucket; go to prev bucket.
	 */
	F_CLR(cursorp, H_OK | H_NOMORE | H_DELETED);

	/*
	 * First handle the duplicates.  Either you'll get the key here
	 * or you'll exit the duplicate set and drop into the code below
	 * to handle backing up through keys.
	 */
	if (F_ISSET(cursorp, H_ISDUP)) {
		if (cursorp->dpgno == PGNO_INVALID) {
			/* Duplicates are on-page. */
			if (cursorp->dup_off != 0) {
				if ((ret = __ham_get_cpage(hashp,
				    cursorp, mode)) != 0)
					return (ret);
				else {
					HASH_CURSOR *h;
					h = cursorp;
					memcpy(&h->dup_len, HKEYDATA_DATA(
					    H_PAIRDATA(h->pagep, h->bndx))
					    + h->dup_off - sizeof(db_indx_t),
					    sizeof(db_indx_t));
					cursorp->dup_off -=
					    DUP_SIZE(cursorp->dup_len);
					cursorp->dndx--;
					return (__ham_item(hashp,
					    cursorp, mode));
				}
			}
		} else if (cursorp->dndx > 0) {	/* Duplicates are off-page. */
			cursorp->dndx--;
			return (__ham_item(hashp, cursorp, mode));
		} else if ((ret = __ham_get_cpage(hashp, cursorp, mode)) != 0)
			return (ret);
		else if (PREV_PGNO(cursorp->dpagep) == PGNO_INVALID) {
			F_CLR(cursorp, H_ISDUP); /* End of dups */
			cursorp->dpgno = PGNO_INVALID;
			if (cursorp->dpagep != NULL)
				(void)__ham_put_page(hashp->dbp,
				    cursorp->dpagep, 0);
			cursorp->dpagep = NULL;
		} else if ((ret = __ham_next_cpage(hashp, cursorp,
		    PREV_PGNO(cursorp->dpagep), 0, H_ISDUP)) != 0)
			return (ret);
		else {
			cursorp->dndx = NUM_ENT(cursorp->pagep) - 1;
			return (__ham_item(hashp, cursorp, mode));
		}
	}

	/*
	 * If we get here, we are not in a duplicate set, and just need
	 * to back up the cursor.  There are still three cases:
	 * midpage, beginning of page, beginning of bucket.
	 */

	if (cursorp->bndx == 0) { 		/* Beginning of page. */
		if ((ret = __ham_get_cpage(hashp, cursorp, mode)) != 0)
			return (ret);
		cursorp->pgno = PREV_PGNO(cursorp->pagep);
		if (cursorp->pgno == PGNO_INVALID) {
			/* Beginning of bucket. */
			F_SET(cursorp, H_NOMORE);
			return (DB_NOTFOUND);
		} else if ((ret = __ham_next_cpage(hashp,
		    cursorp, cursorp->pgno, 0, 0)) != 0)
			return (ret);
		else
			cursorp->bndx = H_NUMPAIRS(cursorp->pagep);
	}

	/*
	 * Either we've got the cursor set up to be decremented, or we
	 * have to find the end of a bucket.
	 */
	if (cursorp->bndx == NDX_INVALID) {
		if (cursorp->pagep == NULL)
			next_pgno = BUCKET_TO_PAGE(hashp, cursorp->bucket);
		else
			goto got_page;

		do {
			if ((ret = __ham_next_cpage(hashp,
			    cursorp, next_pgno, 0, 0)) != 0)
				return (ret);
got_page:		next_pgno = NEXT_PGNO(cursorp->pagep);
			cursorp->bndx = H_NUMPAIRS(cursorp->pagep);
		} while (next_pgno != PGNO_INVALID);

		if (cursorp->bndx == 0) {
			/* Bucket was empty. */
			F_SET(cursorp, H_NOMORE);
			return (DB_NOTFOUND);
		}
	}

	cursorp->bndx--;

	return (__ham_item(hashp, cursorp, mode));
}

/*
 * Sets the cursor to the next key/data pair on a page.
 *
 * PUBLIC: int __ham_item_next __P((HTAB *, HASH_CURSOR *, db_lockmode_t));
 */
int
__ham_item_next(hashp, cursorp, mode)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
	db_lockmode_t mode;
{
	/*
	 * Deleted on-page duplicates are a weird case. If we delete the last
	 * one, then our cursor is at the very end of a duplicate set and
	 * we actually need to go on to the next key.
	 */
	if (F_ISSET(cursorp, H_DELETED)) {
		if (cursorp->bndx != NDX_INVALID &&
		    F_ISSET(cursorp, H_ISDUP) &&
		    cursorp->dpgno == PGNO_INVALID &&
		    cursorp->dup_tlen == cursorp->dup_off) {
			F_CLR(cursorp, H_ISDUP);
			cursorp->dpgno = PGNO_INVALID;
			cursorp->bndx++;
		}
		F_CLR(cursorp, H_DELETED);
	} else if (cursorp->bndx == NDX_INVALID) {
		cursorp->bndx = 0;
		cursorp->dpgno = PGNO_INVALID;
		F_CLR(cursorp, H_ISDUP);
	} else if (F_ISSET(cursorp, H_ISDUP) && cursorp->dpgno != PGNO_INVALID)
		cursorp->dndx++;
	else if (F_ISSET(cursorp, H_ISDUP)) {
		cursorp->dndx++;
		cursorp->dup_off += DUP_SIZE(cursorp->dup_len);
		if (cursorp->dup_off >= cursorp->dup_tlen) {
			F_CLR(cursorp, H_ISDUP);
			cursorp->dpgno = PGNO_INVALID;
			cursorp->bndx++;
		}
	} else
		cursorp->bndx++;

	return (__ham_item(hashp, cursorp, mode));
}

/*
 * PUBLIC: void __ham_putitem __P((PAGE *p, const DBT *, int));
 *
 * This is a little bit sleazy in that we're overloading the meaning
 * of the H_OFFPAGE type here.  When we recover deletes, we have the
 * entire entry instead of having only the DBT, so we'll pass type
 * H_OFFPAGE to mean, "copy the whole entry" as opposed to constructing
 * an H_KEYDATA around it.
 */
void
__ham_putitem(p, dbt, type)
	PAGE *p;
	const DBT *dbt;
	int type;
{
	u_int16_t n, off;

	n = NUM_ENT(p);

	/* Put the item element on the page. */
	if (type == H_OFFPAGE) {
		off = HOFFSET(p) - dbt->size;
		HOFFSET(p) = p->inp[n] = off;
		memcpy(P_ENTRY(p, n), dbt->data, dbt->size);
	} else {
		off = HOFFSET(p) - HKEYDATA_SIZE(dbt->size);
		HOFFSET(p) = p->inp[n] = off;
		PUT_HKEYDATA(P_ENTRY(p, n), dbt->data, dbt->size, type);
	}

	/* Adjust page info. */
	NUM_ENT(p) += 1;
}

/*
 * PUBLIC: void __ham_reputpair
 * PUBLIC:    __P((PAGE *p, u_int32_t, u_int32_t, const DBT *, const DBT *));
 *
 * This is a special case to restore a key/data pair to its original
 * location during recovery.  We are guaranteed that the pair fits
 * on the page and is not the last pair on the page (because if it's
 * the last pair, the normal insert works).
 */
void
__ham_reputpair(p, psize, ndx, key, data)
	PAGE *p;
	u_int32_t psize, ndx;
	const DBT *key, *data;
{
	db_indx_t i, movebytes, newbytes;
	u_int8_t *from;

	/* First shuffle the existing items up on the page.  */
	movebytes =
	    (ndx == 0 ? psize : p->inp[H_DATAINDEX(ndx - 1)]) - HOFFSET(p);
	newbytes = key->size + data->size;
	from = (u_int8_t *)p + HOFFSET(p);
	memmove(from - newbytes, from, movebytes);

	/*
	 * Adjust the indices and move them up 2 spaces. Note that we
	 * have to check the exit condition inside the loop just in case
	 * we are dealing with index 0 (db_indx_t's are unsigned).
	 */
	for (i = NUM_ENT(p) - 1; ; i-- ) {
		p->inp[i + 2] = p->inp[i] - newbytes;
		if (i == H_KEYINDEX(ndx))
			break;
	}

	/* Put the key and data on the page. */
	p->inp[H_KEYINDEX(ndx)] =
	    (ndx == 0 ? psize : p->inp[H_DATAINDEX(ndx - 1)]) - key->size;
	p->inp[H_DATAINDEX(ndx)] = p->inp[H_KEYINDEX(ndx)] - data->size;
	memcpy(P_ENTRY(p, H_KEYINDEX(ndx)), key->data, key->size);
	memcpy(P_ENTRY(p, H_DATAINDEX(ndx)), data->data, data->size);

	/* Adjust page info. */
	HOFFSET(p) -= newbytes;
	NUM_ENT(p) += 2;
}


/*
 * PUBLIC: int __ham_del_pair __P((HTAB *, HASH_CURSOR *, int));
 *
 * XXX
 * TODO: if the item is an offdup, delete the other pages and then remove
 * the pair. If the offpage page is 0, then you can just remove the pair.
 */
int
__ham_del_pair(hashp, cursorp, reclaim_page)
	HTAB *hashp;
	HASH_CURSOR *cursorp;
	int reclaim_page;
{
	DBT data_dbt, key_dbt;
	DB_ENV *dbenv;
	DB_LSN new_lsn, *n_lsn, tmp_lsn;
	PAGE *p;
	db_indx_t ndx;
	db_pgno_t chg_pgno, pgno;
	int ret, tret;

	dbenv = hashp->dbp->dbenv;
	ndx = cursorp->bndx;
	if (cursorp->pagep == NULL && (ret =
	    __ham_get_page(hashp->dbp, cursorp->pgno, &cursorp->pagep)) != 0)
		return (ret);

	p = cursorp->pagep;

	/*
	 * We optimize for the normal case which is when neither the key nor
	 * the data are large.  In this case, we write a single log record
	 * and do the delete.  If either is large, we'll call __big_delete
	 * to remove the big item and then update the page to remove the
	 * entry referring to the big item.
	 */
	ret = 0;
	if (HPAGE_PTYPE(H_PAIRKEY(p, ndx)) == H_OFFPAGE) {
		memcpy(&pgno, HOFFPAGE_PGNO(P_ENTRY(p, H_KEYINDEX(ndx))),
		    sizeof(db_pgno_t));
		ret = __db_doff(hashp->dbp, pgno, __ham_del_page);
	}

	if (ret == 0)
		switch (HPAGE_PTYPE(H_PAIRDATA(p, ndx))) {
		case H_OFFPAGE:
			memcpy(&pgno,
			    HOFFPAGE_PGNO(P_ENTRY(p, H_DATAINDEX(ndx))),
			    sizeof(db_pgno_t));
			ret = __db_doff(hashp->dbp, pgno, __ham_del_page);
			break;
		case H_OFFDUP:
			memcpy(&pgno,
			    HOFFDUP_PGNO(P_ENTRY(p, H_DATAINDEX(ndx))),
			    sizeof(db_pgno_t));
			ret = __db_ddup(hashp->dbp, pgno, __ham_del_page);
			F_CLR(cursorp, H_ISDUP);
			break;
		case H_DUPLICATE:
			/*
			 * If we delete a pair that is/was a duplicate, then
			 * we had better clear the flag so that we update the
			 * cursor appropriately.
			 */
			F_CLR(cursorp, H_ISDUP);
			break;
		}

	if (ret)
		return (ret);

	/* Now log the delete off this page. */
	if (DB_LOGGING(hashp->dbp)) {
		key_dbt.data = P_ENTRY(p, H_KEYINDEX(ndx));
		key_dbt.size =
		    LEN_HITEM(p, hashp->hdr->pagesize, H_KEYINDEX(ndx));
		data_dbt.data = P_ENTRY(p, H_DATAINDEX(ndx));
		data_dbt.size =
		    LEN_HITEM(p, hashp->hdr->pagesize, H_DATAINDEX(ndx));

		if ((ret = __ham_insdel_log(dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0, DELPAIR,
		    hashp->dbp->log_fileid, PGNO(p), (u_int32_t)ndx,
		    &LSN(p), &key_dbt, &data_dbt)) != 0)
			return (ret);

		/* Move lsn onto page. */
		LSN(p) = new_lsn;
	}

	__ham_dpair(hashp->dbp, p, ndx);

	/*
	 * If we are locking, we will not maintain this.
	 * XXXX perhaps we can retain incremental numbers and apply them
	 * later.
	 */
	if (!F_ISSET(hashp->dbp, DB_AM_LOCKING))
		--hashp->hdr->nelem;

	/*
	 * If we need to reclaim the page, then check if the page is empty.
	 * There are two cases.  If it's empty and it's not the first page
	 * in the bucket (i.e., the bucket page) then we can simply remove
	 * it. If it is the first chain in the bucket, then we need to copy
	 * the second page into it and remove the second page.
	 */
	if (reclaim_page && NUM_ENT(p) == 0 && PREV_PGNO(p) == PGNO_INVALID &&
	    NEXT_PGNO(p) != PGNO_INVALID) {
		PAGE *n_pagep, *nn_pagep;
		db_pgno_t tmp_pgno;

		/*
		 * First page in chain is empty and we know that there
		 * are more pages in the chain.
		 */
		if ((ret =
		    __ham_get_page(hashp->dbp, NEXT_PGNO(p), &n_pagep)) != 0)
			return (ret);

		if (NEXT_PGNO(n_pagep) != PGNO_INVALID) {
			if ((ret =
			    __ham_get_page(hashp->dbp, NEXT_PGNO(n_pagep),
			    &nn_pagep)) != 0) {
				(void) __ham_put_page(hashp->dbp, n_pagep, 0);
				return (ret);
			}
		}

		if (DB_LOGGING(hashp->dbp)) {
			key_dbt.data = n_pagep;
			key_dbt.size = hashp->hdr->pagesize;
			if ((ret = __ham_copypage_log(dbenv->lg_info,
			    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0,
			    hashp->dbp->log_fileid, PGNO(p), &LSN(p),
			    PGNO(n_pagep), &LSN(n_pagep), NEXT_PGNO(n_pagep),
			    NEXT_PGNO(n_pagep) == PGNO_INVALID ? NULL :
			    &LSN(nn_pagep), &key_dbt)) != 0)
				return (ret);

			/* Move lsn onto page. */
			LSN(p) = new_lsn;	/* Structure assignment. */
			LSN(n_pagep) = new_lsn;
			if (NEXT_PGNO(n_pagep) != PGNO_INVALID)
				LSN(nn_pagep) = new_lsn;
		}
		if (NEXT_PGNO(n_pagep) != PGNO_INVALID) {
			PREV_PGNO(nn_pagep) = PGNO(p);
			(void)__ham_put_page(hashp->dbp, nn_pagep, 1);
		}

		tmp_pgno = PGNO(p);
		tmp_lsn = LSN(p);
		memcpy(p, n_pagep, hashp->hdr->pagesize);
		PGNO(p) = tmp_pgno;
		LSN(p) = tmp_lsn;
		PREV_PGNO(p) = PGNO_INVALID;

		/*
		 * Cursor is advanced to the beginning of the next page.
		 */
		cursorp->bndx = 0;
		cursorp->pgno = PGNO(p);
		F_SET(cursorp, H_DELETED);
		chg_pgno = PGNO(p);
		if ((ret = __ham_dirty_page(hashp, p)) != 0 ||
		    (ret = __ham_del_page(hashp->dbp, n_pagep)) != 0)
			return (ret);
	} else if (reclaim_page &&
	    NUM_ENT(p) == 0 && PREV_PGNO(p) != PGNO_INVALID) {
		PAGE *n_pagep, *p_pagep;

		if ((ret =
		    __ham_get_page(hashp->dbp, PREV_PGNO(p), &p_pagep)) != 0)
			return (ret);

		if (NEXT_PGNO(p) != PGNO_INVALID) {
			if ((ret = __ham_get_page(hashp->dbp,
			    NEXT_PGNO(p), &n_pagep)) != 0) {
				(void)__ham_put_page(hashp->dbp, p_pagep, 0);
				return (ret);
			}
			n_lsn = &LSN(n_pagep);
		} else {
			n_pagep = NULL;
			n_lsn = NULL;
		}

		NEXT_PGNO(p_pagep) = NEXT_PGNO(p);
		if (n_pagep != NULL)
			PREV_PGNO(n_pagep) = PGNO(p_pagep);

		if (DB_LOGGING(hashp->dbp)) {
			if ((ret = __ham_newpage_log(dbenv->lg_info,
			    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0, DELOVFL,
			    hashp->dbp->log_fileid, PREV_PGNO(p), &LSN(p_pagep),
			    PGNO(p), &LSN(p), NEXT_PGNO(p), n_lsn)) != 0)
				return (ret);

			/* Move lsn onto page. */
			LSN(p_pagep) = new_lsn;	/* Structure assignment. */
			if (n_pagep)
				LSN(n_pagep) = new_lsn;
			LSN(p) = new_lsn;
		}
		cursorp->pgno = NEXT_PGNO(p);
		cursorp->bndx = 0;
		/*
		 * Since we are about to delete the cursor page and we have
		 * just moved the cursor, we need to make sure that the
		 * old page pointer isn't left hanging around in the cursor.
		 */
		cursorp->pagep = NULL;
		chg_pgno = PGNO(p);
		ret = __ham_del_page(hashp->dbp, p);
		if ((tret = __ham_put_page(hashp->dbp, p_pagep, 1)) != 0 &&
		    ret == 0)
			ret = tret;
		if (n_pagep != NULL &&
		    (tret = __ham_put_page(hashp->dbp, n_pagep, 1)) != 0 &&
		    ret == 0)
			ret = tret;
		if (ret != 0)
			return (ret);
	} else {
		/*
		 * Mark item deleted so that we don't try to return it, and
		 * so that we update the cursor correctly on the next call
		 * to next.
		 */
		F_SET(cursorp, H_DELETED);
		chg_pgno = cursorp->pgno;
		ret = __ham_dirty_page(hashp, p);
	}
	__ham_c_update(cursorp, chg_pgno, 0, 0, 0);

	/*
	 * Since we just deleted a pair from the master page, anything
	 * in cursorp->dpgno should be cleared.
	 */
	cursorp->dpgno = PGNO_INVALID;

	F_CLR(cursorp, H_OK);
	return (ret);
}

/*
 * __ham_replpair --
 *	Given the key data indicated by the cursor, replace part/all of it
 *	according to the fields in the dbt.
 *
 * PUBLIC: int __ham_replpair __P((HTAB *, HASH_CURSOR *, DBT *, u_int32_t));
 */
int
__ham_replpair(hashp, hcp, dbt, make_dup)
	HTAB *hashp;
	HASH_CURSOR *hcp;
	DBT *dbt;
	u_int32_t make_dup;
{
	DBT old_dbt, tdata, tmp;
	DB_LSN	new_lsn;
	int32_t change;			/* XXX: Possible overflow. */
	u_int32_t len;
	int is_big, ret, type;
	u_int8_t *beg, *dest, *end, *hk, *src;

	/*
	 * Big item replacements are handled in generic code.
	 * Items that fit on the current page fall into 4 classes.
	 * 1. On-page element, same size
	 * 2. On-page element, new is bigger (fits)
	 * 3. On-page element, new is bigger (does not fit)
	 * 4. On-page element, old is bigger
	 * Numbers 1, 2, and 4 are essentially the same (and should
	 * be the common case).  We handle case 3 as a delete and
	 * add.
	 */

	/*
	 * We need to compute the number of bytes that we are adding or
	 * removing from the entry.  Normally, we can simply substract
	 * the number of bytes we are replacing (dbt->dlen) from the
	 * number of bytes we are inserting (dbt->size).  However, if
	 * we are doing a partial put off the end of a record, then this
	 * formula doesn't work, because we are essentially adding
	 * new bytes.
	 */
	change = dbt->size - dbt->dlen;

	hk = H_PAIRDATA(hcp->pagep, hcp->bndx);
	is_big = HPAGE_PTYPE(hk) == H_OFFPAGE;

	if (is_big)
		memcpy(&len, HOFFPAGE_TLEN(hk), sizeof(u_int32_t));
	else
		len = LEN_HKEYDATA(hcp->pagep,
		    hashp->dbp->pgsize, H_DATAINDEX(hcp->bndx));

	if (dbt->doff + dbt->dlen > len)
		change += dbt->doff + dbt->dlen - len;


	if (change > (int32_t)P_FREESPACE(hcp->pagep) || is_big) {
		/*
		 * Case 3 -- two subcases.
		 * A. This is not really a partial operation, but an overwrite.
		 *    Simple del and add works.
		 * B. This is a partial and we need to construct the data that
		 *    we are really inserting (yuck).
		 * In both cases, we need to grab the key off the page (in
		 * some cases we could do this outside of this routine; for
		 * cleanliness we do it here.  If you happen to be on a big
		 * key, this could be a performance hit).
		 */
		tmp.flags = 0;
		F_SET(&tmp, DB_DBT_MALLOC | DB_DBT_INTERNAL);
		if ((ret =
		    __db_ret(hashp->dbp, hcp->pagep, H_KEYINDEX(hcp->bndx),
		    &tmp, &hcp->big_key, &hcp->big_keylen)) != 0)
			return (ret);

		if (dbt->doff == 0 && dbt->dlen == len) {
			ret = __ham_del_pair(hashp, hcp, 0);
			if (ret == 0)
			    ret = __ham_add_el(hashp,
			        hcp, &tmp, dbt, H_KEYDATA);
		} else {					/* Case B */
			type = HPAGE_PTYPE(hk) != H_OFFPAGE ?
			    HPAGE_PTYPE(hk) : H_KEYDATA;
			tdata.flags = 0;
			F_SET(&tdata, DB_DBT_MALLOC | DB_DBT_INTERNAL);

			if ((ret = __db_ret(hashp->dbp, hcp->pagep,
			    H_DATAINDEX(hcp->bndx), &tdata, &hcp->big_data,
			    &hcp->big_datalen)) != 0)
				goto err;

			/* Now we can delete the item. */
			if ((ret = __ham_del_pair(hashp, hcp, 0)) != 0) {
				__db_free(tdata.data);
				goto err;
			}

			/* Now shift old data around to make room for new. */
			if (change > 0) {
				tdata.data = (void *)__db_realloc(tdata.data,
				    tdata.size + change);
				memset((u_int8_t *)tdata.data + tdata.size,
				    0, change);
			}
			if (tdata.data == NULL)
				return (ENOMEM);
			end = (u_int8_t *)tdata.data + tdata.size;

			src = (u_int8_t *)tdata.data + dbt->doff + dbt->dlen;
			if (src < end && tdata.size > dbt->doff + dbt->dlen) {
				len = tdata.size - dbt->doff - dbt->dlen;
				dest = src + change;
				memmove(dest, src, len);
			}
			memcpy((u_int8_t *)tdata.data + dbt->doff,
			    dbt->data, dbt->size);
			tdata.size += change;

			/* Now add the pair. */
			ret = __ham_add_el(hashp, hcp, &tmp, &tdata, type);
			__db_free(tdata.data);
		}
err:		__db_free(tmp.data);
		return (ret);
	}

	/*
	 * Set up pointer into existing data. Do it before the log
	 * message so we can use it inside of the log setup.
	 */
	beg = HKEYDATA_DATA(H_PAIRDATA(hcp->pagep, hcp->bndx));
	beg += dbt->doff;

	/*
	 * If we are going to have to move bytes at all, figure out
	 * all the parameters here.  Then log the call before moving
	 * anything around.
	 */
	if (DB_LOGGING(hashp->dbp)) {
		old_dbt.data = beg;
		old_dbt.size = dbt->dlen;
		if ((ret = __ham_replace_log(hashp->dbp->dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0,
		    hashp->dbp->log_fileid, PGNO(hcp->pagep),
		    (u_int32_t)H_DATAINDEX(hcp->bndx), &LSN(hcp->pagep),
		    (u_int32_t)dbt->doff, &old_dbt, dbt, make_dup)) != 0)
			return (ret);

		LSN(hcp->pagep) = new_lsn;	/* Structure assignment. */
	}

	__ham_onpage_replace(hcp->pagep, hashp->dbp->pgsize,
	    (u_int32_t)H_DATAINDEX(hcp->bndx), (int32_t)dbt->doff, change, dbt);

	return (0);
}

/*
 * Replace data on a page with new data, possibly growing or shrinking what's
 * there.  This is called on two different occasions. On one (from replpair)
 * we are interested in changing only the data.  On the other (from recovery)
 * we are replacing the entire data (header and all) with a new element.  In
 * the latter case, the off argument is negative.
 * pagep: the page that we're changing
 * ndx: page index of the element that is growing/shrinking.
 * off: Offset at which we are beginning the replacement.
 * change: the number of bytes (+ or -) that the element is growing/shrinking.
 * dbt: the new data that gets written at beg.
 * PUBLIC: void __ham_onpage_replace __P((PAGE *, size_t, u_int32_t, int32_t,
 * PUBLIC:     int32_t,  DBT *));
 */
void
__ham_onpage_replace(pagep, pgsize, ndx, off, change, dbt)
	PAGE *pagep;
	size_t pgsize;
	u_int32_t ndx;
	int32_t off;
	int32_t change;
	DBT *dbt;
{
	db_indx_t i;
	int32_t len;
	u_int8_t *src, *dest;
	int zero_me;

	if (change != 0) {
		zero_me = 0;
		src = (u_int8_t *)(pagep) + HOFFSET(pagep);
		if (off < 0)
			len = pagep->inp[ndx] - HOFFSET(pagep);
		else if ((u_int32_t)off >= LEN_HKEYDATA(pagep, pgsize, ndx)) {
			len = HKEYDATA_DATA(P_ENTRY(pagep, ndx)) +
			    LEN_HKEYDATA(pagep, pgsize, ndx) - src;
			zero_me = 1;
		} else
			len = (HKEYDATA_DATA(P_ENTRY(pagep, ndx)) + off) - src;
		dest = src - change;
		memmove(dest, src, len);
		if (zero_me)
			memset(dest + len, 0, change);

		/* Now update the indices. */
		for (i = ndx; i < NUM_ENT(pagep); i++)
			pagep->inp[i] -= change;
		HOFFSET(pagep) -= change;
	}
	if (off >= 0)
		memcpy(HKEYDATA_DATA(P_ENTRY(pagep, ndx)) + off,
		    dbt->data, dbt->size);
	else
		memcpy(P_ENTRY(pagep, ndx), dbt->data, dbt->size);
}

/*
 * PUBLIC: int __ham_split_page __P((HTAB *, u_int32_t, u_int32_t));
 */
int
__ham_split_page(hashp, obucket, nbucket)
	HTAB *hashp;
	u_int32_t obucket, nbucket;
{
	DBT key, page_dbt;
	DB_ENV *dbenv;
	DB_LSN new_lsn;
	PAGE **pp, *old_pagep, *temp_pagep, *new_pagep;
	db_indx_t n;
	db_pgno_t bucket_pgno, next_pgno;
	u_int32_t big_len, len;
	int ret, tret;
	void *big_buf;

	dbenv = hashp->dbp->dbenv;
	temp_pagep = old_pagep = new_pagep = NULL;

	bucket_pgno = BUCKET_TO_PAGE(hashp, obucket);
	if ((ret = __ham_get_page(hashp->dbp, bucket_pgno, &old_pagep)) != 0)
		return (ret);
	if ((ret = __ham_new_page(hashp, BUCKET_TO_PAGE(hashp, nbucket), P_HASH,
	    &new_pagep)) != 0)
		goto err;

	temp_pagep = hashp->split_buf;
	memcpy(temp_pagep, old_pagep, hashp->hdr->pagesize);

	if (DB_LOGGING(hashp->dbp)) {
		page_dbt.size = hashp->hdr->pagesize;
		page_dbt.data = old_pagep;
		if ((ret = __ham_splitdata_log(dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0,
		    hashp->dbp->log_fileid, SPLITOLD, PGNO(old_pagep),
		    &page_dbt, &LSN(old_pagep))) != 0)
			goto err;
	}

	P_INIT(old_pagep, hashp->hdr->pagesize, PGNO(old_pagep), PGNO_INVALID,
	    PGNO_INVALID, 0, P_HASH);

	if (DB_LOGGING(hashp->dbp))
		LSN(old_pagep) = new_lsn;	/* Structure assignment. */

	big_len = 0;
	big_buf = NULL;
	key.flags = 0;
	while (temp_pagep != NULL) {
		for (n = 0; n < (db_indx_t)H_NUMPAIRS(temp_pagep); n++) {
			if ((ret =
			    __db_ret(hashp->dbp, temp_pagep, H_KEYINDEX(n),
			    &key, &big_buf, &big_len)) != 0)
				goto err;

			if (__ham_call_hash(hashp, key.data, key.size)
			    == obucket)
				pp = &old_pagep;
			else
				pp = &new_pagep;

			/*
			 * Figure out how many bytes we need on the new
			 * page to store the key/data pair.
			 */

			len = LEN_HITEM(temp_pagep, hashp->hdr->pagesize,
			    H_DATAINDEX(n)) +
			    LEN_HITEM(temp_pagep, hashp->hdr->pagesize,
			    H_KEYINDEX(n)) +
			    2 * sizeof(db_indx_t);

			if (P_FREESPACE(*pp) < len) {
				if (DB_LOGGING(hashp->dbp)) {
					page_dbt.size = hashp->hdr->pagesize;
					page_dbt.data = *pp;
					if ((ret = __ham_splitdata_log(
					    dbenv->lg_info,
					    (DB_TXN *)hashp->dbp->txn,
					    &new_lsn, 0,
					    hashp->dbp->log_fileid, SPLITNEW,
					    PGNO(*pp), &page_dbt,
					    &LSN(*pp))) != 0)
						goto err;
					LSN(*pp) = new_lsn;
				}
				if ((ret = __ham_add_ovflpage(hashp,
				    *pp, 1, pp)) != 0)
					goto err;
			}
			__ham_copy_item(hashp, temp_pagep, H_KEYINDEX(n), *pp);
			__ham_copy_item(hashp, temp_pagep, H_DATAINDEX(n), *pp);
		}
		next_pgno = NEXT_PGNO(temp_pagep);

		/* Clear temp_page; if it's a link overflow page, free it. */
		if (PGNO(temp_pagep) != bucket_pgno && (ret =
		    __ham_del_page(hashp->dbp, temp_pagep)) != 0)
			goto err;

		if (next_pgno == PGNO_INVALID)
			temp_pagep = NULL;
		else if ((ret =
		    __ham_get_page(hashp->dbp, next_pgno, &temp_pagep)) != 0)
			goto err;

		if (temp_pagep != NULL && DB_LOGGING(hashp->dbp)) {
			page_dbt.size = hashp->hdr->pagesize;
			page_dbt.data = temp_pagep;
			if ((ret = __ham_splitdata_log(dbenv->lg_info,
			    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0,
			    hashp->dbp->log_fileid, SPLITOLD, PGNO(temp_pagep),
			    &page_dbt, &LSN(temp_pagep))) != 0)
				goto err;
			LSN(temp_pagep) = new_lsn;
		}
	}
	if (big_buf != NULL)
		__db_free(big_buf);

	/*
	 * If the original bucket spanned multiple pages, then we've got
	 * a pointer to a page that used to be on the bucket chain.  It
	 * should be deleted.
	 */
	if (temp_pagep != NULL && PGNO(temp_pagep) != bucket_pgno &&
	    (ret = __ham_del_page(hashp->dbp, temp_pagep)) != 0)
		goto err;

	/*
	 * Write new buckets out.
	 */
	if (DB_LOGGING(hashp->dbp)) {
		page_dbt.size = hashp->hdr->pagesize;
		page_dbt.data = old_pagep;
		if ((ret = __ham_splitdata_log(dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0,
		    hashp->dbp->log_fileid, SPLITNEW, PGNO(old_pagep),
		    &page_dbt, &LSN(old_pagep))) != 0)
			goto err;
		LSN(old_pagep) = new_lsn;

		page_dbt.data = new_pagep;
		if ((ret = __ham_splitdata_log(dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0,
		    hashp->dbp->log_fileid, SPLITNEW, PGNO(new_pagep),
		    &page_dbt, &LSN(new_pagep))) != 0)
			goto err;
		LSN(new_pagep) = new_lsn;
	}
	ret = __ham_put_page(hashp->dbp, old_pagep, 1);
	if ((tret = __ham_put_page(hashp->dbp, new_pagep, 1)) != 0 &&
	    ret == 0)
		ret = tret;

	if (0) {
err:		if (old_pagep != NULL)
			(void)__ham_put_page(hashp->dbp, old_pagep, 1);
		if (new_pagep != NULL)
			(void)__ham_put_page(hashp->dbp, new_pagep, 1);
		if (temp_pagep != NULL && PGNO(temp_pagep) != bucket_pgno)
			(void)__ham_put_page(hashp->dbp, temp_pagep, 1);
	}
	return (ret);
}

/*
 * Add the given pair to the page.  The page in question may already be
 * held (i.e. it was already gotten).  If it is, then the page is passed
 * in via the pagep parameter.  On return, pagep will contain the page
 * to which we just added something.  This allows us to link overflow
 * pages and return the new page having correctly put the last page.
 *
 * PUBLIC: int __ham_add_el
 * PUBLIC:    __P((HTAB *, HASH_CURSOR *, const DBT *, const DBT *, int));
 */
int
__ham_add_el(hashp, hcp, key, val, type)
	HTAB *hashp;
	HASH_CURSOR *hcp;
	const DBT *key, *val;
	int type;
{
	const DBT *pkey, *pdata;
	DBT key_dbt, data_dbt;
	DB_LSN new_lsn;
	HOFFPAGE doff, koff;
	db_pgno_t next_pgno;
	u_int32_t data_size, key_size, pairsize, rectype;
	int do_expand, is_keybig, is_databig, ret;
	int key_type, data_type;

	do_expand = 0;

	if (hcp->pagep == NULL && (ret = __ham_get_page(hashp->dbp,
	    hcp->seek_found_page != PGNO_INVALID ?  hcp->seek_found_page :
	    hcp->pgno, &hcp->pagep)) != 0)
		return (ret);

	key_size = HKEYDATA_PSIZE(key->size);
	data_size = HKEYDATA_PSIZE(val->size);
	is_keybig = ISBIG(hashp, key->size);
	is_databig = ISBIG(hashp, val->size);
	if (is_keybig)
		key_size = HOFFPAGE_PSIZE;
	if (is_databig)
		data_size = HOFFPAGE_PSIZE;

	pairsize = key_size + data_size;

	/* Advance to first page in chain with room for item. */
	while (H_NUMPAIRS(hcp->pagep) && NEXT_PGNO(hcp->pagep) !=
	    PGNO_INVALID) {
		/*
		 * This may not be the end of the chain, but the pair may fit
		 * anyway.  Check if it's a bigpair that fits or a regular
		 * pair that fits.
		 */
		if (P_FREESPACE(hcp->pagep) >= pairsize)
			break;
		next_pgno = NEXT_PGNO(hcp->pagep);
		if ((ret =
		    __ham_next_cpage(hashp, hcp, next_pgno, 0, 0)) != 0)
			return (ret);
	}

	/*
	 * Check if we need to allocate a new page.
	 */
	if (P_FREESPACE(hcp->pagep) < pairsize) {
		do_expand = 1;
		if ((ret = __ham_add_ovflpage(hashp,
		    hcp->pagep, 1, &hcp->pagep)) !=  0)
			return (ret);
		hcp->pgno = PGNO(hcp->pagep);
	}

	/*
	 * Update cursor.
	 */
	hcp->bndx = H_NUMPAIRS(hcp->pagep);
	F_CLR(hcp, H_DELETED);
	if (is_keybig) {
		if ((ret = __db_poff(hashp->dbp,
		    key, &koff.pgno, __ham_overflow_page)) != 0)
			return (ret);
		koff.type = H_OFFPAGE;
		koff.tlen = key->size;
		key_dbt.data = &koff;
		key_dbt.size = sizeof(koff);
		pkey = &key_dbt;
		key_type = H_OFFPAGE;
	} else {
		pkey = key;
		key_type = H_KEYDATA;
	}

	if (is_databig) {
		if ((ret = __db_poff(hashp->dbp,
		    val, &doff.pgno, __ham_overflow_page)) != 0)
			return (ret);
		doff.type = H_OFFPAGE;
		doff.tlen = val->size;
		data_dbt.data = &doff;
		data_dbt.size = sizeof(doff);
		pdata = &data_dbt;
		data_type = H_OFFPAGE;
	} else {
		pdata = val;
		data_type = type;
	}

	if (DB_LOGGING(hashp->dbp)) {
		rectype = PUTPAIR;
		if (is_databig)
			rectype |= PAIR_DATAMASK;
		if (is_keybig)
			rectype |= PAIR_KEYMASK;

		if ((ret = __ham_insdel_log(hashp->dbp->dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0, rectype,
		    hashp->dbp->log_fileid, PGNO(hcp->pagep),
		    (u_int32_t)H_NUMPAIRS(hcp->pagep),
		    &LSN(hcp->pagep), pkey, pdata)) != 0)
			return (ret);

		/* Move lsn onto page. */
		LSN(hcp->pagep) = new_lsn;	/* Structure assignment. */
	}

	__ham_putitem(hcp->pagep, pkey, key_type);
	__ham_putitem(hcp->pagep, pdata, data_type);

	/*
	 * For splits, we are going to update item_info's page number
	 * field, so that we can easily return to the same page the
	 * next time we come in here.  For other operations, this shouldn't
	 * matter, since odds are this is the last thing that happens before
	 * we return to the user program.
	 */
	hcp->pgno = PGNO(hcp->pagep);

	/*
	 * XXX Maybe keep incremental numbers here
	 */
	if (!F_ISSET(hashp->dbp, DB_AM_LOCKING))
		hashp->hdr->nelem++;

	if (do_expand || (hashp->hdr->ffactor != 0 &&
	    (u_int32_t)H_NUMPAIRS(hcp->pagep) > hashp->hdr->ffactor))
		F_SET(hcp, H_EXPAND);
	return (0);
}


/*
 * Special __putitem call used in splitting -- copies one entry to
 * another.  Works for all types of hash entries (H_OFFPAGE, H_KEYDATA,
 * H_DUPLICATE, H_OFFDUP).  Since we log splits at a high level, we
 * do not need to do any logging here.
 *
 * PUBLIC: void __ham_copy_item __P((HTAB *, PAGE *, u_int32_t, PAGE *));
 */
void
__ham_copy_item(hashp, src_page, src_ndx, dest_page)
	HTAB *hashp;
	PAGE *src_page;
	u_int32_t src_ndx;
	PAGE *dest_page;
{
	u_int32_t len;
	void *src, *dest;

	/*
	 * Copy the key and data entries onto this new page.
	 */
	src = P_ENTRY(src_page, src_ndx);

	/* Set up space on dest. */
	len = LEN_HITEM(src_page, hashp->hdr->pagesize, src_ndx);
	HOFFSET(dest_page) -= len;
	dest_page->inp[NUM_ENT(dest_page)] = HOFFSET(dest_page);
	dest = P_ENTRY(dest_page, NUM_ENT(dest_page));
	NUM_ENT(dest_page)++;

	memcpy(dest, src, len);
}

/*
 *
 * Returns:
 *      pointer on success
 *      NULL on error
 *
 * PUBLIC: int __ham_add_ovflpage __P((HTAB *, PAGE *, int, PAGE **));
 */
int
__ham_add_ovflpage(hashp, pagep, release, pp)
	HTAB *hashp;
	PAGE *pagep;
	int release;
	PAGE **pp;
{
	DB_ENV *dbenv;
	DB_LSN new_lsn;
	PAGE *new_pagep;
	int ret;

	dbenv = hashp->dbp->dbenv;

	if ((ret = __ham_overflow_page(hashp->dbp, P_HASH, &new_pagep)) != 0)
		return (ret);

	if (DB_LOGGING(hashp->dbp)) {
		if ((ret = __ham_newpage_log(dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0, PUTOVFL,
		    hashp->dbp->log_fileid, PGNO(pagep), &LSN(pagep),
		    PGNO(new_pagep), &LSN(new_pagep), PGNO_INVALID, NULL)) != 0)
			return (ret);

		/* Move lsn onto page. */
		LSN(pagep) = LSN(new_pagep) = new_lsn;
	}
	NEXT_PGNO(pagep) = PGNO(new_pagep);
	PREV_PGNO(new_pagep) = PGNO(pagep);

	if (release)
		ret = __ham_put_page(hashp->dbp, pagep, 1);

	hashp->hash_overflows++;
	*pp = new_pagep;
	return (ret);
}


/*
 * PUBLIC: int __ham_new_page __P((HTAB *, u_int32_t, u_int32_t, PAGE **));
 */
int
__ham_new_page(hashp, addr, type, pp)
	HTAB *hashp;
	u_int32_t addr, type;
	PAGE **pp;
{
	PAGE *pagep;
	int ret;

	if ((ret = memp_fget(hashp->dbp->mpf,
	    &addr, DB_MPOOL_CREATE, &pagep)) != 0)
		return (ret);

#ifdef DEBUG_SLOW
	__account_page(hashp, addr, 1);
#endif
	/* This should not be necessary because page-in should do it. */
	P_INIT(pagep,
	    hashp->hdr->pagesize, addr, PGNO_INVALID, PGNO_INVALID, 0, type);

	*pp = pagep;
	return (0);
}

/*
 * PUBLIC: int __ham_del_page __P((DB *, PAGE *));
 */
int
__ham_del_page(dbp, pagep)
	DB *dbp;
	PAGE *pagep;
{
	DB_LSN new_lsn;
	HTAB *hashp;
	int ret;

	hashp = (HTAB *)dbp->internal;
	ret = 0;
	DIRTY_META(hashp, ret);
	if (ret != 0) {
		if (ret != EAGAIN)
			__db_err(hashp->dbp->dbenv,
			    "free_ovflpage: unable to lock meta data page %s\n",
			    strerror(ret));
		/*
		 * If we are going to return an error, then we should free
		 * the page, so it doesn't stay pinned forever.
		 */
		(void)__ham_put_page(hashp->dbp, pagep, 0);
		return (ret);
	}

	if (DB_LOGGING(hashp->dbp)) {
		if ((ret = __ham_newpgno_log(hashp->dbp->dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0, DELPGNO,
		    hashp->dbp->log_fileid, PGNO(pagep), hashp->hdr->last_freed,
		    (u_int32_t)TYPE(pagep), NEXT_PGNO(pagep), P_INVALID,
		    &LSN(pagep), &hashp->hdr->lsn)) != 0)
			return (ret);

		hashp->hdr->lsn = new_lsn;
		LSN(pagep) = new_lsn;
	}

#ifdef DIAGNOSTIC
	{
		db_pgno_t __pgno;
		DB_LSN __lsn;
		__pgno = pagep->pgno;
		__lsn = pagep->lsn;
		memset(pagep, 0xff, dbp->pgsize);
		pagep->pgno = __pgno;
		pagep->lsn = __lsn;
	}
#endif
	TYPE(pagep) = P_INVALID;
	NEXT_PGNO(pagep) = hashp->hdr->last_freed;
	hashp->hdr->last_freed = PGNO(pagep);

	return (__ham_put_page(hashp->dbp, pagep, 1));
}


/*
 * PUBLIC: int __ham_put_page __P((DB *, PAGE *, int32_t));
 */
int
__ham_put_page(dbp, pagep, is_dirty)
	DB *dbp;
	PAGE *pagep;
	int32_t is_dirty;
{
#ifdef DEBUG_SLOW
	__account_page((HTAB *)dbp->cookie,
	    ((BKT *)((char *)pagep - sizeof(BKT)))->pgno, -1);
#endif
	return (memp_fput(dbp->mpf, pagep, (is_dirty ? DB_MPOOL_DIRTY : 0)));
}

/*
 * __ham_dirty_page --
 *	Mark a page dirty.
 *
 * PUBLIC: int __ham_dirty_page __P((HTAB *, PAGE *));
 */
int
__ham_dirty_page(hashp, pagep)
	HTAB *hashp;
	PAGE *pagep;
{
	return (memp_fset(hashp->dbp->mpf, pagep, DB_MPOOL_DIRTY));
}

/*
 * PUBLIC: int __ham_get_page __P((DB *, db_pgno_t, PAGE **));
 */
int
__ham_get_page(dbp, addr, pagep)
	DB *dbp;
	db_pgno_t addr;
	PAGE **pagep;
{
	int ret;

	ret = memp_fget(dbp->mpf, &addr, DB_MPOOL_CREATE, pagep);
#ifdef DEBUG_SLOW
	if (*pagep != NULL)
		__account_page((HTAB *)dbp->internal, addr, 1);
#endif
	return (ret);
}

/*
 * PUBLIC: int __ham_overflow_page __P((DB *, u_int32_t, PAGE **));
 */
int
__ham_overflow_page(dbp, type, pp)
	DB *dbp;
	u_int32_t type;
	PAGE **pp;
{
	DB_LSN *lsnp, new_lsn;
	HTAB *hashp;
	PAGE *p;
	db_pgno_t new_addr, next_free, newalloc_flag;
	u_int32_t offset, splitnum;
	int ret;

	hashp = (HTAB *)dbp->internal;

	ret = 0;
	DIRTY_META(hashp, ret);
	if (ret != 0)
		return (ret);

	/*
	 * This routine is split up into two parts.  First we have
	 * to figure out the address of the new page that we are
	 * allocating.  Then we have to log the allocation.  Only
	 * after the log do we get to complete allocation of the
	 * new page.
	 */
	new_addr = hashp->hdr->last_freed;
	if (new_addr != PGNO_INVALID) {
		if ((ret = __ham_get_page(hashp->dbp, new_addr, &p)) != 0)
			return (ret);
		next_free = NEXT_PGNO(p);
		lsnp = &LSN(p);
		newalloc_flag = 0;
	} else {
		splitnum = hashp->hdr->ovfl_point;
		hashp->hdr->spares[splitnum]++;
		offset = hashp->hdr->spares[splitnum] -
		    (splitnum ? hashp->hdr->spares[splitnum - 1] : 0);
		new_addr = PGNO_OF(hashp, hashp->hdr->ovfl_point, offset);
		if (new_addr > MAX_PAGES(hashp)) {
			__db_err(hashp->dbp->dbenv, "hash: out of file pages");
			hashp->hdr->spares[splitnum]--;
			return (ENOMEM);
		}
		next_free = PGNO_INVALID;
		p = NULL;
		lsnp = NULL;
		newalloc_flag = 1;
	}

	if (DB_LOGGING(hashp->dbp)) {
		if ((ret = __ham_newpgno_log(hashp->dbp->dbenv->lg_info,
		    (DB_TXN *)hashp->dbp->txn, &new_lsn, 0, ALLOCPGNO,
		    hashp->dbp->log_fileid, new_addr, next_free,
		    0, newalloc_flag, type, lsnp, &hashp->hdr->lsn)) != 0)
			return (ret);

		hashp->hdr->lsn = new_lsn;
		if (lsnp != NULL)
			*lsnp = new_lsn;
	}

	if (p != NULL) {
		/* We just took something off the free list, initialize it. */
		hashp->hdr->last_freed = next_free;
		P_INIT(p, hashp->hdr->pagesize, PGNO(p), PGNO_INVALID,
		    PGNO_INVALID, 0, (u_int8_t)type);
	} else {
		/* Get the new page. */
		if ((ret = __ham_new_page(hashp, new_addr, type, &p)) != 0)
			return (ret);
	}
	if (DB_LOGGING(hashp->dbp))
		LSN(p) = new_lsn;

	*pp = p;
	return (0);
}

#ifdef DEBUG
/*
 * PUBLIC: #ifdef DEBUG
 * PUBLIC: db_pgno_t __bucket_to_page __P((HTAB *, db_pgno_t));
 * PUBLIC: #endif
 */
db_pgno_t
__bucket_to_page(hashp, n)
	HTAB *hashp;
	db_pgno_t n;
{
	int ret_val;

	ret_val = n + 1;
	if (n != 0)
		ret_val += hashp->hdr->spares[__db_log2(n + 1) - 1];
	return (ret_val);
}
#endif

/*
 * Create a bunch of overflow pages at the current split point.
 * PUBLIC: void __ham_init_ovflpages __P((HTAB *));
 */
void
__ham_init_ovflpages(hp)
	HTAB *hp;
{
	DB_LSN new_lsn;
	PAGE *p;
	db_pgno_t last_pgno, new_pgno;
	u_int32_t i, curpages, numpages;

	curpages = hp->hdr->spares[hp->hdr->ovfl_point] -
	    hp->hdr->spares[hp->hdr->ovfl_point - 1];
	numpages = hp->hdr->ovfl_point + 1 - curpages;

	last_pgno = hp->hdr->last_freed;
	new_pgno = PGNO_OF(hp, hp->hdr->ovfl_point, curpages + 1);
	if (DB_LOGGING(hp->dbp)) {
		(void)__ham_ovfl_log(hp->dbp->dbenv->lg_info,
		    (DB_TXN *)hp->dbp->txn, &new_lsn, 0,
		    hp->dbp->log_fileid, new_pgno,
		    numpages, last_pgno, hp->hdr->ovfl_point, &hp->hdr->lsn);
		hp->hdr->lsn = new_lsn;
	} else
		ZERO_LSN(new_lsn);

	hp->hdr->spares[hp->hdr->ovfl_point] += numpages;
	for (i = numpages; i > 0; i--) {
		if (__ham_new_page(hp,
		    PGNO_OF(hp, hp->hdr->ovfl_point, curpages + i),
		    P_INVALID, &p) != 0)
			break;
		LSN(p) = new_lsn;
		NEXT_PGNO(p) = last_pgno;
		last_pgno = PGNO(p);
		(void)__ham_put_page(hp->dbp, p, 1);
	}
	hp->hdr->last_freed = last_pgno;
}

/*
 * PUBLIC: int __ham_get_cpage __P((HTAB *, HASH_CURSOR *, db_lockmode_t));
 */
int
__ham_get_cpage(hashp, hcp, mode)
	HTAB *hashp;
	HASH_CURSOR *hcp;
	db_lockmode_t mode;
{
	int ret;

	if (hcp->lock == 0 && F_ISSET(hashp->dbp, DB_AM_LOCKING) &&
	    (ret = __ham_lock_bucket(hashp->dbp, hcp, mode)) != 0)
		return (ret);

	if (hcp->pagep == NULL) {
		if (hcp->pgno == PGNO_INVALID) {
			hcp->pgno = BUCKET_TO_PAGE(hashp, hcp->bucket);
			hcp->bndx = 0;
		}

		if ((ret =
		    __ham_get_page(hashp->dbp, hcp->pgno, &hcp->pagep)) != 0)
			return (ret);
	}

	if (hcp->dpgno != PGNO_INVALID && hcp->dpagep == NULL)
		if ((ret =
		    __ham_get_page(hashp->dbp, hcp->dpgno, &hcp->dpagep)) != 0)
			return (ret);
	return (0);
}

/*
 * Get a new page at the cursor, putting the last page if necessary.
 * If the flag is set to H_ISDUP, then we are talking about the
 * duplicate page, not the main page.
 *
 * PUBLIC: int __ham_next_cpage
 * PUBLIC:    __P((HTAB *, HASH_CURSOR *, db_pgno_t, int, u_int32_t));
 */
int
__ham_next_cpage(hashp, hcp, pgno, dirty, flags)
	HTAB *hashp;
	HASH_CURSOR *hcp;
	db_pgno_t pgno;
	int dirty;
	u_int32_t flags;
{
	PAGE *p;
	int ret;

	if (LF_ISSET(H_ISDUP) && hcp->dpagep != NULL &&
	    (ret = __ham_put_page(hashp->dbp, hcp->dpagep, dirty)) != 0)
		return (ret);
	else if (!LF_ISSET(H_ISDUP) && hcp->pagep != NULL &&
	    (ret = __ham_put_page(hashp->dbp, hcp->pagep, dirty)) != 0)
		return (ret);

	if ((ret = __ham_get_page(hashp->dbp, pgno, &p)) != 0)
		return (ret);

	if (LF_ISSET(H_ISDUP)) {
		hcp->dpagep = p;
		hcp->dpgno = pgno;
		hcp->dndx = 0;
	} else {
		hcp->pagep = p;
		hcp->pgno = pgno;
		hcp->bndx = 0;
	}

	return (0);
}

/*
 * __ham_lock_bucket --
 *	Get the lock on a particular bucket.
 */
static int
__ham_lock_bucket(dbp, hcp, mode)
	DB *dbp;
	HASH_CURSOR *hcp;
	db_lockmode_t mode;
{
	int ret;

	/*
	 * What a way to trounce on the memory system.  It might be
	 * worth copying the lk_info into the hashp.
	 */
	ret = 0;
	dbp->lock.pgno = (db_pgno_t)(hcp->bucket);
	ret = lock_get(dbp->dbenv->lk_info,
	    dbp->txn == NULL ?  dbp->locker : dbp->txn->txnid, 0,
	    &dbp->lock_dbt, mode, &hcp->lock);

	return (ret < 0 ? EAGAIN : ret);
}

/*
 * __ham_dpair --
 *	Delete a pair on a page, paying no attention to what the pair
 *	represents.  The caller is responsible for freeing up duplicates
 *	or offpage entries that might be referenced by this pair.
 *
 * PUBLIC: void __ham_dpair __P((DB *, PAGE *, u_int32_t));
 */
void
__ham_dpair(dbp, p, pndx)
	DB *dbp;
	PAGE *p;
	u_int32_t pndx;
{
	db_indx_t delta, n;
	u_int8_t *dest, *src;

	/*
	 * Compute "delta", the amount we have to shift all of the
	 * offsets.  To find the delta, we just need to calculate
	 * the size of the pair of elements we are removing.
	 */
	delta = H_PAIRSIZE(p, dbp->pgsize, pndx);

	/*
	 * The hard case: we want to remove something other than
	 * the last item on the page.  We need to shift data and
	 * offsets down.
	 */
	if ((db_indx_t)pndx != H_NUMPAIRS(p) - 1) {
		/*
		 * Move the data: src is the first occupied byte on
		 * the page. (Length is delta.)
		 */
		src = (u_int8_t *)p + HOFFSET(p);

		/*
		 * Destination is delta bytes beyond src.  This might
		 * be an overlapping copy, so we have to use memmove.
		 */
		dest = src + delta;
		memmove(dest, src, p->inp[H_DATAINDEX(pndx)] - HOFFSET(p));
	}

	/* Adjust the offsets. */
	for (n = (db_indx_t)pndx; n < (db_indx_t)(H_NUMPAIRS(p) - 1); n++) {
		p->inp[H_KEYINDEX(n)] = p->inp[H_KEYINDEX(n+1)] + delta;
		p->inp[H_DATAINDEX(n)] = p->inp[H_DATAINDEX(n+1)] + delta;
	}

	/* Adjust page metadata. */
	HOFFSET(p) = HOFFSET(p) + delta;
	NUM_ENT(p) = NUM_ENT(p) - 2;
}

#ifdef DEBUG_SLOW
static void
__account_page(hashp, pgno, inout)
	HTAB *hashp;
	db_pgno_t pgno;
	int inout;
{
	static struct {
		db_pgno_t pgno;
		int times;
	} list[100];
	static int last;
	int i, j;

	if (inout == -1)			/* XXX: Kluge */
		inout = 0;

	/* Find page in list. */
	for (i = 0; i < last; i++)
		if (list[i].pgno == pgno)
			break;
	/* Not found. */
	if (i == last) {
		list[last].times = inout;
		list[last].pgno = pgno;
		last++;
	}
	list[i].times = inout;
	if (list[i].times == 0) {
		for (j = i; j < last; j++)
			list[j] = list[j + 1];
		last--;
	}
	for (i = 0; i < last; i++, list[i].times++)
		if (list[i].times > 20 &&
		    !__is_bitmap_pgno(hashp, list[i].pgno))
			(void)fprintf(stderr,
			    "Warning: pg %lu has been out for %d times\n",
			    (u_long)list[i].pgno, list[i].times);
}
#endif /* DEBUG_SLOW */