Fix compiler warning due to missing function prototype.
[svn.git] / subversion / libsvn_fs_base / bdb / changes-table.c
blob1380a5e96f3412e79863438816f71742e7d0c3da
1 /* changes-table.c : operations on the `changes' table
3 * ====================================================================
4 * Copyright (c) 2000-2007 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"
20 #include <apr_hash.h>
21 #include <apr_tables.h>
23 #include "svn_fs.h"
24 #include "svn_pools.h"
25 #include "svn_path.h"
26 #include "../fs.h"
27 #include "../err.h"
28 #include "../trail.h"
29 #include "../id.h"
30 #include "../util/fs_skels.h"
31 #include "../../libsvn_fs/fs-loader.h"
32 #include "bdb-err.h"
33 #include "dbt.h"
34 #include "changes-table.h"
37 #include "svn_private_config.h"
40 /*** Creating and opening the changes table. ***/
42 int
43 svn_fs_bdb__open_changes_table(DB **changes_p,
44 DB_ENV *env,
45 svn_boolean_t create)
47 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
48 DB *changes;
50 BDB_ERR(svn_fs_bdb__check_version());
51 BDB_ERR(db_create(&changes, env, 0));
53 /* Enable duplicate keys. This allows us to store the changes
54 one-per-row. Note: this must occur before ->open(). */
55 BDB_ERR(changes->set_flags(changes, DB_DUP));
57 BDB_ERR((changes->open)(SVN_BDB_OPEN_PARAMS(changes, NULL),
58 "changes", 0, DB_BTREE,
59 open_flags, 0666));
61 *changes_p = changes;
62 return 0;
67 /*** Storing and retrieving changes. ***/
69 svn_error_t *
70 svn_fs_bdb__changes_add(svn_fs_t *fs,
71 const char *key,
72 change_t *change,
73 trail_t *trail,
74 apr_pool_t *pool)
76 base_fs_data_t *bfd = fs->fsap_data;
77 DBT query, value;
78 skel_t *skel;
80 /* Convert native type to skel. */
81 SVN_ERR(svn_fs_base__unparse_change_skel(&skel, change, pool));
83 /* Store a new record into the database. */
84 svn_fs_base__str_to_dbt(&query, key);
85 svn_fs_base__skel_to_dbt(&value, skel, pool);
86 svn_fs_base__trail_debug(trail, "changes", "put");
87 SVN_ERR(BDB_WRAP(fs, _("creating change"),
88 bfd->changes->put(bfd->changes, trail->db_txn,
89 &query, &value, 0)));
91 return SVN_NO_ERROR;
95 svn_error_t *
96 svn_fs_bdb__changes_delete(svn_fs_t *fs,
97 const char *key,
98 trail_t *trail,
99 apr_pool_t *pool)
101 int db_err;
102 DBT query;
103 base_fs_data_t *bfd = fs->fsap_data;
105 svn_fs_base__trail_debug(trail, "changes", "del");
106 db_err = bfd->changes->del(bfd->changes, trail->db_txn,
107 svn_fs_base__str_to_dbt(&query, key), 0);
109 /* If there're no changes for KEY, that is acceptable. Any other
110 error should be propogated to the caller, though. */
111 if ((db_err) && (db_err != DB_NOTFOUND))
113 SVN_ERR(BDB_WRAP(fs, _("deleting changes"), db_err));
116 return SVN_NO_ERROR;
120 /* Merge the internal-use-only CHANGE into a hash of public-FS
121 svn_fs_path_change_t CHANGES, collapsing multiple changes into a
122 single succinct change per path. */
123 static svn_error_t *
124 fold_change(apr_hash_t *changes,
125 const change_t *change)
127 apr_pool_t *pool = apr_hash_pool_get(changes);
128 svn_fs_path_change_t *old_change, *new_change;
129 const char *path;
131 if ((old_change = apr_hash_get(changes, change->path, APR_HASH_KEY_STRING)))
133 /* This path already exists in the hash, so we have to merge
134 this change into the already existing one. */
136 /* Since the path already exists in the hash, we don't have to
137 dup the allocation for the path itself. */
138 path = change->path;
140 /* Sanity check: only allow NULL node revision ID in the
141 `reset' case. */
142 if ((! change->noderev_id) && (change->kind != svn_fs_path_change_reset))
143 return svn_error_create
144 (SVN_ERR_FS_CORRUPT, NULL,
145 _("Missing required node revision ID"));
147 /* Sanity check: we should be talking about the same node
148 revision ID as our last change except where the last change
149 was a deletion. */
150 if (change->noderev_id
151 && (! svn_fs_base__id_eq(old_change->node_rev_id,
152 change->noderev_id))
153 && (old_change->change_kind != svn_fs_path_change_delete))
154 return svn_error_create
155 (SVN_ERR_FS_CORRUPT, NULL,
156 _("Invalid change ordering: new node revision ID without delete"));
158 /* Sanity check: an add, replacement, or reset must be the first
159 thing to follow a deletion. */
160 if ((old_change->change_kind == svn_fs_path_change_delete)
161 && (! ((change->kind == svn_fs_path_change_replace)
162 || (change->kind == svn_fs_path_change_reset)
163 || (change->kind == svn_fs_path_change_add))))
164 return svn_error_create
165 (SVN_ERR_FS_CORRUPT, NULL,
166 _("Invalid change ordering: non-add change on deleted path"));
168 /* Now, merge that change in. */
169 switch (change->kind)
171 case svn_fs_path_change_reset:
172 /* A reset here will simply remove the path change from the
173 hash. */
174 old_change = NULL;
175 break;
177 case svn_fs_path_change_delete:
178 if (old_change->change_kind == svn_fs_path_change_add)
180 /* If the path was introduced in this transaction via an
181 add, and we are deleting it, just remove the path
182 altogether. */
183 old_change = NULL;
185 else
187 /* A deletion overrules all previous changes. */
188 old_change->change_kind = svn_fs_path_change_delete;
189 old_change->text_mod = change->text_mod;
190 old_change->prop_mod = change->prop_mod;
192 break;
194 case svn_fs_path_change_add:
195 case svn_fs_path_change_replace:
196 /* An add at this point must be following a previous delete,
197 so treat it just like a replace. */
198 old_change->change_kind = svn_fs_path_change_replace;
199 old_change->node_rev_id = svn_fs_base__id_copy(change->noderev_id,
200 pool);
201 old_change->text_mod = change->text_mod;
202 old_change->prop_mod = change->prop_mod;
203 break;
205 case svn_fs_path_change_modify:
206 default:
207 if (change->text_mod)
208 old_change->text_mod = TRUE;
209 if (change->prop_mod)
210 old_change->prop_mod = TRUE;
211 break;
214 /* Point our new_change to our (possibly modified) old_change. */
215 new_change = old_change;
217 else
219 /* This change is new to the hash, so make a new public change
220 structure from the internal one (in the hash's pool), and dup
221 the path into the hash's pool, too. */
222 new_change = apr_pcalloc(pool, sizeof(*new_change));
223 new_change->node_rev_id = svn_fs_base__id_copy(change->noderev_id,
224 pool);
225 new_change->change_kind = change->kind;
226 new_change->text_mod = change->text_mod;
227 new_change->prop_mod = change->prop_mod;
228 path = apr_pstrdup(pool, change->path);
231 /* Add (or update) this path. */
232 apr_hash_set(changes, path, APR_HASH_KEY_STRING, new_change);
234 return SVN_NO_ERROR;
238 svn_error_t *
239 svn_fs_bdb__changes_fetch(apr_hash_t **changes_p,
240 svn_fs_t *fs,
241 const char *key,
242 trail_t *trail,
243 apr_pool_t *pool)
245 base_fs_data_t *bfd = fs->fsap_data;
246 DBC *cursor;
247 DBT query, result;
248 int db_err = 0, db_c_err = 0;
249 svn_error_t *err = SVN_NO_ERROR;
250 apr_hash_t *changes = apr_hash_make(pool);
251 apr_pool_t *subpool = svn_pool_create(pool);
253 /* Get a cursor on the first record matching KEY, and then loop over
254 the records, adding them to the return array. */
255 svn_fs_base__trail_debug(trail, "changes", "cursor");
256 SVN_ERR(BDB_WRAP(fs, _("creating cursor for reading changes"),
257 bfd->changes->cursor(bfd->changes, trail->db_txn,
258 &cursor, 0)));
260 /* Advance the cursor to the key that we're looking for. */
261 svn_fs_base__str_to_dbt(&query, key);
262 svn_fs_base__result_dbt(&result);
263 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_SET);
264 if (! db_err)
265 svn_fs_base__track_dbt(&result, pool);
267 while (! db_err)
269 change_t *change;
270 skel_t *result_skel;
272 /* Clear the per-iteration subpool. */
273 svn_pool_clear(subpool);
275 /* RESULT now contains a change record associated with KEY. We
276 need to parse that skel into an change_t structure ... */
277 result_skel = svn_fs_base__parse_skel(result.data, result.size,
278 subpool);
279 if (! result_skel)
281 err = svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
282 _("Error reading changes for key '%s'"),
283 key);
284 goto cleanup;
286 err = svn_fs_base__parse_change_skel(&change, result_skel, subpool);
287 if (err)
288 goto cleanup;
290 /* ... and merge it with our return hash. */
291 err = fold_change(changes, change);
292 if (err)
293 goto cleanup;
295 /* Now, if our change was a deletion or replacement, we have to
296 blow away any changes thus far on paths that are (or, were)
297 children of this path.
298 ### i won't bother with another iteration pool here -- at
299 most we talking about a few extra dups of paths into what
300 is already a temporary subpool.
302 if ((change->kind == svn_fs_path_change_delete)
303 || (change->kind == svn_fs_path_change_replace))
305 apr_hash_index_t *hi;
307 for (hi = apr_hash_first(subpool, changes);
309 hi = apr_hash_next(hi))
311 /* KEY is the path. */
312 const void *hashkey;
313 apr_ssize_t klen;
314 apr_hash_this(hi, &hashkey, &klen, NULL);
316 /* If we come across our own path, ignore it. */
317 if (strcmp(change->path, hashkey) == 0)
318 continue;
320 /* If we come across a child of our path, remove it. */
321 if (svn_path_is_child(change->path, hashkey, subpool))
322 apr_hash_set(changes, hashkey, klen, NULL);
326 /* Advance the cursor to the next record with this same KEY, and
327 fetch that record. */
328 svn_fs_base__result_dbt(&result);
329 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_NEXT_DUP);
330 if (! db_err)
331 svn_fs_base__track_dbt(&result, pool);
334 /* Destroy the per-iteration subpool. */
335 svn_pool_destroy(subpool);
337 /* If there are no (more) change records for this KEY, we're
338 finished. Just return the (possibly empty) array. Any other
339 error, however, needs to get handled appropriately. */
340 if (db_err && (db_err != DB_NOTFOUND))
341 err = BDB_WRAP(fs, _("fetching changes"), db_err);
343 cleanup:
344 /* Close the cursor. */
345 db_c_err = svn_bdb_dbc_close(cursor);
347 /* If we had an error prior to closing the cursor, return the error. */
348 if (err)
349 return err;
351 /* If our only error thus far was when we closed the cursor, return
352 that error. */
353 if (db_c_err)
354 SVN_ERR(BDB_WRAP(fs, _("closing changes cursor"), db_c_err));
356 /* Finally, set our return variable and get outta here. */
357 *changes_p = changes;
358 return SVN_NO_ERROR;
362 svn_error_t *
363 svn_fs_bdb__changes_fetch_raw(apr_array_header_t **changes_p,
364 svn_fs_t *fs,
365 const char *key,
366 trail_t *trail,
367 apr_pool_t *pool)
369 base_fs_data_t *bfd = fs->fsap_data;
370 DBC *cursor;
371 DBT query, result;
372 int db_err = 0, db_c_err = 0;
373 svn_error_t *err = SVN_NO_ERROR;
374 change_t *change;
375 apr_array_header_t *changes = apr_array_make(pool, 4, sizeof(change));
377 /* Get a cursor on the first record matching KEY, and then loop over
378 the records, adding them to the return array. */
379 svn_fs_base__trail_debug(trail, "changes", "cursor");
380 SVN_ERR(BDB_WRAP(fs, _("creating cursor for reading changes"),
381 bfd->changes->cursor(bfd->changes, trail->db_txn,
382 &cursor, 0)));
384 /* Advance the cursor to the key that we're looking for. */
385 svn_fs_base__str_to_dbt(&query, key);
386 svn_fs_base__result_dbt(&result);
387 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_SET);
388 if (! db_err)
389 svn_fs_base__track_dbt(&result, pool);
391 while (! db_err)
393 skel_t *result_skel;
395 /* RESULT now contains a change record associated with KEY. We
396 need to parse that skel into an change_t structure ... */
397 result_skel = svn_fs_base__parse_skel(result.data, result.size, pool);
398 if (! result_skel)
400 err = svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
401 _("Error reading changes for key '%s'"),
402 key);
403 goto cleanup;
405 err = svn_fs_base__parse_change_skel(&change, result_skel, pool);
406 if (err)
407 goto cleanup;
409 /* ... and add it to our return array. */
410 APR_ARRAY_PUSH(changes, change_t *) = change;
412 /* Advance the cursor to the next record with this same KEY, and
413 fetch that record. */
414 svn_fs_base__result_dbt(&result);
415 db_err = svn_bdb_dbc_get(cursor, &query, &result, DB_NEXT_DUP);
416 if (! db_err)
417 svn_fs_base__track_dbt(&result, pool);
420 /* If there are no (more) change records for this KEY, we're
421 finished. Just return the (possibly empty) array. Any other
422 error, however, needs to get handled appropriately. */
423 if (db_err && (db_err != DB_NOTFOUND))
424 err = BDB_WRAP(fs, _("fetching changes"), db_err);
426 cleanup:
427 /* Close the cursor. */
428 db_c_err = svn_bdb_dbc_close(cursor);
430 /* If we had an error prior to closing the cursor, return the error. */
431 if (err)
432 return err;
434 /* If our only error thus far was when we closed the cursor, return
435 that error. */
436 if (db_c_err)
437 SVN_ERR(BDB_WRAP(fs, _("closing changes cursor"), db_c_err));
439 /* Finally, set our return variable and get outta here. */
440 *changes_p = changes;
441 return SVN_NO_ERROR;