Derive eq and copy traits on single/multi database helpers

Signed-off-by: Victor Porof <victor.porof@gmail.com>
without.crypto
Victor Porof 5 years ago
parent d97acd3be6
commit c81ae4b97b
  1. 4
      examples/iterator.rs
  2. 8
      examples/simple-store.rs
  3. 14
      src/env.rs
  4. 1
      src/store/integer.rs
  5. 1
      src/store/integermulti.rs
  6. 1
      src/store/multi.rs
  7. 1
      src/store/single.rs
  8. 14
      tests/test_txn.rs

@ -35,7 +35,7 @@ fn main() {
let k = created_arc.read().unwrap();
let store = k.open_single("store", StoreOptions::create()).unwrap();
populate_store(&k, &store).unwrap();
populate_store(&k, store).unwrap();
let reader = k.read().unwrap();
@ -64,7 +64,7 @@ fn main() {
}
}
fn populate_store(k: &Rkv<LmdbEnvironment>, store: &SingleStore<LmdbDatabase>) -> Result<(), StoreError> {
fn populate_store(k: &Rkv<LmdbEnvironment>, store: SingleStore<LmdbDatabase>) -> Result<(), StoreError> {
let mut writer = k.write()?;
for (country, city) in vec![
("Canada", Value::Str("Ottawa")),

@ -27,7 +27,7 @@ use rkv::{
type MultiStore = rkv::MultiStore<LmdbDatabase>;
type Writer<'env> = rkv::Writer<LmdbRwTransaction<'env>>;
fn getput<'env, 's>(store: &MultiStore, writer: &'env mut Writer, ids: &'s mut Vec<String>) {
fn getput<'env, 's>(store: MultiStore, writer: &'env mut Writer, ids: &'s mut Vec<String>) {
let keys = vec!["str1", "str2", "str3"];
// we convert the writer into a cursor so that we can safely read
for k in keys.iter() {
@ -46,7 +46,7 @@ fn getput<'env, 's>(store: &MultiStore, writer: &'env mut Writer, ids: &'s mut V
}
}
fn delete(store: &MultiStore, writer: &mut Writer) {
fn delete(store: MultiStore, writer: &mut Writer) {
let keys = vec!["str1", "str2", "str3"];
let vals = vec!["string uno", "string quatro", "string siete"];
// we convert the writer into a cursor so that we can safely read
@ -96,10 +96,10 @@ fn main() {
multistore.put(&mut writer, "str3", &Value::Str("string siete")).unwrap();
multistore.put(&mut writer, "str3", &Value::Str("string ocho")).unwrap();
multistore.put(&mut writer, "str3", &Value::Str("string nueve")).unwrap();
getput(&multistore, &mut writer, &mut ids);
getput(multistore, &mut writer, &mut ids);
writer.commit().unwrap();
let mut writer = k.write().unwrap();
delete(&multistore, &mut writer);
delete(multistore, &mut writer);
writer.commit().unwrap();
}

@ -734,7 +734,7 @@ mod tests {
// as the Value::I64 borrows an immutable reference to the Writer.
// So we extract and copy its primitive value.
fn get_existing_foo(store: &SingleStore<LmdbDatabase>, writer: &Writer<LmdbRwTransaction>) -> Option<i64> {
fn get_existing_foo(store: SingleStore<LmdbDatabase>, writer: &Writer<LmdbRwTransaction>) -> Option<i64> {
match store.get(writer, "foo").expect("read") {
Some(Value::I64(val)) => Some(val),
_ => None,
@ -742,11 +742,11 @@ mod tests {
}
let mut writer = k.write().expect("writer");
let mut existing = get_existing_foo(&sk, &writer).unwrap_or(99);
let mut existing = get_existing_foo(sk, &writer).unwrap_or(99);
existing += 1;
sk.put(&mut writer, "foo", &Value::I64(existing)).expect("success");
let updated = get_existing_foo(&sk, &writer).unwrap_or(99);
let updated = get_existing_foo(sk, &writer).unwrap_or(99);
assert_eq!(updated, 100);
writer.commit().expect("commit");
}
@ -1246,7 +1246,7 @@ mod tests {
let root = Builder::new().prefix("test_multiple_thread").tempdir().expect("tempdir");
fs::create_dir_all(root.path()).expect("dir created");
let rkv_arc = Arc::new(RwLock::new(Rkv::new::<backend::Lmdb>(root.path()).expect("new succeeded")));
let store_arc = Arc::new(rkv_arc.read().unwrap().open_single("test", StoreOptions::create()).expect("opened"));
let store = rkv_arc.read().unwrap().open_single("test", StoreOptions::create()).expect("opened");
let num_threads = 10;
let mut write_handles = Vec::with_capacity(num_threads as usize);
@ -1260,11 +1260,10 @@ mod tests {
// For each KV pair, spawn a thread that writes it to the store.
for i in 0..num_threads {
let rkv_arc = rkv_arc.clone();
let store_arc = store_arc.clone();
write_handles.push(thread::spawn(move || {
let rkv = rkv_arc.write().expect("rkv");
let mut writer = rkv.write().expect("writer");
store_arc.put(&mut writer, i.to_string(), &Value::U64(i)).expect("written");
store.put(&mut writer, i.to_string(), &Value::U64(i)).expect("written");
writer.commit().unwrap();
}));
}
@ -1276,11 +1275,10 @@ mod tests {
// and returns its value.
for i in 0..num_threads {
let rkv_arc = rkv_arc.clone();
let store_arc = store_arc.clone();
read_handles.push(thread::spawn(move || {
let rkv = rkv_arc.read().expect("rkv");
let reader = rkv.read().expect("reader");
let value = match store_arc.get(&reader, i.to_string()) {
let value = match store.get(&reader, i.to_string()) {
Ok(Some(Value::U64(value))) => value,
Ok(Some(_)) => panic!("value type unexpected"),
Ok(None) => panic!("value not found"),

@ -28,6 +28,7 @@ use crate::value::Value;
type EmptyResult = Result<(), StoreError>;
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct IntegerStore<D, K> {
inner: SingleStore<D>,
phantom: PhantomData<K>,

@ -33,6 +33,7 @@ use crate::value::Value;
type EmptyResult = Result<(), StoreError>;
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct MultiIntegerStore<D, K> {
inner: MultiStore<D>,
phantom: PhantomData<K>,

@ -27,6 +27,7 @@ use crate::value::Value;
type EmptyResult = Result<(), StoreError>;
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct MultiStore<D> {
db: D,
}

@ -27,6 +27,7 @@ use crate::value::Value;
type EmptyResult = Result<(), StoreError>;
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct SingleStore<D> {
db: D,
}

@ -48,9 +48,9 @@ fn read_many() {
let root = Builder::new().prefix("test_txns").tempdir().expect("tempdir");
fs::create_dir_all(root.path()).expect("dir created");
let k = Rkv::new::<Lmdb>(root.path()).expect("new succeeded");
let samplestore = &k.open_single("s", StoreOptions::create()).expect("open");
let datestore = &k.open_multi("m", StoreOptions::create()).expect("open");
let valuestore = &k.open_multi("m", StoreOptions::create()).expect("open");
let samplestore = k.open_single("s", StoreOptions::create()).expect("open");
let datestore = k.open_multi("m", StoreOptions::create()).expect("open");
let valuestore = k.open_multi("m", StoreOptions::create()).expect("open");
{
let mut writer = k.write().expect("env write lock");
@ -89,7 +89,7 @@ fn read_many() {
}
}
fn get_ids_by_field<'env, T>(txn: &'env T, store: &MultiStore, field: &str) -> Vec<u64>
fn get_ids_by_field<'env, T>(txn: &'env T, store: MultiStore, field: &str) -> Vec<u64>
where
T: Readable<'env, Database = LmdbDatabase, RoCursor = LmdbRoCursor<'env>>,
{
@ -103,7 +103,7 @@ where
.collect::<Vec<u64>>()
}
fn get_samples<'env, T>(txn: &'env T, samplestore: &SingleStore, ids: &[u64]) -> Vec<String>
fn get_samples<'env, T>(txn: &'env T, samplestore: SingleStore, ids: &[u64]) -> Vec<String>
where
T: Readable<'env, Database = LmdbDatabase, RoCursor = LmdbRoCursor<'env>>,
{
@ -119,11 +119,11 @@ where
.collect::<Vec<String>>()
}
fn put_sample(txn: &mut Writer<LmdbRwTransaction>, samplestore: &SingleStore, id: u64, value: &str) {
fn put_sample(txn: &mut Writer<LmdbRwTransaction>, samplestore: SingleStore, id: u64, value: &str) {
let idbytes = id.to_be_bytes();
samplestore.put(txn, &idbytes, &Value::Str(value)).expect("put id");
}
fn put_id_field(txn: &mut Writer<LmdbRwTransaction>, store: &MultiStore, field: &str, id: u64) {
fn put_id_field(txn: &mut Writer<LmdbRwTransaction>, store: MultiStore, field: &str, id: u64) {
store.put(txn, field, &Value::U64(id)).expect("put id");
}

Loading…
Cancel
Save