1 /* copies-table.c : operations on the `copies' 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 * ====================================================================
25 #include "bdb_compat.h"
27 #include "private/svn_skel.h"
31 #include "../key-gen.h"
33 #include "../util/fs_skels.h"
35 #include "../../libsvn_fs/fs-loader.h"
37 #include "copies-table.h"
38 #include "rev-table.h"
40 #include "svn_private_config.h"
44 svn_fs_bdb__open_copies_table(DB **copies_p,
48 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
51 BDB_ERR(svn_fs_bdb__check_version());
52 BDB_ERR(db_create(&copies, env, 0));
53 BDB_ERR((copies->open)(SVN_BDB_OPEN_PARAMS(copies, NULL),
54 "copies", 0, DB_BTREE,
57 /* Create the initial `next-key' table entry. */
61 BDB_ERR(copies->put(copies, 0,
62 svn_fs_base__str_to_dbt(&key, NEXT_KEY_KEY),
63 svn_fs_base__str_to_dbt(&value, "0"), 0));
71 /* Store COPY as a copy named COPY_ID in FS as part of TRAIL. */
72 /* ### only has one caller; might not need to be abstracted */
74 put_copy(svn_fs_t *fs,
80 base_fs_data_t *bfd = fs->fsap_data;
81 svn_skel_t *copy_skel;
84 /* Convert native type to skel. */
85 SVN_ERR(svn_fs_base__unparse_copy_skel(©_skel, copy, pool));
87 /* Only in the context of this function do we know that the DB call
88 will not attempt to modify COPY_ID, so the cast belongs here. */
89 svn_fs_base__str_to_dbt(&key, copy_id);
90 svn_fs_base__skel_to_dbt(&value, copy_skel, pool);
91 svn_fs_base__trail_debug(trail, "copies", "put");
92 return BDB_WRAP(fs, N_("storing copy record"),
93 bfd->copies->put(bfd->copies, trail->db_txn,
99 svn_fs_bdb__reserve_copy_id(const char **id_p,
104 base_fs_data_t *bfd = fs->fsap_data;
107 char next_key[MAX_KEY_SIZE];
110 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY);
112 /* Get the current value associated with the `next-key' key in the
114 svn_fs_base__trail_debug(trail, "copies", "get");
115 SVN_ERR(BDB_WRAP(fs, N_("allocating new copy ID (getting 'next-key')"),
116 bfd->copies->get(bfd->copies, trail->db_txn, &query,
117 svn_fs_base__result_dbt(&result),
119 svn_fs_base__track_dbt(&result, pool);
121 /* Set our return value. */
122 *id_p = apr_pstrmemdup(pool, result.data, result.size);
124 /* Bump to future key. */
126 svn_fs_base__next_key(result.data, &len, next_key);
127 svn_fs_base__trail_debug(trail, "copies", "put");
128 db_err = bfd->copies->put(bfd->copies, trail->db_txn,
129 svn_fs_base__str_to_dbt(&query, NEXT_KEY_KEY),
130 svn_fs_base__str_to_dbt(&result, next_key),
133 return BDB_WRAP(fs, N_("bumping next copy key"), db_err);
138 svn_fs_bdb__create_copy(svn_fs_t *fs,
140 const char *src_path,
141 const char *src_txn_id,
142 const svn_fs_id_t *dst_noderev_id,
149 copy.src_path = src_path;
150 copy.src_txn_id = src_txn_id;
151 copy.dst_noderev_id = dst_noderev_id;
152 return put_copy(fs, ©, copy_id, trail, pool);
157 svn_fs_bdb__delete_copy(svn_fs_t *fs,
162 base_fs_data_t *bfd = fs->fsap_data;
166 svn_fs_base__str_to_dbt(&key, copy_id);
167 svn_fs_base__trail_debug(trail, "copies", "del");
168 db_err = bfd->copies->del(bfd->copies, trail->db_txn, &key, 0);
169 if (db_err == DB_NOTFOUND)
170 return svn_fs_base__err_no_such_copy(fs, copy_id);
171 return BDB_WRAP(fs, N_("deleting entry from 'copies' table"), db_err);
176 svn_fs_bdb__get_copy(copy_t **copy_p,
182 base_fs_data_t *bfd = fs->fsap_data;
188 /* Only in the context of this function do we know that the DB call
189 will not attempt to modify copy_id, so the cast belongs here. */
190 svn_fs_base__trail_debug(trail, "copies", "get");
191 db_err = bfd->copies->get(bfd->copies, trail->db_txn,
192 svn_fs_base__str_to_dbt(&key, copy_id),
193 svn_fs_base__result_dbt(&value),
195 svn_fs_base__track_dbt(&value, pool);
197 if (db_err == DB_NOTFOUND)
198 return svn_fs_base__err_no_such_copy(fs, copy_id);
199 SVN_ERR(BDB_WRAP(fs, N_("reading copy"), db_err));
201 /* Unparse COPY skel */
202 skel = svn_skel__parse(value.data, value.size, pool);
204 return svn_fs_base__err_corrupt_copy(fs, copy_id);
206 /* Convert skel to native type. */
207 SVN_ERR(svn_fs_base__parse_copy_skel(©, skel, pool));