Update.
[glibc.git] / db2 / btree / bt_close.c
blob9df5c717e6d1109626131378827f4b8bf8f7824a
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
7 /*
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
16 * Mike Olson.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
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
44 * SUCH DAMAGE.
47 #include "config.h"
49 #ifndef lint
50 static const char sccsid[] = "@(#)bt_close.c 10.32 (Sleepycat) 5/6/98";
51 #endif /* not lint */
53 #ifndef NO_SYSTEM_INCLUDES
54 #include <sys/types.h>
56 #include <string.h>
57 #endif
59 #include "db_int.h"
60 #include "db_page.h"
61 #include "btree.h"
63 static void __bam_upstat __P((DB *dbp));
66 * __bam_close --
67 * Close a btree.
69 * PUBLIC: int __bam_close __P((DB *));
71 int
72 __bam_close(dbp)
73 DB *dbp;
75 BTREE *t;
77 DEBUG_LWRITE(dbp, NULL, "bam_close", NULL, NULL, 0);
79 t = dbp->internal;
81 /* Update tree statistics. */
82 __bam_upstat(dbp);
84 /* Free any allocated memory. */
85 if (t->bt_rkey.data)
86 FREE(t->bt_rkey.data, t->bt_rkey.size);
87 if (t->bt_rdata.data)
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));
93 dbp->internal = NULL;
95 return (0);
99 * __bam_sync --
100 * Sync the btree to disk.
102 * PUBLIC: int __bam_sync __P((DB *, u_int32_t));
105 __bam_sync(argdbp, flags)
106 DB *argdbp;
107 u_int32_t flags;
109 DB *dbp;
110 int ret;
112 DEBUG_LWRITE(argdbp, NULL, "bam_sync", NULL, NULL, flags);
114 /* Check for invalid flags. */
115 if ((ret = __db_syncchk(argdbp, flags)) != 0)
116 return (ret);
118 /* If it wasn't possible to modify the file, we're done. */
119 if (F_ISSET(argdbp, DB_AM_INMEM | DB_AM_RDONLY))
120 return (0);
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)
126 ret = 0;
128 PUTHANDLE(dbp);
129 return (ret);
133 * __bam_upstat --
134 * Update tree statistics.
136 static void
137 __bam_upstat(dbp)
138 DB *dbp;
140 BTREE *t;
141 BTMETA *meta;
142 DB_LOCK metalock;
143 db_pgno_t pgno;
144 u_int32_t flags;
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,
150 * anyway.
152 if (F_ISSET(dbp, DB_AM_INMEM | DB_AM_RDONLY))
153 return;
155 flags = 0;
156 pgno = PGNO_METADATA;
158 /* Lock and retrieve the page. */
159 if (__bam_lget(dbp, 0, pgno, DB_LOCK_WRITE, &metalock) != 0)
160 return;
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)
166 goto err;
168 /* Update the statistics. */
169 t = dbp->internal;
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);