use crate::blobstore::Blob;
use crate::common::universal_io::UniversalRead;
use crate::segment::index::field_index::map_index::MapIndexKey;
use crate::segment::index::field_index::map_index::immutable_map_index::ImmutableMapIndex;
use crate::segment::index::field_index::map_index::mutable_map_index::read_only::ReadOnlyAppendableMapIndex;
use crate::segment::index::field_index::map_index::on_disk_map_index::OnDiskMapIndex;
mod lifecycle;
mod live_reload;
mod read_ops;
pub enum ReadOnlyMapIndex<N: MapIndexKey + ?Sized, S: UniversalRead>
where
Vec<<N as MapIndexKey>::Owned>: Blob + Send + Sync,
{
Appendable(ReadOnlyAppendableMapIndex<N, S>),
Immutable(ImmutableMapIndex<N, S>),
OnDisk(OnDiskMapIndex<N, S>),
}
#[cfg(test)]
mod tests {
use crate::common::counter::hardware_counter::HardwareCounterCell;
use crate::common::types::PointOffsetType;
use crate::common::universal_io::{MmapFile, ReadOnly, UniversalRead, UniversalReadFileOps};
use itertools::Itertools as _;
use serde_json::Value;
use tempfile::TempDir;
use super::super::MapIndex;
use super::ReadOnlyMapIndex;
use crate::segment::index::field_index::{FieldIndexBuilderTrait, PayloadFieldIndexRead};
use crate::segment::json_path::JsonPath;
use crate::segment::types::{FieldCondition, Match};
#[test]
fn parent_open_appendable_round_trip() {
let dir = TempDir::with_prefix("ro_map_parent_gridstore").unwrap();
let hw_counter = HardwareCounterCell::new();
{
let mut builder = MapIndex::<str>::builder_mutable(dir.path().to_path_buf(), false);
builder.init().unwrap();
let entries: &[(PointOffsetType, &[&str])] = &[
(0, &["red", "green"]),
(1, &["green"]),
(2, &["blue", "red"]),
];
for (idx, values) in entries {
let values: Vec<Value> = values.iter().map(|v| Value::from(*v)).collect();
let values_ref: Vec<_> = values.iter().collect();
builder.add_point(*idx, &values_ref, &hw_counter).unwrap();
}
builder.finalize().unwrap();
}
type RoFs = <ReadOnly<MmapFile> as UniversalRead>::Fs;
let fs = RoFs::from_context(Default::default()).unwrap();
let index: ReadOnlyMapIndex<str, ReadOnly<MmapFile>> =
ReadOnlyMapIndex::open_appendable(&fs, dir.path().to_path_buf())
.unwrap()
.unwrap();
assert!(matches!(index, ReadOnlyMapIndex::Appendable(_)));
assert_eq!(index.count_indexed_points().unwrap(), 3);
let key = JsonPath::new("color");
let red = FieldCondition::new_match(key.clone(), Match::from("red".to_string()));
let blue = FieldCondition::new_match(key, Match::from("blue".to_string()));
assert_eq!(
index
.filter(&red, &hw_counter)
.unwrap()
.unwrap()
.collect_vec(),
vec![0, 2],
);
assert_eq!(
index
.filter(&blue, &hw_counter)
.unwrap()
.unwrap()
.collect_vec(),
vec![2],
);
}
}