From: Ronnie Sahlberg Date: Tue, 7 Aug 2007 22:20:46 +0000 (+1000) Subject: when inserting data in the tree, if there was already a node with the X-Git-Tag: tevent-0.9.20~348^2~2442^2~14 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=49f0317b214f185b2dbb86975130bf1611eaa97d;p=thirdparty%2Fsamba.git when inserting data in the tree, if there was already a node with the same key then replace the data in the node with the new data and return the pointer to the previous data held in the node. this allows a caller to avoid having to first check if a node already exists before inserting a possibly duplicate/colliding entry and lets the caller do whatever it needs to do after the fact. (This used to be ctdb commit 6634cabb910c26400780d51727ff2d1ba5e16e36) --- diff --git a/ctdb/common/rb_tree.c b/ctdb/common/rb_tree.c index 002d505f17d..57b0a86ab09 100644 --- a/ctdb/common/rb_tree.c +++ b/ctdb/common/rb_tree.c @@ -530,9 +530,10 @@ trbt_create_node(trbt_tree_t *tree, trbt_node_t *parent, uint32_t key, void *dat /* insert a new node in the tree. if there is already a node with a matching key in the tree - we reurn an error + we replace it with the new data and return a pointer to the old data + in case the caller wants to take any special action */ -int +void * trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data) { trbt_node_t *node; @@ -544,16 +545,23 @@ trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data) node = trbt_create_node(tree, NULL, key, data); tree->tree=node; - return 0; + return NULL; } /* it was not the new root so walk the tree until we find where to * insert this new leaf. */ while(1){ - /* this node already exists, so just return an error */ + /* this node already exists, replace data and return the + old data + */ if(key==node->key32){ - return -1; + void *old_data; + + old_data = node->data; + node->data = talloc_steal(node, data); + + return old_data; } if(keykey32) { if(!node->left){ @@ -587,7 +595,7 @@ trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data) /* node will now point to the newly created node */ node->rb_color=TRBT_RED; trbt_insert_case1(tree, node); - return 0; + return NULL; } void * diff --git a/ctdb/common/rb_tree.h b/ctdb/common/rb_tree.h index e53a092bfc3..603ebfad34b 100644 --- a/ctdb/common/rb_tree.h +++ b/ctdb/common/rb_tree.h @@ -37,10 +37,19 @@ typedef struct _trbt_tree_t { - +/* Create a RB tree */ trbt_tree_t *trbt_create(TALLOC_CTX *memctx); + +/* Lookup a node in the tree and return a pointer to data or NULL */ void *trbt_lookup32(trbt_tree_t *tree, uint32_t key); -int trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data); + +/* Insert a new node into the tree. If there was already a node with this + key the pointer to the previous data is returned. + The tree will talloc_steal() the data inserted into the tree . +*/ +void *trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data); + +/* Delete a node from the tree and free all data associated with it */ void trbt_delete32(trbt_tree_t *tree, uint32_t key);