1use std::sync::Arc;
2
3use crate::*;
4
5pub struct Node<K, V> {
6 pub key: Option<Item<K, V>>,
7 ttl: Option<Duration>,
8 length: usize,
9 pub children: Vec<N<K, V>>,
10}
11
12impl<K, V> Node<K, V>
13where
14 K: Ord,
15{
16 pub fn instance(children: Vec<N<K, V>>) -> N<K, V> {
17 let key = if children.is_empty() {
18 None
19 } else {
20 children[0].key().cloned()
21 };
22
23 let mut ttl = None;
24
25 let mut length = 0;
26
27 for c in children.iter() {
28 if let Some(t) = c.ttl() {
29 if let Some(t1) = &ttl {
30 if t < t1 {
31 ttl = Some(*t);
32 }
33 } else {
34 ttl = Some(*t);
35 }
36 }
37
38 length += c.len();
39 }
40
41 Arc::new(BTreeType::Node(Self {
42 key,
43 length,
44 ttl,
45 children,
46 }))
47 }
48
49 pub fn put(&self, m: usize, k: K, v: V, ttl: Option<Duration>) -> PutResult<K, V> {
50 let index = self.search_index(&k);
51
52 let (values, old) = self.children[index].put(m, k, v, ttl);
53
54 let mut children = Vec::with_capacity(self.children.len() + values.len());
55
56 children.extend(self.children[..index].iter().cloned());
57 children.extend(values);
58 children.extend(self.children[index + 1..].iter().cloned());
59
60 if children.len() < m {
61 return (vec![Self::instance(children)], old);
62 }
63
64 let mid = m / 2;
65
66 let left = children[..mid].to_vec();
67 let right = children[mid..].to_vec();
68
69 (vec![Self::instance(left), Self::instance(right)], old)
70 }
71
72 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
73 where
74 K: Borrow<Q> + Ord,
75 Q: Ord,
76 {
77 self.children[self.search_index(k)].get(k)
78 }
79
80 pub fn remove<Q: ?Sized>(&self, k: &Q) -> Option<(N<K, V>, Item<K, V>)>
81 where
82 K: Borrow<Q> + Ord,
83 Q: Ord,
84 {
85 let index = self.search_index(k);
86
87 let (child, item) = self.children[index].remove(k)?;
88
89 let mut children = Vec::with_capacity(self.children.len() - 1);
90 children.extend(self.children[..index].iter().cloned());
91 if child.len() > 0 {
92 children.push(child);
93 }
94 children.extend(self.children[index + 1..].iter().cloned());
95
96 Some((Self::instance(children), item))
97 }
98
99 pub fn expir(&self) -> Option<N<K, V>> {
100 let now = now();
101 match self.ttl {
102 Some(t) if t < now => {
103 let children = self
104 .children
105 .iter()
106 .filter_map(|c| {
107 let c = c.expir();
108 if c.len() > 0 {
109 Some(c)
110 } else {
111 None
112 }
113 })
114 .collect();
115 Some(Self::instance(children))
116 }
117 _ => None,
118 }
119 }
120
121 pub fn write(&self, m: usize, mut actions: BTreeMap<K, Action<V>>) -> Vec<N<K, V>> {
122 let mut children = Vec::with_capacity(self.children.len() + actions.len());
123
124 let mut start_index = 0;
125
126 loop {
127 if let Some((k, _)) = actions.first_key_value() {
128 let index = self.search_index(k);
129
130 if start_index < index {
131 children.extend_from_slice(&self.children[start_index..index]);
132 }
133
134 if index + 1 < self.children.len() {
135 if let Some(k) = self.children[index + 1].key() {
137 let temp = actions.split_off(&k.0);
138 children.extend(self.children[index].write(m, actions));
139 start_index = index + 1;
140 actions = temp;
141 }
142 } else {
143 children.extend(self.children[index].write(m, actions));
144 break;
145 }
146 } else {
147 children.extend_from_slice(&self.children[start_index..]);
148 break;
149 }
150 }
151
152 children
153 .chunks(m)
154 .filter_map(|c| {
155 if c.is_empty() {
156 None
157 } else {
158 Some(Self::instance(c.to_vec()))
159 }
160 })
161 .collect()
162 }
163
164 pub fn len(&self) -> usize {
165 self.length
166 }
167
168 pub fn split_off<Q: ?Sized>(&self, k: &Q) -> (N<K, V>, N<K, V>)
169 where
170 K: Borrow<Q> + Ord,
171 Q: Ord,
172 {
173 let index = self.search_index(k);
174
175 let (l, r) = self.children[index].split_off(k);
176
177 let mut left = Vec::with_capacity(index);
178 left.extend_from_slice(&self.children[..index]);
179 if l.len() > 0 {
180 left.push(l);
181 }
182
183 let mut right = Vec::with_capacity(self.children.len() - index);
184 if r.len() > 0 {
185 right.push(r);
186 }
187 right.extend_from_slice(&self.children[index + 1..]);
188
189 (Self::instance(left), Self::instance(right))
190 }
191
192 pub fn search_index<Q: ?Sized>(&self, k: &Q) -> usize
193 where
194 K: Borrow<Q> + Ord,
195 Q: Ord,
196 {
197 match self.children.binary_search_by(|c| cmp(c.key(), Some(k))) {
198 Ok(i) => i,
199 Err(i) => {
200 if i == 0 {
201 i
202 } else {
203 i - 1
204 }
205 }
206 }
207 }
208
209 pub(crate) fn ttl(&self) -> Option<&Duration>
210 where
211 K: Ord,
212 {
213 self.ttl.as_ref()
214 }
215}