Mark many merge tests as skip-against-old-server.
[svn.git] / subversion / libsvn_fs_base / bdb / reps-table.c
blob7b7fb1c94c128784e2aee9ae8cddda8040cdae87
1 /* reps-table.c : operations on the `representations' table
3 * ====================================================================
4 * Copyright (c) 2000-2004 CollabNet. All rights reserved.
6 * This software is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at http://subversion.tigris.org/license-1.html.
9 * If newer versions of this license are posted there, you may use a
10 * newer version instead, at your option.
12 * This software consists of voluntary contributions made by many
13 * individuals. For exact contribution history, see the revision
14 * history and logs, available at http://subversion.tigris.org/.
15 * ====================================================================
18 #include "bdb_compat.h"
19 #include "svn_fs.h"
20 #include "../fs.h"
21 #include "../util/fs_skels.h"
22 #include "../err.h"
23 #include "dbt.h"
24 #include "../trail.h"
25 #include "../key-gen.h"
26 #include "../../libsvn_fs/fs-loader.h"
27 #include "bdb-err.h"
28 #include "reps-table.h"
29 #include "strings-table.h"
32 #include "svn_private_config.h"
35 /*** Creating and opening the representations table. ***/
37 int
38 svn_fs_bdb__open_reps_table(DB **reps_p,
39 DB_ENV *env,
40 svn_boolean_t create)
42 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
43 DB *reps;
45 BDB_ERR(svn_fs_bdb__check_version());
46 BDB_ERR(db_create(&reps, env, 0));
47 BDB_ERR((reps->open)(SVN_BDB_OPEN_PARAMS(reps, NULL),
48 "representations", 0, DB_BTREE,
49 open_flags, 0666));
51 /* Create the `next-key' table entry. */
52 if (create)
54 DBT key, value;
56 BDB_ERR(reps->put
57 (reps, 0,
58 svn_fs_base__str_to_dbt(&key, NEXT_KEY_KEY),
59 svn_fs_base__str_to_dbt(&value, "0"), 0));
62 *reps_p = reps;
63 return 0;
68 /*** Storing and retrieving reps. ***/
70 svn_error_t *
71 svn_fs_bdb__read_rep(representation_t **rep_p,
72 svn_fs_t *fs,
73 const char *key,
74 trail_t *trail,
75 apr_pool_t *pool)
77 base_fs_data_t *bfd = fs->fsap_data;
78 skel_t *skel;
79 int db_err;
80 DBT query, result;
82 svn_fs_base__trail_debug(trail, "representations", "get");
83 db_err = bfd->representations->get(bfd->representations,
84 trail->db_txn,
85 svn_fs_base__str_to_dbt(&query, key),
86 svn_fs_base__result_dbt(&result), 0);
87 svn_fs_base__track_dbt(&result, pool);
89 /* If there's no such node, return an appropriately specific error. */
90 if (db_err == DB_NOTFOUND)
91 return svn_error_createf
92 (SVN_ERR_FS_NO_SUCH_REPRESENTATION, 0,
93 _("No such representation '%s'"), key);
95 /* Handle any other error conditions. */
96 SVN_ERR(BDB_WRAP(fs, _("reading representation"), db_err));
98 /* Parse the REPRESENTATION skel. */
99 skel = svn_fs_base__parse_skel(result.data, result.size, pool);
101 /* Convert to a native type. */
102 SVN_ERR(svn_fs_base__parse_representation_skel(rep_p, skel, pool));
104 return SVN_NO_ERROR;
108 svn_error_t *
109 svn_fs_bdb__write_rep(svn_fs_t *fs,
110 const char *key,
111 const representation_t *rep,
112 trail_t *trail,
113 apr_pool_t *pool)
115 base_fs_data_t *bfd = fs->fsap_data;
116 DBT query, result;
117 skel_t *skel;
119 /* Convert from native type to skel. */
120 SVN_ERR(svn_fs_base__unparse_representation_skel(&skel, rep, pool));
122 /* Now write the record. */
123 svn_fs_base__trail_debug(trail, "representations", "put");
124 SVN_ERR(BDB_WRAP(fs, _("storing representation"),
125 bfd->representations->put
126 (bfd->representations, trail->db_txn,
127 svn_fs_base__str_to_dbt(&query, key),
128 svn_fs_base__skel_to_dbt(&result, skel, pool),
129 0)));
131 return SVN_NO_ERROR;
135 svn_error_t *
136 svn_fs_bdb__write_new_rep(const char **key,
137 svn_fs_t *fs,
138 const representation_t *rep,
139 trail_t *trail,
140 apr_pool_t *pool)
142 base_fs_data_t *bfd = fs->fsap_data;
143 DBT query, result;
144 int db_err;
145 apr_size_t len;
146 char next_key[MAX_KEY_SIZE];
148 /* ### todo: see issue #409 for why bumping the key as part of this
149 trail is problematic. */
151 /* Get the current value associated with `next-key'. */
152 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY);
153 svn_fs_base__trail_debug(trail, "representations", "get");
154 SVN_ERR(BDB_WRAP(fs, _("allocating new representation (getting next-key)"),
155 bfd->representations->get
156 (bfd->representations, trail->db_txn, &query,
157 svn_fs_base__result_dbt(&result), 0)));
159 svn_fs_base__track_dbt(&result, pool);
161 /* Store the new rep. */
162 *key = apr_pstrmemdup(pool, result.data, result.size);
163 SVN_ERR(svn_fs_bdb__write_rep(fs, *key, rep, trail, pool));
165 /* Bump to future key. */
166 len = result.size;
167 svn_fs_base__next_key(result.data, &len, next_key);
168 svn_fs_base__trail_debug(trail, "representations", "put");
169 db_err = bfd->representations->put
170 (bfd->representations, trail->db_txn,
171 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY),
172 svn_fs_base__str_to_dbt(&result, next_key),
175 SVN_ERR(BDB_WRAP(fs, _("bumping next representation key"), db_err));
177 return SVN_NO_ERROR;
181 svn_error_t *
182 svn_fs_bdb__delete_rep(svn_fs_t *fs,
183 const char *key,
184 trail_t *trail,
185 apr_pool_t *pool)
187 base_fs_data_t *bfd = fs->fsap_data;
188 int db_err;
189 DBT query;
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 SVN_ERR(BDB_WRAP(fs, _("deleting representation"), db_err));
205 return SVN_NO_ERROR;