Rust leveldb bindings

Almost-complete bindings for leveldb for Rust

.

Documentation

Rust version policy

leveldb is built and tested on stable releases of Rust. This are currently 1.31.0 and 1.43.1. Nightlies might not build at any point and failures are allowed. There are no known issues with nightlies, though.

Prerequisites

snappy and leveldb need to be installed. On Ubuntu, I recommend:

sudo apt-get install libleveldb-dev libsnappy-dev

Usage

If your project is using Cargo, drop the following lines in your Cargo.toml:

[dependencies]

leveldb = "0.8"

Development

Make sure you have all prerequisites installed. Run

$ cargo build

for building and

$ cargo test

to run the test suite.

Examples

extern crate tempdir;
extern crate leveldb;

use tempdir::TempDir;
use leveldb::database::Database;
use leveldb::iterator::Iterable;
use leveldb::kv::KV;
use leveldb::options::{Options,WriteOptions,ReadOptions};

fn main() {
  let tempdir = TempDir::new("demo").unwrap();
  let path = tempdir.path();

  let mut options = Options::new();
  options.create_if_missing = true;
  let mut database = match Database::open(path, options) {
      Ok(db) => { db },
      Err(e) => { panic!("failed to open database: {:?}", e) }
  };

  let write_opts = WriteOptions::new();
  match database.put(write_opts, 1, &[1]) {
      Ok(_) => { () },
      Err(e) => { panic!("failed to write to database: {:?}", e) }
  };

  let read_opts = ReadOptions::new();
  let res = database.get(read_opts, 1);

  match res {
    Ok(data) => {
      assert!(data.is_some());
      assert_eq!(data, Some(vec![1]));
    }
    Err(e) => { panic!("failed reading data: {:?}", e) }
  }

  let read_opts = ReadOptions::new();
  let mut iter = database.iter(read_opts);
  let entry = iter.next();
  assert_eq!(
    entry,
    Some((1, vec![1]))
  );
}

Open issues

  • Filter policies are missing
  • Iterators with arbirary start and end points are unsupported

License

MIT, see LICENSE

Issues

Collection of the latest Issues

Sword-Smith

Sword-Smith

0

How do I use this database in tests? Do I have to create a file on disk, or is there an in-memory mock object I can use instead?

palkeo

palkeo

1

Hi,

I think this library is missing the method to estimate the on-disk size of a certain key range, that's in the leveldb library.

If I make a PR to implement that is there a chance to get it upstreamed?

Thanks for your library, it's very useful !

palkeo

palkeo

bug
2

If I instantiate a reverse iterator like so:

The first key it yields is the a key that's not in the [stop_key, start_key] range, but is actually the first one after start_key.

Example: if my db has keys ["a", "c", "e", "g"] and start_key is "d", stop_key is "a". Then the first key yielded is "e", which is outside of the "d"->"a" range I am asking.

Kixunil

Kixunil

10

I'm wondering why there are so many traits in the crate. Traits are used for abstractions, but from what I've seen they don't abstract anything. Wouldn't it be simpler to not have them? Or, if abstraction is desirable, move the traits into separate crate and generalize them little bit more. This way they could be implemented for various databases. (So people who want to use other databases don't need to have leveldb in their dependencies.)

I imagine something like this:


trait KVDatabase {
    type BorrowedKey;
    /// Value that is returned from `get` method
    type OwnedValue: Borrow<Self::BorrowedValue>;
    /// Value that can be passed to `put` and `delete` methods
    type BorrowedValue;
    type GetError;
    type PutError;
    type DeleteError;
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">get</span></span>(&amp;<span class="hljs-keyword">self</span>, &amp;Self::BorrowedKey) -&gt; <span class="hljs-built_in">Result</span>&lt;<span class="hljs-built_in">Option</span>&lt;Self::OwnedValue&gt;, Self::GetError&gt;;
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">put</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, &amp;Self::BorrowedKey, Self::BorrowedValue) -&gt; <span class="hljs-built_in">Result</span>&lt;(), Self::PutError&gt;;
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">delete</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, &amp;Self::BorrowedKey) -&gt; <span class="hljs-built_in">Result</span>&lt;<span class="hljs-built_in">bool</span>, Self::DeleteError&gt;;

}

trait IterKVDatabase: KVDatabase { type OwnedKey: Borrow<Self::BorrowedKey>; type KVIterator: Iterator<Item=(Self::OwnedKey, Self::OwnedValue)>; type KeyIterator: Iterator<Item=Self::OwnedKey>; type ValueIterator: Iterator<Item=Self::OwnedValue>;

fn iter(&self) -> Self::KVIterator; fn iter_keys(&self) -> Self::KeyIterator; fn iter_values(&self) -> Self::ValueIterator; }

astro

astro

1

It seems that these things are being refactored. I'd like to keep this issue open until these things have been resolved so that others don't spend a lot of time with these problems like I did.

  • The trait signatures of db-key 0.0.5 and 0.1.0 are very different. As Cargo allows having multiple versions of the same package, a dependency on db-key version * made my project pull in 0.1.0 while the leveldb crate still uses 0.0.5, rendering implementation of Key impossible for me. Limiting my db-key dependency to 0.0.5 fixed that although that doesn't feel very future-proof.
  • Am I supposed to pull in multiple crates (leveldb and db-key) into my own project for one piece of functionality (leveldb). I know modularity is good, but perhaps a re-export would make things easier, especially to avoid getting different versions of db-key.
  • The crate is called db-key but gets mangled by cargo/rustc to db_key, all while the repo is called key. That's confusing. Did I find the right repository?
  • levledb-0.8.1 is tagged in git but not released on crates.io.

I might add more as I progress. Note that I'm still a Rust beginner.

enex

enex

16

In my opinion the way the key is implemented is not very userfriendly, becaus the user has to define a new type to use it with the database. Instead just using &[u8] or the following would make it much simpler.

Another way would be to implement Key for &[u8].

Information - Updated Feb 28, 2022

Stars: 147
Forks: 42
Issues: 16

Repositories & Extras

macOS/iOS Security framework for Rust

MIT license (LICENSE-MIT or

macOS/iOS Security framework for Rust

A fast and flexible CSV reader and writer for Rust, with support for Serde

Dual-licensed under MIT or the If you're new to Rust, the

A fast and flexible CSV reader and writer for Rust, with support for Serde

Fast linear time &amp; space suffix arrays for Rust

Dual-licensed under MIT or the If you just want the details on how construction algorithm used, see the

Fast linear time &amp; space suffix arrays for Rust

OpenGliderNetwork client for Rust based on actix

MIT license (LICENSE-MIT or

OpenGliderNetwork client for Rust based on actix

The arkworks ecosystem consist of Rust libraries for designing and working with zero knowledge succinct...

This library is released under the MIT License and the Apache v2 License (see License)

The arkworks ecosystem consist of Rust libraries for designing and working with zero knowledge succinct...

Threshold Secret Sharing

Efficient pure-Rust library for MIT license (LICENSE-MIT or

Threshold Secret Sharing

The low-level io_uring userspace interface for Rust

The low-level MIT license (LICENSE-MIT or

The low-level io_uring userspace interface for Rust

An attempt to build a compiler from scratch for a simple language, using Rust

New issues and pull requests are welcome on GitHub at MIT License

An attempt to build a compiler from scratch for a simple language, using Rust

iCalendar in Rust

Please double check the MIT license (LICENSE-MIT or issues or comprehensive pull requests are welcome!

iCalendar in Rust

Rust library for low-level abstraction of MIPS32 processors

This project is licensed under the terms of the MIT license

Rust library for low-level abstraction of MIPS32 processors

A pure Rust library for reading/writing Windows

A pure Rust library for reading/writing License

A pure Rust library for reading/writing Windows
Facebook Instagram Twitter GitHub Dribbble
Privacy