use std::path::{Path, PathBuf};
use crate::common::fs::{atomic_save_json, read_json};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::segment::common::anonymize::Anonymize;
use crate::segment::common::operation_error::OperationResult;
use crate::segment::types::VectorStorageDatatype;
pub const SPARSE_INDEX_CONFIG_FILE: &str = "sparse_index_config.json";
#[derive(
Default, Hash, Debug, Deserialize, Serialize, JsonSchema, Eq, PartialEq, Copy, Clone,
)]
pub enum SparseIndexType {
#[default]
MutableRam,
ImmutableRam,
Mmap,
}
impl SparseIndexType {
pub fn is_appendable(self) -> bool {
self == Self::MutableRam
}
pub fn is_immutable(self) -> bool {
self != Self::MutableRam
}
pub fn is_on_disk(self) -> bool {
self == Self::Mmap
}
pub fn is_persisted(self) -> bool {
self == Self::Mmap || self == Self::ImmutableRam
}
pub fn from_on_disk(on_disk: bool) -> Self {
if on_disk {
Self::Mmap
} else {
Self::MutableRam
}
}
}
#[derive(
Debug, Deserialize, Serialize, JsonSchema, Copy, Clone, PartialEq, Eq, Default,
)]
#[serde(rename_all = "snake_case")]
pub struct SparseIndexConfig {
pub full_scan_threshold: Option<usize>,
pub index_type: SparseIndexType,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub datatype: Option<VectorStorageDatatype>,
}
impl SparseIndexConfig {
pub fn new(
full_scan_threshold: Option<usize>,
index_type: SparseIndexType,
datatype: Option<VectorStorageDatatype>,
) -> Self {
SparseIndexConfig {
full_scan_threshold,
index_type,
datatype,
}
}
pub fn get_config_path(path: &Path) -> PathBuf {
path.join(SPARSE_INDEX_CONFIG_FILE)
}
pub fn load(path: &Path) -> OperationResult<Self> {
Ok(read_json(path)?)
}
pub fn save(&self, path: &Path) -> OperationResult<()> {
Ok(atomic_save_json(path, self)?)
}
}