1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use anyhow::Result;

use crate::storage::mkvs::{
    cache::Cache,
    tree::{
        Depth, Key, KeyTrait, NodeBox, NodeKind, NodePointer, NodePtrRef, NodeRef, Tree, Value,
    },
};

use super::lookup::FetcherSyncGet;

impl Tree {
    /// Remove entry with given key, returning the value at the key if the key was previously
    /// in the database.
    pub fn remove(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        let boxed_key = key.to_vec();
        let pending_root = self.cache.borrow().get_pending_root();

        // Remember where the path from root to target node ends (will end).
        self.cache.borrow_mut().mark_position();

        let (new_root, _, old_val) = self._remove(pending_root, 0, &boxed_key)?;
        self.cache.borrow_mut().set_pending_root(new_root);

        Ok(old_val)
    }

    fn _remove(
        &mut self,
        ptr: NodePtrRef,
        bit_depth: Depth,
        key: &Key,
    ) -> Result<(NodePtrRef, bool, Option<Value>)> {
        let node_ref = self
            .cache
            .borrow_mut()
            .deref_node_ptr(ptr.clone(), Some(FetcherSyncGet::new(key, true)))?;

        match classify_noderef!(?node_ref) {
            NodeKind::None => {
                // Remove from nil node.
                Ok((NodePointer::null_ptr(), false, None))
            }
            NodeKind::Internal => {
                // Remove from internal node and recursively collapse the path, if needed.
                let node_ref = node_ref.unwrap();
                let (changed, old_val): (bool, Option<Value>);
                let (remaining_leaf, remaining_left, remaining_right): (
                    Option<NodeRef>,
                    Option<NodeRef>,
                    Option<NodeRef>,
                );
                if let NodeBox::Internal(ref mut n) = *node_ref.borrow_mut() {
                    // Remove from internal node and recursively collapse the branch, if
                    // needed.
                    let bit_length = bit_depth + n.label_bit_length;

                    if key.bit_length() < bit_length {
                        // Lookup key is too short for the current n.Label, so it doesn't exist.
                        return Ok((ptr, false, None));
                    }

                    let (new_child, c, o) = if key.bit_length() == bit_length {
                        self._remove(n.leaf_node.clone(), bit_depth, key)?
                    } else if key.get_bit(bit_length) {
                        self._remove(n.right.clone(), bit_length, key)?
                    } else {
                        self._remove(n.left.clone(), bit_length, key)?
                    };

                    changed = c;
                    old_val = o;

                    if key.bit_length() == bit_length {
                        n.leaf_node = new_child;
                    } else if key.get_bit(bit_length) {
                        n.right = new_child;
                    } else {
                        n.left = new_child;
                    }

                    // Fetch and check the remaining children.
                    // NOTE: The leaf node is always included with the internal node.
                    remaining_leaf = n.leaf_node.borrow().node.clone();
                    remaining_left = self
                        .cache
                        .borrow_mut()
                        .deref_node_ptr(n.left.clone(), Some(FetcherSyncGet::new(key, true)))?;
                    remaining_right = self
                        .cache
                        .borrow_mut()
                        .deref_node_ptr(n.right.clone(), Some(FetcherSyncGet::new(key, true)))?;
                } else {
                    unreachable!("node kind is Internal");
                }

                // If exactly one child including LeafNode remains, collapse it.
                match remaining_leaf {
                    Some(_) => match remaining_left {
                        Some(_) => (),
                        None => match remaining_right {
                            None => {
                                let nd_leaf = noderef_as!(node_ref, Internal).leaf_node.clone();
                                noderef_as_mut!(node_ref, Internal).leaf_node =
                                    NodePointer::null_ptr();
                                self.cache.borrow_mut().remove_node(ptr);
                                return Ok((nd_leaf, true, old_val));
                            }
                            Some(_) => (),
                        },
                    },
                    None => {
                        let mut nd_child: Option<NodeRef> = None;
                        let mut node_ptr: NodePtrRef = NodePointer::null_ptr();
                        let mut both_children = true;
                        match remaining_left {
                            Some(_) => match remaining_right {
                                None => {
                                    node_ptr = noderef_as!(node_ref, Internal).left.clone();
                                    noderef_as_mut!(node_ref, Internal).left =
                                        NodePointer::null_ptr();
                                    nd_child = remaining_left;
                                    both_children = false;
                                }
                                Some(_) => (),
                            },
                            None => match remaining_right {
                                None => (),
                                Some(_) => {
                                    node_ptr = noderef_as!(node_ref, Internal).right.clone();
                                    noderef_as_mut!(node_ref, Internal).right =
                                        NodePointer::null_ptr();
                                    nd_child = remaining_right;
                                    both_children = false;
                                }
                            },
                        }

                        if !both_children {
                            // If child is an internal node, also fix the label.
                            if let Some(nd_child) = nd_child {
                                if let NodeKind::Internal = classify_noderef!(nd_child) {
                                    if let NodeBox::Internal(ref mut inode) = *nd_child.borrow_mut()
                                    {
                                        inode.label = noderef_as!(node_ref, Internal).label.merge(
                                            noderef_as!(node_ref, Internal).label_bit_length,
                                            &inode.label,
                                            inode.label_bit_length,
                                        );
                                        inode.label_bit_length +=
                                            noderef_as!(node_ref, Internal).label_bit_length;
                                        inode.clean = false;
                                        node_ptr.borrow_mut().clean = false;
                                    }
                                }
                            }

                            self.cache.borrow_mut().remove_node(ptr);
                            return Ok((node_ptr, true, old_val));
                        }
                    }
                };

                // Two or more children including leaf_node remain, just mark dirty bit.
                if changed {
                    noderef_as_mut!(node_ref, Internal).clean = false;
                    ptr.borrow_mut().clean = false;
                    // No longer eligible for eviction as it is dirty.
                    self.cache
                        .borrow_mut()
                        .rollback_node(ptr.clone(), NodeKind::Internal);
                }

                Ok((ptr, changed, old_val))
            }
            NodeKind::Leaf => {
                // Remove from leaf node.
                let node_ref = node_ref.unwrap();
                if noderef_as!(node_ref, Leaf).key == *key {
                    let old_val = noderef_as!(node_ref, Leaf).value.clone();
                    self.cache.borrow_mut().remove_node(ptr);
                    return Ok((NodePointer::null_ptr(), true, Some(old_val)));
                }

                Ok((ptr, false, None))
            }
        }
    }
}