Skip to content

Commit

Permalink
Allow both borrowed and owned keys
Browse files Browse the repository at this point in the history
  • Loading branch information
skade committed Sep 20, 2017
1 parent f81e086 commit 1dbc8e3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
19 changes: 10 additions & 9 deletions src/database/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::error::Error;
use database::key::Key;
use std::ptr;
use std::slice::from_raw_parts;
use std::borrow::Borrow;
use libc::{c_char, size_t, c_void};
use leveldb_sys::*;

Expand All @@ -16,7 +17,7 @@ pub trait KV<K: Key> {
/// get a value from the database.
///
/// The passed key will be compared using the comparator.
fn get<'a>(&self, options: ReadOptions<'a, K>, key: K) -> Result<Option<Vec<u8>>, Error>;
fn get<'a, BK: Borrow<K>>(&self, options: ReadOptions<'a, K>, key: BK) -> Result<Option<Vec<u8>>, Error>;
/// put a binary value into the database.
///
/// If the key is already present in the database, it will be overwritten.
Expand All @@ -25,14 +26,14 @@ pub trait KV<K: Key> {
///
/// The database will be synced to disc if `options.sync == true`. This is
/// NOT the default.
fn put(&self, options: WriteOptions, key: K, value: &[u8]) -> Result<(), Error>;
fn put<BK: Borrow<K>>(&self, options: WriteOptions, key: BK, value: &[u8]) -> Result<(), Error>;
/// delete a value from the database.
///
/// The passed key will be compared using the comparator.
///
/// The database will be synced to disc if `options.sync == true`. This is
/// NOT the default.
fn delete(&self, options: WriteOptions, key: K) -> Result<(), Error>;
fn delete<BK: Borrow<K>>(&self, options: WriteOptions, key: BK) -> Result<(), Error>;
}

impl<K: Key> KV<K> for Database<K> {
Expand All @@ -44,9 +45,9 @@ impl<K: Key> KV<K> for Database<K> {
///
/// The database will be synced to disc if `options.sync == true`. This is
/// NOT the default.
fn put(&self, options: WriteOptions, key: K, value: &[u8]) -> Result<(), Error> {
fn put<BK: Borrow<K>>(&self, options: WriteOptions, key: BK, value: &[u8]) -> Result<(), Error> {
unsafe {
key.as_slice(|k| {
key.borrow().as_slice(|k| {
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_put(self.database.ptr,
Expand All @@ -73,9 +74,9 @@ impl<K: Key> KV<K> for Database<K> {
///
/// The database will be synced to disc if `options.sync == true`. This is
/// NOT the default.
fn delete(&self, options: WriteOptions, key: K) -> Result<(), Error> {
fn delete<BK: Borrow<K>>(&self, options: WriteOptions, key: BK) -> Result<(), Error> {
unsafe {
key.as_slice(|k| {
key.borrow().as_slice(|k| {
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_delete(self.database.ptr,
Expand All @@ -96,9 +97,9 @@ impl<K: Key> KV<K> for Database<K> {
/// get a value from the database.
///
/// The passed key will be compared using the comparator.
fn get<'a>(&self, options: ReadOptions<'a, K>, key: K) -> Result<Option<Vec<u8>>, Error> {
fn get<'a, BK: Borrow<K>>(&self, options: ReadOptions<'a, K>, key: BK) -> Result<Option<Vec<u8>>, Error> {
unsafe {
key.as_slice(|k| {
key.borrow().as_slice(|k| {
let mut error = ptr::null_mut();
let mut length: size_t = 0;
let c_readoptions = c_readoptions(&options);
Expand Down
6 changes: 4 additions & 2 deletions src/database/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use database::error::Error;
use database::options::ReadOptions;
use database::iterator::{Iterable, Iterator, KeyIterator, ValueIterator};

use std::borrow::Borrow;

#[allow(missing_docs)]
struct RawSnapshot {
db_ptr: *mut leveldb_t,
Expand Down Expand Up @@ -62,9 +64,9 @@ impl<'a, K: Key> Snapshot<'a, K> {
/// fetches a key from the database
///
/// Inserts this snapshot into ReadOptions before reading
pub fn get(&'a self,
pub fn get<BK: Borrow<K>>(&'a self,
mut options: ReadOptions<'a, K>,
key: K)
key: BK)
-> Result<Option<Vec<u8>>, Error> {
options.snapshot = Some(self);
self.database.get(options, key)
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use utils::{open_database,tmpdir,db_put_simple};
use leveldb::snapshots::Snapshots;
use leveldb::options::{ReadOptions};
use leveldb::iterator::{Iterable};
use leveldb::database::kv::{KV};

#[test]
fn test_snapshots() {
Expand Down

0 comments on commit 1dbc8e3

Please sign in to comment.