Show More
@@ -1,1069 +1,1069 b'' | |||
|
1 | 1 | // Copyright 2018-2020 Georges Racinet <georges.racinet@octobus.net> |
|
2 | 2 | // and Mercurial contributors |
|
3 | 3 | // |
|
4 | 4 | // This software may be used and distributed according to the terms of the |
|
5 | 5 | // GNU General Public License version 2 or any later version. |
|
6 | 6 | //! Indexing facilities for fast retrieval of `Revision` from `Node` |
|
7 | 7 | //! |
|
8 | 8 | //! This provides a variation on the 16-ary radix tree that is |
|
9 | 9 | //! provided as "nodetree" in revlog.c, ready for append-only persistence |
|
10 | 10 | //! on disk. |
|
11 | 11 | //! |
|
12 | 12 | //! Following existing implicit conventions, the "nodemap" terminology |
|
13 | 13 | //! is used in a more abstract context. |
|
14 | 14 | |
|
15 | 15 | use super::{ |
|
16 | 16 | node::NULL_NODE, Node, NodePrefix, Revision, RevlogIndex, NULL_REVISION, |
|
17 | 17 | }; |
|
18 | 18 | |
|
19 | 19 | use bytes_cast::{unaligned, BytesCast}; |
|
20 | 20 | use std::cmp::max; |
|
21 | 21 | use std::fmt; |
|
22 | 22 | use std::mem::{self, align_of, size_of}; |
|
23 | 23 | use std::ops::Deref; |
|
24 | 24 | use std::ops::Index; |
|
25 | 25 | |
|
26 | 26 | #[derive(Debug, PartialEq)] |
|
27 | 27 | pub enum NodeMapError { |
|
28 | 28 | MultipleResults, |
|
29 | 29 | /// A `Revision` stored in the nodemap could not be found in the index |
|
30 | 30 | RevisionNotInIndex(Revision), |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | /// Mapping system from Mercurial nodes to revision numbers. |
|
34 | 34 | /// |
|
35 | 35 | /// ## `RevlogIndex` and `NodeMap` |
|
36 | 36 | /// |
|
37 | 37 | /// One way to think about their relationship is that |
|
38 | 38 | /// the `NodeMap` is a prefix-oriented reverse index of the `Node` information |
|
39 | 39 | /// carried by a [`RevlogIndex`]. |
|
40 | 40 | /// |
|
41 | 41 | /// Many of the methods in this trait take a `RevlogIndex` argument |
|
42 | 42 | /// which is used for validation of their results. This index must naturally |
|
43 | 43 | /// be the one the `NodeMap` is about, and it must be consistent. |
|
44 | 44 | /// |
|
45 | 45 | /// Notably, the `NodeMap` must not store |
|
46 | 46 | /// information about more `Revision` values than there are in the index. |
|
47 | 47 | /// In these methods, an encountered `Revision` is not in the index, a |
|
48 | 48 | /// [`RevisionNotInIndex`] error is returned. |
|
49 | 49 | /// |
|
50 | 50 | /// In insert operations, the rule is thus that the `NodeMap` must always |
|
51 | 51 | /// be updated after the `RevlogIndex` |
|
52 | 52 | /// be updated first, and the `NodeMap` second. |
|
53 | 53 | /// |
|
54 | 54 | /// [`RevisionNotInIndex`]: enum.NodeMapError.html#variant.RevisionNotInIndex |
|
55 | 55 | /// [`RevlogIndex`]: ../trait.RevlogIndex.html |
|
56 | 56 | pub trait NodeMap { |
|
57 | 57 | /// Find the unique `Revision` having the given `Node` |
|
58 | 58 | /// |
|
59 | 59 | /// If no Revision matches the given `Node`, `Ok(None)` is returned. |
|
60 | 60 | fn find_node( |
|
61 | 61 | &self, |
|
62 | 62 | index: &impl RevlogIndex, |
|
63 | 63 | node: &Node, |
|
64 | 64 | ) -> Result<Option<Revision>, NodeMapError> { |
|
65 | 65 | self.find_bin(index, node.into()) |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | /// Find the unique Revision whose `Node` starts with a given binary prefix |
|
69 | 69 | /// |
|
70 | 70 | /// If no Revision matches the given prefix, `Ok(None)` is returned. |
|
71 | 71 | /// |
|
72 | 72 | /// If several Revisions match the given prefix, a [`MultipleResults`] |
|
73 | 73 | /// error is returned. |
|
74 | 74 | fn find_bin<'a>( |
|
75 | 75 | &self, |
|
76 | 76 | idx: &impl RevlogIndex, |
|
77 | 77 | prefix: NodePrefix, |
|
78 | 78 | ) -> Result<Option<Revision>, NodeMapError>; |
|
79 | 79 | |
|
80 | 80 | /// Give the size of the shortest node prefix that determines |
|
81 | 81 | /// the revision uniquely. |
|
82 | 82 | /// |
|
83 | 83 | /// From a binary node prefix, if it is matched in the node map, this |
|
84 | 84 | /// returns the number of hexadecimal digits that would had sufficed |
|
85 | 85 | /// to find the revision uniquely. |
|
86 | 86 | /// |
|
87 | 87 | /// Returns `None` if no `Revision` could be found for the prefix. |
|
88 | 88 | /// |
|
89 | 89 | /// If several Revisions match the given prefix, a [`MultipleResults`] |
|
90 | 90 | /// error is returned. |
|
91 | 91 | fn unique_prefix_len_bin<'a>( |
|
92 | 92 | &self, |
|
93 | 93 | idx: &impl RevlogIndex, |
|
94 | 94 | node_prefix: NodePrefix, |
|
95 | 95 | ) -> Result<Option<usize>, NodeMapError>; |
|
96 | 96 | |
|
97 | 97 | /// Same as `unique_prefix_len_bin`, with a full `Node` as input |
|
98 | 98 | fn unique_prefix_len_node( |
|
99 | 99 | &self, |
|
100 | 100 | idx: &impl RevlogIndex, |
|
101 | 101 | node: &Node, |
|
102 | 102 | ) -> Result<Option<usize>, NodeMapError> { |
|
103 | 103 | self.unique_prefix_len_bin(idx, node.into()) |
|
104 | 104 | } |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | pub trait MutableNodeMap: NodeMap { |
|
108 | 108 | fn insert<I: RevlogIndex>( |
|
109 | 109 | &mut self, |
|
110 | 110 | index: &I, |
|
111 | 111 | node: &Node, |
|
112 | 112 | rev: Revision, |
|
113 | 113 | ) -> Result<(), NodeMapError>; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | /// Low level NodeTree [`Blocks`] elements |
|
117 | 117 | /// |
|
118 | 118 | /// These are exactly as for instance on persistent storage. |
|
119 | 119 | type RawElement = unaligned::I32Be; |
|
120 | 120 | |
|
121 | 121 | /// High level representation of values in NodeTree |
|
122 | 122 | /// [`Blocks`](struct.Block.html) |
|
123 | 123 | /// |
|
124 | 124 | /// This is the high level representation that most algorithms should |
|
125 | 125 | /// use. |
|
126 | 126 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
127 | 127 | enum Element { |
|
128 | 128 | Rev(Revision), |
|
129 | 129 | Block(usize), |
|
130 | 130 | None, |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | impl From<RawElement> for Element { |
|
134 | 134 | /// Conversion from low level representation, after endianness conversion. |
|
135 | 135 | /// |
|
136 | 136 | /// See [`Block`](struct.Block.html) for explanation about the encoding. |
|
137 | 137 | fn from(raw: RawElement) -> Element { |
|
138 | 138 | let int = raw.get(); |
|
139 | 139 | if int >= 0 { |
|
140 | 140 | Element::Block(int as usize) |
|
141 | 141 | } else if int == -1 { |
|
142 | 142 | Element::None |
|
143 | 143 | } else { |
|
144 | 144 | Element::Rev(-int - 2) |
|
145 | 145 | } |
|
146 | 146 | } |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | impl From<Element> for RawElement { |
|
150 | 150 | fn from(element: Element) -> RawElement { |
|
151 | 151 | RawElement::from(match element { |
|
152 | 152 | Element::None => 0, |
|
153 | 153 | Element::Block(i) => i as i32, |
|
154 | 154 | Element::Rev(rev) => -rev - 2, |
|
155 | 155 | }) |
|
156 | 156 | } |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | /// A logical block of the `NodeTree`, packed with a fixed size. |
|
160 | 160 | /// |
|
161 | 161 | /// These are always used in container types implementing `Index<Block>`, |
|
162 | 162 | /// such as `&Block` |
|
163 | 163 | /// |
|
164 | 164 | /// As an array of integers, its ith element encodes that the |
|
165 | 165 | /// ith potential edge from the block, representing the ith hexadecimal digit |
|
166 | 166 | /// (nybble) `i` is either: |
|
167 | 167 | /// |
|
168 | 168 | /// - absent (value -1) |
|
169 | 169 | /// - another `Block` in the same indexable container (value β₯ 0) |
|
170 | 170 | /// - a `Revision` leaf (value β€ -2) |
|
171 | 171 | /// |
|
172 | 172 | /// Endianness has to be fixed for consistency on shared storage across |
|
173 | 173 | /// different architectures. |
|
174 | 174 | /// |
|
175 | 175 | /// A key difference with the C `nodetree` is that we need to be |
|
176 | 176 | /// able to represent the [`Block`] at index 0, hence -1 is the empty marker |
|
177 | 177 | /// rather than 0 and the `Revision` range upper limit of -2 instead of -1. |
|
178 | 178 | /// |
|
179 | 179 | /// Another related difference is that `NULL_REVISION` (-1) is not |
|
180 | 180 | /// represented at all, because we want an immutable empty nodetree |
|
181 | 181 | /// to be valid. |
|
182 | 182 | |
|
183 | 183 | const ELEMENTS_PER_BLOCK: usize = 16; // number of different values in a nybble |
|
184 | 184 | |
|
185 | 185 | #[derive(Copy, Clone, BytesCast, PartialEq)] |
|
186 | 186 | #[repr(transparent)] |
|
187 | 187 | pub struct Block([RawElement; ELEMENTS_PER_BLOCK]); |
|
188 | 188 | |
|
189 | 189 | impl Block { |
|
190 | 190 | fn new() -> Self { |
|
191 | 191 | let absent_node = RawElement::from(-1); |
|
192 | 192 | Block([absent_node; ELEMENTS_PER_BLOCK]) |
|
193 | 193 | } |
|
194 | 194 | |
|
195 | 195 | fn get(&self, nybble: u8) -> Element { |
|
196 | 196 | self.0[nybble as usize].into() |
|
197 | 197 | } |
|
198 | 198 | |
|
199 | 199 | fn set(&mut self, nybble: u8, element: Element) { |
|
200 | 200 | self.0[nybble as usize] = element.into() |
|
201 | 201 | } |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | impl fmt::Debug for Block { |
|
205 | 205 | /// sparse representation for testing and debugging purposes |
|
206 | 206 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
207 | 207 | f.debug_map() |
|
208 | 208 | .entries((0..16).filter_map(|i| match self.get(i) { |
|
209 | 209 | Element::None => None, |
|
210 | 210 | element => Some((i, element)), |
|
211 | 211 | })) |
|
212 | 212 | .finish() |
|
213 | 213 | } |
|
214 | 214 | } |
|
215 | 215 | |
|
216 | 216 | /// A mutable 16-radix tree with the root block logically at the end |
|
217 | 217 | /// |
|
218 | 218 | /// Because of the append only nature of our node trees, we need to |
|
219 | 219 | /// keep the original untouched and store new blocks separately. |
|
220 | 220 | /// |
|
221 | 221 | /// The mutable root `Block` is kept apart so that we don't have to rebump |
|
222 | 222 | /// it on each insertion. |
|
223 | 223 | pub struct NodeTree { |
|
224 | 224 | readonly: Box<dyn Deref<Target = [Block]> + Send>, |
|
225 | 225 | growable: Vec<Block>, |
|
226 | 226 | root: Block, |
|
227 | 227 | masked_inner_blocks: usize, |
|
228 | 228 | } |
|
229 | 229 | |
|
230 | 230 | impl Index<usize> for NodeTree { |
|
231 | 231 | type Output = Block; |
|
232 | 232 | |
|
233 | 233 | fn index(&self, i: usize) -> &Block { |
|
234 | 234 | let ro_len = self.readonly.len(); |
|
235 | 235 | if i < ro_len { |
|
236 | 236 | &self.readonly[i] |
|
237 | 237 | } else if i == ro_len + self.growable.len() { |
|
238 | 238 | &self.root |
|
239 | 239 | } else { |
|
240 | 240 | &self.growable[i - ro_len] |
|
241 | 241 | } |
|
242 | 242 | } |
|
243 | 243 | } |
|
244 | 244 | |
|
245 | 245 | /// Return `None` unless the `Node` for `rev` has given prefix in `index`. |
|
246 | 246 | fn has_prefix_or_none( |
|
247 | 247 | idx: &impl RevlogIndex, |
|
248 | 248 | prefix: NodePrefix, |
|
249 | 249 | rev: Revision, |
|
250 | 250 | ) -> Result<Option<Revision>, NodeMapError> { |
|
251 | 251 | idx.node(rev) |
|
252 | 252 | .ok_or_else(|| NodeMapError::RevisionNotInIndex(rev)) |
|
253 | 253 | .map(|node| { |
|
254 | 254 | if prefix.is_prefix_of(node) { |
|
255 | 255 | Some(rev) |
|
256 | 256 | } else { |
|
257 | 257 | None |
|
258 | 258 | } |
|
259 | 259 | }) |
|
260 | 260 | } |
|
261 | 261 | |
|
262 | 262 | /// validate that the candidate's node starts indeed with given prefix, |
|
263 | 263 | /// and treat ambiguities related to `NULL_REVISION`. |
|
264 | 264 | /// |
|
265 | 265 | /// From the data in the NodeTree, one can only conclude that some |
|
266 | 266 | /// revision is the only one for a *subprefix* of the one being looked up. |
|
267 | 267 | fn validate_candidate( |
|
268 | 268 | idx: &impl RevlogIndex, |
|
269 | 269 | prefix: NodePrefix, |
|
270 | 270 | candidate: (Option<Revision>, usize), |
|
271 | 271 | ) -> Result<(Option<Revision>, usize), NodeMapError> { |
|
272 | 272 | let (rev, steps) = candidate; |
|
273 | 273 | if let Some(nz_nybble) = prefix.first_different_nybble(&NULL_NODE) { |
|
274 | 274 | rev.map_or(Ok((None, steps)), |r| { |
|
275 | 275 | has_prefix_or_none(idx, prefix, r) |
|
276 | 276 | .map(|opt| (opt, max(steps, nz_nybble + 1))) |
|
277 | 277 | }) |
|
278 | 278 | } else { |
|
279 | 279 | // the prefix is only made of zeros; NULL_REVISION always matches it |
|
280 | 280 | // and any other *valid* result is an ambiguity |
|
281 | 281 | match rev { |
|
282 | 282 | None => Ok((Some(NULL_REVISION), steps + 1)), |
|
283 | 283 | Some(r) => match has_prefix_or_none(idx, prefix, r)? { |
|
284 | 284 | None => Ok((Some(NULL_REVISION), steps + 1)), |
|
285 | 285 | _ => Err(NodeMapError::MultipleResults), |
|
286 | 286 | }, |
|
287 | 287 | } |
|
288 | 288 | } |
|
289 | 289 | } |
|
290 | 290 | |
|
291 | 291 | impl NodeTree { |
|
292 | 292 | /// Initiate a NodeTree from an immutable slice-like of `Block` |
|
293 | 293 | /// |
|
294 | 294 | /// We keep `readonly` and clone its root block if it isn't empty. |
|
295 | 295 | fn new(readonly: Box<dyn Deref<Target = [Block]> + Send>) -> Self { |
|
296 | 296 | let root = readonly.last().cloned().unwrap_or_else(Block::new); |
|
297 | 297 | NodeTree { |
|
298 | 298 | readonly, |
|
299 | 299 | growable: Vec::new(), |
|
300 | 300 | root, |
|
301 | 301 | masked_inner_blocks: 0, |
|
302 | 302 | } |
|
303 | 303 | } |
|
304 | 304 | |
|
305 | 305 | /// Create from an opaque bunch of bytes |
|
306 | 306 | /// |
|
307 | 307 | /// The created `NodeTreeBytes` from `buffer`, |
|
308 | 308 | /// of which exactly `amount` bytes are used. |
|
309 | 309 | /// |
|
310 | 310 | /// - `buffer` could be derived from `PyBuffer` and `Mmap` objects. |
|
311 | 311 | /// - `offset` allows for the final file format to include fixed data |
|
312 | 312 | /// (generation number, behavioural flags) |
|
313 | 313 | /// - `amount` is expressed in bytes, and is not automatically derived from |
|
314 | 314 | /// `bytes`, so that a caller that manages them atomically can perform |
|
315 | 315 | /// temporary disk serializations and still rollback easily if needed. |
|
316 | 316 | /// First use-case for this would be to support Mercurial shell hooks. |
|
317 | 317 | /// |
|
318 | 318 | /// panics if `buffer` is smaller than `amount` |
|
319 | 319 | pub fn load_bytes( |
|
320 | 320 | bytes: Box<dyn Deref<Target = [u8]> + Send>, |
|
321 | 321 | amount: usize, |
|
322 | 322 | ) -> Self { |
|
323 | 323 | NodeTree::new(Box::new(NodeTreeBytes::new(bytes, amount))) |
|
324 | 324 | } |
|
325 | 325 | |
|
326 | 326 | /// Retrieve added `Block` and the original immutable data |
|
327 | 327 | pub fn into_readonly_and_added( |
|
328 | 328 | self, |
|
329 | 329 | ) -> (Box<dyn Deref<Target = [Block]> + Send>, Vec<Block>) { |
|
330 | 330 | let mut vec = self.growable; |
|
331 | 331 | let readonly = self.readonly; |
|
332 | 332 | if readonly.last() != Some(&self.root) { |
|
333 | 333 | vec.push(self.root); |
|
334 | 334 | } |
|
335 | 335 | (readonly, vec) |
|
336 | 336 | } |
|
337 | 337 | |
|
338 | 338 | /// Retrieve added `Blocks` as bytes, ready to be written to persistent |
|
339 | 339 | /// storage |
|
340 | 340 | pub fn into_readonly_and_added_bytes( |
|
341 | 341 | self, |
|
342 | 342 | ) -> (Box<dyn Deref<Target = [Block]> + Send>, Vec<u8>) { |
|
343 | 343 | let (readonly, vec) = self.into_readonly_and_added(); |
|
344 | 344 | // Prevent running `v`'s destructor so we are in complete control |
|
345 | 345 | // of the allocation. |
|
346 | 346 | let vec = mem::ManuallyDrop::new(vec); |
|
347 | 347 | |
|
348 | 348 | // Transmute the `Vec<Block>` to a `Vec<u8>`. Blocks are contiguous |
|
349 | 349 | // bytes, so this is perfectly safe. |
|
350 | 350 | let bytes = unsafe { |
|
351 | 351 | // Check for compatible allocation layout. |
|
352 | 352 | // (Optimized away by constant-folding + dead code elimination.) |
|
353 | 353 | assert_eq!(size_of::<Block>(), 64); |
|
354 | 354 | assert_eq!(align_of::<Block>(), 1); |
|
355 | 355 | |
|
356 | 356 | // /!\ Any use of `vec` after this is use-after-free. |
|
357 | 357 | // TODO: use `into_raw_parts` once stabilized |
|
358 | 358 | Vec::from_raw_parts( |
|
359 | 359 | vec.as_ptr() as *mut u8, |
|
360 | 360 | vec.len() * size_of::<Block>(), |
|
361 | 361 | vec.capacity() * size_of::<Block>(), |
|
362 | 362 | ) |
|
363 | 363 | }; |
|
364 | 364 | (readonly, bytes) |
|
365 | 365 | } |
|
366 | 366 | |
|
367 | 367 | /// Total number of blocks |
|
368 | 368 | fn len(&self) -> usize { |
|
369 | 369 | self.readonly.len() + self.growable.len() + 1 |
|
370 | 370 | } |
|
371 | 371 | |
|
372 | 372 | /// Implemented for completeness |
|
373 | 373 | /// |
|
374 | 374 | /// A `NodeTree` always has at least the mutable root block. |
|
375 | 375 | #[allow(dead_code)] |
|
376 | 376 | fn is_empty(&self) -> bool { |
|
377 | 377 | false |
|
378 | 378 | } |
|
379 | 379 | |
|
380 | 380 | /// Main working method for `NodeTree` searches |
|
381 | 381 | /// |
|
382 | 382 | /// The first returned value is the result of analysing `NodeTree` data |
|
383 | 383 | /// *alone*: whereas `None` guarantees that the given prefix is absent |
|
384 | 384 | /// from the `NodeTree` data (but still could match `NULL_NODE`), with |
|
385 | 385 | /// `Some(rev)`, it is to be understood that `rev` is the unique `Revision` |
|
386 | 386 | /// that could match the prefix. Actually, all that can be inferred from |
|
387 | 387 | /// the `NodeTree` data is that `rev` is the revision with the longest |
|
388 | 388 | /// common node prefix with the given prefix. |
|
389 | 389 | /// |
|
390 | 390 | /// The second returned value is the size of the smallest subprefix |
|
391 | 391 | /// of `prefix` that would give the same result, i.e. not the |
|
392 | 392 | /// `MultipleResults` error variant (again, using only the data of the |
|
393 | 393 | /// `NodeTree`). |
|
394 | 394 | fn lookup( |
|
395 | 395 | &self, |
|
396 | 396 | prefix: NodePrefix, |
|
397 | 397 | ) -> Result<(Option<Revision>, usize), NodeMapError> { |
|
398 | 398 | for (i, visit_item) in self.visit(prefix).enumerate() { |
|
399 | 399 | if let Some(opt) = visit_item.final_revision() { |
|
400 | 400 | return Ok((opt, i + 1)); |
|
401 | 401 | } |
|
402 | 402 | } |
|
403 | 403 | Err(NodeMapError::MultipleResults) |
|
404 | 404 | } |
|
405 | 405 | |
|
406 |
fn visit |
|
|
406 | fn visit(&self, prefix: NodePrefix) -> NodeTreeVisitor { | |
|
407 | 407 | NodeTreeVisitor { |
|
408 | 408 | nt: self, |
|
409 | 409 | prefix, |
|
410 | 410 | visit: self.len() - 1, |
|
411 | 411 | nybble_idx: 0, |
|
412 | 412 | done: false, |
|
413 | 413 | } |
|
414 | 414 | } |
|
415 | 415 | /// Return a mutable reference for `Block` at index `idx`. |
|
416 | 416 | /// |
|
417 | 417 | /// If `idx` lies in the immutable area, then the reference is to |
|
418 | 418 | /// a newly appended copy. |
|
419 | 419 | /// |
|
420 | 420 | /// Returns (new_idx, glen, mut_ref) where |
|
421 | 421 | /// |
|
422 | 422 | /// - `new_idx` is the index of the mutable `Block` |
|
423 | 423 | /// - `mut_ref` is a mutable reference to the mutable Block. |
|
424 | 424 | /// - `glen` is the new length of `self.growable` |
|
425 | 425 | /// |
|
426 | 426 | /// Note: the caller wouldn't be allowed to query `self.growable.len()` |
|
427 | 427 | /// itself because of the mutable borrow taken with the returned `Block` |
|
428 | 428 | fn mutable_block(&mut self, idx: usize) -> (usize, &mut Block, usize) { |
|
429 | 429 | let ro_blocks = &self.readonly; |
|
430 | 430 | let ro_len = ro_blocks.len(); |
|
431 | 431 | let glen = self.growable.len(); |
|
432 | 432 | if idx < ro_len { |
|
433 | 433 | self.masked_inner_blocks += 1; |
|
434 | 434 | self.growable.push(ro_blocks[idx]); |
|
435 | 435 | (glen + ro_len, &mut self.growable[glen], glen + 1) |
|
436 | 436 | } else if glen + ro_len == idx { |
|
437 | 437 | (idx, &mut self.root, glen) |
|
438 | 438 | } else { |
|
439 | 439 | (idx, &mut self.growable[idx - ro_len], glen) |
|
440 | 440 | } |
|
441 | 441 | } |
|
442 | 442 | |
|
443 | 443 | /// Main insertion method |
|
444 | 444 | /// |
|
445 | 445 | /// This will dive in the node tree to find the deepest `Block` for |
|
446 | 446 | /// `node`, split it as much as needed and record `node` in there. |
|
447 | 447 | /// The method then backtracks, updating references in all the visited |
|
448 | 448 | /// blocks from the root. |
|
449 | 449 | /// |
|
450 | 450 | /// All the mutated `Block` are copied first to the growable part if |
|
451 | 451 | /// needed. That happens for those in the immutable part except the root. |
|
452 | 452 | pub fn insert<I: RevlogIndex>( |
|
453 | 453 | &mut self, |
|
454 | 454 | index: &I, |
|
455 | 455 | node: &Node, |
|
456 | 456 | rev: Revision, |
|
457 | 457 | ) -> Result<(), NodeMapError> { |
|
458 | 458 | let ro_len = &self.readonly.len(); |
|
459 | 459 | |
|
460 | 460 | let mut visit_steps: Vec<_> = self.visit(node.into()).collect(); |
|
461 | 461 | let read_nybbles = visit_steps.len(); |
|
462 | 462 | // visit_steps cannot be empty, since we always visit the root block |
|
463 | 463 | let deepest = visit_steps.pop().unwrap(); |
|
464 | 464 | |
|
465 | 465 | let (mut block_idx, mut block, mut glen) = |
|
466 | 466 | self.mutable_block(deepest.block_idx); |
|
467 | 467 | |
|
468 | 468 | if let Element::Rev(old_rev) = deepest.element { |
|
469 | 469 | let old_node = index |
|
470 | 470 | .node(old_rev) |
|
471 | 471 | .ok_or_else(|| NodeMapError::RevisionNotInIndex(old_rev))?; |
|
472 | 472 | if old_node == node { |
|
473 | 473 | return Ok(()); // avoid creating lots of useless blocks |
|
474 | 474 | } |
|
475 | 475 | |
|
476 | 476 | // Looping over the tail of nybbles in both nodes, creating |
|
477 | 477 | // new blocks until we find the difference |
|
478 | 478 | let mut new_block_idx = ro_len + glen; |
|
479 | 479 | let mut nybble = deepest.nybble; |
|
480 | 480 | for nybble_pos in read_nybbles..node.nybbles_len() { |
|
481 | 481 | block.set(nybble, Element::Block(new_block_idx)); |
|
482 | 482 | |
|
483 | 483 | let new_nybble = node.get_nybble(nybble_pos); |
|
484 | 484 | let old_nybble = old_node.get_nybble(nybble_pos); |
|
485 | 485 | |
|
486 | 486 | if old_nybble == new_nybble { |
|
487 | 487 | self.growable.push(Block::new()); |
|
488 | 488 | block = &mut self.growable[glen]; |
|
489 | 489 | glen += 1; |
|
490 | 490 | new_block_idx += 1; |
|
491 | 491 | nybble = new_nybble; |
|
492 | 492 | } else { |
|
493 | 493 | let mut new_block = Block::new(); |
|
494 | 494 | new_block.set(old_nybble, Element::Rev(old_rev)); |
|
495 | 495 | new_block.set(new_nybble, Element::Rev(rev)); |
|
496 | 496 | self.growable.push(new_block); |
|
497 | 497 | break; |
|
498 | 498 | } |
|
499 | 499 | } |
|
500 | 500 | } else { |
|
501 | 501 | // Free slot in the deepest block: no splitting has to be done |
|
502 | 502 | block.set(deepest.nybble, Element::Rev(rev)); |
|
503 | 503 | } |
|
504 | 504 | |
|
505 | 505 | // Backtrack over visit steps to update references |
|
506 | 506 | while let Some(visited) = visit_steps.pop() { |
|
507 | 507 | let to_write = Element::Block(block_idx); |
|
508 | 508 | if visit_steps.is_empty() { |
|
509 | 509 | self.root.set(visited.nybble, to_write); |
|
510 | 510 | break; |
|
511 | 511 | } |
|
512 | 512 | let (new_idx, block, _) = self.mutable_block(visited.block_idx); |
|
513 | 513 | if block.get(visited.nybble) == to_write { |
|
514 | 514 | break; |
|
515 | 515 | } |
|
516 | 516 | block.set(visited.nybble, to_write); |
|
517 | 517 | block_idx = new_idx; |
|
518 | 518 | } |
|
519 | 519 | Ok(()) |
|
520 | 520 | } |
|
521 | 521 | |
|
522 | 522 | /// Make the whole `NodeTree` logically empty, without touching the |
|
523 | 523 | /// immutable part. |
|
524 | 524 | pub fn invalidate_all(&mut self) { |
|
525 | 525 | self.root = Block::new(); |
|
526 | 526 | self.growable = Vec::new(); |
|
527 | 527 | self.masked_inner_blocks = self.readonly.len(); |
|
528 | 528 | } |
|
529 | 529 | |
|
530 | 530 | /// Return the number of blocks in the readonly part that are currently |
|
531 | 531 | /// masked in the mutable part. |
|
532 | 532 | /// |
|
533 | 533 | /// The `NodeTree` structure has no efficient way to know how many blocks |
|
534 | 534 | /// are already unreachable in the readonly part. |
|
535 | 535 | /// |
|
536 | 536 | /// After a call to `invalidate_all()`, the returned number can be actually |
|
537 | 537 | /// bigger than the whole readonly part, a conventional way to mean that |
|
538 | 538 | /// all the readonly blocks have been masked. This is what is really |
|
539 | 539 | /// useful to the caller and does not require to know how many were |
|
540 | 540 | /// actually unreachable to begin with. |
|
541 | 541 | pub fn masked_readonly_blocks(&self) -> usize { |
|
542 | 542 | if let Some(readonly_root) = self.readonly.last() { |
|
543 | 543 | if readonly_root == &self.root { |
|
544 | 544 | return 0; |
|
545 | 545 | } |
|
546 | 546 | } else { |
|
547 | 547 | return 0; |
|
548 | 548 | } |
|
549 | 549 | self.masked_inner_blocks + 1 |
|
550 | 550 | } |
|
551 | 551 | } |
|
552 | 552 | |
|
553 | 553 | pub struct NodeTreeBytes { |
|
554 | 554 | buffer: Box<dyn Deref<Target = [u8]> + Send>, |
|
555 | 555 | len_in_blocks: usize, |
|
556 | 556 | } |
|
557 | 557 | |
|
558 | 558 | impl NodeTreeBytes { |
|
559 | 559 | fn new( |
|
560 | 560 | buffer: Box<dyn Deref<Target = [u8]> + Send>, |
|
561 | 561 | amount: usize, |
|
562 | 562 | ) -> Self { |
|
563 | 563 | assert!(buffer.len() >= amount); |
|
564 | 564 | let len_in_blocks = amount / size_of::<Block>(); |
|
565 | 565 | NodeTreeBytes { |
|
566 | 566 | buffer, |
|
567 | 567 | len_in_blocks, |
|
568 | 568 | } |
|
569 | 569 | } |
|
570 | 570 | } |
|
571 | 571 | |
|
572 | 572 | impl Deref for NodeTreeBytes { |
|
573 | 573 | type Target = [Block]; |
|
574 | 574 | |
|
575 | 575 | fn deref(&self) -> &[Block] { |
|
576 | 576 | Block::slice_from_bytes(&self.buffer, self.len_in_blocks) |
|
577 | 577 | // `NodeTreeBytes::new` already asserted that `self.buffer` is |
|
578 | 578 | // large enough. |
|
579 | 579 | .unwrap() |
|
580 | 580 | .0 |
|
581 | 581 | } |
|
582 | 582 | } |
|
583 | 583 | |
|
584 | 584 | struct NodeTreeVisitor<'n> { |
|
585 | 585 | nt: &'n NodeTree, |
|
586 | 586 | prefix: NodePrefix, |
|
587 | 587 | visit: usize, |
|
588 | 588 | nybble_idx: usize, |
|
589 | 589 | done: bool, |
|
590 | 590 | } |
|
591 | 591 | |
|
592 | 592 | #[derive(Debug, PartialEq, Clone)] |
|
593 | 593 | struct NodeTreeVisitItem { |
|
594 | 594 | block_idx: usize, |
|
595 | 595 | nybble: u8, |
|
596 | 596 | element: Element, |
|
597 | 597 | } |
|
598 | 598 | |
|
599 | 599 | impl<'n> Iterator for NodeTreeVisitor<'n> { |
|
600 | 600 | type Item = NodeTreeVisitItem; |
|
601 | 601 | |
|
602 | 602 | fn next(&mut self) -> Option<Self::Item> { |
|
603 | 603 | if self.done || self.nybble_idx >= self.prefix.nybbles_len() { |
|
604 | 604 | return None; |
|
605 | 605 | } |
|
606 | 606 | |
|
607 | 607 | let nybble = self.prefix.get_nybble(self.nybble_idx); |
|
608 | 608 | self.nybble_idx += 1; |
|
609 | 609 | |
|
610 | 610 | let visit = self.visit; |
|
611 | 611 | let element = self.nt[visit].get(nybble); |
|
612 | 612 | if let Element::Block(idx) = element { |
|
613 | 613 | self.visit = idx; |
|
614 | 614 | } else { |
|
615 | 615 | self.done = true; |
|
616 | 616 | } |
|
617 | 617 | |
|
618 | 618 | Some(NodeTreeVisitItem { |
|
619 | 619 | block_idx: visit, |
|
620 | 620 | nybble, |
|
621 | 621 | element, |
|
622 | 622 | }) |
|
623 | 623 | } |
|
624 | 624 | } |
|
625 | 625 | |
|
626 | 626 | impl NodeTreeVisitItem { |
|
627 | 627 | // Return `Some(opt)` if this item is final, with `opt` being the |
|
628 | 628 | // `Revision` that it may represent. |
|
629 | 629 | // |
|
630 | 630 | // If the item is not terminal, return `None` |
|
631 | 631 | fn final_revision(&self) -> Option<Option<Revision>> { |
|
632 | 632 | match self.element { |
|
633 | 633 | Element::Block(_) => None, |
|
634 | 634 | Element::Rev(r) => Some(Some(r)), |
|
635 | 635 | Element::None => Some(None), |
|
636 | 636 | } |
|
637 | 637 | } |
|
638 | 638 | } |
|
639 | 639 | |
|
640 | 640 | impl From<Vec<Block>> for NodeTree { |
|
641 | 641 | fn from(vec: Vec<Block>) -> Self { |
|
642 | 642 | Self::new(Box::new(vec)) |
|
643 | 643 | } |
|
644 | 644 | } |
|
645 | 645 | |
|
646 | 646 | impl fmt::Debug for NodeTree { |
|
647 | 647 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
648 | 648 | let readonly: &[Block] = &*self.readonly; |
|
649 | 649 | write!( |
|
650 | 650 | f, |
|
651 | 651 | "readonly: {:?}, growable: {:?}, root: {:?}", |
|
652 | 652 | readonly, self.growable, self.root |
|
653 | 653 | ) |
|
654 | 654 | } |
|
655 | 655 | } |
|
656 | 656 | |
|
657 | 657 | impl Default for NodeTree { |
|
658 | 658 | /// Create a fully mutable empty NodeTree |
|
659 | 659 | fn default() -> Self { |
|
660 | 660 | NodeTree::new(Box::new(Vec::new())) |
|
661 | 661 | } |
|
662 | 662 | } |
|
663 | 663 | |
|
664 | 664 | impl NodeMap for NodeTree { |
|
665 | 665 | fn find_bin<'a>( |
|
666 | 666 | &self, |
|
667 | 667 | idx: &impl RevlogIndex, |
|
668 | 668 | prefix: NodePrefix, |
|
669 | 669 | ) -> Result<Option<Revision>, NodeMapError> { |
|
670 | 670 | validate_candidate(idx, prefix, self.lookup(prefix)?) |
|
671 | 671 | .map(|(opt, _shortest)| opt) |
|
672 | 672 | } |
|
673 | 673 | |
|
674 | 674 | fn unique_prefix_len_bin<'a>( |
|
675 | 675 | &self, |
|
676 | 676 | idx: &impl RevlogIndex, |
|
677 | 677 | prefix: NodePrefix, |
|
678 | 678 | ) -> Result<Option<usize>, NodeMapError> { |
|
679 | 679 | validate_candidate(idx, prefix, self.lookup(prefix)?) |
|
680 | 680 | .map(|(opt, shortest)| opt.map(|_rev| shortest)) |
|
681 | 681 | } |
|
682 | 682 | } |
|
683 | 683 | |
|
684 | 684 | #[cfg(test)] |
|
685 | 685 | mod tests { |
|
686 | 686 | use super::NodeMapError::*; |
|
687 | 687 | use super::*; |
|
688 | 688 | use crate::revlog::node::{hex_pad_right, Node}; |
|
689 | 689 | use std::collections::HashMap; |
|
690 | 690 | |
|
691 | 691 | /// Creates a `Block` using a syntax close to the `Debug` output |
|
692 | 692 | macro_rules! block { |
|
693 | 693 | {$($nybble:tt : $variant:ident($val:tt)),*} => ( |
|
694 | 694 | { |
|
695 | 695 | let mut block = Block::new(); |
|
696 | 696 | $(block.set($nybble, Element::$variant($val)));*; |
|
697 | 697 | block |
|
698 | 698 | } |
|
699 | 699 | ) |
|
700 | 700 | } |
|
701 | 701 | |
|
702 | 702 | #[test] |
|
703 | 703 | fn test_block_debug() { |
|
704 | 704 | let mut block = Block::new(); |
|
705 | 705 | block.set(1, Element::Rev(3)); |
|
706 | 706 | block.set(10, Element::Block(0)); |
|
707 | 707 | assert_eq!(format!("{:?}", block), "{1: Rev(3), 10: Block(0)}"); |
|
708 | 708 | } |
|
709 | 709 | |
|
710 | 710 | #[test] |
|
711 | 711 | fn test_block_macro() { |
|
712 | 712 | let block = block! {5: Block(2)}; |
|
713 | 713 | assert_eq!(format!("{:?}", block), "{5: Block(2)}"); |
|
714 | 714 | |
|
715 | 715 | let block = block! {13: Rev(15), 5: Block(2)}; |
|
716 | 716 | assert_eq!(format!("{:?}", block), "{5: Block(2), 13: Rev(15)}"); |
|
717 | 717 | } |
|
718 | 718 | |
|
719 | 719 | #[test] |
|
720 | 720 | fn test_raw_block() { |
|
721 | 721 | let mut raw = [255u8; 64]; |
|
722 | 722 | |
|
723 | 723 | let mut counter = 0; |
|
724 | 724 | for val in [0_i32, 15, -2, -1, -3].iter() { |
|
725 | 725 | for byte in val.to_be_bytes().iter() { |
|
726 | 726 | raw[counter] = *byte; |
|
727 | 727 | counter += 1; |
|
728 | 728 | } |
|
729 | 729 | } |
|
730 | 730 | let (block, _) = Block::from_bytes(&raw).unwrap(); |
|
731 | 731 | assert_eq!(block.get(0), Element::Block(0)); |
|
732 | 732 | assert_eq!(block.get(1), Element::Block(15)); |
|
733 | 733 | assert_eq!(block.get(3), Element::None); |
|
734 | 734 | assert_eq!(block.get(2), Element::Rev(0)); |
|
735 | 735 | assert_eq!(block.get(4), Element::Rev(1)); |
|
736 | 736 | } |
|
737 | 737 | |
|
738 | 738 | type TestIndex = HashMap<Revision, Node>; |
|
739 | 739 | |
|
740 | 740 | impl RevlogIndex for TestIndex { |
|
741 | 741 | fn node(&self, rev: Revision) -> Option<&Node> { |
|
742 | 742 | self.get(&rev) |
|
743 | 743 | } |
|
744 | 744 | |
|
745 | 745 | fn len(&self) -> usize { |
|
746 | 746 | self.len() |
|
747 | 747 | } |
|
748 | 748 | } |
|
749 | 749 | |
|
750 | 750 | /// Pad hexadecimal Node prefix with zeros on the right |
|
751 | 751 | /// |
|
752 | 752 | /// This avoids having to repeatedly write very long hexadecimal |
|
753 | 753 | /// strings for test data, and brings actual hash size independency. |
|
754 | 754 | #[cfg(test)] |
|
755 | 755 | fn pad_node(hex: &str) -> Node { |
|
756 | 756 | Node::from_hex(&hex_pad_right(hex)).unwrap() |
|
757 | 757 | } |
|
758 | 758 | |
|
759 | 759 | /// Pad hexadecimal Node prefix with zeros on the right, then insert |
|
760 | 760 | fn pad_insert(idx: &mut TestIndex, rev: Revision, hex: &str) { |
|
761 | 761 | idx.insert(rev, pad_node(hex)); |
|
762 | 762 | } |
|
763 | 763 | |
|
764 | 764 | fn sample_nodetree() -> NodeTree { |
|
765 | 765 | NodeTree::from(vec![ |
|
766 | 766 | block![0: Rev(9)], |
|
767 | 767 | block![0: Rev(0), 1: Rev(9)], |
|
768 | 768 | block![0: Block(1), 1:Rev(1)], |
|
769 | 769 | ]) |
|
770 | 770 | } |
|
771 | 771 | |
|
772 | 772 | fn hex(s: &str) -> NodePrefix { |
|
773 | 773 | NodePrefix::from_hex(s).unwrap() |
|
774 | 774 | } |
|
775 | 775 | |
|
776 | 776 | #[test] |
|
777 | 777 | fn test_nt_debug() { |
|
778 | 778 | let nt = sample_nodetree(); |
|
779 | 779 | assert_eq!( |
|
780 | 780 | format!("{:?}", nt), |
|
781 | 781 | "readonly: \ |
|
782 | 782 | [{0: Rev(9)}, {0: Rev(0), 1: Rev(9)}, {0: Block(1), 1: Rev(1)}], \ |
|
783 | 783 | growable: [], \ |
|
784 | 784 | root: {0: Block(1), 1: Rev(1)}", |
|
785 | 785 | ); |
|
786 | 786 | } |
|
787 | 787 | |
|
788 | 788 | #[test] |
|
789 | 789 | fn test_immutable_find_simplest() -> Result<(), NodeMapError> { |
|
790 | 790 | let mut idx: TestIndex = HashMap::new(); |
|
791 | 791 | pad_insert(&mut idx, 1, "1234deadcafe"); |
|
792 | 792 | |
|
793 | 793 | let nt = NodeTree::from(vec![block! {1: Rev(1)}]); |
|
794 | 794 | assert_eq!(nt.find_bin(&idx, hex("1"))?, Some(1)); |
|
795 | 795 | assert_eq!(nt.find_bin(&idx, hex("12"))?, Some(1)); |
|
796 | 796 | assert_eq!(nt.find_bin(&idx, hex("1234de"))?, Some(1)); |
|
797 | 797 | assert_eq!(nt.find_bin(&idx, hex("1a"))?, None); |
|
798 | 798 | assert_eq!(nt.find_bin(&idx, hex("ab"))?, None); |
|
799 | 799 | |
|
800 | 800 | // and with full binary Nodes |
|
801 | 801 | assert_eq!(nt.find_node(&idx, idx.get(&1).unwrap())?, Some(1)); |
|
802 | 802 | let unknown = Node::from_hex(&hex_pad_right("3d")).unwrap(); |
|
803 | 803 | assert_eq!(nt.find_node(&idx, &unknown)?, None); |
|
804 | 804 | Ok(()) |
|
805 | 805 | } |
|
806 | 806 | |
|
807 | 807 | #[test] |
|
808 | 808 | fn test_immutable_find_one_jump() { |
|
809 | 809 | let mut idx = TestIndex::new(); |
|
810 | 810 | pad_insert(&mut idx, 9, "012"); |
|
811 | 811 | pad_insert(&mut idx, 0, "00a"); |
|
812 | 812 | |
|
813 | 813 | let nt = sample_nodetree(); |
|
814 | 814 | |
|
815 | 815 | assert_eq!(nt.find_bin(&idx, hex("0")), Err(MultipleResults)); |
|
816 | 816 | assert_eq!(nt.find_bin(&idx, hex("01")), Ok(Some(9))); |
|
817 | 817 | assert_eq!(nt.find_bin(&idx, hex("00")), Err(MultipleResults)); |
|
818 | 818 | assert_eq!(nt.find_bin(&idx, hex("00a")), Ok(Some(0))); |
|
819 | 819 | assert_eq!(nt.unique_prefix_len_bin(&idx, hex("00a")), Ok(Some(3))); |
|
820 | 820 | assert_eq!(nt.find_bin(&idx, hex("000")), Ok(Some(NULL_REVISION))); |
|
821 | 821 | } |
|
822 | 822 | |
|
823 | 823 | #[test] |
|
824 | 824 | fn test_mutated_find() -> Result<(), NodeMapError> { |
|
825 | 825 | let mut idx = TestIndex::new(); |
|
826 | 826 | pad_insert(&mut idx, 9, "012"); |
|
827 | 827 | pad_insert(&mut idx, 0, "00a"); |
|
828 | 828 | pad_insert(&mut idx, 2, "cafe"); |
|
829 | 829 | pad_insert(&mut idx, 3, "15"); |
|
830 | 830 | pad_insert(&mut idx, 1, "10"); |
|
831 | 831 | |
|
832 | 832 | let nt = NodeTree { |
|
833 | 833 | readonly: sample_nodetree().readonly, |
|
834 | 834 | growable: vec![block![0: Rev(1), 5: Rev(3)]], |
|
835 | 835 | root: block![0: Block(1), 1:Block(3), 12: Rev(2)], |
|
836 | 836 | masked_inner_blocks: 1, |
|
837 | 837 | }; |
|
838 | 838 | assert_eq!(nt.find_bin(&idx, hex("10"))?, Some(1)); |
|
839 | 839 | assert_eq!(nt.find_bin(&idx, hex("c"))?, Some(2)); |
|
840 | 840 | assert_eq!(nt.unique_prefix_len_bin(&idx, hex("c"))?, Some(1)); |
|
841 | 841 | assert_eq!(nt.find_bin(&idx, hex("00")), Err(MultipleResults)); |
|
842 | 842 | assert_eq!(nt.find_bin(&idx, hex("000"))?, Some(NULL_REVISION)); |
|
843 | 843 | assert_eq!(nt.unique_prefix_len_bin(&idx, hex("000"))?, Some(3)); |
|
844 | 844 | assert_eq!(nt.find_bin(&idx, hex("01"))?, Some(9)); |
|
845 | 845 | assert_eq!(nt.masked_readonly_blocks(), 2); |
|
846 | 846 | Ok(()) |
|
847 | 847 | } |
|
848 | 848 | |
|
849 | 849 | struct TestNtIndex { |
|
850 | 850 | index: TestIndex, |
|
851 | 851 | nt: NodeTree, |
|
852 | 852 | } |
|
853 | 853 | |
|
854 | 854 | impl TestNtIndex { |
|
855 | 855 | fn new() -> Self { |
|
856 | 856 | TestNtIndex { |
|
857 | 857 | index: HashMap::new(), |
|
858 | 858 | nt: NodeTree::default(), |
|
859 | 859 | } |
|
860 | 860 | } |
|
861 | 861 | |
|
862 | 862 | fn insert( |
|
863 | 863 | &mut self, |
|
864 | 864 | rev: Revision, |
|
865 | 865 | hex: &str, |
|
866 | 866 | ) -> Result<(), NodeMapError> { |
|
867 | 867 | let node = pad_node(hex); |
|
868 | 868 | self.index.insert(rev, node.clone()); |
|
869 | 869 | self.nt.insert(&self.index, &node, rev)?; |
|
870 | 870 | Ok(()) |
|
871 | 871 | } |
|
872 | 872 | |
|
873 | 873 | fn find_hex( |
|
874 | 874 | &self, |
|
875 | 875 | prefix: &str, |
|
876 | 876 | ) -> Result<Option<Revision>, NodeMapError> { |
|
877 | 877 | self.nt.find_bin(&self.index, hex(prefix)) |
|
878 | 878 | } |
|
879 | 879 | |
|
880 | 880 | fn unique_prefix_len_hex( |
|
881 | 881 | &self, |
|
882 | 882 | prefix: &str, |
|
883 | 883 | ) -> Result<Option<usize>, NodeMapError> { |
|
884 | 884 | self.nt.unique_prefix_len_bin(&self.index, hex(prefix)) |
|
885 | 885 | } |
|
886 | 886 | |
|
887 | 887 | /// Drain `added` and restart a new one |
|
888 | 888 | fn commit(self) -> Self { |
|
889 | 889 | let mut as_vec: Vec<Block> = |
|
890 | 890 | self.nt.readonly.iter().map(|block| block.clone()).collect(); |
|
891 | 891 | as_vec.extend(self.nt.growable); |
|
892 | 892 | as_vec.push(self.nt.root); |
|
893 | 893 | |
|
894 | 894 | Self { |
|
895 | 895 | index: self.index, |
|
896 | 896 | nt: NodeTree::from(as_vec).into(), |
|
897 | 897 | } |
|
898 | 898 | } |
|
899 | 899 | } |
|
900 | 900 | |
|
901 | 901 | #[test] |
|
902 | 902 | fn test_insert_full_mutable() -> Result<(), NodeMapError> { |
|
903 | 903 | let mut idx = TestNtIndex::new(); |
|
904 | 904 | idx.insert(0, "1234")?; |
|
905 | 905 | assert_eq!(idx.find_hex("1")?, Some(0)); |
|
906 | 906 | assert_eq!(idx.find_hex("12")?, Some(0)); |
|
907 | 907 | |
|
908 | 908 | // let's trigger a simple split |
|
909 | 909 | idx.insert(1, "1a34")?; |
|
910 | 910 | assert_eq!(idx.nt.growable.len(), 1); |
|
911 | 911 | assert_eq!(idx.find_hex("12")?, Some(0)); |
|
912 | 912 | assert_eq!(idx.find_hex("1a")?, Some(1)); |
|
913 | 913 | |
|
914 | 914 | // reinserting is a no_op |
|
915 | 915 | idx.insert(1, "1a34")?; |
|
916 | 916 | assert_eq!(idx.nt.growable.len(), 1); |
|
917 | 917 | assert_eq!(idx.find_hex("12")?, Some(0)); |
|
918 | 918 | assert_eq!(idx.find_hex("1a")?, Some(1)); |
|
919 | 919 | |
|
920 | 920 | idx.insert(2, "1a01")?; |
|
921 | 921 | assert_eq!(idx.nt.growable.len(), 2); |
|
922 | 922 | assert_eq!(idx.find_hex("1a"), Err(NodeMapError::MultipleResults)); |
|
923 | 923 | assert_eq!(idx.find_hex("12")?, Some(0)); |
|
924 | 924 | assert_eq!(idx.find_hex("1a3")?, Some(1)); |
|
925 | 925 | assert_eq!(idx.find_hex("1a0")?, Some(2)); |
|
926 | 926 | assert_eq!(idx.find_hex("1a12")?, None); |
|
927 | 927 | |
|
928 | 928 | // now let's make it split and create more than one additional block |
|
929 | 929 | idx.insert(3, "1a345")?; |
|
930 | 930 | assert_eq!(idx.nt.growable.len(), 4); |
|
931 | 931 | assert_eq!(idx.find_hex("1a340")?, Some(1)); |
|
932 | 932 | assert_eq!(idx.find_hex("1a345")?, Some(3)); |
|
933 | 933 | assert_eq!(idx.find_hex("1a341")?, None); |
|
934 | 934 | |
|
935 | 935 | // there's no readonly block to mask |
|
936 | 936 | assert_eq!(idx.nt.masked_readonly_blocks(), 0); |
|
937 | 937 | Ok(()) |
|
938 | 938 | } |
|
939 | 939 | |
|
940 | 940 | #[test] |
|
941 | 941 | fn test_unique_prefix_len_zero_prefix() { |
|
942 | 942 | let mut idx = TestNtIndex::new(); |
|
943 | 943 | idx.insert(0, "00000abcd").unwrap(); |
|
944 | 944 | |
|
945 | 945 | assert_eq!(idx.find_hex("000"), Err(NodeMapError::MultipleResults)); |
|
946 | 946 | // in the nodetree proper, this will be found at the first nybble |
|
947 | 947 | // yet the correct answer for unique_prefix_len is not 1, nor 1+1, |
|
948 | 948 | // but the first difference with `NULL_NODE` |
|
949 | 949 | assert_eq!(idx.unique_prefix_len_hex("00000a"), Ok(Some(6))); |
|
950 | 950 | assert_eq!(idx.unique_prefix_len_hex("00000ab"), Ok(Some(6))); |
|
951 | 951 | |
|
952 | 952 | // same with odd result |
|
953 | 953 | idx.insert(1, "00123").unwrap(); |
|
954 | 954 | assert_eq!(idx.unique_prefix_len_hex("001"), Ok(Some(3))); |
|
955 | 955 | assert_eq!(idx.unique_prefix_len_hex("0012"), Ok(Some(3))); |
|
956 | 956 | |
|
957 | 957 | // these are unchanged of course |
|
958 | 958 | assert_eq!(idx.unique_prefix_len_hex("00000a"), Ok(Some(6))); |
|
959 | 959 | assert_eq!(idx.unique_prefix_len_hex("00000ab"), Ok(Some(6))); |
|
960 | 960 | } |
|
961 | 961 | |
|
962 | 962 | #[test] |
|
963 | 963 | fn test_insert_extreme_splitting() -> Result<(), NodeMapError> { |
|
964 | 964 | // check that the splitting loop is long enough |
|
965 | 965 | let mut nt_idx = TestNtIndex::new(); |
|
966 | 966 | let nt = &mut nt_idx.nt; |
|
967 | 967 | let idx = &mut nt_idx.index; |
|
968 | 968 | |
|
969 | 969 | let node0_hex = hex_pad_right("444444"); |
|
970 | 970 | let mut node1_hex = hex_pad_right("444444").clone(); |
|
971 | 971 | node1_hex.pop(); |
|
972 | 972 | node1_hex.push('5'); |
|
973 | 973 | let node0 = Node::from_hex(&node0_hex).unwrap(); |
|
974 | 974 | let node1 = Node::from_hex(&node1_hex).unwrap(); |
|
975 | 975 | |
|
976 | 976 | idx.insert(0, node0.clone()); |
|
977 | 977 | nt.insert(idx, &node0, 0)?; |
|
978 | 978 | idx.insert(1, node1.clone()); |
|
979 | 979 | nt.insert(idx, &node1, 1)?; |
|
980 | 980 | |
|
981 | 981 | assert_eq!(nt.find_bin(idx, (&node0).into())?, Some(0)); |
|
982 | 982 | assert_eq!(nt.find_bin(idx, (&node1).into())?, Some(1)); |
|
983 | 983 | Ok(()) |
|
984 | 984 | } |
|
985 | 985 | |
|
986 | 986 | #[test] |
|
987 | 987 | fn test_insert_partly_immutable() -> Result<(), NodeMapError> { |
|
988 | 988 | let mut idx = TestNtIndex::new(); |
|
989 | 989 | idx.insert(0, "1234")?; |
|
990 | 990 | idx.insert(1, "1235")?; |
|
991 | 991 | idx.insert(2, "131")?; |
|
992 | 992 | idx.insert(3, "cafe")?; |
|
993 | 993 | let mut idx = idx.commit(); |
|
994 | 994 | assert_eq!(idx.find_hex("1234")?, Some(0)); |
|
995 | 995 | assert_eq!(idx.find_hex("1235")?, Some(1)); |
|
996 | 996 | assert_eq!(idx.find_hex("131")?, Some(2)); |
|
997 | 997 | assert_eq!(idx.find_hex("cafe")?, Some(3)); |
|
998 | 998 | // we did not add anything since init from readonly |
|
999 | 999 | assert_eq!(idx.nt.masked_readonly_blocks(), 0); |
|
1000 | 1000 | |
|
1001 | 1001 | idx.insert(4, "123A")?; |
|
1002 | 1002 | assert_eq!(idx.find_hex("1234")?, Some(0)); |
|
1003 | 1003 | assert_eq!(idx.find_hex("1235")?, Some(1)); |
|
1004 | 1004 | assert_eq!(idx.find_hex("131")?, Some(2)); |
|
1005 | 1005 | assert_eq!(idx.find_hex("cafe")?, Some(3)); |
|
1006 | 1006 | assert_eq!(idx.find_hex("123A")?, Some(4)); |
|
1007 | 1007 | // we masked blocks for all prefixes of "123", including the root |
|
1008 | 1008 | assert_eq!(idx.nt.masked_readonly_blocks(), 4); |
|
1009 | 1009 | |
|
1010 | 1010 | eprintln!("{:?}", idx.nt); |
|
1011 | 1011 | idx.insert(5, "c0")?; |
|
1012 | 1012 | assert_eq!(idx.find_hex("cafe")?, Some(3)); |
|
1013 | 1013 | assert_eq!(idx.find_hex("c0")?, Some(5)); |
|
1014 | 1014 | assert_eq!(idx.find_hex("c1")?, None); |
|
1015 | 1015 | assert_eq!(idx.find_hex("1234")?, Some(0)); |
|
1016 | 1016 | // inserting "c0" is just splitting the 'c' slot of the mutable root, |
|
1017 | 1017 | // it doesn't mask anything |
|
1018 | 1018 | assert_eq!(idx.nt.masked_readonly_blocks(), 4); |
|
1019 | 1019 | |
|
1020 | 1020 | Ok(()) |
|
1021 | 1021 | } |
|
1022 | 1022 | |
|
1023 | 1023 | #[test] |
|
1024 | 1024 | fn test_invalidate_all() -> Result<(), NodeMapError> { |
|
1025 | 1025 | let mut idx = TestNtIndex::new(); |
|
1026 | 1026 | idx.insert(0, "1234")?; |
|
1027 | 1027 | idx.insert(1, "1235")?; |
|
1028 | 1028 | idx.insert(2, "131")?; |
|
1029 | 1029 | idx.insert(3, "cafe")?; |
|
1030 | 1030 | let mut idx = idx.commit(); |
|
1031 | 1031 | |
|
1032 | 1032 | idx.nt.invalidate_all(); |
|
1033 | 1033 | |
|
1034 | 1034 | assert_eq!(idx.find_hex("1234")?, None); |
|
1035 | 1035 | assert_eq!(idx.find_hex("1235")?, None); |
|
1036 | 1036 | assert_eq!(idx.find_hex("131")?, None); |
|
1037 | 1037 | assert_eq!(idx.find_hex("cafe")?, None); |
|
1038 | 1038 | // all the readonly blocks have been masked, this is the |
|
1039 | 1039 | // conventional expected response |
|
1040 | 1040 | assert_eq!(idx.nt.masked_readonly_blocks(), idx.nt.readonly.len() + 1); |
|
1041 | 1041 | Ok(()) |
|
1042 | 1042 | } |
|
1043 | 1043 | |
|
1044 | 1044 | #[test] |
|
1045 | 1045 | fn test_into_added_empty() { |
|
1046 | 1046 | assert!(sample_nodetree().into_readonly_and_added().1.is_empty()); |
|
1047 | 1047 | assert!(sample_nodetree() |
|
1048 | 1048 | .into_readonly_and_added_bytes() |
|
1049 | 1049 | .1 |
|
1050 | 1050 | .is_empty()); |
|
1051 | 1051 | } |
|
1052 | 1052 | |
|
1053 | 1053 | #[test] |
|
1054 | 1054 | fn test_into_added_bytes() -> Result<(), NodeMapError> { |
|
1055 | 1055 | let mut idx = TestNtIndex::new(); |
|
1056 | 1056 | idx.insert(0, "1234")?; |
|
1057 | 1057 | let mut idx = idx.commit(); |
|
1058 | 1058 | idx.insert(4, "cafe")?; |
|
1059 | 1059 | let (_, bytes) = idx.nt.into_readonly_and_added_bytes(); |
|
1060 | 1060 | |
|
1061 | 1061 | // only the root block has been changed |
|
1062 | 1062 | assert_eq!(bytes.len(), size_of::<Block>()); |
|
1063 | 1063 | // big endian for -2 |
|
1064 | 1064 | assert_eq!(&bytes[4..2 * 4], [255, 255, 255, 254]); |
|
1065 | 1065 | // big endian for -6 |
|
1066 | 1066 | assert_eq!(&bytes[12 * 4..13 * 4], [255, 255, 255, 250]); |
|
1067 | 1067 | Ok(()) |
|
1068 | 1068 | } |
|
1069 | 1069 | } |
General Comments 0
You need to be logged in to leave comments.
Login now