2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
8 * Copyright (c) 1990, 1993, 1994, 1995, 1996
9 * Keith Bostic. All rights reserved.
12 * Copyright (c) 1990, 1993, 1994, 1995
13 * The Regents of the University of California. All rights reserved.
15 * This code is derived from software contributed to Berkeley by
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 static const char sccsid
[] = "@(#)bt_close.c 10.32 (Sleepycat) 5/6/98";
53 #ifndef NO_SYSTEM_INCLUDES
54 #include <sys/types.h>
63 static void __bam_upstat
__P((DB
*dbp
));
69 * PUBLIC: int __bam_close __P((DB *));
77 DEBUG_LWRITE(dbp
, NULL
, "bam_close", NULL
, NULL
, 0);
81 /* Update tree statistics. */
84 /* Free any allocated memory. */
86 FREE(t
->bt_rkey
.data
, t
->bt_rkey
.size
);
88 FREE(t
->bt_rdata
.data
, t
->bt_rdata
.ulen
);
89 if (t
->bt_sp
!= t
->bt_stack
)
90 FREE(t
->bt_sp
, (t
->bt_esp
- t
->bt_sp
) * sizeof(EPG
));
92 FREE(t
, sizeof(BTREE
));
100 * Sync the btree to disk.
102 * PUBLIC: int __bam_sync __P((DB *, u_int32_t));
105 __bam_sync(argdbp
, flags
)
112 DEBUG_LWRITE(argdbp
, NULL
, "bam_sync", NULL
, NULL
, flags
);
114 /* Check for invalid flags. */
115 if ((ret
= __db_syncchk(argdbp
, flags
)) != 0)
118 /* If it wasn't possible to modify the file, we're done. */
119 if (F_ISSET(argdbp
, DB_AM_INMEM
| DB_AM_RDONLY
))
122 GETHANDLE(argdbp
, NULL
, &dbp
, ret
);
124 /* Flush any dirty pages from the cache to the backing file. */
125 if ((ret
= memp_fsync(dbp
->mpf
)) == DB_INCOMPLETE
)
134 * Update tree statistics.
147 * We use a no-op log call to log the update of the statistics onto the
148 * metadata page. The Db->close call isn't transaction protected to
149 * start with, and I'm not sure what undoing a statistics update means,
152 if (F_ISSET(dbp
, DB_AM_INMEM
| DB_AM_RDONLY
))
156 pgno
= PGNO_METADATA
;
158 /* Lock and retrieve the page. */
159 if (__bam_lget(dbp
, 0, pgno
, DB_LOCK_WRITE
, &metalock
) != 0)
161 if (__bam_pget(dbp
, (PAGE
**)&meta
, &pgno
, 0) == 0) {
162 /* Log the change. */
163 if (DB_LOGGING(dbp
) &&
164 __db_noop_log(dbp
->dbenv
->lg_info
, dbp
->txn
, &LSN(meta
), 0,
165 dbp
->log_fileid
, PGNO_METADATA
, &LSN(meta
)) != 0)
168 /* Update the statistics. */
170 __bam_add_mstat(&t
->lstat
, &meta
->stat
);
172 flags
= DB_MPOOL_DIRTY
;
175 err
: (void)memp_fput(dbp
->mpf
, (PAGE
*)meta
, flags
);
176 (void)__BT_LPUT(dbp
, metalock
);