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"
21 #include <apr_tables.h>
24 #include "svn_pools.h"
30 #include "../util/fs_skels.h"
31 #include "../../libsvn_fs/fs-loader.h"
34 #include "changes-table.h"
37 #include "svn_private_config.h"
40 /*** Creating and opening the changes table. ***/
43 svn_fs_bdb__open_changes_table(DB
**changes_p
,
47 const u_int32_t open_flags
= (create
? (DB_CREATE
| DB_EXCL
) : 0);
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
,
67 /*** Storing and retrieving changes. ***/
70 svn_fs_bdb__changes_add(svn_fs_t
*fs
,
76 base_fs_data_t
*bfd
= fs
->fsap_data
;
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
,
96 svn_fs_bdb__changes_delete(svn_fs_t
*fs
,
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
));
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. */
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
;
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. */
140 /* Sanity check: only allow NULL node revision ID in the
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
150 if (change
->noderev_id
151 && (! svn_fs_base__id_eq(old_change
->node_rev_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
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
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
;
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
,
201 old_change
->text_mod
= change
->text_mod
;
202 old_change
->prop_mod
= change
->prop_mod
;
205 case svn_fs_path_change_modify
:
207 if (change
->text_mod
)
208 old_change
->text_mod
= TRUE
;
209 if (change
->prop_mod
)
210 old_change
->prop_mod
= TRUE
;
214 /* Point our new_change to our (possibly modified) old_change. */
215 new_change
= old_change
;
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
,
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
);
239 svn_fs_bdb__changes_fetch(apr_hash_t
**changes_p
,
245 base_fs_data_t
*bfd
= fs
->fsap_data
;
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
,
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
);
265 svn_fs_base__track_dbt(&result
, pool
);
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
,
281 err
= svn_error_createf(SVN_ERR_FS_CORRUPT
, NULL
,
282 _("Error reading changes for key '%s'"),
286 err
= svn_fs_base__parse_change_skel(&change
, result_skel
, subpool
);
290 /* ... and merge it with our return hash. */
291 err
= fold_change(changes
, change
);
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. */
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)
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
);
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
);
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. */
351 /* If our only error thus far was when we closed the cursor, return
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
;
363 svn_fs_bdb__changes_fetch_raw(apr_array_header_t
**changes_p
,
369 base_fs_data_t
*bfd
= fs
->fsap_data
;
372 int db_err
= 0, db_c_err
= 0;
373 svn_error_t
*err
= SVN_NO_ERROR
;
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
,
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
);
389 svn_fs_base__track_dbt(&result
, pool
);
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
);
400 err
= svn_error_createf(SVN_ERR_FS_CORRUPT
, NULL
,
401 _("Error reading changes for key '%s'"),
405 err
= svn_fs_base__parse_change_skel(&change
, result_skel
, pool
);
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
);
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
);
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. */
434 /* If our only error thus far was when we closed the cursor, return
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
;