fn btree_insert_internal(btree: &CellRef, key: CellRef, value: CellRef) -> BTreeInsertResult {
    match &**btree {
        // Search b-tree nodes
        SafasCell::BTree(key_values, child_nodes) => {
            for idx in 0..=key_values.len() {
                // Values are ordered by key. We add an extra test for the final value to avoid a double implementation of the match rules
                let ordering = if idx < key_values.len() { (&*key_values[idx].0).partial_cmp(&*key) } else { Some(Ordering::Greater) };

                match ordering {
                    Some(Ordering::Less)    => { },

                    Some(Ordering::Equal)   => {
                        // Found the key: just replace it here
                        let mut new_key_values  = key_values.clone();
                        new_key_values[idx]     = (key, value);

                        return BTreeInsertResult::NewBTree(SafasCell::BTree(new_key_values, child_nodes.clone()).into());
                    },

                    Some(Ordering::Greater) => { 
                        if child_nodes.len() > 0 {
                            // Need to insert in this child node
                            match btree_insert_internal(&child_nodes[idx], key, value) {
                                BTreeInsertResult::NewBTree(new_btree) => {
                                    // Replace the child node with the new one
                                    let mut new_child_nodes = child_nodes.clone();
                                    new_child_nodes[idx] = new_btree;

                                    return BTreeInsertResult::NewBTree(SafasCell::BTree(key_values.clone(), new_child_nodes).into())
                                },

                                BTreeInsertResult::Split(left_btree, right_btree, median) => {
                                    return btree_insert_new_key_at_subtree(key_values, child_nodes, median, left_btree, right_btree);
                                },

                                // Pass errors along
                                BTreeInsertResult::Err(err) => { return BTreeInsertResult::Err(err) }
                            }
                        } else {
                            return btree_insert_new_key_at_leaf(key_values, child_nodes, key, value);
                        }
                    },
                    None                    => { return BTreeInsertResult::Err(RuntimeError::CannotCompare(key_values[idx].0.clone(), key)); }
                }
            }

            // Can't reach here as we'll eventually hit the Ordering::Greater case
            unreachable!()
        },

        // Just replace the nil value with a b-tree with a single value
        SafasCell::Nil => BTreeInsertResult::NewBTree(SafasCell::BTree(vec![(key, value)], vec![]).into()),

        // Other values are not b-trees
        _ => BTreeInsertResult::Err(RuntimeError::NotImplemented)
    }
}
///
/// Implementation of the btree insert operation. This will insert or replace the key with the specified value
///
Implementation of the btree insert operation. This will insert or replace the key with the specified value
Implementation of the btree insert operation . This will insert or replace the key with the specified value
fn btree_insert_internal(btree: &CellRef, key: CellRef, value: CellRef) -> BTreeInsertResult {
    match &**btree {
        // Search b-tree nodes
        SafasCell::BTree(key_values, child_nodes) => {
            for idx in 0..=key_values.len() {
                // Values are ordered by key. We add an extra test for the final value to avoid a double implementation of the match rules
                let ordering = if idx < key_values.len() { (&*key_values[idx].0).partial_cmp(&*key) } else { Some(Ordering::Greater) };
                match ordering {
                    Some(Ordering::Less)    => { },
                    Some(Ordering::Equal)   => {
                        // Found the key: just replace it here
                        let mut new_key_values  = key_values.clone();
                        new_key_values[idx]     = (key, value);
                        return BTreeInsertResult::NewBTree(SafasCell::BTree(new_key_values, child_nodes.clone()).into());
                    },
                    Some(Ordering::Greater) => { 
                        if child_nodes.len() > 0 {
                            // Need to insert in this child node
                            match btree_insert_internal(&child_nodes[idx], key, value) {
                                BTreeInsertResult::NewBTree(new_btree) => {
                                    // Replace the child node with the new one
                                    let mut new_child_nodes = child_nodes.clone();
                                    new_child_nodes[idx] = new_btree;
                                    return BTreeInsertResult::NewBTree(SafasCell::BTree(key_values.clone(), new_child_nodes).into())
                                },
                                BTreeInsertResult::Split(left_btree, right_btree, median) => {
                                    return btree_insert_new_key_at_subtree(key_values, child_nodes, median, left_btree, right_btree);
                                },
                                // Pass errors along
                                BTreeInsertResult::Err(err) => { return BTreeInsertResult::Err(err) }
                            }
                        } else {
                            return btree_insert_new_key_at_leaf(key_values, child_nodes, key, value);
                        }
                    },
                    None                    => { return BTreeInsertResult::Err(RuntimeError::CannotCompare(key_values[idx].0.clone(), key)); }
                }
            }
            // Can't reach here as we'll eventually hit the Ordering::Greater case
            unreachable!()
        },
        // Just replace the nil value with a b-tree with a single value
        SafasCell::Nil => BTreeInsertResult::NewBTree(SafasCell::BTree(vec![(key, value)], vec![]).into()),
        // Other values are not b-trees
        _ => BTreeInsertResult::Err(RuntimeError::NotImplemented)
    }
}
fn btree_insert_internal ( btree : & CellRef , key : CellRef , value : CellRef ) -> BTreeInsertResult { match & * * btree { // Search b-tree nodes SafasCell :: BTree ( key_values , child_nodes ) => { for idx in 0 ..= key_values . len ( ) { // Values are ordered by key. We add an extra test for the final value to avoid a double implementation of the match rules let ordering = if idx < key_values . len ( ) { ( & * key_values [ idx ] . 0 ) . partial_cmp ( & * key ) } else { Some ( Ordering :: Greater ) } ; match ordering { Some ( Ordering :: Less ) => { } , Some ( Ordering :: Equal ) => { // Found the key: just replace it here let mut new_key_values = key_values . clone ( ) ; new_key_values [ idx ] = ( key , value ) ; return BTreeInsertResult :: NewBTree ( SafasCell :: BTree ( new_key_values , child_nodes . clone ( ) ) . into ( ) ) ; } , Some ( Ordering :: Greater ) => { if child_nodes . len ( ) > 0 { // Need to insert in this child node match btree_insert_internal ( & child_nodes [ idx ] , key , value ) { BTreeInsertResult :: NewBTree ( new_btree ) => { // Replace the child node with the new one let mut new_child_nodes = child_nodes . clone ( ) ; new_child_nodes [ idx ] = new_btree ; return BTreeInsertResult :: NewBTree ( SafasCell :: BTree ( key_values . clone ( ) , new_child_nodes ) . into ( ) ) } , BTreeInsertResult :: Split ( left_btree , right_btree , median ) => { return btree_insert_new_key_at_subtree ( key_values , child_nodes , median , left_btree , right_btree ) ; } , // Pass errors along BTreeInsertResult :: Err ( err ) => { return BTreeInsertResult :: Err ( err ) } } } else { return btree_insert_new_key_at_leaf ( key_values , child_nodes , key , value ) ; } } , None => { return BTreeInsertResult :: Err ( RuntimeError :: CannotCompare ( key_values [ idx ] . 0 . clone ( ) , key ) ) ; } } } // Can't reach here as we'll eventually hit the Ordering::Greater case unreachable ! ( ) } , // Just replace the nil value with a b-tree with a single value SafasCell :: Nil => BTreeInsertResult :: NewBTree ( SafasCell :: BTree ( vec ! [ ( key value ) ] , vec ! [ ] ) . into ( ) ) , // Other values are not b-trees _ => BTreeInsertResult :: Err ( RuntimeError :: NotImplemented ) } }
Implementation of the btree insert operation.
Implementation of the btree insert operation .
btree
&CellRef
key
CellRef
value
CellRef
btree
&CellRef
key
CellRef
value
CellRef