2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2001
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id: bt_recno.c,v 11.81 2001/07/02 01:05:35 bostic Exp $";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
32
static int __ram_add __P((DBC *, db_recno_t *, DBT *, u_int32_t, u_int32_t));
33
static int __ram_source __P((DB *));
34
static int __ram_sread __P((DBC *, db_recno_t));
35
static int __ram_update __P((DBC *, db_recno_t, int));
38
* In recno, there are two meanings to the on-page "deleted" flag. If we're
39
* re-numbering records, it means the record was implicitly created. We skip
40
* over implicitly created records if doing a cursor "next" or "prev", and
41
* return DB_KEYEMPTY if they're explicitly requested.. If not re-numbering
42
* records, it means that the record was implicitly created, or was deleted.
43
* We skip over implicitly created or deleted records if doing a cursor "next"
44
* or "prev", and return DB_KEYEMPTY if they're explicitly requested.
46
* If we're re-numbering records, then we have to detect in the cursor that
47
* a record was deleted, and adjust the cursor as necessary on the next get.
48
* If we're not re-numbering records, then we can detect that a record has
49
* been deleted by looking at the actual on-page record, so we completely
50
* ignore the cursor's delete flag. This is different from the B+tree code.
51
* It also maintains whether the cursor references a deleted record in the
52
* cursor, and it doesn't always check the on-page value.
54
#define CD_SET(cp) { \
55
if (F_ISSET(cp, C_RENUMBER)) \
56
F_SET(cp, C_DELETED); \
58
#define CD_CLR(cp) { \
59
if (F_ISSET(cp, C_RENUMBER)) { \
60
F_CLR(cp, C_DELETED); \
61
cp->order = INVALID_ORDER; \
64
#define CD_ISSET(cp) \
65
(F_ISSET(cp, C_RENUMBER) && F_ISSET(cp, C_DELETED))
68
* Macros for comparing the ordering of two cursors.
69
* cp1 comes before cp2 iff one of the following holds:
70
* cp1's recno is less than cp2's recno
71
* recnos are equal, both deleted, and cp1's order is less than cp2's
72
* recnos are equal, cp1 deleted, and cp2 not deleted
74
#define C_LESSTHAN(cp1, cp2) \
75
(((cp1)->recno < (cp2)->recno) || \
76
(((cp1)->recno == (cp2)->recno) && \
77
((CD_ISSET((cp1)) && CD_ISSET((cp2)) && (cp1)->order < (cp2)->order) || \
78
(CD_ISSET((cp1)) && !CD_ISSET((cp2))))))
81
* cp1 is equal to cp2 iff their recnos and delete flags are identical,
82
* and if the delete flag is set their orders are also identical.
84
#define C_EQUAL(cp1, cp2) \
85
((cp1)->recno == (cp2)->recno && CD_ISSET((cp1)) == CD_ISSET((cp2)) && \
86
(!CD_ISSET((cp1)) || (cp1)->order == (cp2)->order))
89
* Do we need to log the current cursor adjustment?
91
#define CURADJ_LOG(dbc) \
92
(DB_LOGGING((dbc)) && (dbc)->txn != NULL && (dbc)->txn->parent != NULL)
96
* Recno open function.
98
* PUBLIC: int __ram_open __P((DB *, const char *, db_pgno_t, u_int32_t));
101
__ram_open(dbp, name, base_pgno, flags)
111
t = dbp->bt_internal;
113
/* Initialize the remaining fields/methods of the DB. */
114
dbp->stat = __bam_stat;
116
/* Start up the tree. */
117
if ((ret = __bam_read_root(dbp, name, base_pgno, flags)) != 0)
121
* If the user specified a source tree, open it and map it in.
124
* We don't complain if the user specified transactions or threads.
125
* It's possible to make it work, but you'd better know what you're
128
if (t->re_source != NULL && (ret = __ram_source(dbp)) != 0)
131
/* If we're snapshotting an underlying source file, do it now. */
132
if (F_ISSET(dbp, DB_RE_SNAPSHOT)) {
133
/* Allocate a cursor. */
134
if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
137
/* Do the snapshot. */
138
if ((ret = __ram_update(dbc,
139
DB_MAX_RECORDS, 0)) != 0 && ret == DB_NOTFOUND)
142
/* Discard the cursor. */
143
if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
152
* Recno append function.
154
* PUBLIC: int __ram_append __P((DBC *, DBT *, DBT *));
157
__ram_append(dbc, key, data)
164
cp = (BTREE_CURSOR *)dbc->internal;
167
* Make sure we've read in all of the backing source file. If
168
* we found the record or it simply didn't exist, add the
171
ret = __ram_update(dbc, DB_MAX_RECORDS, 0);
172
if (ret == 0 || ret == DB_NOTFOUND)
173
ret = __ram_add(dbc, &cp->recno, data, DB_APPEND, 0);
175
/* Return the record number. */
177
ret = __db_retcopy(dbc->dbp, key, &cp->recno, sizeof(cp->recno),
178
&dbc->rkey->data, &dbc->rkey->ulen);
185
* Recno cursor->c_del function.
187
* PUBLIC: int __ram_c_del __P((DBC *));
200
int exact, ret, stack;
203
cp = (BTREE_CURSOR *)dbc->internal;
204
t = dbp->bt_internal;
208
* The semantics of cursors during delete are as follows: in
209
* non-renumbering recnos, records are replaced with a marker
210
* containing a delete flag. If the record referenced by this cursor
211
* has already been deleted, we will detect that as part of the delete
212
* operation, and fail.
214
* In renumbering recnos, cursors which represent deleted items
215
* are flagged with the C_DELETED flag, and it is an error to
216
* call c_del a second time without an intervening cursor motion.
219
return (DB_KEYEMPTY);
221
/* Search the tree for the key; delete only deletes exact matches. */
222
if ((ret = __bam_rsearch(dbc, &cp->recno, S_DELETE, 1, &exact)) != 0)
229
cp->page = cp->csp->page;
230
cp->pgno = cp->csp->page->pgno;
231
cp->indx = cp->csp->indx;
234
* If re-numbering records, the on-page deleted flag can only mean
235
* that this record was implicitly created. Applications aren't
236
* permitted to delete records they never created, return an error.
238
* If not re-numbering records, the on-page deleted flag means that
239
* this record was implicitly created, or, was deleted at some time.
240
* The former is an error because applications aren't permitted to
241
* delete records they never created, the latter is an error because
242
* if the record was "deleted", we could never have found it.
244
if (B_DISSET(GET_BKEYDATA(cp->page, cp->indx)->type)) {
249
if (F_ISSET(cp, C_RENUMBER)) {
250
/* Delete the item, adjust the counts, adjust the cursors. */
251
if ((ret = __bam_ditem(dbc, cp->page, cp->indx)) != 0)
253
__bam_adjust(dbc, -1);
254
if (__ram_ca(dbc, CA_DELETE) > 0 &&
255
CURADJ_LOG(dbc) && (ret = __bam_rcuradj_log(dbp->dbenv,
256
dbc->txn, &lsn, 0, dbp->log_fileid, CA_DELETE,
257
cp->root, cp->recno, cp->order)) != 0)
261
* If the page is empty, delete it.
263
* We never delete a root page. First, root pages of primary
264
* databases never go away, recno or otherwise. However, if
265
* it's the root page of an off-page duplicates database, then
266
* it can be deleted. We don't delete it here because we have
267
* no way of telling the primary database page holder (e.g.,
268
* the hash access method) that its page element should cleaned
269
* up because the underlying tree is gone. So, we keep the page
270
* around until the last cursor referencing the empty tree is
271
* are closed, and then clean it up.
273
if (NUM_ENT(cp->page) == 0 && PGNO(cp->page) != cp->root) {
275
* We already have a locked stack of pages. However,
276
* there are likely entries in the stack that aren't
277
* going to be emptied by removing the single reference
278
* to the emptied page (or one of its parents).
280
for (epg = cp->sp; epg <= cp->csp; ++epg)
281
if (NUM_ENT(epg->page) <= 1)
285
* We want to delete a single item out of the last page
286
* that we're not deleting, back up to that page.
288
ret = __bam_dpages(dbc, --epg);
291
* Regardless of the return from __bam_dpages, it will
292
* discard our stack and pinned page.
298
/* Use a delete/put pair to replace the record with a marker. */
299
if ((ret = __bam_ditem(dbc, cp->page, cp->indx)) != 0)
302
B_TSET(bk.type, B_KEYDATA, 1);
304
memset(&hdr, 0, sizeof(hdr));
306
hdr.size = SSZA(BKEYDATA, data);
307
memset(&data, 0, sizeof(data));
308
data.data = (void *)"";
310
if ((ret = __db_pitem(dbc,
311
cp->page, cp->indx, BKEYDATA_SIZE(0), &hdr, &data)) != 0)
318
__bam_stkrel(dbc, STK_CLRDBC);
325
* Recno cursor->c_get function.
327
* PUBLIC: int __ram_c_get
328
* PUBLIC: __P((DBC *, DBT *, DBT *, u_int32_t, db_pgno_t *));
331
__ram_c_get(dbc, key, data, flags, pgnop)
341
COMPQUIET(pgnop, NULL);
344
cp = (BTREE_CURSOR *)dbc->internal;
346
LF_CLR(DB_MULTIPLE|DB_MULTIPLE_KEY);
347
retry: switch (flags) {
350
* If we're using mutable records and the deleted flag is
351
* set, the cursor is pointing at a nonexistent record;
355
return (DB_KEYEMPTY);
359
* If we're not in an off-page dup set, we know there's no
360
* next duplicate since recnos don't have them. If we
361
* are in an off-page dup set, the next item assuredly is
362
* a dup, so we set flags to DB_NEXT and keep going.
364
if (!F_ISSET(dbc, DBC_OPD))
365
return (DB_NOTFOUND);
369
* Recno databases don't have duplicates, set flags to DB_NEXT
376
* If record numbers are mutable: if we just deleted a record,
377
* we have to avoid incrementing the record number so that we
378
* return the right record by virtue of renumbering the tree.
383
if (cp->recno != RECNO_OOB) {
394
* Recno databases don't have duplicates, set flags to DB_PREV
400
if (cp->recno != RECNO_OOB) {
401
if (cp->recno == 1) {
411
if (((ret = __ram_update(dbc,
412
DB_MAX_RECORDS, 0)) != 0) && ret != DB_NOTFOUND)
414
if ((ret = __bam_nrecs(dbc, &cp->recno)) != 0)
416
if (cp->recno == 0) {
423
* If we're doing a join and these are offpage dups,
424
* we want to keep searching forward from after the
425
* current cursor position. Increment the recno by 1,
426
* then proceed as for a DB_SET.
428
* Otherwise, we know there are no additional matching
429
* data, as recnos don't have dups. return DB_NOTFOUND.
431
if (F_ISSET(dbc, DBC_OPD)) {
440
* If we're searching a set of off-page dups, we start
441
* a new linear search from the first record. Otherwise,
442
* we compare the single data item associated with the
443
* requested record for a match.
445
if (F_ISSET(dbc, DBC_OPD)) {
452
if ((ret = __ram_getno(dbc, key, &cp->recno, 0)) != 0)
456
ret = __db_unknown_flag(dbp->dbenv, "__ram_c_get", flags);
461
* For DB_PREV, DB_LAST, DB_SET and DB_SET_RANGE, we have already
462
* called __ram_update() to make sure sufficient records have been
463
* read from the backing source file. Do it now for DB_CURRENT (if
464
* the current record was deleted we may need more records from the
465
* backing file for a DB_CURRENT operation), DB_FIRST and DB_NEXT.
466
* (We don't have to test for flags == DB_FIRST, because the switch
467
* statement above re-set flags to DB_NEXT in that case.)
469
if ((flags == DB_NEXT || flags == DB_CURRENT) && ((ret =
470
__ram_update(dbc, cp->recno, 0)) != 0) && ret != DB_NOTFOUND)
473
for (;; ++cp->recno) {
474
/* Search the tree for the record. */
475
if ((ret = __bam_rsearch(dbc, &cp->recno,
476
F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND,
485
* Copy the page into the cursor, discarding any lock we
486
* are currently holding.
488
cp->page = cp->csp->page;
489
cp->pgno = cp->csp->page->pgno;
490
cp->indx = cp->csp->indx;
491
(void)__TLPUT(dbc, cp->lock);
492
cp->lock = cp->csp->lock;
493
cp->lock_mode = cp->csp->lock_mode;
496
* If re-numbering records, the on-page deleted flag means this
497
* record was implicitly created. If not re-numbering records,
498
* the on-page deleted flag means this record was implicitly
499
* created, or, it was deleted at some time. Regardless, we
500
* skip such records if doing cursor next/prev operations or
501
* walking through off-page duplicates, and fail if they were
502
* requested explicitly by the application.
504
if (B_DISSET(GET_BKEYDATA(cp->page, cp->indx)->type))
508
(void)__bam_stkrel(dbc, STK_CLRDBC);
512
* If we're an OPD tree, we don't care
513
* about matching a record number on a
514
* DB_GET_BOTH--everything belongs to the
515
* same tree. A normal recno should give
516
* up and return DB_NOTFOUND if the matching
519
if (F_ISSET(dbc, DBC_OPD)) {
520
(void)__bam_stkrel(dbc, STK_CLRDBC);
530
if (flags == DB_GET_BOTH || flags == DB_GET_BOTHC) {
531
if ((ret = __bam_cmp(dbp, data,
532
cp->page, cp->indx, __bam_defcmp, &cmp)) != 0)
536
if (!F_ISSET(dbc, DBC_OPD)) {
540
(void)__bam_stkrel(dbc, STK_CLRDBC);
545
/* Return the key if the user didn't give us one. */
546
if (!F_ISSET(dbc, DBC_OPD)) {
547
if (flags != DB_SET && flags != DB_SET_RANGE)
548
ret = __db_retcopy(dbp,
549
key, &cp->recno, sizeof(cp->recno),
550
&dbc->rkey->data, &dbc->rkey->ulen);
551
F_SET(key, DB_DBT_ISSET);
554
/* The cursor was reset, no further delete adjustment is necessary. */
562
* Recno cursor->c_put function.
564
* PUBLIC: int __ram_c_put __P((DBC *, DBT *, DBT *, u_int32_t, db_pgno_t *));
567
__ram_c_put(dbc, key, data, flags, pgnop)
576
int exact, nc, ret, t_ret;
580
COMPQUIET(pgnop, NULL);
583
cp = (BTREE_CURSOR *)dbc->internal;
586
* DB_KEYFIRST and DB_KEYLAST mean different things if they're
587
* used in an off-page duplicate tree. If we're an off-page
588
* duplicate tree, they really mean "put at the beginning of the
589
* tree" and "put at the end of the tree" respectively, so translate
590
* them to something else.
592
if (F_ISSET(dbc, DBC_OPD))
599
if ((ret = __ram_add(dbc,
600
&cp->recno, data, DB_APPEND, 0)) != 0)
602
if (CURADJ_LOG(dbc) &&
603
(ret = __bam_rcuradj_log(dbp->dbenv,
604
dbc->txn, &lsn, 0, dbp->log_fileid, CA_ICURRENT,
605
cp->root, cp->recno, cp->order)))
611
* Handle normal DB_KEYFIRST/DB_KEYLAST; for a recno, which has
612
* no duplicates, these are identical and mean "put the given
613
* datum at the given recno".
615
* Note that the code here used to be in __ram_put; now, we
616
* go through the access-method-common __db_put function, which
617
* handles DB_NOOVERWRITE, so we and __ram_add don't have to.
619
if (flags == DB_KEYFIRST || flags == DB_KEYLAST) {
620
ret = __ram_getno(dbc, key, &cp->recno, 1);
621
if (ret == 0 || ret == DB_NOTFOUND)
622
ret = __ram_add(dbc, &cp->recno, data, 0, 0);
627
* If we're putting with a cursor that's marked C_DELETED, we need to
628
* take special care; the cursor doesn't "really" reference the item
629
* corresponding to its current recno, but instead is "between" that
630
* record and the current one. Translate the actual insert into
631
* DB_BEFORE, and let the __ram_ca work out the gory details of what
632
* should wind up pointing where.
639
split: if ((ret = __bam_rsearch(dbc, &cp->recno, S_INSERT, 1, &exact)) != 0)
642
* An inexact match is okay; it just means we're one record past the
643
* end, which is reasonable if we're marked deleted.
645
DB_ASSERT(exact || CD_ISSET(cp));
647
cp->page = cp->csp->page;
648
cp->pgno = cp->csp->page->pgno;
649
cp->indx = cp->csp->indx;
651
ret = __bam_iitem(dbc, key, data, iiflags, 0);
652
t_ret = __bam_stkrel(dbc, STK_CLRDBC);
654
if (t_ret != 0 && (ret == 0 || ret == DB_NEEDSPLIT))
656
else if (ret == DB_NEEDSPLIT) {
658
if ((ret = __bam_split(dbc, arg, NULL)) != 0)
665
switch (flags) { /* Adjust the cursors. */
667
nc = __ram_ca(dbc, CA_IAFTER);
670
* We only need to adjust this cursor forward if we truly added
671
* the item after the current recno, rather than remapping it
674
if (iiflags == DB_AFTER)
677
/* Only log if __ram_ca found any relevant cursors. */
678
if (nc > 0 && CURADJ_LOG(dbc) &&
679
(ret = __bam_rcuradj_log(dbp->dbenv,
680
dbc->txn, &lsn, 0, dbp->log_fileid, CA_IAFTER,
681
cp->root, cp->recno, cp->order)) != 0)
685
nc = __ram_ca(dbc, CA_IBEFORE);
688
/* Only log if __ram_ca found any relevant cursors. */
689
if (nc > 0 && CURADJ_LOG(dbc) &&
690
(ret = __bam_rcuradj_log(dbp->dbenv,
691
dbc->txn, &lsn, 0, dbp->log_fileid, CA_IBEFORE,
692
cp->root, cp->recno, cp->order)) != 0)
697
* We only need to do an adjustment if we actually
698
* added an item, which we only would have done if the
699
* cursor was marked deleted.
701
* Only log if __ram_ca found any relevant cursors.
703
if (CD_ISSET(cp) && __ram_ca(dbc, CA_ICURRENT) > 0 &&
704
CURADJ_LOG(dbc) && (ret = __bam_rcuradj_log(
705
dbp->dbenv, dbc->txn, &lsn, 0, dbp->log_fileid,
706
CA_ICURRENT, cp->root, cp->recno, cp->order)) != 0)
711
/* Return the key if we've created a new record. */
712
if (!F_ISSET(dbc, DBC_OPD) && (flags == DB_AFTER || flags == DB_BEFORE))
713
ret = __db_retcopy(dbp, key, &cp->recno,
714
sizeof(cp->recno), &dbc->rkey->data, &dbc->rkey->ulen);
716
/* The cursor was reset, no further delete adjustment is necessary. */
724
* Adjust cursors. Returns the number of relevant cursors.
726
* PUBLIC: int __ram_ca __P((DBC *, ca_recno_arg));
729
__ram_ca(dbc_arg, op)
733
BTREE_CURSOR *cp, *cp_arg;
743
cp_arg = (BTREE_CURSOR *)dbc_arg->internal;
744
recno = cp_arg->recno;
749
* It only makes sense to adjust cursors if we're a renumbering
750
* recno; we should only be called if this is one.
752
DB_ASSERT(F_ISSET(cp_arg, C_RENUMBER));
754
MUTEX_THREAD_LOCK(dbenv, dbenv->dblist_mutexp);
756
* Adjust the cursors. See the comment in __bam_ca_delete().
759
* If we're doing a delete, we need to find the highest
760
* order of any cursor currently pointing at this item,
761
* so we can assign a higher order to the newly deleted
762
* cursor. Unfortunately, this requires a second pass through
765
if (op == CA_DELETE) {
767
for (ldbp = __dblist_get(dbenv, dbp->adj_fileid);
768
ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
769
ldbp = LIST_NEXT(ldbp, dblistlinks)) {
770
MUTEX_THREAD_LOCK(dbenv, dbp->mutexp);
771
for (dbc = TAILQ_FIRST(&ldbp->active_queue);
772
dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
773
cp = (BTREE_CURSOR *)dbc->internal;
774
if (cp_arg->root == cp->root &&
775
recno == cp->recno && CD_ISSET(cp) &&
777
order = cp->order + 1;
779
MUTEX_THREAD_UNLOCK(dbenv, dbp->mutexp);
782
order = INVALID_ORDER;
784
/* Now go through and do the actual adjustments. */
785
for (ldbp = __dblist_get(dbenv, dbp->adj_fileid);
786
ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
787
ldbp = LIST_NEXT(ldbp, dblistlinks)) {
788
MUTEX_THREAD_LOCK(dbenv, dbp->mutexp);
789
for (dbc = TAILQ_FIRST(&ldbp->active_queue);
790
dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
791
cp = (BTREE_CURSOR *)dbc->internal;
792
if (cp_arg->root != cp->root)
798
if (recno < cp->recno) {
801
* If the adjustment made them equal,
802
* we have to merge the orders.
804
if (recno == cp->recno && CD_ISSET(cp))
806
} else if (recno == cp->recno &&
814
* IBEFORE is just like IAFTER, except that we
815
* adjust cursors on the current record too.
817
if (C_EQUAL(cp_arg, cp)) {
825
* If the original cursor wasn't deleted, we
826
* just did a replacement and so there's no
827
* need to adjust anything--we shouldn't have
828
* gotten this far. Otherwise, we behave
829
* much like an IAFTER, except that all
830
* cursors pointing to the current item get
831
* marked undeleted and point to the new
834
DB_ASSERT(CD_ISSET(cp_arg));
835
if (C_EQUAL(cp_arg, cp)) {
841
iafter: if (!adjusted && C_LESSTHAN(cp_arg, cp)) {
845
if (recno == cp->recno && adjusted)
847
* If we've moved this cursor's recno,
848
* split its order number--i.e.,
849
* decrement it by enough so that
850
* the lowest cursor moved has order 1.
851
* cp_arg->order is the split point,
852
* so decrement by one less than that.
854
cp->order -= (cp_arg->order - 1);
858
MUTEX_THREAD_UNLOCK(dbp->dbenv, dbp->mutexp);
860
MUTEX_THREAD_UNLOCK(dbenv, dbenv->dblist_mutexp);
867
* Check the user's record number, and make sure we've seen it.
869
* PUBLIC: int __ram_getno __P((DBC *, const DBT *, db_recno_t *, int));
872
__ram_getno(dbc, key, rep, can_create)
883
/* Check the user's record number. */
884
if ((recno = *(db_recno_t *)key->data) == 0) {
885
__db_err(dbp->dbenv, "illegal record number of 0");
892
* Btree can neither create records nor read them in. Recno can
893
* do both, see if we can find the record.
895
return (dbc->dbtype == DB_RECNO ?
896
__ram_update(dbc, recno, can_create) : 0);
901
* Ensure the tree has records up to and including the specified one.
904
__ram_update(dbc, recno, can_create)
916
t = dbp->bt_internal;
919
* If we can't create records and we've read the entire backing input
922
if (!can_create && t->re_eof)
926
* If we haven't seen this record yet, try to get it from the original
929
if ((ret = __bam_nrecs(dbc, &nrecs)) != 0)
931
if (!t->re_eof && recno > nrecs) {
932
if ((ret = __ram_sread(dbc, recno)) != 0 && ret != DB_NOTFOUND)
934
if ((ret = __bam_nrecs(dbc, &nrecs)) != 0)
939
* If we can create records, create empty ones up to the requested
942
if (!can_create || recno <= nrecs + 1)
945
rdata = &dbc->my_rdata;
949
while (recno > ++nrecs)
950
if ((ret = __ram_add(dbc,
951
&nrecs, rdata, 0, BI_DELETED)) != 0)
958
* Load information about the backing file.
968
t = dbp->bt_internal;
970
/* Find the real name, and swap out the one we had before. */
971
if ((ret = __db_appname(dbp->dbenv,
972
DB_APP_DATA, NULL, t->re_source, 0, NULL, &source)) != 0)
974
__os_freestr(dbp->dbenv, t->re_source);
975
t->re_source = source;
979
* It's possible that the backing source file is read-only. We don't
980
* much care other than we'll complain if there are any modifications
981
* when it comes time to write the database back to the source.
983
if ((t->re_fp = fopen(t->re_source, "r")) == NULL) {
985
__db_err(dbp->dbenv, "%s: %s", t->re_source, db_strerror(ret));
995
* Rewrite the backing file.
997
* PUBLIC: int __ram_writeback __P((DB *));
1000
__ram_writeback(dbp)
1010
u_int8_t delim, *pad;
1012
t = dbp->bt_internal;
1016
/* If the file wasn't modified, we're done. */
1017
if (!t->re_modified)
1020
/* If there's no backing source file, we're done. */
1021
if (t->re_source == NULL) {
1026
/* Allocate a cursor. */
1027
if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
1031
* Read any remaining records into the tree.
1034
* This is why we can't support transactions when applications specify
1035
* backing (re_source) files. At this point we have to read in the
1036
* rest of the records from the file so that we can write all of the
1037
* records back out again, which could modify a page for which we'd
1038
* have to log changes and which we don't have locked. This could be
1039
* partially fixed by taking a snapshot of the entire file during the
1040
* DB->open as DB->open is transaction protected. But, if a checkpoint
1041
* occurs then, the part of the log holding the copy of the file could
1042
* be discarded, and that would make it impossible to recover in the
1043
* face of disaster. This could all probably be fixed, but it would
1044
* require transaction protecting the backing source file.
1047
* This could be made to work now that we have transactions protecting
1048
* file operations. Margo has specifically asked for the privilege of
1052
__ram_update(dbc, DB_MAX_RECORDS, 0)) != 0 && ret != DB_NOTFOUND)
1056
* Close any existing file handle and re-open the file, truncating it.
1058
if (t->re_fp != NULL) {
1059
if (fclose(t->re_fp) != 0) {
1065
if ((fp = fopen(t->re_source, "w")) == NULL) {
1067
__db_err(dbenv, "%s: %s", t->re_source, db_strerror(ret));
1072
* We step through the records, writing each one out. Use the record
1073
* number and the dbp->get() function, instead of a cursor, so we find
1074
* and write out "deleted" or non-existent records.
1076
memset(&key, 0, sizeof(key));
1077
memset(&data, 0, sizeof(data));
1078
key.size = sizeof(db_recno_t);
1082
* We'll need the delimiter if we're doing variable-length records,
1083
* and the pad character if we're doing fixed-length records.
1085
delim = t->re_delim;
1086
if (F_ISSET(dbp, DB_RE_FIXEDLEN)) {
1087
if ((ret = __os_malloc(dbenv, t->re_len, &pad)) != 0)
1089
memset(pad, t->re_pad, t->re_len);
1091
COMPQUIET(pad, NULL);
1092
for (keyno = 1;; ++keyno) {
1093
switch (ret = dbp->get(dbp, NULL, &key, &data, 0)) {
1095
if (fwrite(data.data, 1, data.size, fp) != data.size)
1099
if (F_ISSET(dbp, DB_RE_FIXEDLEN) &&
1100
fwrite(pad, 1, t->re_len, fp) != t->re_len)
1107
if (!F_ISSET(dbp, DB_RE_FIXEDLEN) &&
1108
fwrite(&delim, 1, 1, fp) != 1) {
1109
write_err: ret = errno;
1110
__db_err(dbp->dbenv,
1111
"%s: write failed to backing file: %s",
1112
t->re_source, strerror(ret));
1118
done: /* Close the file descriptor. */
1119
if (fp != NULL && fclose(fp) != 0) {
1122
__db_err(dbenv, "%s: %s", t->re_source, db_strerror(errno));
1125
/* Discard the cursor. */
1126
if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
1137
* Read records from a source file.
1140
__ram_sread(dbc, top)
1149
int ch, ret, was_modified;
1151
t = dbc->dbp->bt_internal;
1153
was_modified = t->re_modified;
1155
if ((ret = __bam_nrecs(dbc, &recno)) != 0)
1159
* Use the record key return memory, it's only a short-term use.
1160
* The record data return memory is used by __bam_iitem, which
1161
* we'll indirectly call, so use the key so as not to collide.
1163
len = F_ISSET(dbp, DB_RE_FIXEDLEN) ? t->re_len : 256;
1164
rdata = &dbc->my_rkey;
1165
if (rdata->ulen < len) {
1166
if ((ret = __os_realloc(
1167
dbp->dbenv, len, &rdata->data)) != 0) {
1175
memset(&data, 0, sizeof(data));
1176
while (recno < top) {
1177
data.data = rdata->data;
1179
if (F_ISSET(dbp, DB_RE_FIXEDLEN))
1180
for (len = t->re_len; len > 0; --len) {
1181
if ((ch = getc(t->re_fp)) == EOF) {
1186
((u_int8_t *)data.data)[data.size++] = ch;
1190
if ((ch = getc(t->re_fp)) == EOF) {
1195
if (ch == t->re_delim)
1198
((u_int8_t *)data.data)[data.size++] = ch;
1199
if (data.size == rdata->ulen) {
1200
if ((ret = __os_realloc(dbp->dbenv,
1202
&rdata->data)) != 0) {
1207
data.data = rdata->data;
1212
* Another process may have read this record from the input
1213
* file and stored it into the database already, in which
1214
* case we don't need to repeat that operation. We detect
1215
* this by checking if the last record we've read is greater
1216
* or equal to the number of records in the database.
1218
if (t->re_last >= recno) {
1220
if ((ret = __ram_add(dbc, &recno, &data, 0, 0)) != 0)
1230
err: if (!was_modified)
1238
* Add records into the tree.
1241
__ram_add(dbc, recnop, data, flags, bi_flags)
1245
u_int32_t flags, bi_flags;
1248
int exact, ret, stack;
1250
cp = (BTREE_CURSOR *)dbc->internal;
1252
retry: /* Find the slot for insertion. */
1253
if ((ret = __bam_rsearch(dbc, recnop,
1254
S_INSERT | (flags == DB_APPEND ? S_APPEND : 0), 1, &exact)) != 0)
1257
cp->page = cp->csp->page;
1258
cp->pgno = cp->csp->page->pgno;
1259
cp->indx = cp->csp->indx;
1262
* The application may modify the data based on the selected record
1265
if (flags == DB_APPEND && dbc->dbp->db_append_recno != NULL &&
1266
(ret = dbc->dbp->db_append_recno(dbc->dbp, data, *recnop)) != 0)
1270
* Select the arguments for __bam_iitem() and do the insert. If the
1271
* key is an exact match, or we're replacing the data item with a
1272
* new data item, replace the current item. If the key isn't an exact
1273
* match, we're inserting a new key/data pair, before the search
1276
switch (ret = __bam_iitem(dbc,
1277
NULL, data, exact ? DB_CURRENT : DB_BEFORE, bi_flags)) {
1280
* Don't adjust anything.
1282
* If we inserted a record, no cursors need adjusting because
1283
* the only new record it's possible to insert is at the very
1284
* end of the tree. The necessary adjustments to the internal
1285
* page counts were made by __bam_iitem().
1287
* If we overwrote a record, no cursors need adjusting because
1288
* future DBcursor->get calls will simply return the underlying
1289
* record (there's no adjustment made for the DB_CURRENT flag
1290
* when a cursor get operation immediately follows a cursor
1291
* delete operation, and the normal adjustment for the DB_NEXT
1292
* flag is still correct).
1296
/* Discard the stack of pages and split the page. */
1297
(void)__bam_stkrel(dbc, STK_CLRDBC);
1300
if ((ret = __bam_split(dbc, recnop, NULL)) != 0)
1310
__bam_stkrel(dbc, STK_CLRDBC);