diff --git a/src/core/global.rs b/src/core/global.rs index cf6fb4410..299230b03 100644 --- a/src/core/global.rs +++ b/src/core/global.rs @@ -6,7 +6,3 @@ pub type FieldId = u32; #[derive(Clone,Debug,PartialEq,PartialOrd,Eq,Hash)] pub struct Field(pub FieldId); - -// pub trait SeekableIterator: Iterator { -// pub fn seek(&mut self, el: &T) -> bool; -// } diff --git a/src/core/serial.rs b/src/core/serial.rs index 370f421b1..452f2c2ae 100644 --- a/src/core/serial.rs +++ b/src/core/serial.rs @@ -11,12 +11,10 @@ pub trait DocCursor: Iterator { fn doc(&self) -> DocId; } - // TODO make iteration over Fields somehow sorted // (Not only forms) -pub trait TermCursor<'a> { + +pub trait TermCursor<'a> { type DocCur: DocCursor; - fn advance(&mut self,) -> bool; - fn get_term(&self) -> Term<'a>; - fn doc_cursor(&self) -> Self::DocCur; + fn next(&mut self,) -> Option<(Term<'a>, Self::DocCur)>; } diff --git a/src/core/writer.rs b/src/core/writer.rs index 033fd5acc..69433e7ed 100644 --- a/src/core/writer.rs +++ b/src/core/writer.rs @@ -160,6 +160,40 @@ pub struct CIWTermCursor<'a> { impl<'a> CIWTermCursor<'a> { + fn advance(&mut self,) -> bool { + let next_form = self.next_form(); + if next_form { + true + } + else { + if self.next_field() { + self.advance() + } + else { + false + } + } + } + + fn get_term(&self) -> Term<'a> { + Term { + field: self.field.clone(), + text: self.current_form_postings.as_ref().unwrap().form, + } + } + + fn doc_cursor(&self,) -> CIWDocCursor<'a> { + CIWDocCursor { + docs_it: self.current_form_postings + .as_ref() + .unwrap() + .postings + .doc_ids + .iter(), + current: None + } + } + fn next_form(&mut self,) -> bool { match self.form_it.next() { @@ -189,41 +223,17 @@ impl<'a> CIWTermCursor<'a> { } } + impl<'a> TermCursor<'a> for CIWTermCursor<'a> { type DocCur = CIWDocCursor<'a>; - fn get_term(&self) -> Term<'a> { - Term { - field: self.field.clone(), - text: self.current_form_postings.as_ref().unwrap().form, - } - } - - fn doc_cursor(&self,) -> CIWDocCursor<'a> { - CIWDocCursor { - docs_it: self.current_form_postings - .as_ref() - .unwrap() - .postings - .doc_ids - .iter(), - current: None - } - } - - fn advance(&mut self,) -> bool { - let next_form = self.next_form(); - if next_form { - true + fn next(&mut self,) -> Option<(Term<'a>, CIWDocCursor<'a>)> { + if self.advance() { + Some((self.get_term(), self.doc_cursor())) } else { - if self.next_field() { - self.advance() - } - else { - false - } + None } } } @@ -231,7 +241,6 @@ impl<'a> TermCursor<'a> for CIWTermCursor<'a> { // // TODO use a Term type // - impl<'a> SerializableSegment<'a> for ClosedIndexWriter { type TermCur = CIWTermCursor<'a>; diff --git a/tests/core.rs b/tests/core.rs index ad21d0eb7..4e6545bf6 100644 --- a/tests/core.rs +++ b/tests/core.rs @@ -51,16 +51,22 @@ fn test_indexing() { { let mut doc = Document::new(); doc.set(Field(1), "a b c d"); - // TODO make iteration over Fields somehow sorted index_writer.add(doc); } let mut closed_index_writer: ClosedIndexWriter = index_writer.close(); let mut term_cursor = closed_index_writer.term_cursor(); loop { - if !term_cursor.advance() { - break; + match term_cursor.next() { + Some((term, doc_it)) => { + println!("{:?}", term); + for doc in doc_it { + println!(" doc {}", doc); + } + }, + None => { + break; + } } - show_term(&term_cursor); } assert!(false); } @@ -71,28 +77,6 @@ fn test_indexing() { } -fn show_term<'a, T: TermCursor<'a>>(term_cursor: &T) { - println!("{:?}", term_cursor.get_term()); - let doc_cursor = term_cursor.doc_cursor(); - for doc in doc_cursor { - println!("doc({})", doc); - } -} - -// fn show_doc_cursor<'a, D: DocCursor>(mut doc_cursor: D) { -// loop { -// match doc_cursor.next() { -// Some(doc) => { -// println!(" {}", doc); -// }, -// None => { -// break; -// } -// } -// } -// } - - #[test] fn test_new_segment() { let SegmentId(segment_name) = generate_segment_name();