Code clean up.

This commit is contained in:
Paul Masurel
2016-08-27 17:00:14 +09:00
parent 5e806c88ef
commit a599614a94
6 changed files with 26 additions and 18 deletions

View File

@@ -62,7 +62,7 @@ impl Index {
pub fn create_in_ram(schema: Schema) -> Index {
let directory = Box::new(RAMDirectory::create());
Index::from_directory(directory, schema).unwrap()
Index::from_directory(directory, schema).unwrap() // unwrap is ok here
}
pub fn create(directory_path: &Path, schema: Schema) -> Result<Index> {
@@ -156,11 +156,15 @@ impl Index {
Ok(())
}
pub fn segments(&self,) -> Vec<Segment> {
self.segment_ids()
pub fn segments(&self,) -> Result<Vec<Segment>> {
let segment_ids = try!(self.segment_ids());
Ok(
segment_ids
.into_iter()
.map(|segment_id| self.segment(segment_id))
.collect()
)
}
pub fn segment(&self, segment_id: SegmentId) -> Segment {
@@ -175,14 +179,17 @@ impl Index {
&mut *self.directory
}
fn segment_ids(&self,) -> Vec<SegmentId> {
self.metas
.read()
.unwrap()
fn segment_ids(&self,) -> Result<Vec<SegmentId>> {
self.metas.read()
.map_err(From::from)
.map(|meta_read| {
meta_read
.segments
.iter()
.cloned()
.collect()
})
}
pub fn new_segment(&self,) -> Segment {
@@ -192,7 +199,7 @@ impl Index {
pub fn save_metas(&mut self,) -> Result<()> {
let mut w = Vec::new();
{
let metas_lock = self.metas.read().unwrap();
let metas_lock = try!(self.metas.read());
try!(write!(&mut w, "{}\n", json::as_pretty_json(&*metas_lock)));
};
self.directory
@@ -203,8 +210,9 @@ impl Index {
pub fn load_searchers(&self,) -> Result<()>{
let res_searchers: Result<Vec<Searcher>> = (0..12)
.map(|_| {
let segments: Vec<Segment> = try!(self.segments());
let segment_readers: Vec<SegmentReader> = try!(
self.segments()
segments
.into_iter()
.map(SegmentReader::open)
.collect()

View File

@@ -182,7 +182,7 @@ impl SegmentReader {
Some(SegmentPostings::from_data(term_info.doc_freq, &postings_data, freq_handler))
}
pub fn read_postings_all_info(&self, term: &Term) -> Option<SegmentPostings> {
pub fn read_postings_all_info(&self, term: &Term) -> SegmentPostings {
let field_entry = self.schema.get_field_entry(term.get_field());
let segment_posting_option = match field_entry.field_type() {
&FieldType::Str(ref text_options) => {
@@ -194,7 +194,7 @@ impl SegmentReader {
}
&FieldType::U32(_) => SegmentPostingsOption::NoFreq
};
self.read_postings(term, segment_posting_option)
self.read_postings(term, segment_posting_option).expect("Read postings all info should not return None")
}
pub fn get_term_info<'a>(&'a self, term: &Term) -> Option<TermInfo> {

View File

@@ -106,7 +106,7 @@ impl<V: BinarySerializable> FstMap<V> {
fn read_value(&self, offset: u64) -> V {
let buffer = self.values_mmap.as_slice();
let mut cursor = Cursor::new(&buffer[(offset as usize)..]);
V::deserialize(&mut cursor).unwrap()
V::deserialize(&mut cursor).expect("Data in FST is corrupted")
}
pub fn get<K: AsRef<[u8]>>(&self, key: K) -> Option<V> {

View File

@@ -93,7 +93,7 @@ impl<'a> PostingsMerger<'a> {
let offset = self.doc_offsets[heap_item.segment_ord];
let reader = &self.readers[heap_item.segment_ord];
let segment_postings = reader.read_postings_all_info(&heap_item.term).unwrap();
let segment_postings = reader.read_postings_all_info(&heap_item.term);
let offset_postings = OffsetPostings::new(segment_postings, offset);
segment_postings_list.push(offset_postings);
}
@@ -111,7 +111,7 @@ impl<'a> PostingsMerger<'a> {
Some(&ref next_heap_it) if next_heap_it.term == heap_it.term => {},
_ => { break; }
}
let next_heap_it = self.heap.pop().unwrap();
let next_heap_it = self.heap.pop().unwrap(); // unwrap is fine here.
self.append_segment(&next_heap_it, &mut segment_postings_list);
}
let chained_posting = ChainedPostings::new(segment_postings_list);
@@ -335,7 +335,7 @@ mod tests {
}
}
{
let segments = index.segments();
let segments = index.segments().unwrap();
let mut index_writer = index.writer_with_num_threads(1).unwrap();
index_writer.merge(&segments).unwrap();
}

View File

@@ -266,7 +266,7 @@ mod tests {
{
let searcher = index.searcher();
let reader = searcher.segment_reader(0);
let mut postings = reader.read_postings_all_info(&Term::from_field_text(text_field, "af")).unwrap();
let mut postings = reader.read_postings_all_info(&Term::from_field_text(text_field, "af"));
assert!(postings.advance());
assert_eq!(postings.doc(), 0);
assert_eq!(postings.term_freq(), 3);

View File

@@ -104,7 +104,7 @@ mod tests {
}
{
let term_a = Term::from_field_text(text_field, "a");
let mut postings_a = segment_reader.read_postings_all_info(&term_a).unwrap();
let mut postings_a = segment_reader.read_postings_all_info(&term_a);
assert_eq!(postings_a.len(), 1000);
assert!(postings_a.advance());
assert_eq!(postings_a.doc(), 0);
@@ -123,7 +123,7 @@ mod tests {
}
{
let term_e = Term::from_field_text(text_field, "e");
let mut postings_e = segment_reader.read_postings_all_info(&term_e).unwrap();
let mut postings_e = segment_reader.read_postings_all_info(&term_e);
assert_eq!(postings_e.len(), 1000 - 2);
for i in 2u32 .. 1000u32 {
assert!(postings_e.advance());