1 /* reps-table.c : operations on the `representations' 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 "../util/fs_skels.h"
30 #include "../key-gen.h"
31 #include "../../libsvn_fs/fs-loader.h"
33 #include "reps-table.h"
34 #include "strings-table.h"
37 #include "svn_private_config.h"
40 /*** Creating and opening the representations table. ***/
43 svn_fs_bdb__open_reps_table(DB **reps_p,
47 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
50 BDB_ERR(svn_fs_bdb__check_version());
51 BDB_ERR(db_create(&reps, env, 0));
52 BDB_ERR((reps->open)(SVN_BDB_OPEN_PARAMS(reps, NULL),
53 "representations", 0, DB_BTREE,
56 /* Create the `next-key' table entry. */
63 svn_fs_base__str_to_dbt(&key, NEXT_KEY_KEY),
64 svn_fs_base__str_to_dbt(&value, "0"), 0));
73 /*** Storing and retrieving reps. ***/
76 svn_fs_bdb__read_rep(representation_t **rep_p,
82 base_fs_data_t *bfd = fs->fsap_data;
87 svn_fs_base__trail_debug(trail, "representations", "get");
88 db_err = bfd->representations->get(bfd->representations,
90 svn_fs_base__str_to_dbt(&query, key),
91 svn_fs_base__result_dbt(&result), 0);
92 svn_fs_base__track_dbt(&result, pool);
94 /* If there's no such node, return an appropriately specific error. */
95 if (db_err == DB_NOTFOUND)
96 return svn_error_createf
97 (SVN_ERR_FS_NO_SUCH_REPRESENTATION, 0,
98 _("No such representation '%s'"), key);
100 /* Handle any other error conditions. */
101 SVN_ERR(BDB_WRAP(fs, N_("reading representation"), db_err));
103 /* Parse the REPRESENTATION skel. */
104 skel = svn_skel__parse(result.data, result.size, pool);
106 /* Convert to a native type. */
107 return svn_fs_base__parse_representation_skel(rep_p, skel, pool);
112 svn_fs_bdb__write_rep(svn_fs_t *fs,
114 const representation_t *rep,
118 base_fs_data_t *bfd = fs->fsap_data;
122 /* Convert from native type to skel. */
123 SVN_ERR(svn_fs_base__unparse_representation_skel(&skel, rep,
126 /* Now write the record. */
127 svn_fs_base__trail_debug(trail, "representations", "put");
128 return BDB_WRAP(fs, N_("storing representation"),
129 bfd->representations->put
130 (bfd->representations, trail->db_txn,
131 svn_fs_base__str_to_dbt(&query, key),
132 svn_fs_base__skel_to_dbt(&result, skel, pool),
138 svn_fs_bdb__write_new_rep(const char **key,
140 const representation_t *rep,
144 base_fs_data_t *bfd = fs->fsap_data;
148 char next_key[MAX_KEY_SIZE];
150 /* ### todo: see issue #409 for why bumping the key as part of this
151 trail is problematic. */
153 /* Get the current value associated with `next-key'. */
154 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY);
155 svn_fs_base__trail_debug(trail, "representations", "get");
156 SVN_ERR(BDB_WRAP(fs, N_("allocating new representation (getting next-key)"),
157 bfd->representations->get
158 (bfd->representations, trail->db_txn, &query,
159 svn_fs_base__result_dbt(&result), 0)));
161 svn_fs_base__track_dbt(&result, pool);
163 /* Store the new rep. */
164 *key = apr_pstrmemdup(pool, result.data, result.size);
165 SVN_ERR(svn_fs_bdb__write_rep(fs, *key, rep, trail, pool));
167 /* Bump to future key. */
169 svn_fs_base__next_key(result.data, &len, next_key);
170 svn_fs_base__trail_debug(trail, "representations", "put");
171 db_err = bfd->representations->put
172 (bfd->representations, trail->db_txn,
173 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY),
174 svn_fs_base__str_to_dbt(&result, next_key),
177 return BDB_WRAP(fs, N_("bumping next representation key"), db_err);
182 svn_fs_bdb__delete_rep(svn_fs_t *fs,
187 base_fs_data_t *bfd = fs->fsap_data;
191 svn_fs_base__trail_debug(trail, "representations", "del");
192 db_err = bfd->representations->del
193 (bfd->representations, trail->db_txn,
194 svn_fs_base__str_to_dbt(&query, key), 0);
196 /* If there's no such node, return an appropriately specific error. */
197 if (db_err == DB_NOTFOUND)
198 return svn_error_createf
199 (SVN_ERR_FS_NO_SUCH_REPRESENTATION, 0,
200 _("No such representation '%s'"), key);
202 /* Handle any other error conditions. */
203 return BDB_WRAP(fs, N_("deleting representation"), db_err);