Mark many merge tests as skip-against-old-server.
[svn.git] / subversion / libsvn_fs_base / bdb / copies-table.c
blobbcacb5ae89c82ecae40bbaabc3863bf800807575
1 /* copies-table.c : operations on the `copies' 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 <string.h>
20 #include "bdb_compat.h"
21 #include "../fs.h"
22 #include "../err.h"
23 #include "../key-gen.h"
24 #include "dbt.h"
25 #include "../util/skel.h"
26 #include "../util/fs_skels.h"
27 #include "../trail.h"
28 #include "../../libsvn_fs/fs-loader.h"
29 #include "bdb-err.h"
30 #include "copies-table.h"
31 #include "rev-table.h"
33 #include "svn_private_config.h"
36 int
37 svn_fs_bdb__open_copies_table(DB **copies_p,
38 DB_ENV *env,
39 svn_boolean_t create)
41 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
42 DB *copies;
44 BDB_ERR(svn_fs_bdb__check_version());
45 BDB_ERR(db_create(&copies, env, 0));
46 BDB_ERR((copies->open)(SVN_BDB_OPEN_PARAMS(copies, NULL),
47 "copies", 0, DB_BTREE,
48 open_flags, 0666));
50 /* Create the initial `next-key' table entry. */
51 if (create)
53 DBT key, value;
54 BDB_ERR(copies->put(copies, 0,
55 svn_fs_base__str_to_dbt(&key, NEXT_KEY_KEY),
56 svn_fs_base__str_to_dbt(&value, "0"), 0));
59 *copies_p = copies;
60 return 0;
64 /* Store COPY as a copy named COPY_ID in FS as part of TRAIL. */
65 /* ### only has one caller; might not need to be abstracted */
66 static svn_error_t *
67 put_copy(svn_fs_t *fs,
68 const copy_t *copy,
69 const char *copy_id,
70 trail_t *trail,
71 apr_pool_t *pool)
73 base_fs_data_t *bfd = fs->fsap_data;
74 skel_t *copy_skel;
75 DBT key, value;
77 /* Convert native type to skel. */
78 SVN_ERR(svn_fs_base__unparse_copy_skel(&copy_skel, copy, pool));
80 /* Only in the context of this function do we know that the DB call
81 will not attempt to modify COPY_ID, so the cast belongs here. */
82 svn_fs_base__str_to_dbt(&key, copy_id);
83 svn_fs_base__skel_to_dbt(&value, copy_skel, pool);
84 svn_fs_base__trail_debug(trail, "copies", "put");
85 SVN_ERR(BDB_WRAP(fs, _("storing copy record"),
86 bfd->copies->put(bfd->copies, trail->db_txn,
87 &key, &value, 0)));
89 return SVN_NO_ERROR;
93 svn_error_t *
94 svn_fs_bdb__reserve_copy_id(const char **id_p,
95 svn_fs_t *fs,
96 trail_t *trail,
97 apr_pool_t *pool)
99 base_fs_data_t *bfd = fs->fsap_data;
100 DBT query, result;
101 apr_size_t len;
102 char next_key[MAX_KEY_SIZE];
103 int db_err;
105 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY);
107 /* Get the current value associated with the `next-key' key in the
108 copies table. */
109 svn_fs_base__trail_debug(trail, "copies", "get");
110 SVN_ERR(BDB_WRAP(fs, _("allocating new copy ID (getting 'next-key')"),
111 bfd->copies->get(bfd->copies, trail->db_txn, &query,
112 svn_fs_base__result_dbt(&result),
113 0)));
114 svn_fs_base__track_dbt(&result, pool);
116 /* Set our return value. */
117 *id_p = apr_pstrmemdup(pool, result.data, result.size);
119 /* Bump to future key. */
120 len = result.size;
121 svn_fs_base__next_key(result.data, &len, next_key);
122 svn_fs_base__trail_debug(trail, "copies", "put");
123 db_err = bfd->copies->put(bfd->copies, trail->db_txn,
124 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY),
125 svn_fs_base__str_to_dbt(&result, next_key),
128 SVN_ERR(BDB_WRAP(fs, _("bumping next copy key"), db_err));
129 return SVN_NO_ERROR;
133 svn_error_t *
134 svn_fs_bdb__create_copy(svn_fs_t *fs,
135 const char *copy_id,
136 const char *src_path,
137 const char *src_txn_id,
138 const svn_fs_id_t *dst_noderev_id,
139 copy_kind_t kind,
140 trail_t *trail,
141 apr_pool_t *pool)
143 copy_t copy;
144 copy.kind = kind;
145 copy.src_path = src_path;
146 copy.src_txn_id = src_txn_id;
147 copy.dst_noderev_id = dst_noderev_id;
148 return put_copy(fs, &copy, copy_id, trail, pool);
152 svn_error_t *
153 svn_fs_bdb__delete_copy(svn_fs_t *fs,
154 const char *copy_id,
155 trail_t *trail,
156 apr_pool_t *pool)
158 base_fs_data_t *bfd = fs->fsap_data;
159 DBT key;
160 int db_err;
162 svn_fs_base__str_to_dbt(&key, copy_id);
163 svn_fs_base__trail_debug(trail, "copies", "del");
164 db_err = bfd->copies->del(bfd->copies, trail->db_txn, &key, 0);
165 if (db_err == DB_NOTFOUND)
166 return svn_fs_base__err_no_such_copy(fs, copy_id);
167 return BDB_WRAP(fs, _("deleting entry from 'copies' table"), db_err);
171 svn_error_t *
172 svn_fs_bdb__get_copy(copy_t **copy_p,
173 svn_fs_t *fs,
174 const char *copy_id,
175 trail_t *trail,
176 apr_pool_t *pool)
178 base_fs_data_t *bfd = fs->fsap_data;
179 DBT key, value;
180 int db_err;
181 skel_t *skel;
182 copy_t *copy;
184 /* Only in the context of this function do we know that the DB call
185 will not attempt to modify copy_id, so the cast belongs here. */
186 svn_fs_base__trail_debug(trail, "copies", "get");
187 db_err = bfd->copies->get(bfd->copies, trail->db_txn,
188 svn_fs_base__str_to_dbt(&key, copy_id),
189 svn_fs_base__result_dbt(&value),
191 svn_fs_base__track_dbt(&value, pool);
193 if (db_err == DB_NOTFOUND)
194 return svn_fs_base__err_no_such_copy(fs, copy_id);
195 SVN_ERR(BDB_WRAP(fs, _("reading copy"), db_err));
197 /* Unparse COPY skel */
198 skel = svn_fs_base__parse_skel(value.data, value.size, pool);
199 if (! skel)
200 return svn_fs_base__err_corrupt_copy(fs, copy_id);
202 /* Convert skel to native type. */
203 SVN_ERR(svn_fs_base__parse_copy_skel(&copy, skel, pool));
204 *copy_p = copy;
205 return SVN_NO_ERROR;