5个版本

0.2.0 2022年2月23日
0.1.3 2021年10月22日
0.1.2 2021年7月28日
0.1.1 2021年7月27日
0.1.0 2021年7月27日

#145 in 数据库实现

每月下载 27

MIT 许可证

52KB
1.5K SLoC

encrypted-sled

encrypted-sled 是一个(几乎)即插即用的替代品/包装,围绕令人惊叹的 sled 内嵌数据库。只需配置加密并正常使用。

示例


let cipher = {
    use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce};
    let mut key = Key::default();
    key.copy_from_slice(b"an example very very secret key.");
    encrypted_sled::EncryptionCipher::<ChaCha20Poly1305, _>::new(
        key,
        encrypted_sled::RandNonce::new(rand::thread_rng()),
        encrypted_sled::EncryptionMode::default(),
    )
};

let db = encrypted_sled::open("my_db", cipher).unwrap();

// insert and get
db.insert(b"yo!", b"v1");
assert_eq!(&db.get(b"yo!").unwrap().unwrap(), b"v1");

// Atomic compare-and-swap.
db.compare_and_swap(
    b"yo!",      // key
    Some(b"v1"), // old value, None for not present
    Some(b"v2"), // new value, None for delete
)
.unwrap();

// Iterates over key-value pairs, starting at the given key.
let scan_key: &[u8] = b"a non-present key before yo!";
let mut iter = db.range(scan_key..).unwrap();
assert_eq!(&iter.next().unwrap().unwrap().0, b"yo!");
assert_eq!(iter.next(), None);

db.remove(b"yo!");
assert_eq!(db.get(b"yo!"), Ok(None));

let other_tree = db.open_tree(b"cool db facts").unwrap();
other_tree.insert(
    b"k1",
    &b"a Db acts like a Tree due to implementing Deref<Target = Tree>"[..]
).unwrap();

待办事项

还有一些功能尚未实现

  • TransactionalTrees(例如同时执行多个树的交易)
  • 数据库导入/导出

一些函数没有优雅地处理加密/解密,因此可能会导致数据损坏,请在自己的风险下使用!加密密钥很可能会破坏这些

  • update_and_fetchfetch_and_update
  • 合并运算符

许可证:MIT

依赖关系

~4MB
~86K SLoC