mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2026-05-19 17:50:42 +00:00
various cleanup
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
use core::serial::*;
|
||||
use core::directory::WritePtr;
|
||||
use fst::MapBuilder;
|
||||
use std::io;
|
||||
use std::io::Error as IOError;
|
||||
use std::io::ErrorKind as IOErrorKind;
|
||||
use byteorder::BigEndian;
|
||||
use core::index::Segment;
|
||||
use core::index::SegmentComponent;
|
||||
@@ -63,7 +60,7 @@ impl SegmentSerializer<()> for SimpleSegmentSerializer {
|
||||
self.store_writer.store(&field_values);
|
||||
}
|
||||
|
||||
fn new_term(&mut self, term: &Term, doc_freq: DocId) -> Result<(), IOError> {
|
||||
fn new_term(&mut self, term: &Term, doc_freq: DocId) -> io::Result<()> {
|
||||
let term_info = TermInfo {
|
||||
doc_freq: doc_freq,
|
||||
postings_offset: self.written_bytes_postings as u32,
|
||||
@@ -73,7 +70,7 @@ impl SegmentSerializer<()> for SimpleSegmentSerializer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_docs(&mut self, doc_ids: &[DocId]) -> Result<(), IOError> {
|
||||
fn write_docs(&mut self, doc_ids: &[DocId]) -> io::Result<()> {
|
||||
// TODO write_all transmuted [u8]
|
||||
let docs_data = self.encoder.encode_sorted(doc_ids);
|
||||
self.written_bytes_postings += try!((docs_data.len() as u32).serialize(&mut self.postings_write));
|
||||
@@ -83,7 +80,7 @@ impl SegmentSerializer<()> for SimpleSegmentSerializer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn close(mut self,) -> Result<(), IOError> {
|
||||
fn close(mut self,) -> io::Result<()> {
|
||||
// TODO handle errors on close
|
||||
try!(self.term_fst_builder
|
||||
.finish());
|
||||
@@ -95,7 +92,7 @@ impl SimpleCodec {
|
||||
// TODO impl packed int
|
||||
// TODO skip lists
|
||||
// TODO make that part of the codec API
|
||||
pub fn serializer(segment: &Segment) -> Result<SimpleSegmentSerializer, IOError> {
|
||||
pub fn serializer(segment: &Segment) -> io::Result<SimpleSegmentSerializer> {
|
||||
let term_write = try!(segment.open_write(SegmentComponent::TERMS));
|
||||
let postings_write = try!(segment.open_write(SegmentComponent::POSTINGS));
|
||||
let store_write = try!(segment.open_write(SegmentComponent::STORE));
|
||||
@@ -112,7 +109,7 @@ impl SimpleCodec {
|
||||
}
|
||||
|
||||
|
||||
pub fn write<I: SerializableSegment>(index: &I, segment: &Segment) -> Result<(), IOError> {
|
||||
pub fn write<I: SerializableSegment>(index: &I, segment: &Segment) -> io::Result<()> {
|
||||
let serializer = try!(SimpleCodec::serializer(segment));
|
||||
index.write(serializer)
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ impl MmapDirectory {
|
||||
// TODO error management
|
||||
let tempdir = try!(TempDir::new("index"));
|
||||
let tempdir_path = PathBuf::from(tempdir.path());
|
||||
let mut directory = MmapDirectory {
|
||||
let directory = MmapDirectory {
|
||||
root_path: PathBuf::from(tempdir_path),
|
||||
mmap_cache: RefCell::new(HashMap::new()),
|
||||
_temp_directory: Some(tempdir)
|
||||
|
||||
@@ -59,10 +59,6 @@ pub struct Index {
|
||||
directory: Arc<RwLock<DirectoryPtr>>,
|
||||
}
|
||||
|
||||
fn could_not_acquire_lock<E>(_: E) -> io::Error {
|
||||
io::Error::new(IOErrorKind::Other, "Could not acquire read lock on directory")
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref META_FILEPATH: PathBuf = PathBuf::from("meta.json");
|
||||
}
|
||||
@@ -184,13 +180,12 @@ impl Index {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save_metas(&self,) -> io::Result<()> {
|
||||
let metas_lock = self.metas.read().unwrap();
|
||||
let encoded = json::encode(&*metas_lock).unwrap();
|
||||
try!(self.directory
|
||||
.write()
|
||||
.map_err(could_not_acquire_lock)
|
||||
).atomic_write(&META_FILEPATH, encoded.as_bytes())
|
||||
pub fn save_metas(&mut self,) -> io::Result<()> {
|
||||
let encoded = {
|
||||
let metas_lock = self.metas.read().unwrap();
|
||||
json::encode(&*metas_lock).unwrap()
|
||||
};
|
||||
try!(self.rw_directory()).atomic_write(&META_FILEPATH, encoded.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,15 +4,12 @@ use core::store::StoreReader;
|
||||
use core::schema::Document;
|
||||
use core::postings::IntersectionPostings;
|
||||
use byteorder::{BigEndian, ReadBytesExt};
|
||||
use core::serialize::BinarySerializable;
|
||||
use core::directory::ReadOnlySource;
|
||||
use std::io::Cursor;
|
||||
use core::schema::DocId;
|
||||
use core::index::SegmentComponent;
|
||||
use core::postings::Postings;
|
||||
use core::simdcompression::Decoder;
|
||||
use std::io::Error as IOError;
|
||||
use std::io::ErrorKind;
|
||||
use std::io;
|
||||
use core::codec::TermInfo;
|
||||
use core::fstmap::FstMap;
|
||||
@@ -100,7 +97,7 @@ impl SegmentReader {
|
||||
self.segment.id()
|
||||
}
|
||||
|
||||
pub fn open(segment: Segment) -> Result<SegmentReader, IOError> {
|
||||
pub fn open(segment: Segment) -> io::Result<SegmentReader> {
|
||||
let source = try!(segment.open_read(SegmentComponent::TERMS));
|
||||
let term_offsets = try!(FstMap::from_source(source));
|
||||
let store_reader = StoreReader::new(try!(segment.open_read(SegmentComponent::STORE)));
|
||||
@@ -118,7 +115,7 @@ impl SegmentReader {
|
||||
}
|
||||
|
||||
pub fn read_postings(&self, offset: usize) -> SegmentPostings {
|
||||
let postings_data = unsafe {&self.postings_data.as_slice()[offset..]};
|
||||
let postings_data = &self.postings_data.as_slice()[offset..];
|
||||
SegmentPostings::from_data(&postings_data)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ use core::schema::DocId;
|
||||
use core::schema::Document;
|
||||
use core::collector::Collector;
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use core::schema::Term;
|
||||
use std::io::Error as IOError;
|
||||
use std::io::ErrorKind as IOErrorKind;
|
||||
|
||||
|
||||
pub struct Searcher {
|
||||
segments: Vec<SegmentReader>,
|
||||
@@ -28,7 +28,7 @@ impl Searcher {
|
||||
segment_reader.get_doc(doc_id)
|
||||
}
|
||||
|
||||
fn add_segment(&mut self, segment: Segment) -> Result<(), IOError> {
|
||||
fn add_segment(&mut self, segment: Segment) -> io::Result<()> {
|
||||
SegmentReader::open(segment.clone())
|
||||
.map(|segment_reader| {
|
||||
let segment_ord = self.segments.len();
|
||||
|
||||
@@ -28,7 +28,7 @@ impl fmt::Debug for DebugSegmentSerializer {
|
||||
impl DebugSegmentSerializer {
|
||||
|
||||
pub fn debug_string<S: SerializableSegment>(index: &S) -> String {
|
||||
let mut serializer = DebugSegmentSerializer::new();
|
||||
let serializer = DebugSegmentSerializer::new();
|
||||
index.write(serializer).unwrap()
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ impl DebugSegmentSerializer {
|
||||
|
||||
impl SegmentSerializer<String> for DebugSegmentSerializer {
|
||||
fn new_term(&mut self, term: &Term, doc_freq: DocId) -> Result<(), io::Error> {
|
||||
self.text.push_str(&format!("{:?}\n", term));
|
||||
self.text.push_str(&format!("{:?} - docfreq{}\n", term, doc_freq));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#![feature(test)]
|
||||
#[cfg_attr(test, feature(test))]
|
||||
#[allow(unused_imports)]
|
||||
|
||||
#[macro_use]
|
||||
|
||||
Reference in New Issue
Block a user