This commit is contained in:
Paul Masurel
2016-01-20 01:05:24 +09:00
parent 22ebd6cd5c
commit d002ee2aaf
5 changed files with 50 additions and 23 deletions

View File

@@ -4,7 +4,11 @@ use core::schema::Term;
use core::directory::SharedMmapMemory;
use fst::Streamer;
use fst;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::borrow::Borrow;
use std::io::Cursor;
use core::global::DocId;
use core::serial::DocCursor;
pub struct SegmentIndexReader {
segment: Segment,
@@ -12,9 +16,7 @@ pub struct SegmentIndexReader {
postings_data: SharedMmapMemory,
}
impl SegmentIndexReader {
fn term_cursor<'a>(&'a self) -> SegmentTermCur<'a> {
SegmentTermCur {
segment: &self.segment,
@@ -22,13 +24,37 @@ impl SegmentIndexReader {
postings_data: self.postings_data.borrow(),
}
}
}
pub struct SegmentDocCursor<'a> {
postings_data: &'a [u8],
offset: usize,
postings_data: Cursor<&'a [u8]>,
num_docs: DocId,
current_doc: DocId,
}
impl<'a> Iterator for SegmentDocCursor<'a> {
type Item = DocId;
fn next(&mut self) -> Option<DocId> {
if self.num_docs == 0 {
None
}
else {
self.num_docs -= 1;
self.current_doc = self.postings_data.read_u32::<LittleEndian>().unwrap();
Some(self.current_doc)
}
}
}
impl<'a> DocCursor for SegmentDocCursor<'a> {
fn doc(&self) -> DocId{
self.current_doc
}
fn len(&self) -> DocId {
self.num_docs
}
}
struct SegmentTermCur<'a> {
@@ -41,11 +67,16 @@ impl<'a> SegmentTermCur<'a> {
fn next(&mut self,) -> Option<(Term, SegmentDocCursor<'a>)> {
match self.fst_streamer.next() {
Some((k, offset)) => {
Some((k, offset_u64)) => {
let term = Term::from(k);
let offset = offset_u64 as usize;
let data = &self.postings_data[offset..];
let mut cursor = Cursor::new(data);
let num_docs = cursor.read_u32::<LittleEndian>().unwrap();
let doc_cursor = SegmentDocCursor {
postings_data: self.postings_data,
offset: offset as usize,
postings_data: cursor,
num_docs: num_docs,
current_doc: 0,
};
Some((term, doc_cursor))
},