changeset 52:b557e42af582

Add Table::get() example to example program.
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 14 Mar 2018 20:39:00 +0100
parents b851387cdf9c
children deef5b97336d
files README.md examples/rw_sstable/src/main.rs
diffstat 2 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/README.md	Wed Mar 14 20:38:44 2018 +0100
+++ b/README.md	Wed Mar 14 20:39:00 2018 +0100
@@ -10,7 +10,7 @@
 * Writing a table, using `TableBuilder`. The entries have to be added in
   sorted order. The data doesn't have to be written to disk; any type
   implementing `Write` works.
-* Reading a table, using `TableReader`. Again, the source is generic; any type
+* Reading a table, using `Table`. Again, the source is generic; any type
   implementing `Read + Seek` can be used.
 
 Note that the tables and some other structures are generic over the ordering of
--- a/examples/rw_sstable/src/main.rs	Wed Mar 14 20:38:44 2018 +0100
+++ b/examples/rw_sstable/src/main.rs	Wed Mar 14 20:39:00 2018 +0100
@@ -32,7 +32,7 @@
     Ok(())
 }
 
-fn read_table(p: &Path) -> Result<()> {
+fn read_table(p: &Path) -> Result<sstable::Table> {
     let tr = sstable::Table::new_from_file(sstable::Options::default(), p)?;
     let mut iter = tr.iter();
     while iter.advance() {
@@ -43,12 +43,22 @@
             String::from_utf8(v).unwrap()
         );
     }
-    Ok(())
+    Ok(tr)
+}
+
+fn lookup(t: &sstable::Table, key: &str) -> Result<Option<String>> {
+    Ok(t.get(key.as_bytes())?.map(|v| unsafe { String::from_utf8_unchecked(v) }))
 }
 
 fn main() {
     let path = PathBuf::from("/tmp/some.random.sstable");
     // Read a couple key/value pairs to a table in /tmp and read them back.
     write_table(&path).expect("writing the table failed");
-    read_table(&path).expect("Reading the table failed");
+    let tr = read_table(&path).expect("Reading the table failed");
+
+    println!("=== lookups ===");
+    println!("{} => {:?}", "000", lookup(&tr, "000").unwrap());
+    println!("{} => {:?}", "def", lookup(&tr, "def").unwrap());
+    println!("{} => {:?}", "zzy", lookup(&tr, "zzy").unwrap());
+    println!("{} => {:?}", "zzz", lookup(&tr, "zzz").unwrap());
 }