1 /* reps-table.c : operations on the `representations' 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 "bdb_compat.h"
21 #include "../util/fs_skels.h"
25 #include "../key-gen.h"
26 #include "../../libsvn_fs/fs-loader.h"
28 #include "reps-table.h"
29 #include "strings-table.h"
32 #include "svn_private_config.h"
35 /*** Creating and opening the representations table. ***/
38 svn_fs_bdb__open_reps_table(DB
**reps_p
,
42 const u_int32_t open_flags
= (create
? (DB_CREATE
| DB_EXCL
) : 0);
45 BDB_ERR(svn_fs_bdb__check_version());
46 BDB_ERR(db_create(&reps
, env
, 0));
47 BDB_ERR((reps
->open
)(SVN_BDB_OPEN_PARAMS(reps
, NULL
),
48 "representations", 0, DB_BTREE
,
51 /* Create the `next-key' table entry. */
58 svn_fs_base__str_to_dbt(&key
, NEXT_KEY_KEY
),
59 svn_fs_base__str_to_dbt(&value
, "0"), 0));
68 /*** Storing and retrieving reps. ***/
71 svn_fs_bdb__read_rep(representation_t
**rep_p
,
77 base_fs_data_t
*bfd
= fs
->fsap_data
;
82 svn_fs_base__trail_debug(trail
, "representations", "get");
83 db_err
= bfd
->representations
->get(bfd
->representations
,
85 svn_fs_base__str_to_dbt(&query
, key
),
86 svn_fs_base__result_dbt(&result
), 0);
87 svn_fs_base__track_dbt(&result
, pool
);
89 /* If there's no such node, return an appropriately specific error. */
90 if (db_err
== DB_NOTFOUND
)
91 return svn_error_createf
92 (SVN_ERR_FS_NO_SUCH_REPRESENTATION
, 0,
93 _("No such representation '%s'"), key
);
95 /* Handle any other error conditions. */
96 SVN_ERR(BDB_WRAP(fs
, _("reading representation"), db_err
));
98 /* Parse the REPRESENTATION skel. */
99 skel
= svn_fs_base__parse_skel(result
.data
, result
.size
, pool
);
101 /* Convert to a native type. */
102 SVN_ERR(svn_fs_base__parse_representation_skel(rep_p
, skel
, pool
));
109 svn_fs_bdb__write_rep(svn_fs_t
*fs
,
111 const representation_t
*rep
,
115 base_fs_data_t
*bfd
= fs
->fsap_data
;
119 /* Convert from native type to skel. */
120 SVN_ERR(svn_fs_base__unparse_representation_skel(&skel
, rep
, pool
));
122 /* Now write the record. */
123 svn_fs_base__trail_debug(trail
, "representations", "put");
124 SVN_ERR(BDB_WRAP(fs
, _("storing representation"),
125 bfd
->representations
->put
126 (bfd
->representations
, trail
->db_txn
,
127 svn_fs_base__str_to_dbt(&query
, key
),
128 svn_fs_base__skel_to_dbt(&result
, skel
, pool
),
136 svn_fs_bdb__write_new_rep(const char **key
,
138 const representation_t
*rep
,
142 base_fs_data_t
*bfd
= fs
->fsap_data
;
146 char next_key
[MAX_KEY_SIZE
];
148 /* ### todo: see issue #409 for why bumping the key as part of this
149 trail is problematic. */
151 /* Get the current value associated with `next-key'. */
152 svn_fs_base__str_to_dbt(&query
, NEXT_KEY_KEY
);
153 svn_fs_base__trail_debug(trail
, "representations", "get");
154 SVN_ERR(BDB_WRAP(fs
, _("allocating new representation (getting next-key)"),
155 bfd
->representations
->get
156 (bfd
->representations
, trail
->db_txn
, &query
,
157 svn_fs_base__result_dbt(&result
), 0)));
159 svn_fs_base__track_dbt(&result
, pool
);
161 /* Store the new rep. */
162 *key
= apr_pstrmemdup(pool
, result
.data
, result
.size
);
163 SVN_ERR(svn_fs_bdb__write_rep(fs
, *key
, rep
, trail
, pool
));
165 /* Bump to future key. */
167 svn_fs_base__next_key(result
.data
, &len
, next_key
);
168 svn_fs_base__trail_debug(trail
, "representations", "put");
169 db_err
= bfd
->representations
->put
170 (bfd
->representations
, trail
->db_txn
,
171 svn_fs_base__str_to_dbt(&query
, NEXT_KEY_KEY
),
172 svn_fs_base__str_to_dbt(&result
, next_key
),
175 SVN_ERR(BDB_WRAP(fs
, _("bumping next representation key"), db_err
));
182 svn_fs_bdb__delete_rep(svn_fs_t
*fs
,
187 base_fs_data_t
*bfd
= fs
->fsap_data
;
191 svn_fs_base__trail_debug(trail
, "representations", "del");
192 db_err
= bfd
->representations
->del
193 (bfd
->representations
, trail
->db_txn
,
194 svn_fs_base__str_to_dbt(&query
, key
), 0);
196 /* If there's no such node, return an appropriately specific error. */
197 if (db_err
== DB_NOTFOUND
)
198 return svn_error_createf
199 (SVN_ERR_FS_NO_SUCH_REPRESENTATION
, 0,
200 _("No such representation '%s'"), key
);
202 /* Handle any other error conditions. */
203 SVN_ERR(BDB_WRAP(fs
, _("deleting representation"), db_err
));