Skip to content

Commit 5cb44a8

Browse files
committed
support a art_insert_no_replace API
1 parent 73c4635 commit 5cb44a8

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/art.c

+26-10
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ static int prefix_mismatch(const art_node *n, const unsigned char *key, int key_
546546
return idx;
547547
}
548548

549-
static void* recursive_insert(art_node *n, art_node **ref, const unsigned char *key, int key_len, void *value, int depth, int *old) {
549+
static void* recursive_insert(art_node *n, art_node **ref, const unsigned char *key, int key_len, void *value, int depth, int *old, int replace) {
550550
// If we are at a NULL node, inject a leaf
551551
if (!n) {
552552
*ref = (art_node*)SET_LEAF(make_leaf(key, key_len, value));
@@ -561,7 +561,7 @@ static void* recursive_insert(art_node *n, art_node **ref, const unsigned char *
561561
if (!leaf_matches(l, key, key_len, depth)) {
562562
*old = 1;
563563
void *old_val = l->value;
564-
l->value = value;
564+
if(replace) l->value = value;
565565
return old_val;
566566
}
567567

@@ -622,7 +622,7 @@ RECURSE_SEARCH:;
622622
// Find a child to recurse to
623623
art_node **child = find_child(n, key[depth]);
624624
if (child) {
625-
return recursive_insert(*child, child, key, key_len, value, depth+1, old);
625+
return recursive_insert(*child, child, key, key_len, value, depth+1, old, replace);
626626
}
627627

628628
// No child, node goes within us
@@ -632,17 +632,33 @@ RECURSE_SEARCH:;
632632
}
633633

634634
/**
635-
* Inserts a new value into the ART tree
636-
* @arg t The tree
637-
* @arg key The key
638-
* @arg key_len The length of the key
639-
* @arg value Opaque value.
640-
* @return NULL if the item was newly inserted, otherwise
635+
* inserts a new value into the art tree
636+
* @arg t the tree
637+
* @arg key the key
638+
* @arg key_len the length of the key
639+
* @arg value opaque value.
640+
* @return null if the item was newly inserted, otherwise
641641
* the old value pointer is returned.
642642
*/
643643
void* art_insert(art_tree *t, const unsigned char *key, int key_len, void *value) {
644644
int old_val = 0;
645-
void *old = recursive_insert(t->root, &t->root, key, key_len, value, 0, &old_val);
645+
void *old = recursive_insert(t->root, &t->root, key, key_len, value, 0, &old_val, 1);
646+
if (!old_val) t->size++;
647+
return old;
648+
}
649+
650+
/**
651+
* inserts a new value into the art tree (no replace)
652+
* @arg t the tree
653+
* @arg key the key
654+
* @arg key_len the length of the key
655+
* @arg value opaque value.
656+
* @return null if the item was newly inserted, otherwise
657+
* the old value pointer is returned.
658+
*/
659+
void* art_insert_no_replace(art_tree *t, const unsigned char *key, int key_len, void *value) {
660+
int old_val = 0;
661+
void *old = recursive_insert(t->root, &t->root, key, key_len, value, 0, &old_val, 0);
646662
if (!old_val) t->size++;
647663
return old;
648664
}

src/art.h

+17-6
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,27 @@ inline uint64_t art_size(art_tree *t) {
129129
#endif
130130

131131
/**
132-
* Inserts a new value into the ART tree
133-
* @arg t The tree
134-
* @arg key The key
135-
* @arg key_len The length of the key
136-
* @arg value Opaque value.
137-
* @return NULL if the item was newly inserted, otherwise
132+
* inserts a new value into the art tree
133+
* @arg t the tree
134+
* @arg key the key
135+
* @arg key_len the length of the key
136+
* @arg value opaque value.
137+
* @return null if the item was newly inserted, otherwise
138138
* the old value pointer is returned.
139139
*/
140140
void* art_insert(art_tree *t, const unsigned char *key, int key_len, void *value);
141141

142+
/**
143+
* inserts a new value into the art tree (not replacing)
144+
* @arg t the tree
145+
* @arg key the key
146+
* @arg key_len the length of the key
147+
* @arg value opaque value.
148+
* @return null if the item was newly inserted, otherwise
149+
* the old value pointer is returned.
150+
*/
151+
void* art_insert_no_replace(art_tree *t, const unsigned char *key, int key_len, void *value);
152+
142153
/**
143154
* Deletes a value from the ART tree
144155
* @arg t The tree

0 commit comments

Comments
 (0)