index_settings should be optional

This commit is contained in:
Pascal Seitz
2021-04-26 11:34:19 +02:00
parent c01c175744
commit 46beb2a989
3 changed files with 21 additions and 45 deletions

View File

@@ -131,11 +131,8 @@ impl IndexBuilder {
let mmap_directory = MmapDirectory::create_from_tempdir()?;
self.create(mmap_directory)
}
fn get_settings_or_default(&self) -> IndexSettings {
self.index_settings
.as_ref()
.cloned()
.unwrap_or_else(|| IndexSettings::default())
fn get_settings_or_default(&self) -> Option<IndexSettings> {
self.index_settings.as_ref().cloned()
}
fn get_expect_schema(&self) -> crate::Result<Schema> {
Ok(self
@@ -180,7 +177,7 @@ impl IndexBuilder {
pub struct Index {
directory: ManagedDirectory,
schema: Schema,
settings: IndexSettings,
settings: Option<IndexSettings>,
executor: Arc<Executor>,
tokenizers: TokenizerManager,
inventory: SegmentMetaInventory,
@@ -268,12 +265,13 @@ impl Index {
pub fn create<Dir: Directory>(
dir: Dir,
schema: Schema,
settings: IndexSettings,
settings: Option<IndexSettings>,
) -> crate::Result<Index> {
IndexBuilder::new()
.schema(schema)
.settings(settings)
.create(dir)
let mut builder = IndexBuilder::new().schema(schema);
if let Some(settings) = settings {
builder = builder.settings(settings);
}
builder.create(dir)
}
/// Creates a new index given a directory and an `IndexMeta`.
@@ -453,7 +451,7 @@ impl Index {
/// Accessor to the index settings
///
pub fn settings(&self) -> &IndexSettings {
pub fn settings(&self) -> &Option<IndexSettings> {
&self.settings
}
/// Accessor to the index schema
@@ -570,12 +568,7 @@ mod tests {
#[test]
fn open_or_create_should_open() {
let directory = RamDirectory::create();
assert!(Index::create(
directory.clone(),
throw_away_schema(),
IndexSettings::default()
)
.is_ok());
assert!(Index::create(directory.clone(), throw_away_schema(), None).is_ok());
assert!(Index::exists(&directory).unwrap());
assert!(Index::open_or_create(directory, throw_away_schema()).is_ok());
}
@@ -583,30 +576,15 @@ mod tests {
#[test]
fn create_should_wipeoff_existing() {
let directory = RamDirectory::create();
assert!(Index::create(
directory.clone(),
throw_away_schema(),
IndexSettings::default()
)
.is_ok());
assert!(Index::create(directory.clone(), throw_away_schema(), None).is_ok());
assert!(Index::exists(&directory).unwrap());
assert!(Index::create(
directory.clone(),
Schema::builder().build(),
IndexSettings::default()
)
.is_ok());
assert!(Index::create(directory.clone(), Schema::builder().build(), None).is_ok());
}
#[test]
fn open_or_create_exists_but_schema_does_not_match() {
let directory = RamDirectory::create();
assert!(Index::create(
directory.clone(),
throw_away_schema(),
IndexSettings::default()
)
.is_ok());
assert!(Index::create(directory.clone(), throw_away_schema(), None).is_ok());
assert!(Index::exists(&directory).unwrap());
assert!(Index::open_or_create(directory.clone(), throw_away_schema()).is_ok());
let err = Index::open_or_create(directory, Schema::builder().build());
@@ -741,7 +719,7 @@ mod tests {
let directory = RamDirectory::create();
let schema = throw_away_schema();
let field = schema.get_field("num_likes").unwrap();
let index = Index::create(directory.clone(), schema, IndexSettings::default()).unwrap();
let index = Index::create(directory.clone(), schema, None).unwrap();
let mut writer = index.writer_with_num_threads(8, 24_000_000).unwrap();
for i in 0u64..8_000u64 {

View File

@@ -196,7 +196,6 @@ impl InnerSegmentMeta {
/// Search Index Settings
#[derive(Clone, Default, Serialize)]
pub struct IndexSettings {}
/// Meta information about the `Index`.
///
/// This object is serialized on disk in the `meta.json` file.
@@ -208,7 +207,7 @@ pub struct IndexSettings {}
#[derive(Clone, Serialize)]
pub struct IndexMeta {
/// `IndexSettings` to configure index options.
pub index_settings: IndexSettings,
pub index_settings: Option<IndexSettings>,
/// List of `SegmentMeta` informations associated to each finalized segment of the index.
pub segments: Vec<SegmentMeta>,
/// Index `Schema`
@@ -236,7 +235,7 @@ struct UntrackedIndexMeta {
impl UntrackedIndexMeta {
pub fn track(self, inventory: &SegmentMetaInventory) -> IndexMeta {
IndexMeta {
index_settings: IndexSettings::default(),
index_settings: None,
segments: self
.segments
.into_iter()
@@ -257,7 +256,7 @@ impl IndexMeta {
/// Opstamp will the value `0u64`.
pub fn with_schema(schema: Schema) -> IndexMeta {
IndexMeta {
index_settings: IndexSettings::default(),
index_settings: None,
segments: vec![],
schema,
opstamp: 0u64,
@@ -289,7 +288,6 @@ impl fmt::Debug for IndexMeta {
mod tests {
use super::IndexMeta;
use super::IndexSettings;
use crate::schema::{Schema, TEXT};
use serde_json;
@@ -301,7 +299,7 @@ mod tests {
schema_builder.build()
};
let index_metas = IndexMeta {
index_settings: IndexSettings::default(),
index_settings: None,
segments: Vec::new(),
schema,
opstamp: 0u64,

View File

@@ -46,7 +46,7 @@ const NUM_MERGE_THREADS: usize = 4;
/// This method is not part of tantivy's public API
pub fn save_new_metas(
schema: Schema,
index_settings: IndexSettings,
index_settings: Option<IndexSettings>,
directory: &dyn Directory,
) -> crate::Result<()> {
save_metas(
@@ -379,7 +379,7 @@ impl SegmentUpdater {
// Segment 1 from disk 1, Segment 1 from disk 2, etc.
commited_segment_metas.sort_by_key(|segment_meta| -(segment_meta.max_doc() as i32));
let index_meta = IndexMeta {
index_settings: IndexSettings::default(),
index_settings: None,
segments: commited_segment_metas,
schema: index.schema(),
opstamp,