mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2026-10-27 05:56:27 +00:00
renamed directory to index
This commit is contained in:
+45
-46
@@ -31,29 +31,29 @@ pub fn generate_segment_name() -> SegmentId {
|
||||
}
|
||||
|
||||
#[derive(Clone,Debug,RustcDecodable, RustcEncodable)]
|
||||
pub struct DirectoryMeta {
|
||||
pub struct IndexMeta {
|
||||
segments: Vec<String>,
|
||||
schema: Schema,
|
||||
}
|
||||
|
||||
impl DirectoryMeta {
|
||||
fn new() -> DirectoryMeta {
|
||||
DirectoryMeta {
|
||||
impl IndexMeta {
|
||||
fn new() -> IndexMeta {
|
||||
IndexMeta {
|
||||
segments: Vec::new(),
|
||||
schema: Schema::new(),
|
||||
}
|
||||
}
|
||||
fn with_schema(schema: Schema) -> DirectoryMeta {
|
||||
DirectoryMeta {
|
||||
fn with_schema(schema: Schema) -> IndexMeta {
|
||||
IndexMeta {
|
||||
segments: Vec::new(),
|
||||
schema: schema,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Directory {
|
||||
impl fmt::Debug for Index {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Directory({:?})", self.inner_directory.read().unwrap().index_path)
|
||||
write!(f, "Index({:?})", self.inner_index.read().unwrap().index_path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,41 +68,40 @@ fn sync_file(filepath: &PathBuf) -> Result<(), IOError> {
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Directory {
|
||||
inner_directory: Arc<RwLock<InnerDirectory>>,
|
||||
pub struct Index {
|
||||
inner_index: Arc<RwLock<InnerIndex>>,
|
||||
}
|
||||
|
||||
|
||||
pub enum CreateError {
|
||||
RootDirectoryDoesNotExist,
|
||||
DirectoryAlreadyExists,
|
||||
RootIndexDoesNotExist,
|
||||
IndexAlreadyExists,
|
||||
CannotOpenMetaFile,
|
||||
}
|
||||
|
||||
struct DirectoryError;
|
||||
struct IndexError;
|
||||
|
||||
|
||||
|
||||
impl Directory {
|
||||
impl Index {
|
||||
|
||||
pub fn create(filepath: &Path, schema: Schema) -> Result<Directory, CreateError> {
|
||||
let inner_directory = try!(InnerDirectory::create(filepath, schema));
|
||||
Ok(Directory {
|
||||
inner_directory: Arc::new(RwLock::new(inner_directory)),
|
||||
pub fn create(filepath: &Path, schema: Schema) -> Result<Index, CreateError> {
|
||||
let inner_index = try!(InnerIndex::create(filepath, schema));
|
||||
Ok(Index {
|
||||
inner_index: Arc::new(RwLock::new(inner_index)),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn create_from_tempdir(schema: Schema) -> Result<Directory, IOError> {
|
||||
let inner_directory = try!(InnerDirectory::create_from_tempdir(schema));
|
||||
Ok(Directory {
|
||||
inner_directory: Arc::new(RwLock::new(inner_directory)),
|
||||
pub fn create_from_tempdir(schema: Schema) -> Result<Index, IOError> {
|
||||
let inner_index = try!(InnerIndex::create_from_tempdir(schema));
|
||||
Ok(Index {
|
||||
inner_index: Arc::new(RwLock::new(inner_index)),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn open<P: AsRef<Path>>(filepath: &P) -> Result<Directory, IOError> {
|
||||
let inner_directory = try!(InnerDirectory::open(filepath));
|
||||
Ok(Directory {
|
||||
inner_directory: Arc::new(RwLock::new(inner_directory)),
|
||||
pub fn open<P: AsRef<Path>>(filepath: &P) -> Result<Index, IOError> {
|
||||
let inner_index = try!(InnerIndex::open(filepath));
|
||||
Ok(Index {
|
||||
inner_index: Arc::new(RwLock::new(inner_index)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -110,16 +109,16 @@ impl Directory {
|
||||
self.get_read().unwrap().metas.schema.clone()
|
||||
}
|
||||
|
||||
fn get_write(&mut self) -> Result<RwLockWriteGuard<InnerDirectory>, IOError> {
|
||||
self.inner_directory
|
||||
fn get_write(&mut self) -> Result<RwLockWriteGuard<InnerIndex>, IOError> {
|
||||
self.inner_index
|
||||
.write()
|
||||
.map_err(|e| IOError::new(IOErrorKind::Other,
|
||||
format!("Failed acquiring lock on directory.\n
|
||||
It can happen if another thread panicked! Error was: {:?}", e) ))
|
||||
}
|
||||
|
||||
fn get_read(&self) -> Result<RwLockReadGuard<InnerDirectory>, IOError> {
|
||||
self.inner_directory
|
||||
fn get_read(&self) -> Result<RwLockReadGuard<InnerIndex>, IOError> {
|
||||
self.inner_index
|
||||
.read()
|
||||
.map_err(|e| IOError::new(IOErrorKind::Other,
|
||||
format!("Failed acquiring lock on directory.\n
|
||||
@@ -133,7 +132,7 @@ impl Directory {
|
||||
|
||||
|
||||
pub fn load_metas(&self,) -> Result<(), IOError> {
|
||||
self.inner_directory
|
||||
self.inner_index
|
||||
.write()
|
||||
.unwrap() // only fail when another thread has already panicked.
|
||||
.load_metas()
|
||||
@@ -145,7 +144,7 @@ impl Directory {
|
||||
|
||||
pub fn segments(&self,) -> Vec<Segment> {
|
||||
// TODO handle error
|
||||
self.inner_directory
|
||||
self.inner_index
|
||||
.read()
|
||||
.unwrap()
|
||||
.segment_ids()
|
||||
@@ -176,10 +175,10 @@ impl Directory {
|
||||
}
|
||||
|
||||
|
||||
struct InnerDirectory {
|
||||
struct InnerIndex {
|
||||
index_path: PathBuf,
|
||||
mmap_cache: RefCell<HashMap<PathBuf, MmapReadOnly>>,
|
||||
metas: DirectoryMeta,
|
||||
metas: IndexMeta,
|
||||
_temp_directory: Option<TempDir>,
|
||||
}
|
||||
|
||||
@@ -189,7 +188,7 @@ fn create_tempdir() -> Result<TempDir, IOError> {
|
||||
}
|
||||
|
||||
|
||||
impl InnerDirectory {
|
||||
impl InnerIndex {
|
||||
|
||||
// TODO find a rusty way to hide that, while keeping
|
||||
// it visible for IndexWriters.
|
||||
@@ -199,34 +198,34 @@ impl InnerDirectory {
|
||||
self.save_metas()
|
||||
}
|
||||
|
||||
pub fn create<P: AsRef<Path>>(filepath: P, schema: Schema) -> Result<InnerDirectory, CreateError> {
|
||||
pub fn create<P: AsRef<Path>>(filepath: P, schema: Schema) -> Result<InnerIndex, CreateError> {
|
||||
let filepath_os_path = filepath.as_ref().as_os_str();
|
||||
let mut directory = InnerDirectory {
|
||||
let mut directory = InnerIndex {
|
||||
index_path: PathBuf::from(&filepath_os_path),
|
||||
mmap_cache: RefCell::new(HashMap::new()),
|
||||
metas: DirectoryMeta::with_schema(schema),
|
||||
metas: IndexMeta::with_schema(schema),
|
||||
_temp_directory: None,
|
||||
};
|
||||
Ok(directory)
|
||||
}
|
||||
|
||||
pub fn create_from_tempdir(schema: Schema) -> Result<InnerDirectory, IOError> {
|
||||
pub fn create_from_tempdir(schema: Schema) -> Result<InnerIndex, IOError> {
|
||||
let tempdir = try!(create_tempdir());
|
||||
let tempdir_path = PathBuf::from(tempdir.path());
|
||||
let mut directory = InnerDirectory {
|
||||
let mut directory = InnerIndex {
|
||||
index_path: PathBuf::from(tempdir_path),
|
||||
mmap_cache: RefCell::new(HashMap::new()),
|
||||
metas: DirectoryMeta::with_schema(schema),
|
||||
metas: IndexMeta::with_schema(schema),
|
||||
_temp_directory: Some(tempdir)
|
||||
};
|
||||
Ok(directory)
|
||||
}
|
||||
|
||||
pub fn open<P: AsRef<Path>>(filepath: &P) -> Result<InnerDirectory, IOError> {
|
||||
let mut directory = InnerDirectory {
|
||||
pub fn open<P: AsRef<Path>>(filepath: &P) -> Result<InnerIndex, IOError> {
|
||||
let mut directory = InnerIndex {
|
||||
index_path: PathBuf::from(filepath.as_ref().as_os_str()),
|
||||
mmap_cache: RefCell::new(HashMap::new()),
|
||||
metas: DirectoryMeta::new(),
|
||||
metas: IndexMeta::new(),
|
||||
_temp_directory: None,
|
||||
};
|
||||
try!(directory.load_metas()); //< does the directory already exists?
|
||||
@@ -320,7 +319,7 @@ pub enum SegmentComponent {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Segment {
|
||||
directory: Directory,
|
||||
directory: Index,
|
||||
segment_id: SegmentId,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use core::reader::SegmentReader;
|
||||
use core::directory::Directory;
|
||||
use core::directory::Index;
|
||||
use core::directory::Segment;
|
||||
use core::directory::SegmentId;
|
||||
use core::schema::DocId;
|
||||
use core::schema::Document;
|
||||
use core::directory::Segment;
|
||||
use core::collector::Collector;
|
||||
use std::collections::HashMap;
|
||||
use core::schema::Term;
|
||||
@@ -44,9 +44,9 @@ impl Searcher {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_directory(directory: Directory) -> Searcher {
|
||||
pub fn for_index(index: Index) -> Searcher {
|
||||
let mut searcher = Searcher::new();
|
||||
for segment in directory.segments().into_iter() {
|
||||
for segment in index.segments().into_iter() {
|
||||
searcher.add_segment(segment);
|
||||
}
|
||||
searcher
|
||||
|
||||
+4
-4
@@ -2,7 +2,7 @@ use core::schema::*;
|
||||
use core::codec::*;
|
||||
use std::io;
|
||||
use std::rc::Rc;
|
||||
use core::directory::Directory;
|
||||
use core::directory::Index;
|
||||
use core::analyzer::SimpleTokenizer;
|
||||
use std::collections::BTreeMap;
|
||||
use core::analyzer::StreamingIterator;
|
||||
@@ -32,18 +32,18 @@ impl PostingsWriter {
|
||||
|
||||
pub struct IndexWriter {
|
||||
segment_writer: Rc<SegmentWriter>,
|
||||
directory: Directory,
|
||||
directory: Index,
|
||||
schema: Schema,
|
||||
}
|
||||
|
||||
fn new_segment_writer(directory: &Directory, ) -> SegmentWriter {
|
||||
fn new_segment_writer(directory: &Index, ) -> SegmentWriter {
|
||||
let segment = directory.new_segment();
|
||||
SegmentWriter::for_segment(segment)
|
||||
}
|
||||
|
||||
impl IndexWriter {
|
||||
|
||||
pub fn open(directory: &Directory) -> IndexWriter {
|
||||
pub fn open(directory: &Index) -> IndexWriter {
|
||||
let schema = directory.schema();
|
||||
IndexWriter {
|
||||
segment_writer: Rc::new(new_segment_writer(&directory)),
|
||||
|
||||
+5
-5
@@ -8,7 +8,7 @@ use tantivy::core::schema::*;
|
||||
use tantivy::core::writer::IndexWriter;
|
||||
use tantivy::core::collector::Collector;
|
||||
use tantivy::core::searcher::Searcher;
|
||||
use tantivy::core::directory::{Directory, generate_segment_name, SegmentId};
|
||||
use tantivy::core::directory::{Index, generate_segment_name, SegmentId};
|
||||
use tantivy::core::reader::SegmentReader;
|
||||
use regex::Regex;
|
||||
use tantivy::core::serial::DebugSegmentSerializer;
|
||||
@@ -49,7 +49,7 @@ fn test_indexing() {
|
||||
let text_fieldtype = FieldOptions::new().set_tokenized_indexed();
|
||||
let text_field = schema.add_field("text", &text_fieldtype);
|
||||
|
||||
let directory = Directory::create_from_tempdir(schema).unwrap();
|
||||
let directory = Index::create_from_tempdir(schema).unwrap();
|
||||
|
||||
{
|
||||
// writing the segment
|
||||
@@ -92,11 +92,11 @@ fn test_searcher() {
|
||||
let mut schema = Schema::new();
|
||||
let text_fieldtype = FieldOptions::new().set_tokenized_indexed();
|
||||
let text_field = schema.add_field("text", &text_fieldtype);
|
||||
let directory = Directory::create_from_tempdir(schema).unwrap();
|
||||
let index = Index::create_from_tempdir(schema).unwrap();
|
||||
|
||||
{
|
||||
// writing the segment
|
||||
let mut index_writer = IndexWriter::open(&directory);
|
||||
let mut index_writer = IndexWriter::open(&index);
|
||||
{
|
||||
let mut doc = Document::new();
|
||||
doc.set(&text_field, "af b");
|
||||
@@ -117,7 +117,7 @@ fn test_searcher() {
|
||||
}
|
||||
{
|
||||
|
||||
let searcher = Searcher::for_directory(directory);
|
||||
let searcher = Searcher::for_index(index);
|
||||
let get_doc_ids = |terms: Vec<Term>| {
|
||||
let mut collector = TestCollector::new();
|
||||
searcher.search(&terms, &mut collector);
|
||||
|
||||
Reference in New Issue
Block a user