2 Unix SMB/CIFS implementation.
3 Database interface wrapper around red-black trees
4 Copyright (C) Volker Lendecke 2007, 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "dbwrap/dbwrap.h"
22 #include "dbwrap/dbwrap_private.h"
23 #include "dbwrap/dbwrap_rbt.h"
24 #include "../lib/util/rbtree.h"
26 #define DBWRAP_RBT_ALIGN(_size_) (((_size_)+15)&~15)
33 struct db_rbt_node *node;
36 /* The structure that ends up in the tree */
39 struct rb_node rb_node;
40 size_t keysize, valuesize;
43 * key and value are appended implicitly, "data" is only here as a
44 * target for offsetof()
51 * Hide the ugly pointer calculations in a function
54 static struct db_rbt_node *db_rbt2node(struct rb_node *node)
56 return (struct db_rbt_node *)
57 ((char *)node - offsetof(struct db_rbt_node, rb_node));
64 static int db_rbt_compare(TDB_DATA a, TDB_DATA b)
68 res = memcmp(a.dptr, b.dptr, MIN(a.dsize, b.dsize));
70 if ((res < 0) || ((res == 0) && (a.dsize < b.dsize))) {
73 if ((res > 0) || ((res == 0) && (a.dsize > b.dsize))) {
80 * dissect a db_rbt_node into its implicit key and value parts
83 static void db_rbt_parse_node(struct db_rbt_node *node,
84 TDB_DATA *key, TDB_DATA *value)
86 key->dptr = ((uint8_t *)node) + offsetof(struct db_rbt_node, data);
87 key->dsize = node->keysize;
88 value->dptr = key->dptr + node->keysize;
89 value->dsize = node->valuesize;
92 static NTSTATUS db_rbt_store(struct db_record *rec, TDB_DATA data, int flag)
94 struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
95 rec->db->private_data, struct db_rbt_ctx);
96 struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
97 struct db_rbt_node *node;
100 struct rb_node * parent;
102 TDB_DATA this_key, this_val;
104 if (rec_priv->node != NULL) {
107 * The record was around previously
110 db_rbt_parse_node(rec_priv->node, &this_key, &this_val);
112 SMB_ASSERT(this_key.dsize == rec->key.dsize);
113 SMB_ASSERT(memcmp(this_key.dptr, rec->key.dptr,
114 this_key.dsize) == 0);
116 if (this_val.dsize >= data.dsize) {
118 * The new value fits into the old space
120 memcpy(this_val.dptr, data.dptr, data.dsize);
121 rec_priv->node->valuesize = data.dsize;
126 node = (struct db_rbt_node *)talloc_size(db_ctx,
127 offsetof(struct db_rbt_node, data) + rec->key.dsize
131 return NT_STATUS_NO_MEMORY;
134 if (rec_priv->node != NULL) {
136 * We need to delete the key from the tree and start fresh,
137 * there's not enough space in the existing record
140 rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
143 * Keep the existing node around for a while: If the record
144 * existed before, we reference the key data in there.
148 ZERO_STRUCT(node->rb_node);
150 node->keysize = rec->key.dsize;
151 node->valuesize = data.dsize;
153 db_rbt_parse_node(node, &this_key, &this_val);
155 memcpy(this_key.dptr, rec->key.dptr, node->keysize);
156 TALLOC_FREE(rec_priv->node);
157 rec_priv->node = node;
159 memcpy(this_val.dptr, data.dptr, node->valuesize);
162 p = &db_ctx->tree.rb_node;
165 struct db_rbt_node *r;
166 TDB_DATA search_key, search_val;
173 db_rbt_parse_node(r, &search_key, &search_val);
175 res = db_rbt_compare(this_key, search_key);
184 smb_panic("someone messed with the tree");
188 rb_link_node(&node->rb_node, parent, p);
189 rb_insert_color(&node->rb_node, &db_ctx->tree);
194 static NTSTATUS db_rbt_delete(struct db_record *rec)
196 struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
197 rec->db->private_data, struct db_rbt_ctx);
198 struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
200 if (rec_priv->node == NULL) {
204 rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
205 TALLOC_FREE(rec_priv->node);
209 struct db_rbt_search_result {
212 struct db_rbt_node* node;
215 static bool db_rbt_search_internal(struct db_context *db, TDB_DATA key,
216 struct db_rbt_search_result *result)
218 struct db_rbt_ctx *ctx = talloc_get_type_abort(
219 db->private_data, struct db_rbt_ctx);
223 struct db_rbt_node *r = NULL;
224 TDB_DATA search_key, search_val;
226 n = ctx->tree.rb_node;
233 db_rbt_parse_node(r, &search_key, &search_val);
235 res = db_rbt_compare(key, search_key);
248 if (result != NULL) {
250 result->key = search_key;
251 result->val = search_val;
254 ZERO_STRUCT(*result);
260 static struct db_record *db_rbt_fetch_locked(struct db_context *db_ctx,
264 struct db_rbt_rec *rec_priv;
265 struct db_record *result;
268 struct db_rbt_search_result res;
270 found = db_rbt_search_internal(db_ctx, key, &res);
273 * In this low-level routine, play tricks to reduce the number of
274 * tallocs to one. Not recommened for general use, but here it pays
278 size = DBWRAP_RBT_ALIGN(sizeof(struct db_record))
279 + sizeof(struct db_rbt_rec);
283 * We need to keep the key around for later store
288 result = (struct db_record *)talloc_size(mem_ctx, size);
289 if (result == NULL) {
293 rec_priv = (struct db_rbt_rec *)
294 ((char *)result + DBWRAP_RBT_ALIGN(sizeof(struct db_record)));
296 result->store = db_rbt_store;
297 result->delete_rec = db_rbt_delete;
298 result->private_data = rec_priv;
300 rec_priv->node = res.node;
301 result->value = res.val;
304 result->key = res.key;
307 result->key.dptr = (uint8_t *)
308 ((char *)rec_priv + sizeof(*rec_priv));
309 result->key.dsize = key.dsize;
310 memcpy(result->key.dptr, key.dptr, key.dsize);
316 static int db_rbt_exists(struct db_context *db, TDB_DATA key)
318 return db_rbt_search_internal(db, key, NULL);
321 static int db_rbt_wipe(struct db_context *db)
323 struct db_rbt_ctx *old_ctx = talloc_get_type_abort(
324 db->private_data, struct db_rbt_ctx);
325 struct db_rbt_ctx *new_ctx = talloc_zero(db, struct db_rbt_ctx);
326 if (new_ctx == NULL) {
329 db->private_data = new_ctx;
330 talloc_free(old_ctx);
334 static NTSTATUS db_rbt_parse_record(struct db_context *db, TDB_DATA key,
335 void (*parser)(TDB_DATA key, TDB_DATA data,
339 struct db_rbt_search_result res;
340 bool found = db_rbt_search_internal(db, key, &res);
343 return NT_STATUS_NOT_FOUND;
345 parser(res.key, res.val, private_data);
349 static int db_rbt_traverse_internal(struct rb_node *n,
350 int (*f)(struct db_record *db,
352 void *private_data, uint32_t* count)
354 struct db_rbt_node *r;
355 struct db_record rec;
362 ret = db_rbt_traverse_internal(n->rb_left, f, private_data, count);
369 db_rbt_parse_node(r, &rec.key, &rec.value);
371 ret = f(&rec, private_data);
377 return db_rbt_traverse_internal(n->rb_right, f, private_data, count);
380 static int db_rbt_traverse(struct db_context *db,
381 int (*f)(struct db_record *db,
385 struct db_rbt_ctx *ctx = talloc_get_type_abort(
386 db->private_data, struct db_rbt_ctx);
389 int ret = db_rbt_traverse_internal(ctx->tree.rb_node, f, private_data, &count);
393 if (count > INT_MAX) {
399 static int db_rbt_get_seqnum(struct db_context *db)
404 static int db_rbt_trans_dummy(struct db_context *db)
407 * Transactions are pretty pointless in-memory, just return success.
412 static void db_rbt_id(struct db_context *db, const uint8_t **id, size_t *idlen)
415 *idlen = sizeof(struct db_context *);
418 struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx)
420 struct db_context *result;
422 result = talloc(mem_ctx, struct db_context);
424 if (result == NULL) {
428 result->private_data = talloc_zero(result, struct db_rbt_ctx);
430 if (result->private_data == NULL) {
435 result->fetch_locked = db_rbt_fetch_locked;
436 result->try_fetch_locked = NULL;
437 result->traverse = db_rbt_traverse;
438 result->traverse_read = db_rbt_traverse;
439 result->get_seqnum = db_rbt_get_seqnum;
440 result->transaction_start = db_rbt_trans_dummy;
441 result->transaction_commit = db_rbt_trans_dummy;
442 result->transaction_cancel = db_rbt_trans_dummy;
443 result->exists = db_rbt_exists;
444 result->wipe = db_rbt_wipe;
445 result->parse_record = db_rbt_parse_record;
446 result->lock_order = 0;
447 result->id = db_rbt_id;
448 result->stored_callback = NULL;