diff options
author | Paul Masurel <paul.masurel@gmail.com> | 2017-02-23 09:48:08 +0900 |
---|---|---|
committer | Paul Masurel <paul.masurel@gmail.com> | 2017-02-23 09:54:54 +0900 |
commit | 503d0295cb3bfa9ef6966cd675a77de00fcd9f03 (patch) | |
tree | c2a9bc6a2df790fdfb7a0d5946b03c82ca5e5088 /src/indexer/doc_opstamp_mapping.rs | |
parent | eb39db44fcf4dfcd0ffda5961a5267386d4a813a (diff) |
issue/43 TODO hunt
Diffstat (limited to 'src/indexer/doc_opstamp_mapping.rs')
-rw-r--r-- | src/indexer/doc_opstamp_mapping.rs | 71 |
1 files changed, 67 insertions, 4 deletions
diff --git a/src/indexer/doc_opstamp_mapping.rs b/src/indexer/doc_opstamp_mapping.rs index 8430024..16eb1ff 100644 --- a/src/indexer/doc_opstamp_mapping.rs +++ b/src/indexer/doc_opstamp_mapping.rs @@ -1,6 +1,22 @@ use std::sync::Arc; use DocId; + +// Doc to opstamp is used to identify which +// document should be deleted. +// +// Since the docset matching the query of a delete operation +// is not computed right when the delete operation is received, +// we need to find a way to evaluate, for each document, +// whether the document was added before or after +// the delete operation. This anteriority is used by comparing +// the docstamp of the document. +// +// The doc to opstamp mapping stores precisely an array +// indexed by doc id and storing the opstamp of the document. +// +// This mapping is (for the moment) stricly increasing +// because of the way document id are allocated. #[derive(Clone)] pub enum DocToOpstampMapping { WithMap(Arc<Vec<u64>>), @@ -13,12 +29,18 @@ impl From<Vec<u64>> for DocToOpstampMapping { } } -impl DocToOpstampMapping { - // TODO Unit test - pub fn compute_doc_limit(&self, opstamp: u64) -> DocId { +impl DocToOpstampMapping { + + /// Given an opstamp return the limit doc id L + /// such that all doc id D such that + // D >= L iff opstamp(D) >= than `target_opstamp`. + // + // The edge case opstamp = some doc opstamp is in practise + // never called. + pub fn compute_doc_limit(&self, target_opstamp: u64) -> DocId { match *self { DocToOpstampMapping::WithMap(ref doc_opstamps) => { - match doc_opstamps.binary_search(&opstamp) { + match doc_opstamps.binary_search(&target_opstamp) { Ok(doc_id) => doc_id as DocId, Err(doc_id) => doc_id as DocId, } @@ -28,3 +50,44 @@ impl DocToOpstampMapping { } } +#[cfg(test)] +mod tests { + + use super::DocToOpstampMapping; + + #[test] + fn test_doc_to_opstamp_mapping_none() { + let doc_to_opstamp_mapping = DocToOpstampMapping::None; + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(1), u32::max_value()); + } + + #[test] + fn test_doc_to_opstamp_mapping_complex() { + { + let doc_to_opstamp_mapping = DocToOpstampMapping::from(vec!()); + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(0u64), 0); + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(2u64), 0); + } + { + let doc_to_opstamp_mapping = DocToOpstampMapping::from(vec!(1u64)); + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(0u64), 0); + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(2u64), 1); + } + { + let doc_to_opstamp_mapping = DocToOpstampMapping::from(vec!(1u64, 12u64, 17u64, 23u64)); + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(0u64), 0); + for i in 2u64..13u64 { + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(i), 1); + } + for i in 13u64..18u64 { + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(i), 2); + } + for i in 18u64..24u64 { + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(i), 3); + } + for i in 24u64..30u64 { + assert_eq!(doc_to_opstamp_mapping.compute_doc_limit(i), 4); + } + } + } +}
\ No newline at end of file |