2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
23
#include "dbinc/db_page.h"
24
#include "dbinc/btree.h"
25
#include "dbinc/db_shash.h"
26
#include "dbinc/lock.h"
28
static int __ram_add __P((DBC *, db_recno_t *, DBT *, u_int32_t, u_int32_t));
29
static int __ram_source __P((DB *));
30
static int __ram_sread __P((DBC *, db_recno_t));
31
static int __ram_update __P((DBC *, db_recno_t, int));
34
* In recno, there are two meanings to the on-page "deleted" flag. If we're
35
* re-numbering records, it means the record was implicitly created. We skip
36
* over implicitly created records if doing a cursor "next" or "prev", and
37
* return DB_KEYEMPTY if they're explicitly requested.. If not re-numbering
38
* records, it means that the record was implicitly created, or was deleted.
39
* We skip over implicitly created or deleted records if doing a cursor "next"
40
* or "prev", and return DB_KEYEMPTY if they're explicitly requested.
42
* If we're re-numbering records, then we have to detect in the cursor that
43
* a record was deleted, and adjust the cursor as necessary on the next get.
44
* If we're not re-numbering records, then we can detect that a record has
45
* been deleted by looking at the actual on-page record, so we completely
46
* ignore the cursor's delete flag. This is different from the B+tree code.
47
* It also maintains whether the cursor references a deleted record in the
48
* cursor, and it doesn't always check the on-page value.
50
#define CD_SET(cp) { \
51
if (F_ISSET(cp, C_RENUMBER)) \
52
F_SET(cp, C_DELETED); \
54
#define CD_CLR(cp) { \
55
if (F_ISSET(cp, C_RENUMBER)) { \
56
F_CLR(cp, C_DELETED); \
57
cp->order = INVALID_ORDER; \
60
#define CD_ISSET(cp) \
61
(F_ISSET(cp, C_RENUMBER) && F_ISSET(cp, C_DELETED))
64
* Macros for comparing the ordering of two cursors.
65
* cp1 comes before cp2 iff one of the following holds:
66
* cp1's recno is less than cp2's recno
67
* recnos are equal, both deleted, and cp1's order is less than cp2's
68
* recnos are equal, cp1 deleted, and cp2 not deleted
70
#define C_LESSTHAN(cp1, cp2) \
71
(((cp1)->recno < (cp2)->recno) || \
72
(((cp1)->recno == (cp2)->recno) && \
73
((CD_ISSET((cp1)) && CD_ISSET((cp2)) && (cp1)->order < (cp2)->order) || \
74
(CD_ISSET((cp1)) && !CD_ISSET((cp2))))))
77
* cp1 is equal to cp2 iff their recnos and delete flags are identical,
78
* and if the delete flag is set their orders are also identical.
80
#define C_EQUAL(cp1, cp2) \
81
((cp1)->recno == (cp2)->recno && CD_ISSET((cp1)) == CD_ISSET((cp2)) && \
82
(!CD_ISSET((cp1)) || (cp1)->order == (cp2)->order))
85
* Do we need to log the current cursor adjustment?
87
#define CURADJ_LOG(dbc) \
88
(DBC_LOGGING((dbc)) && (dbc)->txn != NULL && (dbc)->txn->parent != NULL)
91
* After a search, copy the found page into the cursor, discarding any
92
* currently held lock.
94
#define STACK_TO_CURSOR(cp) { \
95
(cp)->page = (cp)->csp->page; \
96
(cp)->pgno = (cp)->csp->page->pgno; \
97
(cp)->indx = (cp)->csp->indx; \
98
(void)__TLPUT(dbc, (cp)->lock); \
99
(cp)->lock = (cp)->csp->lock; \
100
(cp)->lock_mode = (cp)->csp->lock_mode; \
105
* Recno open function.
107
* PUBLIC: int __ram_open __P((DB *,
108
* PUBLIC: DB_TXN *, const char *, db_pgno_t, u_int32_t));
111
__ram_open(dbp, txn, name, base_pgno, flags)
122
COMPQUIET(name, NULL);
123
t = dbp->bt_internal;
125
/* Initialize the remaining fields/methods of the DB. */
126
dbp->stat = __bam_stat;
128
/* Start up the tree. */
129
if ((ret = __bam_read_root(dbp, txn, base_pgno, flags)) != 0)
133
* If the user specified a source tree, open it and map it in.
136
* We don't complain if the user specified transactions or threads.
137
* It's possible to make it work, but you'd better know what you're
140
if (t->re_source != NULL && (ret = __ram_source(dbp)) != 0)
143
/* If we're snapshotting an underlying source file, do it now. */
144
if (F_ISSET(dbp, DB_AM_SNAPSHOT)) {
145
/* Allocate a cursor. */
146
if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
149
/* Do the snapshot. */
150
if ((ret = __ram_update(dbc,
151
DB_MAX_RECORDS, 0)) != 0 && ret == DB_NOTFOUND)
154
/* Discard the cursor. */
155
if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
164
* Recno append function.
166
* PUBLIC: int __ram_append __P((DBC *, DBT *, DBT *));
169
__ram_append(dbc, key, data)
176
cp = (BTREE_CURSOR *)dbc->internal;
179
* Make sure we've read in all of the backing source file. If
180
* we found the record or it simply didn't exist, add the
183
ret = __ram_update(dbc, DB_MAX_RECORDS, 0);
184
if (ret == 0 || ret == DB_NOTFOUND)
185
ret = __ram_add(dbc, &cp->recno, data, DB_APPEND, 0);
187
/* Return the record number. */
189
ret = __db_retcopy(dbc->dbp->dbenv, key, &cp->recno,
190
sizeof(cp->recno), &dbc->rkey->data, &dbc->rkey->ulen);
197
* Recno cursor->c_del function.
199
* PUBLIC: int __ram_c_del __P((DBC *));
212
int exact, ret, stack;
215
cp = (BTREE_CURSOR *)dbc->internal;
216
t = dbp->bt_internal;
220
* The semantics of cursors during delete are as follows: in
221
* non-renumbering recnos, records are replaced with a marker
222
* containing a delete flag. If the record referenced by this cursor
223
* has already been deleted, we will detect that as part of the delete
224
* operation, and fail.
226
* In renumbering recnos, cursors which represent deleted items
227
* are flagged with the C_DELETED flag, and it is an error to
228
* call c_del a second time without an intervening cursor motion.
231
return (DB_KEYEMPTY);
233
/* Search the tree for the key; delete only deletes exact matches. */
234
if ((ret = __bam_rsearch(dbc, &cp->recno, S_DELETE, 1, &exact)) != 0)
242
/* Copy the page into the cursor. */
246
* If re-numbering records, the on-page deleted flag can only mean
247
* that this record was implicitly created. Applications aren't
248
* permitted to delete records they never created, return an error.
250
* If not re-numbering records, the on-page deleted flag means that
251
* this record was implicitly created, or, was deleted at some time.
252
* The former is an error because applications aren't permitted to
253
* delete records they never created, the latter is an error because
254
* if the record was "deleted", we could never have found it.
256
if (B_DISSET(GET_BKEYDATA(dbp, cp->page, cp->indx)->type)) {
261
if (F_ISSET(cp, C_RENUMBER)) {
262
/* Delete the item, adjust the counts, adjust the cursors. */
263
if ((ret = __bam_ditem(dbc, cp->page, cp->indx)) != 0)
265
__bam_adjust(dbc, -1);
266
if (__ram_ca(dbc, CA_DELETE) > 0 &&
267
CURADJ_LOG(dbc) && (ret = __bam_rcuradj_log(dbp, dbc->txn,
268
&lsn, 0, CA_DELETE, cp->root, cp->recno, cp->order)) != 0)
272
* If the page is empty, delete it.
274
* We never delete a root page. First, root pages of primary
275
* databases never go away, recno or otherwise. However, if
276
* it's the root page of an off-page duplicates database, then
277
* it can be deleted. We don't delete it here because we have
278
* no way of telling the primary database page holder (e.g.,
279
* the hash access method) that its page element should cleaned
280
* up because the underlying tree is gone. So, we keep the page
281
* around until the last cursor referencing the empty tree is
282
* are closed, and then clean it up.
284
if (NUM_ENT(cp->page) == 0 && PGNO(cp->page) != cp->root) {
286
* We already have a locked stack of pages. However,
287
* there are likely entries in the stack that aren't
288
* going to be emptied by removing the single reference
289
* to the emptied page (or one of its parents).
291
for (epg = cp->csp; epg >= cp->sp; --epg)
292
if (NUM_ENT(epg->page) > 1)
296
* We want to delete a single item out of the last page
297
* that we're not deleting.
299
ret = __bam_dpages(dbc, epg);
302
* Regardless of the return from __bam_dpages, it will
303
* discard our stack and pinned page.
309
/* Use a delete/put pair to replace the record with a marker. */
310
if ((ret = __bam_ditem(dbc, cp->page, cp->indx)) != 0)
313
B_TSET(bk.type, B_KEYDATA, 1);
315
memset(&hdr, 0, sizeof(hdr));
317
hdr.size = SSZA(BKEYDATA, data);
318
memset(&data, 0, sizeof(data));
319
data.data = (void *)"";
321
if ((ret = __db_pitem(dbc,
322
cp->page, cp->indx, BKEYDATA_SIZE(0), &hdr, &data)) != 0)
329
__bam_stkrel(dbc, STK_CLRDBC);
336
* Recno cursor->c_get function.
338
* PUBLIC: int __ram_c_get
339
* PUBLIC: __P((DBC *, DBT *, DBT *, u_int32_t, db_pgno_t *));
342
__ram_c_get(dbc, key, data, flags, pgnop)
352
COMPQUIET(pgnop, NULL);
355
cp = (BTREE_CURSOR *)dbc->internal;
357
LF_CLR(DB_MULTIPLE|DB_MULTIPLE_KEY);
358
retry: switch (flags) {
361
* If we're using mutable records and the deleted flag is
362
* set, the cursor is pointing at a nonexistent record;
366
return (DB_KEYEMPTY);
370
* If we're not in an off-page dup set, we know there's no
371
* next duplicate since recnos don't have them. If we
372
* are in an off-page dup set, the next item assuredly is
373
* a dup, so we set flags to DB_NEXT and keep going.
375
if (!F_ISSET(dbc, DBC_OPD))
376
return (DB_NOTFOUND);
380
* Recno databases don't have duplicates, set flags to DB_NEXT
387
* If record numbers are mutable: if we just deleted a record,
388
* we have to avoid incrementing the record number so that we
389
* return the right record by virtue of renumbering the tree.
394
if (cp->recno != RECNO_OOB) {
405
* Recno databases don't have duplicates, set flags to DB_PREV
411
if (cp->recno != RECNO_OOB) {
412
if (cp->recno == 1) {
422
if (((ret = __ram_update(dbc,
423
DB_MAX_RECORDS, 0)) != 0) && ret != DB_NOTFOUND)
425
if ((ret = __bam_nrecs(dbc, &cp->recno)) != 0)
427
if (cp->recno == 0) {
434
* If we're doing a join and these are offpage dups,
435
* we want to keep searching forward from after the
436
* current cursor position. Increment the recno by 1,
437
* then proceed as for a DB_SET.
439
* Otherwise, we know there are no additional matching
440
* data, as recnos don't have dups. return DB_NOTFOUND.
442
if (F_ISSET(dbc, DBC_OPD)) {
450
case DB_GET_BOTH_RANGE:
452
* If we're searching a set of off-page dups, we start
453
* a new linear search from the first record. Otherwise,
454
* we compare the single data item associated with the
455
* requested record for a match.
457
if (F_ISSET(dbc, DBC_OPD)) {
464
if ((ret = __ram_getno(dbc, key, &cp->recno, 0)) != 0)
468
ret = __db_unknown_flag(dbp->dbenv, "__ram_c_get", flags);
473
* For DB_PREV, DB_LAST, DB_SET and DB_SET_RANGE, we have already
474
* called __ram_update() to make sure sufficient records have been
475
* read from the backing source file. Do it now for DB_CURRENT (if
476
* the current record was deleted we may need more records from the
477
* backing file for a DB_CURRENT operation), DB_FIRST and DB_NEXT.
478
* (We don't have to test for flags == DB_FIRST, because the switch
479
* statement above re-set flags to DB_NEXT in that case.)
481
if ((flags == DB_NEXT || flags == DB_CURRENT) && ((ret =
482
__ram_update(dbc, cp->recno, 0)) != 0) && ret != DB_NOTFOUND)
485
for (;; ++cp->recno) {
486
/* Search the tree for the record. */
487
if ((ret = __bam_rsearch(dbc, &cp->recno,
488
F_ISSET(dbc, DBC_RMW) ? S_FIND_WR : S_FIND,
496
/* Copy the page into the cursor. */
500
* If re-numbering records, the on-page deleted flag means this
501
* record was implicitly created. If not re-numbering records,
502
* the on-page deleted flag means this record was implicitly
503
* created, or, it was deleted at some time. Regardless, we
504
* skip such records if doing cursor next/prev operations or
505
* walking through off-page duplicates, and fail if they were
506
* requested explicitly by the application.
508
if (B_DISSET(GET_BKEYDATA(dbp, cp->page, cp->indx)->type))
512
(void)__bam_stkrel(dbc, STK_CLRDBC);
515
case DB_GET_BOTH_RANGE:
517
* If we're an OPD tree, we don't care about
518
* matching a record number on a DB_GET_BOTH
519
* -- everything belongs to the same tree. A
520
* normal recno should give up and return
521
* DB_NOTFOUND if the matching recno is deleted.
523
if (F_ISSET(dbc, DBC_OPD)) {
524
(void)__bam_stkrel(dbc, STK_CLRDBC);
534
if (flags == DB_GET_BOTH ||
535
flags == DB_GET_BOTHC || flags == DB_GET_BOTH_RANGE) {
536
if ((ret = __bam_cmp(dbp, data,
537
cp->page, cp->indx, __bam_defcmp, &cmp)) != 0)
541
if (!F_ISSET(dbc, DBC_OPD)) {
545
(void)__bam_stkrel(dbc, STK_CLRDBC);
550
/* Return the key if the user didn't give us one. */
551
if (!F_ISSET(dbc, DBC_OPD)) {
552
if (flags != DB_GET_BOTH && flags != DB_GET_BOTH_RANGE &&
553
flags != DB_SET && flags != DB_SET_RANGE)
554
ret = __db_retcopy(dbp->dbenv,
555
key, &cp->recno, sizeof(cp->recno),
556
&dbc->rkey->data, &dbc->rkey->ulen);
557
F_SET(key, DB_DBT_ISSET);
560
/* The cursor was reset, no further delete adjustment is necessary. */
568
* Recno cursor->c_put function.
570
* PUBLIC: int __ram_c_put __P((DBC *, DBT *, DBT *, u_int32_t, db_pgno_t *));
573
__ram_c_put(dbc, key, data, flags, pgnop)
582
int exact, nc, ret, t_ret;
586
COMPQUIET(pgnop, NULL);
589
cp = (BTREE_CURSOR *)dbc->internal;
592
* DB_KEYFIRST and DB_KEYLAST mean different things if they're
593
* used in an off-page duplicate tree. If we're an off-page
594
* duplicate tree, they really mean "put at the beginning of the
595
* tree" and "put at the end of the tree" respectively, so translate
596
* them to something else.
598
if (F_ISSET(dbc, DBC_OPD))
605
if ((ret = __ram_add(dbc,
606
&cp->recno, data, DB_APPEND, 0)) != 0)
608
if (CURADJ_LOG(dbc) &&
609
(ret = __bam_rcuradj_log(dbp, dbc->txn, &lsn, 0,
610
CA_ICURRENT, cp->root, cp->recno, cp->order)))
616
* Handle normal DB_KEYFIRST/DB_KEYLAST; for a recno, which has
617
* no duplicates, these are identical and mean "put the given
618
* datum at the given recno".
620
* Note that the code here used to be in __ram_put; now, we
621
* go through the access-method-common __db_put function, which
622
* handles DB_NOOVERWRITE, so we and __ram_add don't have to.
624
if (flags == DB_KEYFIRST || flags == DB_KEYLAST) {
625
ret = __ram_getno(dbc, key, &cp->recno, 1);
626
if (ret == 0 || ret == DB_NOTFOUND)
627
ret = __ram_add(dbc, &cp->recno, data, 0, 0);
632
* If we're putting with a cursor that's marked C_DELETED, we need to
633
* take special care; the cursor doesn't "really" reference the item
634
* corresponding to its current recno, but instead is "between" that
635
* record and the current one. Translate the actual insert into
636
* DB_BEFORE, and let the __ram_ca work out the gory details of what
637
* should wind up pointing where.
644
split: if ((ret = __bam_rsearch(dbc, &cp->recno, S_INSERT, 1, &exact)) != 0)
647
* An inexact match is okay; it just means we're one record past the
648
* end, which is reasonable if we're marked deleted.
650
DB_ASSERT(exact || CD_ISSET(cp));
652
/* Copy the page into the cursor. */
655
ret = __bam_iitem(dbc, key, data, iiflags, 0);
656
t_ret = __bam_stkrel(dbc, STK_CLRDBC);
658
if (t_ret != 0 && (ret == 0 || ret == DB_NEEDSPLIT))
660
else if (ret == DB_NEEDSPLIT) {
662
if ((ret = __bam_split(dbc, arg, NULL)) != 0)
669
switch (flags) { /* Adjust the cursors. */
671
nc = __ram_ca(dbc, CA_IAFTER);
674
* We only need to adjust this cursor forward if we truly added
675
* the item after the current recno, rather than remapping it
678
if (iiflags == DB_AFTER)
681
/* Only log if __ram_ca found any relevant cursors. */
682
if (nc > 0 && CURADJ_LOG(dbc) &&
683
(ret = __bam_rcuradj_log(dbp, dbc->txn, &lsn, 0, CA_IAFTER,
684
cp->root, cp->recno, cp->order)) != 0)
688
nc = __ram_ca(dbc, CA_IBEFORE);
691
/* Only log if __ram_ca found any relevant cursors. */
692
if (nc > 0 && CURADJ_LOG(dbc) &&
693
(ret = __bam_rcuradj_log(dbp, dbc->txn, &lsn, 0, CA_IBEFORE,
694
cp->root, cp->recno, cp->order)) != 0)
699
* We only need to do an adjustment if we actually
700
* added an item, which we only would have done if the
701
* cursor was marked deleted.
703
* Only log if __ram_ca found any relevant cursors.
705
if (CD_ISSET(cp) && __ram_ca(dbc, CA_ICURRENT) > 0 &&
707
(ret = __bam_rcuradj_log(dbp, dbc->txn, &lsn, 0,
708
CA_ICURRENT, cp->root, cp->recno, cp->order)) != 0)
713
/* Return the key if we've created a new record. */
714
if (!F_ISSET(dbc, DBC_OPD) && (flags == DB_AFTER || flags == DB_BEFORE))
715
ret = __db_retcopy(dbp->dbenv, key, &cp->recno,
716
sizeof(cp->recno), &dbc->rkey->data, &dbc->rkey->ulen);
718
/* The cursor was reset, no further delete adjustment is necessary. */
726
* Adjust cursors. Returns the number of relevant cursors.
728
* PUBLIC: int __ram_ca __P((DBC *, ca_recno_arg));
731
__ram_ca(dbc_arg, op)
735
BTREE_CURSOR *cp, *cp_arg;
745
cp_arg = (BTREE_CURSOR *)dbc_arg->internal;
746
recno = cp_arg->recno;
751
* It only makes sense to adjust cursors if we're a renumbering
752
* recno; we should only be called if this is one.
754
DB_ASSERT(F_ISSET(cp_arg, C_RENUMBER));
756
MUTEX_THREAD_LOCK(dbenv, dbenv->dblist_mutexp);
758
* Adjust the cursors. See the comment in __bam_ca_delete().
761
* If we're doing a delete, we need to find the highest
762
* order of any cursor currently pointing at this item,
763
* so we can assign a higher order to the newly deleted
764
* cursor. Unfortunately, this requires a second pass through
767
if (op == CA_DELETE) {
769
for (ldbp = __dblist_get(dbenv, dbp->adj_fileid);
770
ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
771
ldbp = LIST_NEXT(ldbp, dblistlinks)) {
772
MUTEX_THREAD_LOCK(dbenv, dbp->mutexp);
773
for (dbc = TAILQ_FIRST(&ldbp->active_queue);
774
dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
775
cp = (BTREE_CURSOR *)dbc->internal;
776
if (cp_arg->root == cp->root &&
777
recno == cp->recno && CD_ISSET(cp) &&
779
order = cp->order + 1;
781
MUTEX_THREAD_UNLOCK(dbenv, dbp->mutexp);
784
order = INVALID_ORDER;
786
/* Now go through and do the actual adjustments. */
787
for (ldbp = __dblist_get(dbenv, dbp->adj_fileid);
788
ldbp != NULL && ldbp->adj_fileid == dbp->adj_fileid;
789
ldbp = LIST_NEXT(ldbp, dblistlinks)) {
790
MUTEX_THREAD_LOCK(dbenv, dbp->mutexp);
791
for (dbc = TAILQ_FIRST(&ldbp->active_queue);
792
dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
793
cp = (BTREE_CURSOR *)dbc->internal;
794
if (cp_arg->root != cp->root)
800
if (recno < cp->recno) {
803
* If the adjustment made them equal,
804
* we have to merge the orders.
806
if (recno == cp->recno && CD_ISSET(cp))
808
} else if (recno == cp->recno &&
816
* IBEFORE is just like IAFTER, except that we
817
* adjust cursors on the current record too.
819
if (C_EQUAL(cp_arg, cp)) {
827
* If the original cursor wasn't deleted, we
828
* just did a replacement and so there's no
829
* need to adjust anything--we shouldn't have
830
* gotten this far. Otherwise, we behave
831
* much like an IAFTER, except that all
832
* cursors pointing to the current item get
833
* marked undeleted and point to the new
836
DB_ASSERT(CD_ISSET(cp_arg));
837
if (C_EQUAL(cp_arg, cp)) {
843
iafter: if (!adjusted && C_LESSTHAN(cp_arg, cp)) {
847
if (recno == cp->recno && adjusted)
849
* If we've moved this cursor's recno,
850
* split its order number--i.e.,
851
* decrement it by enough so that
852
* the lowest cursor moved has order 1.
853
* cp_arg->order is the split point,
854
* so decrement by one less than that.
856
cp->order -= (cp_arg->order - 1);
860
MUTEX_THREAD_UNLOCK(dbp->dbenv, dbp->mutexp);
862
MUTEX_THREAD_UNLOCK(dbenv, dbenv->dblist_mutexp);
869
* Check the user's record number, and make sure we've seen it.
871
* PUBLIC: int __ram_getno __P((DBC *, const DBT *, db_recno_t *, int));
874
__ram_getno(dbc, key, rep, can_create)
885
/* Check the user's record number. */
886
if ((recno = *(db_recno_t *)key->data) == 0) {
887
__db_err(dbp->dbenv, "illegal record number of 0");
894
* Btree can neither create records nor read them in. Recno can
895
* do both, see if we can find the record.
897
return (dbc->dbtype == DB_RECNO ?
898
__ram_update(dbc, recno, can_create) : 0);
903
* Ensure the tree has records up to and including the specified one.
906
__ram_update(dbc, recno, can_create)
918
t = dbp->bt_internal;
921
* If we can't create records and we've read the entire backing input
924
if (!can_create && t->re_eof)
928
* If we haven't seen this record yet, try to get it from the original
931
if ((ret = __bam_nrecs(dbc, &nrecs)) != 0)
933
if (!t->re_eof && recno > nrecs) {
934
if ((ret = __ram_sread(dbc, recno)) != 0 && ret != DB_NOTFOUND)
936
if ((ret = __bam_nrecs(dbc, &nrecs)) != 0)
941
* If we can create records, create empty ones up to the requested
944
if (!can_create || recno <= nrecs + 1)
947
rdata = &dbc->my_rdata;
951
while (recno > ++nrecs)
952
if ((ret = __ram_add(dbc,
953
&nrecs, rdata, 0, BI_DELETED)) != 0)
960
* Load information about the backing file.
970
t = dbp->bt_internal;
972
/* Find the real name, and swap out the one we had before. */
973
if ((ret = __db_appname(dbp->dbenv,
974
DB_APP_DATA, t->re_source, 0, NULL, &source)) != 0)
976
__os_free(dbp->dbenv, t->re_source);
977
t->re_source = source;
981
* It's possible that the backing source file is read-only. We don't
982
* much care other than we'll complain if there are any modifications
983
* when it comes time to write the database back to the source.
985
if ((t->re_fp = fopen(t->re_source, "r")) == NULL) {
987
__db_err(dbp->dbenv, "%s: %s", t->re_source, db_strerror(ret));
997
* Rewrite the backing file.
999
* PUBLIC: int __ram_writeback __P((DB *));
1002
__ram_writeback(dbp)
1012
u_int8_t delim, *pad;
1014
t = dbp->bt_internal;
1019
/* If the file wasn't modified, we're done. */
1020
if (!t->re_modified)
1023
/* If there's no backing source file, we're done. */
1024
if (t->re_source == NULL) {
1029
/* Allocate a cursor. */
1030
if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
1034
* Read any remaining records into the tree.
1037
* This is why we can't support transactions when applications specify
1038
* backing (re_source) files. At this point we have to read in the
1039
* rest of the records from the file so that we can write all of the
1040
* records back out again, which could modify a page for which we'd
1041
* have to log changes and which we don't have locked. This could be
1042
* partially fixed by taking a snapshot of the entire file during the
1043
* DB->open as DB->open is transaction protected. But, if a checkpoint
1044
* occurs then, the part of the log holding the copy of the file could
1045
* be discarded, and that would make it impossible to recover in the
1046
* face of disaster. This could all probably be fixed, but it would
1047
* require transaction protecting the backing source file.
1050
* This could be made to work now that we have transactions protecting
1051
* file operations. Margo has specifically asked for the privilege of
1055
__ram_update(dbc, DB_MAX_RECORDS, 0)) != 0 && ret != DB_NOTFOUND)
1059
* Close any existing file handle and re-open the file, truncating it.
1061
if (t->re_fp != NULL) {
1062
if (fclose(t->re_fp) != 0) {
1068
if ((fp = fopen(t->re_source, "w")) == NULL) {
1070
__db_err(dbenv, "%s: %s", t->re_source, db_strerror(ret));
1075
* We step through the records, writing each one out. Use the record
1076
* number and the dbp->get() function, instead of a cursor, so we find
1077
* and write out "deleted" or non-existent records. The DB handle may
1078
* be threaded, so allocate memory as we go.
1080
memset(&key, 0, sizeof(key));
1081
key.size = sizeof(db_recno_t);
1083
memset(&data, 0, sizeof(data));
1084
F_SET(&data, DB_DBT_REALLOC);
1087
* We'll need the delimiter if we're doing variable-length records,
1088
* and the pad character if we're doing fixed-length records.
1090
delim = t->re_delim;
1091
if (F_ISSET(dbp, DB_AM_FIXEDLEN)) {
1092
if ((ret = __os_malloc(dbenv, t->re_len, &pad)) != 0)
1094
memset(pad, t->re_pad, t->re_len);
1096
for (keyno = 1;; ++keyno) {
1097
switch (ret = dbp->get(dbp, NULL, &key, &data, 0)) {
1099
if (data.size != 0 && (u_int32_t)fwrite(
1100
data.data, 1, data.size, fp) != data.size)
1104
if (F_ISSET(dbp, DB_AM_FIXEDLEN) &&
1105
(u_int32_t)fwrite(pad, 1, t->re_len, fp) !=
1115
if (!F_ISSET(dbp, DB_AM_FIXEDLEN) &&
1116
fwrite(&delim, 1, 1, fp) != 1) {
1117
write_err: ret = errno;
1118
__db_err(dbp->dbenv,
1119
"%s: write failed to backing file: %s",
1120
t->re_source, strerror(ret));
1126
done: /* Close the file descriptor. */
1127
if (fp != NULL && fclose(fp) != 0) {
1130
__db_err(dbenv, "%s: %s", t->re_source, db_strerror(errno));
1133
/* Discard the cursor. */
1134
if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
1137
/* Discard memory allocated to hold the data items. */
1138
if (data.data != NULL)
1139
__os_ufree(dbenv, data.data);
1141
__os_free(dbenv, pad);
1151
* Read records from a source file.
1154
__ram_sread(dbc, top)
1163
int ch, ret, was_modified;
1165
t = dbc->dbp->bt_internal;
1167
was_modified = t->re_modified;
1169
if ((ret = __bam_nrecs(dbc, &recno)) != 0)
1173
* Use the record key return memory, it's only a short-term use.
1174
* The record data return memory is used by __bam_iitem, which
1175
* we'll indirectly call, so use the key so as not to collide.
1177
len = F_ISSET(dbp, DB_AM_FIXEDLEN) ? t->re_len : 256;
1178
rdata = &dbc->my_rkey;
1179
if (rdata->ulen < len) {
1180
if ((ret = __os_realloc(
1181
dbp->dbenv, len, &rdata->data)) != 0) {
1186
rdata->ulen = (u_int32_t)len;
1189
memset(&data, 0, sizeof(data));
1190
while (recno < top) {
1191
data.data = rdata->data;
1193
if (F_ISSET(dbp, DB_AM_FIXEDLEN))
1194
for (len = t->re_len; len > 0; --len) {
1195
if ((ch = getc(t->re_fp)) == EOF) {
1200
((u_int8_t *)data.data)[data.size++] = ch;
1204
if ((ch = getc(t->re_fp)) == EOF) {
1209
if (ch == t->re_delim)
1212
((u_int8_t *)data.data)[data.size++] = ch;
1213
if (data.size == rdata->ulen) {
1214
if ((ret = __os_realloc(dbp->dbenv,
1216
&rdata->data)) != 0) {
1221
data.data = rdata->data;
1226
* Another process may have read this record from the input
1227
* file and stored it into the database already, in which
1228
* case we don't need to repeat that operation. We detect
1229
* this by checking if the last record we've read is greater
1230
* or equal to the number of records in the database.
1232
if (t->re_last >= recno) {
1234
if ((ret = __ram_add(dbc, &recno, &data, 0, 0)) != 0)
1244
err: if (!was_modified)
1252
* Add records into the tree.
1255
__ram_add(dbc, recnop, data, flags, bi_flags)
1259
u_int32_t flags, bi_flags;
1262
int exact, ret, stack;
1264
cp = (BTREE_CURSOR *)dbc->internal;
1266
retry: /* Find the slot for insertion. */
1267
if ((ret = __bam_rsearch(dbc, recnop,
1268
S_INSERT | (flags == DB_APPEND ? S_APPEND : 0), 1, &exact)) != 0)
1272
/* Copy the page into the cursor. */
1273
STACK_TO_CURSOR(cp);
1276
* The application may modify the data based on the selected record
1279
if (flags == DB_APPEND && dbc->dbp->db_append_recno != NULL &&
1280
(ret = dbc->dbp->db_append_recno(dbc->dbp, data, *recnop)) != 0)
1284
* Select the arguments for __bam_iitem() and do the insert. If the
1285
* key is an exact match, or we're replacing the data item with a
1286
* new data item, replace the current item. If the key isn't an exact
1287
* match, we're inserting a new key/data pair, before the search
1290
switch (ret = __bam_iitem(dbc,
1291
NULL, data, exact ? DB_CURRENT : DB_BEFORE, bi_flags)) {
1294
* Don't adjust anything.
1296
* If we inserted a record, no cursors need adjusting because
1297
* the only new record it's possible to insert is at the very
1298
* end of the tree. The necessary adjustments to the internal
1299
* page counts were made by __bam_iitem().
1301
* If we overwrote a record, no cursors need adjusting because
1302
* future DBcursor->get calls will simply return the underlying
1303
* record (there's no adjustment made for the DB_CURRENT flag
1304
* when a cursor get operation immediately follows a cursor
1305
* delete operation, and the normal adjustment for the DB_NEXT
1306
* flag is still correct).
1310
/* Discard the stack of pages and split the page. */
1311
(void)__bam_stkrel(dbc, STK_CLRDBC);
1314
if ((ret = __bam_split(dbc, recnop, NULL)) != 0)
1324
__bam_stkrel(dbc, STK_CLRDBC);