/* */
This source file includes following definitions.
- __bt_close
- __bt_sync
- bt_meta
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)bt_close.c 8.7 (Berkeley) 8/17/94";
35 #endif /* LIBC_SCCS and not lint */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40 #include <errno.h>
41 #include <stdio.h>
42 #ifdef STDC_HEADERS
43 #include <stdlib.h>
44 #endif
45 #ifdef HAVE_STRING_H
46 #include <string.h>
47 #else
48 #include <strings.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53
54 #include "db.h"
55 #include "btree.h"
56
57 static int bt_meta(BTREE *);
58
59 /**
60 * BT_CLOSE -- Close a btree.
61 *
62 * @param dbp pointer to access method
63 * @param abandon 1: don't sync, 0: sync
64 *
65 * @return #RET_ERROR, #RET_SUCCESS
66 */
67 int
68 __bt_close(dbp, abandon)
69 DB *dbp;
70 int abandon;
71 {
72 BTREE *t;
73 int fd;
74
75 t = dbp->internal;
76
77 /* Toss any page pinned across calls. */
78 if (t->bt_pinned != NULL) {
79 mpool_put(t->bt_mp, t->bt_pinned, 0);
80 t->bt_pinned = NULL;
81 }
82
83 /* Sync the tree. */
84 /*
85 * If abandon flag is set, omit writing to the disk.
86 * Since the writing spend much time, you should use this flag
87 * when you remove the file after closing.
88 */
89 if (!abandon && __bt_sync(dbp, 0) == RET_ERROR)
90 return (RET_ERROR);
91
92 /* Close the memory pool. */
93 if (mpool_close(t->bt_mp) == RET_ERROR)
94 return (RET_ERROR);
95
96 /* Free random memory. */
97 if (t->bt_cursor.key.data != NULL) {
98 free(t->bt_cursor.key.data);
99 t->bt_cursor.key.size = 0;
100 t->bt_cursor.key.data = NULL;
101 }
102 if (t->bt_rkey.data) {
103 free(t->bt_rkey.data);
104 t->bt_rkey.size = 0;
105 t->bt_rkey.data = NULL;
106 }
107 if (t->bt_rdata.data) {
108 free(t->bt_rdata.data);
109 t->bt_rdata.size = 0;
110 t->bt_rdata.data = NULL;
111 }
112
113 fd = t->bt_fd;
114 free(t);
115 free(dbp);
116 return (close(fd) ? RET_ERROR : RET_SUCCESS);
117 }
118
119 /**
120 * BT_SYNC -- sync the btree to disk.
121 *
122 * @param dbp pointer to access method
123 * @param flags
124 *
125 * @return #RET_SUCCESS, #RET_ERROR.
126 */
127 int
128 __bt_sync(dbp, flags)
129 const DB *dbp;
130 u_int flags;
131 {
132 BTREE *t;
133 int status;
134
135 t = dbp->internal;
136
137 /* Toss any page pinned across calls. */
138 if (t->bt_pinned != NULL) {
139 mpool_put(t->bt_mp, t->bt_pinned, 0);
140 t->bt_pinned = NULL;
141 }
142
143 /* Sync doesn't currently take any flags. */
144 if (flags != 0) {
145 errno = EINVAL;
146 return (RET_ERROR);
147 }
148
149 if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
150 return (RET_SUCCESS);
151
152 if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
153 return (RET_ERROR);
154
155 if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
156 F_CLR(t, B_MODIFIED);
157
158 return (status);
159 }
160
161 /**
162 * BT_META -- write the tree meta data to disk.
163 *
164 * @param t tree
165 *
166 * @return #RET_ERROR, #RET_SUCCESS
167 */
168 static int
169 bt_meta(t)
170 BTREE *t;
171 {
172 BTMETA m;
173 void *p;
174
175 if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
176 return (RET_ERROR);
177
178 /* Fill in metadata. */
179 m.magic = BTREEMAGIC;
180 m.version = BTREEVERSION;
181 m.psize = t->bt_psize;
182 m.free = t->bt_free;
183 m.nrecs = t->bt_nrecs;
184 m.flags = F_ISSET(t, SAVEMETA);
185
186 memmove(p, &m, sizeof(BTMETA));
187 mpool_put(t->bt_mp, p, MPOOL_DIRTY);
188 return (RET_SUCCESS);
189 }
/* */