pub struct IsamBuilder<K, V> { /* private fields */ }Expand description
Builder for creating or opening an Isam database with secondary indices.
Obtain a builder via Isam::builder. Call with_index
for each secondary index, then create or open.
Implementations§
Source§impl<K, V> IsamBuilder<K, V>where
K: Serialize + DeserializeOwned + Ord + Clone + Send + 'static,
V: Serialize + DeserializeOwned + Clone + Send + 'static,
impl<K, V> IsamBuilder<K, V>where
K: Serialize + DeserializeOwned + Ord + Clone + Send + 'static,
V: Serialize + DeserializeOwned + Clone + Send + 'static,
Sourcepub fn with_index<E>(self, name: &str, _extractor: E) -> Selfwhere
E: DeriveKey<V>,
pub fn with_index<E>(self, name: &str, _extractor: E) -> Selfwhere
E: DeriveKey<V>,
Register a secondary index to be opened or created alongside the database.
name must be unique within a database. The extractor value is used only
to infer the DeriveKey implementation — it is not stored.
After construction, obtain a typed handle for querying via Isam::index.
Sourcepub fn rebuild_index(self, name: &str) -> Self
pub fn rebuild_index(self, name: &str) -> Self
Mark a secondary index to be fully rebuilt from primary data during open.
The existing .sidb/.sidx files for name are deleted at open time
and repopulated by scanning all primary records. The index must also be
registered via with_index.
§When to use
Call this whenever the DeriveKey extractor logic has changed and the
on-disk index is therefore stale. Without a rebuild, queries against a
stale index will silently return incorrect results.
§Example
use serde::{Serialize, Deserialize};
use highlandcows_isam::{Isam, DeriveKey};
#[derive(Serialize, Deserialize, Clone)]
struct User { name: String, city: String }
struct CityIndex;
impl DeriveKey<User> for CityIndex {
type Key = String;
fn derive(u: &User) -> String { u.city.clone() }
}
// Reopen and force a full rebuild of the "city" index.
let db = Isam::<u64, User>::builder()
.with_index("city", CityIndex)
.rebuild_index("city")
.open(&path)
.unwrap();Sourcepub fn create(self, path: impl AsRef<Path>) -> IsamResult<Isam<K, V>>
pub fn create(self, path: impl AsRef<Path>) -> IsamResult<Isam<K, V>>
Create a new, empty database at path with the registered indices.
§Example
use serde::{Serialize, Deserialize};
use highlandcows_isam::{Isam, DeriveKey};
#[derive(Serialize, Deserialize, Clone)]
struct User { name: String, city: String }
struct CityIndex;
impl DeriveKey<User> for CityIndex {
type Key = String;
fn derive(u: &User) -> String { u.city.clone() }
}
let db = Isam::<u64, User>::builder()
.with_index("city", CityIndex)
.create(&path)
.unwrap();Sourcepub fn open(self, path: impl AsRef<Path>) -> IsamResult<Isam<K, V>>
pub fn open(self, path: impl AsRef<Path>) -> IsamResult<Isam<K, V>>
Open an existing database at path with the registered indices.
Secondary indices must be registered again on every open — the index name links the handle to the files on disk, but the extractor type is not persisted.
§Example
use serde::{Serialize, Deserialize};
use highlandcows_isam::{Isam, DeriveKey};
#[derive(Serialize, Deserialize, Clone)]
struct User { name: String, city: String }
struct CityIndex;
impl DeriveKey<User> for CityIndex {
type Key = String;
fn derive(u: &User) -> String { u.city.clone() }
}
let db = Isam::<u64, User>::builder()
.with_index("city", CityIndex)
.open(&path)
.unwrap();
let city_idx = db.index::<CityIndex>("city");