Struct indexkv::Store [−][src]
pub struct Store<T: Serialize + DeserializeOwned> { /* fields omitted */ }
Implementations
Allocates a new Store containing no values. Opens a file descriptor to path
.
Reads the header and checks to see whether or not the store is valid; at this time, this means making sure the format is fully up-to-date and writing completed.
Provides a sequential stream of all values in the Store. In relational database parlance, this would be a tablescan.
Looks up a single value by key. Will attempt to cache data positions in the backing file
to minimize disk seeking. Returns Error::NotFound when key
does not exist in the
Store.
Looks up multiple values by key. Will find (uncached) keys in sorted order to minimize
disk seeking back and forth. Does not return an error when key
does not exist; calling
code should check for Some
vs None
in the resulting HashMap
for fallibility.
Consumes a TryStream of (key, value) pairs. Sequentially writes all the values to disk, noting their positions, then goes back and sequentially writes down the sorted index.
Making effective use of this error handling mechanism is complex; an example is included below.
The Error type is usually inferrable. A recommended pattern is to have it be your crate’s Error type, or anyhow::Error.
Example:
use std::io;
use futures::TryStreamExt;
use indexkv::{Store, StreamError};
#[derive(Debug, thiserror::Error)]
enum MyError {
#[error("I/O error")]
IO(#[from] io::Error),
#[error("cache error")]
Cache(#[from] indexkv::Error)
}
impl From<StreamError<MyError>> for MyError {
fn from(other: StreamError<MyError>) -> Self {
match other {
StreamError::Caller(e) => e,
StreamError::Internal(e) => MyError::from(e)
}
}
}
async fn write() -> Result<(), MyError> {
let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
let mut store: Store<String> = Store::new(path).await.unwrap();
let stream = futures::stream::iter(
vec![Ok("zero"), Ok("one"), Err(io::Error::from(io::ErrorKind::BrokenPipe)), Ok("two")]
.into_iter()
.enumerate()
.map(|(i, r)| match r {
Ok(v) => Ok((i as u64, v.to_string())),
Err(e) => Err(e)
})
).err_into::<MyError>();
store.write(stream).await?;
Ok(())
}
#[tokio::main]
async fn main() {
let result = write().await;
assert!(matches!(
result,
Err(MyError::IO(_))
));
}
Auto Trait Implementations
impl<T> !RefUnwindSafe for Store<T>
impl<T> !UnwindSafe for Store<T>
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more