This commit is contained in:
Paul Masurel
2016-01-16 17:59:56 +09:00
parent 1ba5bddd7f
commit 67eb27367c
4 changed files with 52 additions and 65 deletions

View File

@@ -6,7 +6,3 @@ pub type FieldId = u32;
#[derive(Clone,Debug,PartialEq,PartialOrd,Eq,Hash)]
pub struct Field(pub FieldId);
// pub trait SeekableIterator<T>: Iterator<T> {
// pub fn seek(&mut self, el: &T) -> bool;
// }

View File

@@ -11,12 +11,10 @@ pub trait DocCursor: Iterator<Item=DocId> {
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)>;
}

View File

@@ -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>;

View File

@@ -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();