pub fn merge<K: AsRef<[u8]>>(
        &self,
        key: K,
        value: Value,
    ) -> Result<(), ()> {
        trace!("merging key {:?}", key.as_ref());
        let _measure = Measure::new(&M.tree_merge);

        if self.context.read_only {
            return Err(Error::Unsupported(
                "the database is in read-only mode".to_owned(),
            ));
        }

        let guard = pin();

        let val_ivec = IVec::from(&*value);

        loop {
            let mut path = self.path_for_key(key.as_ref(), &guard)?;
            let (leaf_frag, leaf_ptr) = path.pop().expect(
                "path_for_key should always return a path \
                 of length >= 2 (root + leaf)",
            );
            let node: &Node = leaf_frag.unwrap_base();

            let mut subscriber_reservation =
                self.subscriptions.reserve(&key);

            let encoded_key = prefix_encode(&node.lo, key.as_ref());
            let frag = Frag::Merge(encoded_key, val_ivec.clone());

            let link = self.context.pagecache.link(
                node.id,
                leaf_ptr.clone(),
                frag.clone(),
                &guard,
            );
            match link {
                Ok(new_cas_key) => {
                    // success
                    if let Some(res) = subscriber_reservation.take() {
                        let event = subscription::Event::Merge(
                            key.as_ref().to_vec(),
                            value.clone(),
                        );

                        res.complete(event);
                    }
                    if node.should_split(
                        self.context.blink_node_split_size as u64,
                    ) {
                        let mut path2 = path
                            .iter()
                            .map(|&(f, ref p)| {
                                (Cow::Borrowed(f), p.clone())
                            })
                            .collect::<Vec<(Cow<'_, Frag>, _)>>();
                        let mut node2 = node.clone();
                        node2.apply(
                            &frag,
                            self.context.merge_operator,
                        );
                        let frag2 = Cow::Owned(Frag::Base(node2));
                        path2.push((frag2, new_cas_key));
                        self.recursive_split(path2, &guard)?;
                    }
                    guard.flush();
                    return Ok(());
                }
                Err(Error::CasFailed(_)) => {}
                Err(other) => {
                    guard.flush();
                    return Err(other.danger_cast());
                }
            }
            M.tree_looped();
        }
    }
/// Merge state directly into a given key's value using the
/// configured merge operator. This allows state to be written
/// into a value directly, without any read-modify-write steps.
/// Merge operators can be used to implement arbitrary data
/// structures.
///
/// # Panics
///
/// Calling `merge` will panic if no merge operator has been
/// configured.
///
/// # Examples
///
/// ```
/// fn concatenate_merge(
///   _key: &[u8],               // the key being merged
///   old_value: Option<&[u8]>,  // the previous value, if one existed
///   merged_bytes: &[u8]        // the new bytes being merged in
/// ) -> Option<Vec<u8>> {       // set the new value, return None to delete
///   let mut ret = old_value
///     .map(|ov| ov.to_vec())
///     .unwrap_or_else(|| vec![]);
///
///   ret.extend_from_slice(merged_bytes);
///
///   Some(ret)
/// }
///
/// let config = sled::ConfigBuilder::new()
///   .temporary(true)
///   .merge_operator(concatenate_merge)
///   .build();
///
/// let tree = sled::Db::start(config).unwrap();
///
/// let k = b"k1";
///
/// tree.set(k, vec![0]);
/// tree.merge(k, vec![1]);
/// tree.merge(k, vec![2]);
/// // assert_eq!(tree.get(k).unwrap().unwrap(), vec![0, 1, 2]);
///
/// // sets replace previously merged data,
/// // bypassing the merge function.
/// tree.set(k, vec![3]);
/// // assert_eq!(tree.get(k), Ok(Some(vec![3])));
///
/// // merges on non-present values will add them
/// tree.del(k);
/// tree.merge(k, vec![4]);
/// // assert_eq!(tree.get(k).unwrap().unwrap(), vec![4]);
/// ```
Merge state directly into a given key's value using the
configured merge operator. This allows state to be written
into a value directly, without any read-modify-write steps.
Merge operators can be used to implement arbitrary data
structures.
Merge state directly into a given key ' s value using the configured merge operator . This allows state to be written into a value directly without any read - modify - write steps . Merge operators can be used to implement arbitrary data structures .
pub fn merge<K: AsRef<[u8]>>(
        &self,
        key: K,
        value: Value,
    ) -> Result<(), ()> {
        trace!("merging key {:?}", key.as_ref());
        let _measure = Measure::new(&M.tree_merge);
        if self.context.read_only {
            return Err(Error::Unsupported(
                "the database is in read-only mode".to_owned(),
            ));
        }
        let guard = pin();
        let val_ivec = IVec::from(&*value);
        loop {
            let mut path = self.path_for_key(key.as_ref(), &guard)?;
            let (leaf_frag, leaf_ptr) = path.pop().expect(
                "path_for_key should always return a path \
                 of length >= 2 (root + leaf)",
            );
            let node: &Node = leaf_frag.unwrap_base();
            let mut subscriber_reservation =
                self.subscriptions.reserve(&key);
            let encoded_key = prefix_encode(&node.lo, key.as_ref());
            let frag = Frag::Merge(encoded_key, val_ivec.clone());
            let link = self.context.pagecache.link(
                node.id,
                leaf_ptr.clone(),
                frag.clone(),
                &guard,
            );
            match link {
                Ok(new_cas_key) => {
                    // success
                    if let Some(res) = subscriber_reservation.take() {
                        let event = subscription::Event::Merge(
                            key.as_ref().to_vec(),
                            value.clone(),
                        );
                        res.complete(event);
                    }
                    if node.should_split(
                        self.context.blink_node_split_size as u64,
                    ) {
                        let mut path2 = path
                            .iter()
                            .map(|&(f, ref p)| {
                                (Cow::Borrowed(f), p.clone())
                            })
                            .collect::<Vec<(Cow<'_, Frag>, _)>>();
                        let mut node2 = node.clone();
                        node2.apply(
                            &frag,
                            self.context.merge_operator,
                        );
                        let frag2 = Cow::Owned(Frag::Base(node2));
                        path2.push((frag2, new_cas_key));
                        self.recursive_split(path2, &guard)?;
                    }
                    guard.flush();
                    return Ok(());
                }
                Err(Error::CasFailed(_)) => {}
                Err(other) => {
                    guard.flush();
                    return Err(other.danger_cast());
                }
            }
            M.tree_looped();
        }
    }
pub fn merge < K : AsRef < [ u8 ] > > ( & self , key : K , value : Value , ) -> Result < ( ) , ( ) > { trace ! ( " " key as_ref ( ) ) ; let _measure = Measure :: new ( & M . tree_merge ) ; if self . context . read_only { return Err ( Error :: Unsupported ( " " . to_owned ( ) , ) ) ; } let guard = pin ( ) ; let val_ivec = IVec :: from ( & * value ) ; loop { let mut path = self . path_for_key ( key . as_ref ( ) , & guard ) ? ; let ( leaf_frag , leaf_ptr ) = path . pop ( ) . expect ( " \
 " , ) ; let node : & Node = leaf_frag . unwrap_base ( ) ; let mut subscriber_reservation = self . subscriptions . reserve ( & key ) ; let encoded_key = prefix_encode ( & node . lo , key . as_ref ( ) ) ; let frag = Frag :: Merge ( encoded_key , val_ivec . clone ( ) ) ; let link = self . context . pagecache . link ( node . id , leaf_ptr . clone ( ) , frag . clone ( ) , & guard , ) ; match link { Ok ( new_cas_key ) => { // success if let Some ( res ) = subscriber_reservation . take ( ) { let event = subscription :: Event :: Merge ( key . as_ref ( ) . to_vec ( ) , value . clone ( ) , ) ; res . complete ( event ) ; } if node . should_split ( self . context . blink_node_split_size as u64 , ) { let mut path2 = path . iter ( ) . map ( | & ( f , ref p ) | { ( Cow :: Borrowed ( f ) , p . clone ( ) ) } ) . collect :: < Vec < ( Cow < ' _ , Frag > , _ ) > > ( ) ; let mut node2 = node . clone ( ) ; node2 . apply ( & frag , self . context . merge_operator , ) ; let frag2 = Cow :: Owned ( Frag :: Base ( node2 ) ) ; path2 . push ( ( frag2 , new_cas_key ) ) ; self . recursive_split ( path2 , & guard ) ? ; } guard . flush ( ) ; return Ok ( ( ) ) ; } Err ( Error :: CasFailed ( _ ) ) => { } Err ( other ) => { guard . flush ( ) ; return Err ( other . danger_cast ( ) ) ; } } M . tree_looped ( ) ; } }
Merge state directly into a given key's value using the
configured merge operator.
Merge state directly into a given key ' s value using the configured merge operator .
&self
key
K
value
Value
&self
key
K
value
Value
panics
Calling `merge` will panic if no merge operator has been
configured.
Calling ` merge ` will panic if no merge operator has been configured .
examples
```
fn concatenate_merge(
_key: &[u8],               // the key being merged
old_value: Option<&[u8]>,  // the previous value, if one existed
merged_bytes: &[u8]        // the new bytes being merged in
) -> Option> {       // set the new value, return None to delete
let mut ret = old_value
.map(|ov| ov.to_vec())
.unwrap_or_else(|| vec![]).

ret.extend_from_slice(merged_bytes).

Some(ret)
}

let config = sled::ConfigBuilder::new()
.temporary(true)
.merge_operator(concatenate_merge)
.build().

let tree = sled::Db::start(config).unwrap().





sets replace previously merged data,
bypassing the merge function.

merges on non-present values will add them
tree.del(k);
tree.merge(k, vec![4]);
assert_eq!(tree.get(k).unwrap().unwrap(), vec![4]);
```
` ` ` fn concatenate_merge ( _key : & [ u8 ] // the key being merged old_value : Option<& [ u8 ] > // the previous value if one existed merged_bytes : & [ u8 ] // the new bytes being merged in ) - > Option > { // set the new value return None to delete let mut ret = old_value . map ( |ov| ov . to_vec () ) . unwrap_or_else ( || vec! [] ) . ret . extend_from_slice ( merged_bytes ) . Some ( ret ) } let config = sled :: ConfigBuilder :: new () . temporary ( true ) . merge_operator ( concatenate_merge ) . build () . let tree = sled :: Db :: start ( config ) . unwrap () . sets replace previously merged data bypassing the merge function . merges on non - present values will add them tree . del ( k ) ; tree . merge ( k vec! [ 4 ] ) ; assert_eq! ( tree . get ( k ) . unwrap () . unwrap () vec! [ 4 ] ) ; ` ` `