| 1 |
// Copyright 2018-2019 Mozilla |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 4 |
// this file except in compliance with the License. You may obtain a copy of the |
| 5 |
// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
// Unless required by applicable law or agreed to in writing, software distributed |
| 7 |
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 8 |
// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 |
// specific language governing permissions and limitations under the License. |
| 10 |
#![allow(clippy::from_over_into)] // TODO: `Into` implementations in [safe/lmdb]/flags.rs |
| 11 |
|
| 12 |
//! A simple, humane, typed key-value storage solution. It supports multiple backend |
| 13 |
//! engines with varying guarantees, such as [LMDB](http://www.lmdb.tech/doc/) for |
| 14 |
//! performance, or "SafeMode" for reliability. |
| 15 |
//! |
| 16 |
//! It aims to achieve the following: |
| 17 |
//! |
| 18 |
//! - Avoid sharp edges (e.g., obscure error codes for common situations). |
| 19 |
//! - Correctly restrict access to one handle per process via a |
| 20 |
//! [Manager](struct.Manager.html). |
| 21 |
//! - Use Rust's type system to make single-typed key stores safe and ergonomic. |
| 22 |
//! - Encode and decode values via [bincode](https://docs.rs/bincode/)/[serde](https://docs.rs/serde/) |
| 23 |
//! and type tags, achieving platform-independent storage and input/output flexibility. |
| 24 |
//! |
| 25 |
//! It exposes these primary abstractions: |
| 26 |
//! |
| 27 |
//! - [Manager](struct.Manager.html): a singleton that controls access to environments |
| 28 |
//! - [Rkv](struct.Rkv.html): an environment contains a set of key/value databases |
| 29 |
//! - [SingleStore](store/single/struct.SingleStore.html): a database contains a set of |
| 30 |
//! key/value pairs |
| 31 |
//! |
| 32 |
//! Keys can be anything that implements `AsRef<[u8]>` or integers |
| 33 |
//! (when accessing an [IntegerStore](store/integer/struct.IntegerStore.html)). |
| 34 |
//! |
| 35 |
//! Values can be any of the types defined by the [Value](value/enum.Value.html) enum, |
| 36 |
//! including: |
| 37 |
//! |
| 38 |
//! - booleans (`Value::Bool`) |
| 39 |
//! - integers (`Value::I64`, `Value::U64`) |
| 40 |
//! - floats (`Value::F64`) |
| 41 |
//! - strings (`Value::Str`) |
| 42 |
//! - blobs (`Value::Blob`) |
| 43 |
//! |
| 44 |
//! See [Value](value/enum.Value.html) for the complete list of supported types. |
| 45 |
//! |
| 46 |
//! ## Basic Usage |
| 47 |
//! ``` |
| 48 |
//! use rkv::{Manager, Rkv, SingleStore, Value, StoreOptions}; |
| 49 |
//! use rkv::backend::{SafeMode, SafeModeEnvironment}; |
| 50 |
//! use std::fs; |
| 51 |
//! use tempfile::Builder; |
| 52 |
//! |
| 53 |
//! // First determine the path to the environment, which is represented on disk as a |
| 54 |
//! // directory containing two files: |
| 55 |
//! // |
| 56 |
//! // * a data file containing the key/value stores |
| 57 |
//! // * a lock file containing metadata about current transactions |
| 58 |
//! // |
| 59 |
//! // In this example, we use the `tempfile` crate to create the directory. |
| 60 |
//! // |
| 61 |
//! let root = Builder::new().prefix("simple-db").tempdir().unwrap(); |
| 62 |
//! fs::create_dir_all(root.path()).unwrap(); |
| 63 |
//! let path = root.path(); |
| 64 |
//! |
| 65 |
//! // The `Manager` enforces that each process opens the same environment at most once by |
| 66 |
//! // caching a handle to each environment that it opens. Use it to retrieve the handle |
| 67 |
//! // to an opened environment—or create one if it hasn't already been opened: |
| 68 |
//! let mut manager = Manager::<SafeModeEnvironment>::singleton().write().unwrap(); |
| 69 |
//! let created_arc = manager.get_or_create(path, Rkv::new::<SafeMode>).unwrap(); |
| 70 |
//! let env = created_arc.read().unwrap(); |
| 71 |
//! |
| 72 |
//! // Then you can use the environment handle to get a handle to a datastore: |
| 73 |
//! let store = env.open_single("mydb", StoreOptions::create()).unwrap(); |
| 74 |
//! |
| 75 |
//! { |
| 76 |
//! // Use a write transaction to mutate the store via a `Writer`. There can be only |
| 77 |
//! // one writer for a given environment, so opening a second one will block until |
| 78 |
//! // the first completes. |
| 79 |
//! let mut writer = env.write().unwrap(); |
| 80 |
//! |
| 81 |
//! // Keys are `AsRef<[u8]>`, while values are `Value` enum instances. Use the `Blob` |
| 82 |
//! // variant to store arbitrary collections of bytes. Putting data returns a |
| 83 |
//! // `Result<(), StoreError>`, where StoreError is an enum identifying the reason |
| 84 |
//! // for a failure. |
| 85 |
//! store.put(&mut writer, "int", &Value::I64(1234)).unwrap(); |
| 86 |
//! store.put(&mut writer, "uint", &Value::U64(1234_u64)).unwrap(); |
| 87 |
//! store.put(&mut writer, "float", &Value::F64(1234.0.into())).unwrap(); |
| 88 |
//! store.put(&mut writer, "instant", &Value::Instant(1528318073700)).unwrap(); |
| 89 |
//! store.put(&mut writer, "boolean", &Value::Bool(true)).unwrap(); |
| 90 |
//! store.put(&mut writer, "string", &Value::Str("Héllo, wörld!")).unwrap(); |
| 91 |
//! store.put(&mut writer, "json", &Value::Json(r#"{"foo":"bar", "number": 1}"#)).unwrap(); |
| 92 |
//! store.put(&mut writer, "blob", &Value::Blob(b"blob")).unwrap(); |
| 93 |
//! |
| 94 |
//! // You must commit a write transaction before the writer goes out of scope, or the |
| 95 |
//! // transaction will abort and the data won't persist. |
| 96 |
//! writer.commit().unwrap(); |
| 97 |
//! } |
| 98 |
//! |
| 99 |
//! { |
| 100 |
//! // Use a read transaction to query the store via a `Reader`. There can be multiple |
| 101 |
//! // concurrent readers for a store, and readers never block on a writer nor other |
| 102 |
//! // readers. |
| 103 |
//! let reader = env.read().expect("reader"); |
| 104 |
//! |
| 105 |
//! // Keys are `AsRef<u8>`, and the return value is `Result<Option<Value>, StoreError>`. |
| 106 |
//! println!("Get int {:?}", store.get(&reader, "int").unwrap()); |
| 107 |
//! println!("Get uint {:?}", store.get(&reader, "uint").unwrap()); |
| 108 |
//! println!("Get float {:?}", store.get(&reader, "float").unwrap()); |
| 109 |
//! println!("Get instant {:?}", store.get(&reader, "instant").unwrap()); |
| 110 |
//! println!("Get boolean {:?}", store.get(&reader, "boolean").unwrap()); |
| 111 |
//! println!("Get string {:?}", store.get(&reader, "string").unwrap()); |
| 112 |
//! println!("Get json {:?}", store.get(&reader, "json").unwrap()); |
| 113 |
//! println!("Get blob {:?}", store.get(&reader, "blob").unwrap()); |
| 114 |
//! |
| 115 |
//! // Retrieving a non-existent value returns `Ok(None)`. |
| 116 |
//! println!("Get non-existent value {:?}", store.get(&reader, "non-existent").unwrap()); |
| 117 |
//! |
| 118 |
//! // A read transaction will automatically close once the reader goes out of scope, |
| 119 |
//! // so isn't necessary to close it explicitly, although you can do so by calling |
| 120 |
//! // `Reader.abort()`. |
| 121 |
//! } |
| 122 |
//! |
| 123 |
//! { |
| 124 |
//! // Aborting a write transaction rolls back the change(s). |
| 125 |
//! let mut writer = env.write().unwrap(); |
| 126 |
//! store.put(&mut writer, "foo", &Value::Str("bar")).unwrap(); |
| 127 |
//! writer.abort(); |
| 128 |
//! let reader = env.read().expect("reader"); |
| 129 |
//! println!("It should be None! ({:?})", store.get(&reader, "foo").unwrap()); |
| 130 |
//! } |
| 131 |
//! |
| 132 |
//! { |
| 133 |
//! // Explicitly aborting a transaction is not required unless an early abort is |
| 134 |
//! // desired, since both read and write transactions will implicitly be aborted once |
| 135 |
//! // they go out of scope. |
| 136 |
//! { |
| 137 |
//! let mut writer = env.write().unwrap(); |
| 138 |
//! store.put(&mut writer, "foo", &Value::Str("bar")).unwrap(); |
| 139 |
//! } |
| 140 |
//! let reader = env.read().expect("reader"); |
| 141 |
//! println!("It should be None! ({:?})", store.get(&reader, "foo").unwrap()); |
| 142 |
//! } |
| 143 |
//! |
| 144 |
//! { |
| 145 |
//! // Deleting a key/value pair also requires a write transaction. |
| 146 |
//! let mut writer = env.write().unwrap(); |
| 147 |
//! store.put(&mut writer, "foo", &Value::Str("bar")).unwrap(); |
| 148 |
//! store.put(&mut writer, "bar", &Value::Str("baz")).unwrap(); |
| 149 |
//! store.delete(&mut writer, "foo").unwrap(); |
| 150 |
//! |
| 151 |
//! // A write transaction also supports reading, and the version of the store that it |
| 152 |
//! // reads includes the changes it has made regardless of the commit state of that |
| 153 |
//! // transaction. |
| 154 |
|
| 155 |
//! // In the code above, "foo" and "bar" were put into the store, then "foo" was |
| 156 |
//! // deleted so only "bar" will return a result when the database is queried via the |
| 157 |
//! // writer. |
| 158 |
//! println!("It should be None! ({:?})", store.get(&writer, "foo").unwrap()); |
| 159 |
//! println!("Get bar ({:?})", store.get(&writer, "bar").unwrap()); |
| 160 |
//! |
| 161 |
//! // But a reader won't see that change until the write transaction is committed. |
| 162 |
//! { |
| 163 |
//! let reader = env.read().expect("reader"); |
| 164 |
//! println!("Get foo {:?}", store.get(&reader, "foo").unwrap()); |
| 165 |
//! println!("Get bar {:?}", store.get(&reader, "bar").unwrap()); |
| 166 |
//! } |
| 167 |
//! writer.commit().unwrap(); |
| 168 |
//! { |
| 169 |
//! let reader = env.read().expect("reader"); |
| 170 |
//! println!("It should be None! ({:?})", store.get(&reader, "foo").unwrap()); |
| 171 |
//! println!("Get bar {:?}", store.get(&reader, "bar").unwrap()); |
| 172 |
//! } |
| 173 |
//! |
| 174 |
//! // Committing a transaction consumes the writer, preventing you from reusing it by |
| 175 |
//! // failing at compile time with an error. This line would report "error[E0382]: |
| 176 |
//! // borrow of moved value: `writer`". |
| 177 |
//! // store.put(&mut writer, "baz", &Value::Str("buz")).unwrap(); |
| 178 |
//! } |
| 179 |
//! |
| 180 |
//! { |
| 181 |
//! // Clearing all the entries in the store with a write transaction. |
| 182 |
//! { |
| 183 |
//! let mut writer = env.write().unwrap(); |
| 184 |
//! store.put(&mut writer, "foo", &Value::Str("bar")).unwrap(); |
| 185 |
//! store.put(&mut writer, "bar", &Value::Str("baz")).unwrap(); |
| 186 |
//! writer.commit().unwrap(); |
| 187 |
//! } |
| 188 |
//! |
| 189 |
//! { |
| 190 |
//! let mut writer = env.write().unwrap(); |
| 191 |
//! store.clear(&mut writer).unwrap(); |
| 192 |
//! writer.commit().unwrap(); |
| 193 |
//! } |
| 194 |
//! |
| 195 |
//! { |
| 196 |
//! let reader = env.read().expect("reader"); |
| 197 |
//! println!("It should be None! ({:?})", store.get(&reader, "foo").unwrap()); |
| 198 |
//! println!("It should be None! ({:?})", store.get(&reader, "bar").unwrap()); |
| 199 |
//! } |
| 200 |
//! |
| 201 |
//! } |
| 202 |
//! |
| 203 |
//! ``` |
| 204 |
|
| 205 |
mod env; |
| 206 |
mod error; |
| 207 |
mod helpers; |
| 208 |
mod manager; |
| 209 |
mod readwrite; |
| 210 |
|
| 211 |
pub mod backend; |
| 212 |
#[cfg(feature = "lmdb")] |
| 213 |
pub mod migrator; |
| 214 |
pub mod store; |
| 215 |
pub mod value; |
| 216 |
|
| 217 |
pub use backend::{DatabaseFlags, EnvironmentFlags, WriteFlags}; |
| 218 |
pub use env::Rkv; |
| 219 |
pub use error::{DataError, MigrateError, StoreError}; |
| 220 |
pub use manager::Manager; |
| 221 |
#[cfg(feature = "lmdb")] |
| 222 |
pub use migrator::Migrator; |
| 223 |
pub use readwrite::{Readable, Reader, Writer}; |
| 224 |
pub use store::{keys::EncodableKey, single::SingleStore, CloseOptions, Options as StoreOptions}; |
| 225 |
pub use value::{OwnedValue, Value}; |
| 226 |
|
| 227 |
#[cfg(feature = "db-dup-sort")] |
| 228 |
pub use store::multi::MultiStore; |
| 229 |
|
| 230 |
#[cfg(feature = "db-int-key")] |
| 231 |
pub use store::integer::IntegerStore; |
| 232 |
#[cfg(feature = "db-int-key")] |
| 233 |
pub use store::keys::PrimitiveInt; |
| 234 |
|
| 235 |
#[cfg(all(feature = "db-dup-sort", feature = "db-int-key"))] |
| 236 |
pub use store::integermulti::MultiIntegerStore; |