1 /* rev-table.c : working with the `revisions' table
3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
20 * ====================================================================
23 #include "bdb_compat.h"
26 #include "private/svn_skel.h"
30 #include "../util/fs_skels.h"
31 #include "../../libsvn_fs/fs-loader.h"
34 #include "rev-table.h"
36 #include "svn_private_config.h"
37 #include "private/svn_fs_util.h"
40 /* Opening/creating the `revisions' table. */
42 int svn_fs_bdb__open_revisions_table(DB **revisions_p,
46 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
49 BDB_ERR(svn_fs_bdb__check_version());
50 BDB_ERR(db_create(&revisions, env, 0));
51 BDB_ERR((revisions->open)(SVN_BDB_OPEN_PARAMS(revisions, NULL),
52 "revisions", 0, DB_RECNO,
55 *revisions_p = revisions;
61 /* Storing and retrieving filesystem revisions. */
65 svn_fs_bdb__get_rev(revision_t **revision_p,
71 base_fs_data_t *bfd = fs->fsap_data;
77 /* Turn the revision number into a Berkeley DB record number.
78 Revisions are numbered starting with zero; Berkeley DB record
79 numbers begin with one. */
80 db_recno_t recno = (db_recno_t) rev + 1;
82 svn_fs_base__trail_debug(trail, "revisions", "get");
83 db_err = bfd->revisions->get(bfd->revisions, trail->db_txn,
84 svn_fs_base__set_dbt(&key, &recno,
86 svn_fs_base__result_dbt(&value),
88 svn_fs_base__track_dbt(&value, pool);
90 /* If there's no such revision, return an appropriately specific error. */
91 if (db_err == DB_NOTFOUND)
92 return svn_fs_base__err_dangling_rev(fs, rev);
94 /* Handle any other error conditions. */
95 SVN_ERR(BDB_WRAP(fs, N_("reading filesystem revision"), db_err));
97 /* Parse REVISION skel. */
98 skel = svn_skel__parse(value.data, value.size, pool);
100 return svn_fs_base__err_corrupt_fs_revision(fs, rev);
102 /* Convert skel to native type. */
103 SVN_ERR(svn_fs_base__parse_revision_skel(&revision, skel, pool));
105 *revision_p = revision;
110 /* Write REVISION to FS as part of TRAIL. If *REV is a valid revision
111 number, write this revision as one that corresponds to *REV, else
112 write a new revision and return its newly created revision number
115 svn_fs_bdb__put_rev(svn_revnum_t *rev,
117 const revision_t *revision,
121 base_fs_data_t *bfd = fs->fsap_data;
123 db_recno_t recno = 0;
127 /* Convert native type to skel. */
128 SVN_ERR(svn_fs_base__unparse_revision_skel(&skel, revision, pool));
130 if (SVN_IS_VALID_REVNUM(*rev))
134 /* Update the filesystem revision with the new skel. */
135 recno = (db_recno_t) *rev + 1;
136 svn_fs_base__trail_debug(trail, "revisions", "put");
137 db_err = bfd->revisions->put
138 (bfd->revisions, trail->db_txn,
139 svn_fs_base__set_dbt(&query, &recno, sizeof(recno)),
140 svn_fs_base__skel_to_dbt(&result, skel, pool), 0);
141 return BDB_WRAP(fs, N_("updating filesystem revision"), db_err);
144 svn_fs_base__trail_debug(trail, "revisions", "put");
145 db_err = bfd->revisions->put(bfd->revisions, trail->db_txn,
146 svn_fs_base__recno_dbt(&key, &recno),
147 svn_fs_base__skel_to_dbt(&value, skel, pool),
149 SVN_ERR(BDB_WRAP(fs, N_("storing filesystem revision"), db_err));
151 /* Turn the record number into a Subversion revision number.
152 Revisions are numbered starting with zero; Berkeley DB record
153 numbers begin with one. */
160 /* Getting the youngest revision. */
164 svn_fs_bdb__youngest_rev(svn_revnum_t *youngest_p,
169 base_fs_data_t *bfd = fs->fsap_data;
175 SVN_ERR(svn_fs__check_fs(fs, TRUE));
177 /* Create a database cursor. */
178 svn_fs_base__trail_debug(trail, "revisions", "cursor");
179 SVN_ERR(BDB_WRAP(fs, N_("getting youngest revision (creating cursor)"),
180 bfd->revisions->cursor(bfd->revisions, trail->db_txn,
183 /* Find the last entry in the `revisions' table. */
184 db_err = svn_bdb_dbc_get(cursor,
185 svn_fs_base__recno_dbt(&key, &recno),
186 svn_fs_base__nodata_dbt(&value),
191 /* Free the cursor. Ignore any error value --- the error above
192 is more interesting. */
193 svn_bdb_dbc_close(cursor);
195 if (db_err == DB_NOTFOUND)
196 /* The revision 0 should always be present, at least. */
199 (SVN_ERR_FS_CORRUPT, 0,
200 "Corrupt DB: revision 0 missing from 'revisions' table, in "
201 "filesystem '%s'", fs->path);
203 SVN_ERR(BDB_WRAP(fs, N_("getting youngest revision (finding last entry)"),
207 /* You can't commit a transaction with open cursors, because:
208 1) key/value pairs don't get deleted until the cursors referring
209 to them are closed, so closing a cursor can fail for various
210 reasons, and txn_commit shouldn't fail that way, and
211 2) using a cursor after committing its transaction can cause
212 undetectable database corruption. */
213 SVN_ERR(BDB_WRAP(fs, N_("getting youngest revision (closing cursor)"),
214 svn_bdb_dbc_close(cursor)));
216 /* Turn the record number into a Subversion revision number.
217 Revisions are numbered starting with zero; Berkeley DB record
218 numbers begin with one. */
219 *youngest_p = recno - 1;