Crates.io | grebedb |
lib.rs | grebedb |
version | 1.0.0 |
source | src |
created_at | 2021-03-29 02:04:56.245754 |
updated_at | 2021-06-04 02:39:38.28382 |
description | Lightweight embedded key-value store/database backed by files in a virtual file system interface |
homepage | |
repository | https://github.com/chfoo/grebedb/ |
max_upload_size | |
id | 374919 |
size | 217,930 |
GrebeDB is a Rust library that provides a lightweight embedded key-value store/database backed by files in a virtual file system interface. It is intended for single-process applications that prefer a key-value database-like interface to data instead operating on file formats directly.
Note: While the library is in a very usable state and a decent amount of effort has been made to ensure it is correct and robust, it has not been extensively tested or used in production. Use with caution and make backups regularly of important data.
Since there are too many key-value stores, the design of the database is immediately described upfront for you to decide whether GrebeDB is fit for your use:
get
, put
, remove
, and cursor
are provided. There's no support for traditional transactions, but the flush
operation provides atomic saving of data. Internal consistency is provided by file copy-on-writes, incrementing revision counters, and atomic file renames.For details about the file format, see format.md.
Remember to add the grebedb
crate dependency to your Cargo.toml.
A GrebeDB database is stored as multiple files in a directory. The following creates a database using the given path and default options:
let options = Options::default();
let mut db = Database::open_path("path/to/empty/directory/", options)?;
Storing, retrieving, and deleting keys is as simple as using the get()
, put()
, and remove()
functions:
db.put("my_key", "hello world")?;
println!("The value of my_key is {:?}", db.get("my_key")?);
db.remove("my_key")?;
println!("The value of my_key is now {:?}", db.get("my_key")?);
To get all the key-values, use cursor()
:
for (key, value) in db.cursor() {
println!("key = {}, value = {}", key, value);
}
The database uses an internal cache and automatically delays writing data to the file system. If you want to ensure all changes has been persisted to the file system at a certain point, use flush()
. This operation saves the data with atomicity, effectively emulating a transaction, before returning:
db.flush()?;
Tip: When inserting a large amount of items, insert keys in sorted order for best performance. Inserting many random keys will be slow as it will require opening, writing, and closing the file that contains the position for it. If you need sorted ID generation, try algorithms based on Twitter Snowflake, MongoDB Object ID, or ULID.
For more information, check the examples directory and the API reference on docs.rs.
By default, the features are enabled:
compression
: zstd
crate is enabled for compressionfile_locking
: fslock
is for cross-platform file lockingsystem
: getrandom
is a dependency for uuid
To disable them, use default-features = false
in your Cargo.toml file.
For a command-line tool to provide basic manipulation (such as import & export for backup) and debugging, see grebedb-tool.
Please use the GitHub Issues, Pull Requests, and Discussions if you have any problems, bug fixes, or suggestions.
Possible improvements:
&mut Vec<u8>
type might be inflexible.The following non-exhaustive features are not in scope and likely won't be implemented:
If you need more features or require better performance, it's likely you need a more powerful database with a Rust binding such as RocksDB or a traditional database like SQLite.
Copyright 2021 Christopher Foo. Licensed under Mozilla Public License 2.0.