pub struct SecondaryIndexHandle<K, V, SK> { /* private fields */ }Expand description
An opaque handle to a registered secondary index, used for point lookups.
Obtained from Isam::register_secondary_index.
Implementations§
Source§impl<K, V, SK> SecondaryIndexHandle<K, V, SK>where
K: Serialize + DeserializeOwned + Ord + Clone,
V: Serialize + DeserializeOwned,
SK: Serialize + DeserializeOwned + Ord + Clone,
impl<K, V, SK> SecondaryIndexHandle<K, V, SK>where
K: Serialize + DeserializeOwned + Ord + Clone,
V: Serialize + DeserializeOwned,
SK: Serialize + DeserializeOwned + Ord + Clone,
Sourcepub fn lookup(
&self,
txn: &mut Transaction<'_, K, V>,
sk: &SK,
) -> IsamResult<Vec<(K, V)>>
pub fn lookup( &self, txn: &mut Transaction<'_, K, V>, sk: &SK, ) -> IsamResult<Vec<(K, V)>>
Return all (primary_key, value) pairs whose secondary key equals sk.
Results are returned in insertion order (not key order). For a
non-existent secondary key the result is an empty Vec.
§Example
use serde::{Serialize, Deserialize};
use highlandcows_isam::{Isam, DeriveKey};
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
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> = Isam::create(&path).unwrap();
let city_idx = db.register_secondary_index("city", CityIndex).unwrap();
let mut txn = db.begin_transaction().unwrap();
db.insert(&mut txn, 1, &User { name: "Alice".into(), city: "London".into() }).unwrap();
db.insert(&mut txn, 2, &User { name: "Bob".into(), city: "London".into() }).unwrap();
db.insert(&mut txn, 3, &User { name: "Carol".into(), city: "Paris".into() }).unwrap();
txn.commit().unwrap();
let mut txn = db.begin_transaction().unwrap();
let mut londoners = city_idx.lookup(&mut txn, &"London".to_string()).unwrap();
londoners.sort_by_key(|(pk, _)| *pk);
assert_eq!(londoners[0].0, 1);
assert_eq!(londoners[1].0, 2);
assert_eq!(city_idx.lookup(&mut txn, &"Berlin".to_string()).unwrap(), vec![]);
txn.commit().unwrap();Auto Trait Implementations§
impl<K, V, SK> Freeze for SecondaryIndexHandle<K, V, SK>
impl<K, V, SK> RefUnwindSafe for SecondaryIndexHandle<K, V, SK>
impl<K, V, SK> Send for SecondaryIndexHandle<K, V, SK>
impl<K, V, SK> Sync for SecondaryIndexHandle<K, V, SK>
impl<K, V, SK> Unpin for SecondaryIndexHandle<K, V, SK>
impl<K, V, SK> UnwindSafe for SecondaryIndexHandle<K, V, SK>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more