gh46 part 2: update examples

without.crypto
Nan Jiang 6 years ago
parent e068f86595
commit 92fd928929
  1. 112
      examples/iterator.rs
  2. 214
      examples/simple-store.rs

@ -7,71 +7,71 @@
//!
//! cargo run --example iterator
// extern crate rkv;
// extern crate tempfile;
extern crate rkv;
extern crate tempfile;
// use rkv::{
// Manager,
// Rkv,
// Store,
// StoreError,
// Value,
// };
// use tempfile::Builder;
use rkv::{
Manager,
Rkv,
Store,
StoreError,
Value,
};
use tempfile::Builder;
// use std::fs;
// use std::str;
use std::fs;
use std::str;
fn main() {
// let root = Builder::new().prefix("iterator").tempdir().unwrap();
// fs::create_dir_all(root.path()).unwrap();
// let p = root.path();
let root = Builder::new().prefix("iterator").tempdir().unwrap();
fs::create_dir_all(root.path()).unwrap();
let p = root.path();
// let created_arc = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();
// let k = created_arc.read().unwrap();
// let store: Store<&str> = k.open_or_create("store").unwrap();
let created_arc = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();
let k = created_arc.read().unwrap();
let store = k.open_or_create("store").unwrap();
// populate_store(&k, &store).unwrap();
populate_store(&k, &store).unwrap();
// let reader = store.read(&k).unwrap();
let reader = k.read::<&str>().unwrap();
// println!("Iterating from the beginning...");
// // Reader::iter_start() iterates from the first item in the store, and
// // returns the (key, value) tuples in order.
// let mut iter = reader.iter_start().unwrap();
// while let Some((country, city)) = iter.next() {
// println!("{}, {:?}", str::from_utf8(country).unwrap(), city);
// }
println!("Iterating from the beginning...");
// Reader::iter_start() iterates from the first item in the store, and
// returns the (key, value) tuples in order.
let mut iter = reader.iter_start(&store).unwrap();
while let Some((country, city)) = iter.next() {
println!("{}, {:?}", str::from_utf8(country).unwrap(), city);
}
// println!("");
// println!("Iterating from the given key...");
// // Reader::iter_from() iterates from the first key equal to or greater
// // than the given key.
// let mut iter = reader.iter_from("Japan").unwrap();
// while let Some((country, city)) = iter.next() {
// println!("{}, {:?}", str::from_utf8(country).unwrap(), city);
// }
println!("");
println!("Iterating from the given key...");
// Reader::iter_from() iterates from the first key equal to or greater
// than the given key.
let mut iter = reader.iter_from(&store, "Japan").unwrap();
while let Some((country, city)) = iter.next() {
println!("{}, {:?}", str::from_utf8(country).unwrap(), city);
}
// println!("");
// println!("Iterating from the given prefix...");
// let mut iter = reader.iter_from("Un").unwrap();
// while let Some((country, city)) = iter.next() {
// println!("{}, {:?}", str::from_utf8(country).unwrap(), city);
// }
// }
println!("");
println!("Iterating from the given prefix...");
let mut iter = reader.iter_from(&store, "Un").unwrap();
while let Some((country, city)) = iter.next() {
println!("{}, {:?}", str::from_utf8(country).unwrap(), city);
}
}
// fn populate_store(k: &Rkv, store: &Store<&str>) -> Result<(), StoreError> {
// let mut writer = store.write(k)?;
// for (country, city) in vec![
// ("Canada", Value::Str("Ottawa")),
// ("United States of America", Value::Str("Washington")),
// ("Germany", Value::Str("Berlin")),
// ("France", Value::Str("Paris")),
// ("Italy", Value::Str("Rome")),
// ("United Kingdom", Value::Str("London")),
// ("Japan", Value::Str("Tokyo")),
// ] {
// writer.put(country, &city)?;
// }
// writer.commit()
fn populate_store(k: &Rkv, store: &Store) -> Result<(), StoreError> {
let mut writer = k.write::<&str>()?;
for (country, city) in vec![
("Canada", Value::Str("Ottawa")),
("United States of America", Value::Str("Washington")),
("Germany", Value::Str("Berlin")),
("France", Value::Str("Paris")),
("Italy", Value::Str("Rome")),
("United Kingdom", Value::Str("London")),
("Japan", Value::Str("Tokyo")),
] {
writer.put(store, country, &city)?;
}
writer.commit()
}

@ -7,117 +7,113 @@
//!
//! cargo run --example simple-store
// extern crate rkv;
// extern crate tempfile;
extern crate rkv;
extern crate tempfile;
// use rkv::{
// Manager,
// Rkv,
// Store,
// Value,
// };
// use tempfile::Builder;
use rkv::{
Manager,
Rkv,
Value,
};
use tempfile::Builder;
// use std::fs;
use std::fs;
fn main() {
// let root = Builder::new().prefix("simple-db").tempdir().unwrap();
// fs::create_dir_all(root.path()).unwrap();
// let p = root.path();
// // The manager enforces that each process opens the same lmdb environment at most once
// let created_arc = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();
// let k = created_arc.read().unwrap();
// // Creates a store called "store"
// let store: Store<&str> = k.open_or_create("store").unwrap();
// println!("Inserting data...");
// {
// // Use a write transaction to mutate the store
// let mut writer = store.write(&k).unwrap();
// writer.put("int", &Value::I64(1234)).unwrap();
// writer.put("uint", &Value::U64(1234_u64)).unwrap();
// writer.put("float", &Value::F64(1234.0.into())).unwrap();
// writer.put("instant", &Value::Instant(1528318073700)).unwrap();
// writer.put("boolean", &Value::Bool(true)).unwrap();
// writer.put("string", &Value::Str("héllo, yöu")).unwrap();
// writer.put("json", &Value::Json(r#"{"foo":"bar", "number": 1}"#)).unwrap();
// writer.put("blob", &Value::Blob(b"blob")).unwrap();
// writer.commit().unwrap();
// }
// println!("Looking up keys...");
// {
// // Use a read transaction to query the store
// let r = &k.read().unwrap();
// println!("Get int {:?}", store.get(r, "int").unwrap());
// println!("Get uint {:?}", store.get(r, "uint").unwrap());
// println!("Get float {:?}", store.get(r, "float").unwrap());
// println!("Get instant {:?}", store.get(r, "instant").unwrap());
// println!("Get boolean {:?}", store.get(r, "boolean").unwrap());
// println!("Get string {:?}", store.get(r, "string").unwrap());
// println!("Get json {:?}", store.get(r, "json").unwrap());
// println!("Get blob {:?}", store.get(r, "blob").unwrap());
// println!("Get non-existent {:?}", store.get(r, "non-existent").unwrap());
// }
// println!("Looking up keys via Reader.get()...");
// {
// // An alternate way to query the store.
// let r = store.read(&k).expect("reader");
// println!("Get int {:?}", r.get("int").unwrap());
// println!("Get uint {:?}", r.get("uint").unwrap());
// println!("Get float {:?}", r.get("float").unwrap());
// println!("Get instant {:?}", r.get("instant").unwrap());
// println!("Get boolean {:?}", r.get("boolean").unwrap());
// println!("Get string {:?}", r.get("string").unwrap());
// println!("Get json {:?}", r.get("json").unwrap());
// println!("Get blob {:?}", r.get("blob").unwrap());
// println!("Get non-existent {:?}", r.get("non-existent").unwrap());
// }
// println!("Looking up keys via Writer.get()...");
// {
// let mut writer = store.write(&k).unwrap();
// writer.put("foo", &Value::Str("bar")).unwrap();
// writer.put("bar", &Value::Str("baz")).unwrap();
// writer.delete("foo").unwrap();
// println!("It should be None! ({:?})", writer.get("foo").unwrap());
// println!("Get bar ({:?})", writer.get("bar").unwrap());
// writer.commit().unwrap();
// let reader = store.read(&k).expect("reader");
// println!("It should be None! ({:?})", reader.get("foo").unwrap());
// println!("Get bar {:?}", reader.get("bar").unwrap());
// }
// println!("Aborting transaction...");
// {
// // Aborting a write transaction rollbacks the change(s)
// let mut writer = store.write(&k).unwrap();
// writer.put("foo", &Value::Str("bar")).unwrap();
// writer.abort();
// let r = &k.read().unwrap();
// println!("It should be None! ({:?})", store.get(r, "foo").unwrap());
// // Explicitly aborting a transaction is not required unless an early
// // abort is desired, since both read and write transactions will
// // implicitly be aborted once they go out of scope.
// }
// println!("Deleting keys...");
// {
// // Deleting a key/value also requires a write transaction
// let mut writer = store.write(&k).unwrap();
// writer.put("foo", &Value::Str("bar")).unwrap();
// writer.delete("foo").unwrap();
// // Write transaction also supports read
// println!("It should be None! ({:?})", writer.get("foo").unwrap());
// writer.commit().unwrap();
// // Committing a transaction consumes the writer, preventing you
// // from reusing it by failing and reporting a compile-time error.
// // This line would report error[E0382]: use of moved value: `writer`.
// // writer.put("baz", &Value::Str("buz")).unwrap();
// }
let root = Builder::new().prefix("simple-db").tempdir().unwrap();
fs::create_dir_all(root.path()).unwrap();
let p = root.path();
// The manager enforces that each process opens the same lmdb environment at most once
let created_arc = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();
let k = created_arc.read().unwrap();
// Creates a store called "store"
let store = k.open_or_create("store").unwrap();
println!("Inserting data...");
{
// Use a writer to mutate the store
let mut writer = k.write::<&str>().unwrap();
writer.put(&store, "int", &Value::I64(1234)).unwrap();
writer.put(&store, "uint", &Value::U64(1234_u64)).unwrap();
writer.put(&store, "float", &Value::F64(1234.0.into())).unwrap();
writer.put(&store, "instant", &Value::Instant(1528318073700)).unwrap();
writer.put(&store, "boolean", &Value::Bool(true)).unwrap();
writer.put(&store, "string", &Value::Str("héllo, yöu")).unwrap();
writer.put(&store, "json", &Value::Json(r#"{"foo":"bar", "number": 1}"#)).unwrap();
writer.put(&store, "blob", &Value::Blob(b"blob")).unwrap();
writer.commit().unwrap();
}
println!("Looking up keys...");
{
// Use a reader to query the store
let reader = k.read::<&str>().unwrap();
println!("Get int {:?}", reader.get(&store, "int").unwrap());
println!("Get uint {:?}", reader.get(&store, "uint").unwrap());
println!("Get float {:?}", reader.get(&store, "float").unwrap());
println!("Get instant {:?}", reader.get(&store, "instant").unwrap());
println!("Get boolean {:?}", reader.get(&store, "boolean").unwrap());
println!("Get string {:?}", reader.get(&store, "string").unwrap());
println!("Get json {:?}", reader.get(&store, "json").unwrap());
println!("Get blob {:?}", reader.get(&store, "blob").unwrap());
println!("Get non-existent {:?}", reader.get(&store, "non-existent").unwrap());
}
println!("Looking up keys via Writer.get()...");
{
let mut writer = k.write::<&str>().unwrap();
writer.put(&store, "foo", &Value::Str("bar")).unwrap();
writer.put(&store, "bar", &Value::Str("baz")).unwrap();
writer.delete(&store, "foo").unwrap();
println!("It should be None! ({:?})", writer.get(&store, "foo").unwrap());
println!("Get bar ({:?})", writer.get(&store, "bar").unwrap());
writer.commit().unwrap();
let reader = k.read::<&str>().expect("reader");
println!("It should be None! ({:?})", reader.get(&store, "foo").unwrap());
println!("Get bar {:?}", reader.get(&store, "bar").unwrap());
}
println!("Aborting transaction...");
{
// Aborting a write transaction rollbacks the change(s)
let mut writer = k.write::<&str>().unwrap();
writer.put(&store, "foo", &Value::Str("bar")).unwrap();
writer.abort();
let reader = k.read::<&str>().expect("reader");
println!("It should be None! ({:?})", reader.get(&store, "foo").unwrap());
// Explicitly aborting a transaction is not required unless an early
// abort is desired, since both read and write transactions will
// implicitly be aborted once they go out of scope.
}
println!("Deleting keys...");
{
// Deleting a key/value also requires a write transaction
let mut writer = k.write::<&str>().unwrap();
writer.put(&store, "foo", &Value::Str("bar")).unwrap();
writer.delete(&store, "foo").unwrap();
println!("It should be None! ({:?})", writer.get(&store, "foo").unwrap());
writer.commit().unwrap();
// Committing a transaction consumes the writer, preventing you
// from reusing it by failing and reporting a compile-time error.
// This line would report error[E0382]: use of moved value: `writer`.
// writer.put(&store, "baz", &Value::Str("buz")).unwrap();
}
println!("Write and read on multiple stores...");
{
let another_store = k.open_or_create("another_store").unwrap();
let mut writer = k.write::<&str>().unwrap();
writer.put(&store, "foo", &Value::Str("bar")).unwrap();
writer.put(&another_store, "foo", &Value::Str("baz")).unwrap();
writer.commit().unwrap();
let reader = k.read::<&str>().unwrap();
println!("Get from store value: {:?}", reader.get(&store, "foo").unwrap());
println!("Get from another store value: {:?}", reader.get(&another_store, "foo").unwrap());
}
}

Loading…
Cancel
Save