Skip to content

Commit

Permalink
remove as_slice from trait Serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
rim99 committed Sep 22, 2024
1 parent 9be8c3d commit 71a6e7f
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 102 deletions.
34 changes: 17 additions & 17 deletions src/database/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use super::error::Error;
use super::Database;
use crate::binding::*;
use crate::database::serializable::from_u8;
use crate::database::serializable::Serializable;
use crate::binding::*;
use crate::options::{c_writeoptions, WriteOptions};
use libc::{c_char, c_void, size_t};
use std::marker::PhantomData;
Expand Down Expand Up @@ -79,28 +79,28 @@ impl<K: Serializable> Writebatch<K> {
/// Batch a put operation
pub fn put(&mut self, key: K, value: &[u8]) {
unsafe {
key.as_slice(|k| {
leveldb_writebatch_put(
self.writebatch.ptr,
k.as_ptr() as *mut c_char,
k.len() as size_t,
value.as_ptr() as *mut c_char,
value.len() as size_t,
);
})
let key = &key.as_u8();

leveldb_writebatch_put(
self.writebatch.ptr,
(key.as_ptr()) as *mut c_char,
(key.len()) as size_t,
value.as_ptr() as *mut c_char,
value.len() as size_t,
);
}
}

/// Batch a delete operation
pub fn delete(&mut self, key: K) {
unsafe {
key.as_slice(|k| {
leveldb_writebatch_delete(
self.writebatch.ptr,
k.as_ptr() as *mut c_char,
k.len() as size_t,
);
})
let key = &key.as_u8();

leveldb_writebatch_delete(
self.writebatch.ptr,
key.as_ptr() as *mut c_char,
key.len() as size_t,
);
}
}

Expand Down
20 changes: 9 additions & 11 deletions src/database/compaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ pub trait Compaction<'a, K: Serializable + 'a> {
impl<'a, K: Serializable + 'a> Compaction<'a, K> for Database<K> {
fn compact(&self, start: &'a K, limit: &'a K) {
unsafe {
start.as_slice(|s| {
limit.as_slice(|l| {
leveldb_compact_range(
self.database.ptr,
s.as_ptr() as *mut c_char,
s.len() as size_t,
l.as_ptr() as *mut c_char,
l.len() as size_t,
);
});
});
let start = &start.as_u8();
let limit = &limit.as_u8();
leveldb_compact_range(
self.database.ptr,
start.as_ptr() as *mut c_char,
start.len() as size_t,
limit.as_ptr() as *mut c_char,
limit.len() as size_t,
)
}
}
}
2 changes: 1 addition & 1 deletion src/database/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//! Comparators allow to override this comparison.
//! The ordering of keys introduced by the compartor influences iteration order.
//! Databases written with one Comparator cannot be opened with another.
use crate::binding::*;
use crate::database::serializable::from_u8;
use crate::database::serializable::Serializable;
use crate::binding::*;
use libc::{c_char, c_void, size_t};
use std::cmp::Ordering;
use std::marker::PhantomData;
Expand Down
16 changes: 8 additions & 8 deletions src/database/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! Iteration is one of the most important parts of leveldb. This module provides
//! Iterators to iterate over key, values and pairs of both.
use super::serializable::{from_u8, Serializable};
use super::options::{c_readoptions, ReadOptions};
use super::serializable::{from_u8, Serializable};
use super::Database;
use crate::binding::{
leveldb_create_iterator, leveldb_iter_destroy, leveldb_iter_key, leveldb_iter_next,
Expand Down Expand Up @@ -186,13 +186,13 @@ pub trait LevelDBIterator<'a, K: Serializable> {

fn seek(&self, key: &K) {
unsafe {
key.as_slice(|k| {
leveldb_iter_seek(
self.raw_iterator(),
k.as_ptr() as *mut c_char,
k.len() as size_t,
);
})
let key = &key.as_u8();

leveldb_iter_seek(
self.raw_iterator(),
key.as_ptr() as *mut c_char,
key.len() as size_t,
);
}
}
}
Expand Down
109 changes: 53 additions & 56 deletions src/database/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::Database;

use super::bytes::Bytes;
use super::error::Error;
use crate::database::serializable::Serializable;
use crate::binding::*;
use crate::database::serializable::Serializable;
use crate::options::{c_readoptions, c_writeoptions, ReadOptions, WriteOptions};
use libc::{c_char, size_t};
use std::borrow::Borrow;
Expand Down Expand Up @@ -69,26 +69,25 @@ impl<K: Serializable> KV<K> for Database<K> {
value: &[u8],
) -> Result<(), Error> {
unsafe {
key.borrow().as_slice(|k| {
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_put(
self.database.ptr,
c_writeoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
value.as_ptr() as *mut c_char,
value.len() as size_t,
&mut error,
);
leveldb_writeoptions_destroy(c_writeoptions);
let k = key.borrow().as_u8();
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_put(
self.database.ptr,
c_writeoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
value.as_ptr() as *mut c_char,
value.len() as size_t,
&mut error,
);
leveldb_writeoptions_destroy(c_writeoptions);

if error == ptr::null_mut() {
Ok(())
} else {
Err(Error::new_from_char(error))
}
})
if error == ptr::null_mut() {
Ok(())
} else {
Err(Error::new_from_char(error))
}
}
}

Expand All @@ -100,23 +99,22 @@ impl<K: Serializable> KV<K> for Database<K> {
/// NOT the default.
fn delete<BK: Borrow<K>>(&self, options: WriteOptions, key: BK) -> Result<(), Error> {
unsafe {
key.borrow().as_slice(|k| {
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_delete(
self.database.ptr,
c_writeoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
&mut error,
);
leveldb_writeoptions_destroy(c_writeoptions);
if error == ptr::null_mut() {
Ok(())
} else {
Err(Error::new_from_char(error))
}
})
let k = key.borrow().as_u8();
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);
leveldb_delete(
self.database.ptr,
c_writeoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
&mut error,
);
leveldb_writeoptions_destroy(c_writeoptions);
if error == ptr::null_mut() {
Ok(())
} else {
Err(Error::new_from_char(error))
}
}
}

Expand All @@ -126,26 +124,25 @@ impl<K: Serializable> KV<K> for Database<K> {
key: BK,
) -> Result<Option<Bytes>, Error> {
unsafe {
key.borrow().as_slice(|k| {
let mut error = ptr::null_mut();
let mut length: size_t = 0;
let c_readoptions = c_readoptions(&options);
let result = leveldb_get(
self.database.ptr,
c_readoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
&mut length,
&mut error,
);
leveldb_readoptions_destroy(c_readoptions);
let k = key.borrow().as_u8();
let mut error = ptr::null_mut();
let mut length: size_t = 0;
let c_readoptions = c_readoptions(&options);
let result = leveldb_get(
self.database.ptr,
c_readoptions,
k.as_ptr() as *mut c_char,
k.len() as size_t,
&mut length,
&mut error,
);
leveldb_readoptions_destroy(c_readoptions);

if error == ptr::null_mut() {
Ok(Bytes::from_raw(result as *mut u8, length))
} else {
Err(Error::new_from_char(error))
}
})
if error == ptr::null_mut() {
Ok(Bytes::from_raw(result as *mut u8, length))
} else {
Err(Error::new_from_char(error))
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub mod compaction;
pub mod comparator;
pub mod error;
pub mod iterator;
pub mod serializable;
pub mod kv;
pub mod management;
pub mod options;
pub mod serializable;
pub mod snapshots;

#[allow(missing_docs)]
Expand Down
4 changes: 0 additions & 4 deletions src/database/serializable.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
pub trait Serializable {
fn from_u8(key: &[u8]) -> Self;
fn as_u8(&self) -> Vec<u8>;
fn as_slice<T, F: Fn(&[u8]) -> T>(&self, f: F) -> T {
let dst = self.as_u8();
f(&dst)
}
}

pub fn from_u8<K: Serializable>(key: &[u8]) -> K {
Expand Down
2 changes: 1 addition & 1 deletion src/database/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use crate::binding::{leveldb_create_snapshot, leveldb_release_snapshot};
use crate::binding::{leveldb_snapshot_t, leveldb_t};

use crate::database::serializable::Serializable;
use crate::database::kv::KV;
use crate::database::serializable::Serializable;
use crate::database::Database;

use crate::database::error::Error;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern crate libc;
#[macro_use]
extern crate ffi_opaque;

pub use crate::binding::{leveldb_major_version, leveldb_minor_version};
pub use crate::database::batch;
pub use crate::database::compaction;
pub use crate::database::comparator;
Expand All @@ -56,11 +57,10 @@ pub use crate::database::kv;
pub use crate::database::management;
pub use crate::database::options;
pub use crate::database::snapshots;
pub use crate::binding::{leveldb_major_version, leveldb_minor_version};

mod binding;
#[allow(missing_docs)]
pub mod database;
mod binding;

/// Library version information
///
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use leveldb::database::serializable::Serializable;
use leveldb::database::kv::KV;
use leveldb::database::serializable::Serializable;
use leveldb::database::Database;
use leveldb::options::{Options, WriteOptions};
use std::path::Path;
Expand Down

0 comments on commit 71a6e7f

Please sign in to comment.