Struct galvanize::reader::Reader
[−]
[src]
pub struct Reader<'a, F: Read + Seek + 'a> { /* fields omitted */ }
Allows you to read from CDB.
Example
Given a file stored at filename
with the following contents:
{
"key": "value",
}
this is how you can read the stored value:
use galvanize::Reader; use std::fs::File; let key = "key".as_bytes(); let mut f = try!(File::open(filename)); let mut cdb_reader = try!(Reader::new(&mut f)); let stored_vals = cdb_reader.get(key); assert_eq!(stored_vals.len(), 1); assert_eq!(&stored_vals[0][..], &"value".as_bytes()[..]); // The CDB contains only one entry: assert_eq!(cdb_reader.len(), 1); // Accessing a key that isn't in the CDB: let non_existing_key = "non_existing_key".as_bytes(); let empty = cdb_reader.get(non_existing_key); assert_eq!(empty.len(), 0); assert!(cdb_reader.get_first(non_existing_key).is_err());
Methods
impl<'a, F: Read + Seek + 'a> Reader<'a, F>
[src]
fn new(file: &'a mut F) -> Result<Reader<'a, F>>
Creates a new Reader
consuming the provided file
.
fn len(&self) -> usize
How many (key, value)
pairs are there in this Read Only CDB.
fn get(&mut self, key: &[u8]) -> Vec<Vec<u8>>
Return a Vec
of all the values under the given key
.
fn keys(&mut self) -> Vec<Vec<u8>>
Return a Vec
of all the keys in this Read Only CDB.
Keep in mind that if there're duplicated keys, they will appear
multiple times in the resulting Vec
.
fn get_first(&mut self, key: &[u8]) -> Result<Vec<u8>>
Pull the value
bytes for the first occurence of the given key
in
this CDB.
fn get_from_pos(&mut self, key: &[u8], index: u32) -> Result<Vec<u8>>
Pull the value
bytes for the index
st occurence of the given key
in this CDB.
impl<'a> Reader<'a, File>
[src]
fn as_writer(self) -> Result<Writer<'a, File>>
Transform this Reader
into a Writer
using the same underlying
file
.
The underlying file will have its hash table truncate
d. This will be
regenerated on Writer
drop.