1 /* changes-table.c : operations on the `changes' 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 <apr_tables.h>
30 #include "svn_pools.h"
36 #include "../util/fs_skels.h"
37 #include "../../libsvn_fs/fs-loader.h"
40 #include "changes-table.h"
42 #include "private/svn_fs_util.h"
43 #include "private/svn_fspath.h"
44 #include "svn_private_config.h"
47 /*** Creating and opening the changes table. ***/
50 svn_fs_bdb__open_changes_table(DB **changes_p,
54 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
57 BDB_ERR(svn_fs_bdb__check_version());
58 BDB_ERR(db_create(&changes, env, 0));
60 /* Enable duplicate keys. This allows us to store the changes
61 one-per-row. Note: this must occur before ->open(). */
62 BDB_ERR(changes->set_flags(changes, DB_DUP));
64 BDB_ERR((changes->open)(SVN_BDB_OPEN_PARAMS(changes, NULL),
65 "changes", 0, DB_BTREE,
74 /*** Storing and retrieving changes. ***/
77 svn_fs_bdb__changes_add(svn_fs_t *fs,
83 base_fs_data_t *bfd = fs->fsap_data;
87 /* Convert native type to skel. */
88 SVN_ERR(svn_fs_base__unparse_change_skel(&skel, change, pool));
90 /* Store a new record into the database. */
91 svn_fs_base__str_to_dbt(&query, key);
92 svn_fs_base__skel_to_dbt(&value, skel, pool);
93 svn_fs_base__trail_debug(trail, "changes", "put");
94 return BDB_WRAP(fs, N_("creating change"),
95 bfd->changes->put(bfd->changes, trail->db_txn,
101 svn_fs_bdb__changes_delete(svn_fs_t *fs,
108 base_fs_data_t *bfd = fs->fsap_data;
110 svn_fs_base__trail_debug(trail, "changes", "del");
111 db_err = bfd->changes->del(bfd->changes, trail->db_txn,
112 svn_fs_base__str_to_dbt(&query, key), 0);
114 /* If there're no changes for KEY, that is acceptable. Any other
115 error should be propagated to the caller, though. */
116 if ((db_err) && (db_err != DB_NOTFOUND))
118 SVN_ERR(BDB_WRAP(fs, N_("deleting changes"), db_err));
124 /* Return a deep FS API type copy of SOURCE in internal format and allocate
125 * the result in RESULT_POOL.
127 static svn_fs_path_change2_t *
128 change_to_fs_change(const change_t *change,
129 apr_pool_t *result_pool)
131 svn_fs_path_change2_t *result = svn_fs__path_change_create_internal(
132 svn_fs_base__id_copy(change->noderev_id,
136 result->text_mod = change->text_mod;
137 result->prop_mod = change->prop_mod;
138 result->node_kind = svn_node_unknown;
139 result->copyfrom_known = FALSE;
144 /* Merge the internal-use-only CHANGE into a hash of public-FS
145 svn_fs_path_change2_t CHANGES, collapsing multiple changes into a
146 single succinct change per path. */
148 fold_change(apr_hash_t *changes,
149 apr_hash_t *deletions,
150 const change_t *change)
152 apr_pool_t *pool = apr_hash_pool_get(changes);
153 svn_fs_path_change2_t *old_change, *new_change;
156 if ((old_change = svn_hash_gets(changes, change->path)))
158 /* This path already exists in the hash, so we have to merge
159 this change into the already existing one. */
161 /* Since the path already exists in the hash, we don't have to
162 dup the allocation for the path itself. */
165 /* Sanity check: only allow NULL node revision ID in the
167 if ((! change->noderev_id) && (change->kind != svn_fs_path_change_reset))
168 return svn_error_create
169 (SVN_ERR_FS_CORRUPT, NULL,
170 _("Missing required node revision ID"));
172 /* Sanity check: we should be talking about the same node
173 revision ID as our last change except where the last change
175 if (change->noderev_id
176 && (! svn_fs_base__id_eq(old_change->node_rev_id,
178 && (old_change->change_kind != svn_fs_path_change_delete))
179 return svn_error_create
180 (SVN_ERR_FS_CORRUPT, NULL,
181 _("Invalid change ordering: new node revision ID without delete"));
183 /* Sanity check: an add, replacement, or reset must be the first
184 thing to follow a deletion. */
185 if ((old_change->change_kind == svn_fs_path_change_delete)
186 && (! ((change->kind == svn_fs_path_change_replace)
187 || (change->kind == svn_fs_path_change_reset)
188 || (change->kind == svn_fs_path_change_add))))
189 return svn_error_create
190 (SVN_ERR_FS_CORRUPT, NULL,
191 _("Invalid change ordering: non-add change on deleted path"));
193 /* Sanity check: an add can't follow anything except
194 a delete or reset. */
195 if ((change->kind == svn_fs_path_change_add)
196 && (old_change->change_kind != svn_fs_path_change_delete)
197 && (old_change->change_kind != svn_fs_path_change_reset))
198 return svn_error_create
199 (SVN_ERR_FS_CORRUPT, NULL,
200 _("Invalid change ordering: add change on preexisting path"));
202 /* Now, merge that change in. */
203 switch (change->kind)
205 case svn_fs_path_change_reset:
206 /* A reset here will simply remove the path change from the
211 case svn_fs_path_change_delete:
212 if (old_change->change_kind == svn_fs_path_change_add)
214 /* If the path was introduced in this transaction via an
215 add, and we are deleting it, just remove the path
219 else if (old_change->change_kind == svn_fs_path_change_replace)
221 /* A deleting a 'replace' restore the original deletion. */
222 new_change = svn_hash_gets(deletions, path);
223 SVN_ERR_ASSERT(new_change);
227 /* A deletion overrules all previous changes. */
228 new_change = old_change;
229 new_change->change_kind = svn_fs_path_change_delete;
230 new_change->text_mod = change->text_mod;
231 new_change->prop_mod = change->prop_mod;
235 case svn_fs_path_change_add:
236 case svn_fs_path_change_replace:
237 /* An add at this point must be following a previous delete,
238 so treat it just like a replace. */
240 new_change = change_to_fs_change(change, pool);
241 new_change->change_kind = svn_fs_path_change_replace;
243 /* Remember the original deletion.
244 * Make sure to allocate the hash key in a durable pool. */
245 svn_hash_sets(deletions,
246 apr_pstrdup(apr_hash_pool_get(deletions), path),
250 case svn_fs_path_change_modify:
252 new_change = old_change;
253 if (change->text_mod)
254 new_change->text_mod = TRUE;
255 if (change->prop_mod)
256 new_change->prop_mod = TRUE;
262 /* This change is new to the hash, so make a new public change
263 structure from the internal one (in the hash's pool), and dup
264 the path into the hash's pool, too. */
265 new_change = change_to_fs_change(change, pool);
266 path = apr_pstrdup(pool, change->path);
269 /* Add (or update) this path. */
270 svn_hash_sets(changes, path, new_change);
277 svn_fs_bdb__changes_fetch(apr_hash_t **changes_p,
283 base_fs_data_t *bfd = fs->fsap_data;
286 int db_err = 0, db_c_err = 0;
287 svn_error_t *err = SVN_NO_ERROR;
288 apr_hash_t *changes = apr_hash_make(pool);
289 apr_pool_t *subpool = svn_pool_create(pool);
290 apr_pool_t *iterpool = svn_pool_create(pool);
291 apr_hash_t *deletions = apr_hash_make(subpool);
293 /* Get a cursor on the first record matching KEY, and then loop over
294 the records, adding them to the return array. */
295 svn_fs_base__trail_debug(trail, "changes", "cursor");
296 SVN_ERR(BDB_WRAP(fs, N_("creating cursor for reading changes"),
297 bfd->changes->cursor(bfd->changes, trail->db_txn,
300 /* Advance the cursor to the key that we're looking for. */
301 svn_fs_base__str_to_dbt(&query, key);
302 svn_fs_base__result_dbt(&result);
303 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_SET);
305 svn_fs_base__track_dbt(&result, pool);
310 svn_skel_t *result_skel;
312 /* Clear the per-iteration subpool. */
313 svn_pool_clear(iterpool);
315 /* RESULT now contains a change record associated with KEY. We
316 need to parse that skel into an change_t structure ... */
317 result_skel = svn_skel__parse(result.data, result.size, iterpool);
320 err = svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
321 _("Error reading changes for key '%s'"),
325 err = svn_fs_base__parse_change_skel(&change, result_skel, iterpool);
329 /* ... and merge it with our return hash. */
330 err = fold_change(changes, deletions, change);
334 /* Now, if our change was a deletion or replacement, we have to
335 blow away any changes thus far on paths that are (or, were)
336 children of this path.
337 ### i won't bother with another iteration pool here -- at
338 most we talking about a few extra dups of paths into what
339 is already a temporary subpool.
341 if ((change->kind == svn_fs_path_change_delete)
342 || (change->kind == svn_fs_path_change_replace))
344 apr_hash_index_t *hi;
346 for (hi = apr_hash_first(iterpool, changes);
348 hi = apr_hash_next(hi))
350 /* KEY is the path. */
353 const char *child_relpath;
355 apr_hash_this(hi, &hashkey, &klen, NULL);
357 /* If we come across our own path, ignore it.
358 If we come across a child of our path, remove it. */
359 child_relpath = svn_fspath__skip_ancestor(change->path, hashkey);
360 if (child_relpath && *child_relpath)
361 apr_hash_set(changes, hashkey, klen, NULL);
365 /* Advance the cursor to the next record with this same KEY, and
366 fetch that record. */
367 svn_fs_base__result_dbt(&result);
368 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_NEXT_DUP);
370 svn_fs_base__track_dbt(&result, pool);
373 /* Destroy the per-iteration subpool. */
374 svn_pool_destroy(iterpool);
375 svn_pool_destroy(subpool);
377 /* If there are no (more) change records for this KEY, we're
378 finished. Just return the (possibly empty) array. Any other
379 error, however, needs to get handled appropriately. */
380 if (db_err && (db_err != DB_NOTFOUND))
381 err = BDB_WRAP(fs, N_("fetching changes"), db_err);
384 /* Close the cursor. */
385 db_c_err = svn_bdb_dbc_close(cursor);
387 /* If we had an error prior to closing the cursor, return the error. */
389 return svn_error_trace(err);
391 /* If our only error thus far was when we closed the cursor, return
394 SVN_ERR(BDB_WRAP(fs, N_("closing changes cursor"), db_c_err));
396 /* Finally, set our return variable and get outta here. */
397 *changes_p = changes;
403 svn_fs_bdb__changes_fetch_raw(apr_array_header_t **changes_p,
409 base_fs_data_t *bfd = fs->fsap_data;
412 int db_err = 0, db_c_err = 0;
413 svn_error_t *err = SVN_NO_ERROR;
415 apr_array_header_t *changes = apr_array_make(pool, 4, sizeof(change));
417 /* Get a cursor on the first record matching KEY, and then loop over
418 the records, adding them to the return array. */
419 svn_fs_base__trail_debug(trail, "changes", "cursor");
420 SVN_ERR(BDB_WRAP(fs, N_("creating cursor for reading changes"),
421 bfd->changes->cursor(bfd->changes, trail->db_txn,
424 /* Advance the cursor to the key that we're looking for. */
425 svn_fs_base__str_to_dbt(&query, key);
426 svn_fs_base__result_dbt(&result);
427 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_SET);
429 svn_fs_base__track_dbt(&result, pool);
433 svn_skel_t *result_skel;
435 /* RESULT now contains a change record associated with KEY. We
436 need to parse that skel into an change_t structure ... */
437 result_skel = svn_skel__parse(result.data, result.size, pool);
440 err = svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
441 _("Error reading changes for key '%s'"),
445 err = svn_fs_base__parse_change_skel(&change, result_skel, pool);
449 /* ... and add it to our return array. */
450 APR_ARRAY_PUSH(changes, change_t *) = change;
452 /* Advance the cursor to the next record with this same KEY, and
453 fetch that record. */
454 svn_fs_base__result_dbt(&result);
455 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_NEXT_DUP);
457 svn_fs_base__track_dbt(&result, pool);
460 /* If there are no (more) change records for this KEY, we're
461 finished. Just return the (possibly empty) array. Any other
462 error, however, needs to get handled appropriately. */
463 if (db_err && (db_err != DB_NOTFOUND))
464 err = BDB_WRAP(fs, N_("fetching changes"), db_err);
467 /* Close the cursor. */
468 db_c_err = svn_bdb_dbc_close(cursor);
470 /* If we had an error prior to closing the cursor, return the error. */
472 return svn_error_trace(err);
474 /* If our only error thus far was when we closed the cursor, return
477 SVN_ERR(BDB_WRAP(fs, N_("closing changes cursor"), db_c_err));
479 /* Finally, set our return variable and get outta here. */
480 *changes_p = changes;