@@ -546,7 +546,7 @@ static int prefix_mismatch(const art_node *n, const unsigned char *key, int key_
546
546
return idx ;
547
547
}
548
548
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 ) {
550
550
// If we are at a NULL node, inject a leaf
551
551
if (!n ) {
552
552
* 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 *
561
561
if (!leaf_matches (l , key , key_len , depth )) {
562
562
* old = 1 ;
563
563
void * old_val = l -> value ;
564
- l -> value = value ;
564
+ if ( replace ) l -> value = value ;
565
565
return old_val ;
566
566
}
567
567
@@ -622,7 +622,7 @@ RECURSE_SEARCH:;
622
622
// Find a child to recurse to
623
623
art_node * * child = find_child (n , key [depth ]);
624
624
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 );
626
626
}
627
627
628
628
// No child, node goes within us
@@ -632,17 +632,33 @@ RECURSE_SEARCH:;
632
632
}
633
633
634
634
/**
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
641
641
* the old value pointer is returned.
642
642
*/
643
643
void * art_insert (art_tree * t , const unsigned char * key , int key_len , void * value ) {
644
644
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 );
646
662
if (!old_val ) t -> size ++ ;
647
663
return old ;
648
664
}
0 commit comments