Skip to content

Commit

Permalink
Rename key.rs -> serializable.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rim99 committed Sep 22, 2024
1 parent 52acb32 commit 16e6e82
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 66 deletions.
18 changes: 9 additions & 9 deletions src/database/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use super::error::Error;
use super::Database;
use crate::database::key::from_u8;
use crate::database::key::Key;
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};
Expand All @@ -25,19 +25,19 @@ impl Drop for RawWritebatch {
}

#[allow(missing_docs)]
pub struct Writebatch<K: Key> {
pub struct Writebatch<K: Serializable> {
#[allow(dead_code)]
writebatch: RawWritebatch,
marker: PhantomData<K>,
}

/// Batch access to the database
pub trait Batch<K: Key> {
pub trait Batch<K: Serializable> {
/// Write a batch to the database, ensuring success for all items or an error
fn write(&self, options: WriteOptions, batch: &Writebatch<K>) -> Result<(), Error>;
}

impl<K: Key> Batch<K> for Database<K> {
impl<K: Serializable> Batch<K> for Database<K> {
fn write(&self, options: WriteOptions, batch: &Writebatch<K>) -> Result<(), Error> {
unsafe {
let mut error = ptr::null_mut();
Expand All @@ -60,7 +60,7 @@ impl<K: Key> Batch<K> for Database<K> {
}
}

impl<K: Key> Writebatch<K> {
impl<K: Serializable> Writebatch<K> {
/// Create a new writebatch
pub fn new() -> Writebatch<K> {
let ptr = unsafe { leveldb_writebatch_create() };
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<K: Key> Writebatch<K> {
/// A trait for iterators to iterate over written batches and check their validity.
pub trait WritebatchIterator {
/// The database key type this iterates over
type K: Key;
type K: Serializable;

/// Callback for put items
fn put(&mut self, key: Self::K, value: &[u8]);
Expand All @@ -131,7 +131,7 @@ pub trait WritebatchIterator {
fn deleted(&mut self, key: Self::K);
}

extern "C" fn put_callback<K: Key, T: WritebatchIterator<K = K>>(
extern "C" fn put_callback<K: Serializable, T: WritebatchIterator<K = K>>(
state: *mut c_void,
key: *const c_char,
keylen: size_t,
Expand All @@ -147,7 +147,7 @@ extern "C" fn put_callback<K: Key, T: WritebatchIterator<K = K>>(
}
}

extern "C" fn deleted_callback<K: Key, T: WritebatchIterator<K = K>>(
extern "C" fn deleted_callback<K: Serializable, T: WritebatchIterator<K = K>>(
state: *mut c_void,
key: *const c_char,
keylen: size_t,
Expand Down
6 changes: 3 additions & 3 deletions src/database/compaction.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Compaction
use super::key::Key;
use super::serializable::Serializable;
use super::Database;
use crate::binding::leveldb_compact_range;
use libc::{c_char, size_t};

pub trait Compaction<'a, K: Key + 'a> {
pub trait Compaction<'a, K: Serializable + 'a> {
fn compact(&self, start: &'a K, limit: &'a K);
}

impl<'a, K: Key + 'a> Compaction<'a, K> for Database<K> {
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| {
Expand Down
12 changes: 6 additions & 6 deletions src/database/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//! 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::database::key::from_u8;
use crate::database::key::Key;
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;
Expand All @@ -19,7 +19,7 @@ use std::slice;
/// * The comparison implementation
pub trait Comparator {
/// The type that the comparator compares.
type K: Key;
type K: Serializable;

/// Return the name of the Comparator
fn name(&self) -> *const c_char;
Expand All @@ -32,12 +32,12 @@ pub trait Comparator {
}

/// OrdComparator is a comparator comparing Keys that implement `Ord`
pub struct OrdComparator<K: Key + Ord> {
pub struct OrdComparator<K: Serializable + Ord> {
name: String,
marker: PhantomData<K>,
}

impl<K: Key + Ord> OrdComparator<K> {
impl<K: Serializable + Ord> OrdComparator<K> {
/// Create a new OrdComparator
pub fn new(name: &str) -> OrdComparator<K> {
OrdComparator {
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn create_comparator<T: Comparator>(x: Box<T>) -> *mut leveldb_comparator_t
}
}

impl<K: Key + Ord> Comparator for OrdComparator<K> {
impl<K: Serializable + Ord> Comparator for OrdComparator<K> {
type K = K;

fn name(&self) -> *const c_char {
Expand Down
34 changes: 17 additions & 17 deletions src/database/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! 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::key::{from_u8, Key};
use super::serializable::{from_u8, Serializable};
use super::options::{c_readoptions, ReadOptions};
use super::Database;
use crate::binding::{
Expand Down Expand Up @@ -30,7 +30,7 @@ impl Drop for RawIterator {
/// An iterator over the leveldb keyspace.
///
/// Returns key and value as a tuple.
pub struct Iterator<'a, K: Key + 'a> {
pub struct Iterator<'a, K: Serializable + 'a> {
start: bool,
// Iterator accesses the Database through a leveldb_iter_t pointer
// but needs to hold the reference for lifetime tracking
Expand All @@ -44,7 +44,7 @@ pub struct Iterator<'a, K: Key + 'a> {
/// An iterator over the leveldb keyspace that browses the keys backwards.
///
/// Returns key and value as a tuple.
pub struct RevIterator<'a, K: Key + 'a> {
pub struct RevIterator<'a, K: Serializable + 'a> {
start: bool,
// Iterator accesses the Database through a leveldb_iter_t pointer
// but needs to hold the reference for lifetime tracking
Expand All @@ -58,33 +58,33 @@ pub struct RevIterator<'a, K: Key + 'a> {
/// An iterator over the leveldb keyspace.
///
/// Returns just the keys.
pub struct KeyIterator<'a, K: Key + 'a> {
pub struct KeyIterator<'a, K: Serializable + 'a> {
inner: Iterator<'a, K>,
}

/// An iterator over the leveldb keyspace that browses the keys backwards.
///
/// Returns just the keys.
pub struct RevKeyIterator<'a, K: Key + 'a> {
pub struct RevKeyIterator<'a, K: Serializable + 'a> {
inner: RevIterator<'a, K>,
}

/// An iterator over the leveldb keyspace.
///
/// Returns just the value.
pub struct ValueIterator<'a, K: Key + 'a> {
pub struct ValueIterator<'a, K: Serializable + 'a> {
inner: Iterator<'a, K>,
}

/// An iterator over the leveldb keyspace that browses the keys backwards.
///
/// Returns just the value.
pub struct RevValueIterator<'a, K: Key + 'a> {
pub struct RevValueIterator<'a, K: Serializable + 'a> {
inner: RevIterator<'a, K>,
}

/// A trait to allow access to the three main iteration styles of leveldb.
pub trait Iterable<'a, K: Key + 'a> {
pub trait Iterable<'a, K: Serializable + 'a> {
/// Return an Iterator iterating over (Key,Value) pairs
fn iter(&'a self, options: ReadOptions<'a, K>) -> Iterator<K>;
/// Returns an Iterator iterating over Keys only.
Expand All @@ -93,7 +93,7 @@ pub trait Iterable<'a, K: Key + 'a> {
fn value_iter(&'a self, options: ReadOptions<'a, K>) -> ValueIterator<K>;
}

impl<'a, K: Key + 'a> Iterable<'a, K> for Database<K> {
impl<'a, K: Serializable + 'a> Iterable<'a, K> for Database<K> {
fn iter(&'a self, options: ReadOptions<'a, K>) -> Iterator<K> {
Iterator::new(self, options)
}
Expand All @@ -109,7 +109,7 @@ impl<'a, K: Key + 'a> Iterable<'a, K> for Database<K> {

#[allow(missing_docs)]
#[allow(unused_attributes)]
pub trait LevelDBIterator<'a, K: Key> {
pub trait LevelDBIterator<'a, K: Serializable> {
type RevIter: LevelDBIterator<'a, K>;

#[inline]
Expand Down Expand Up @@ -197,7 +197,7 @@ pub trait LevelDBIterator<'a, K: Key> {
}
}

impl<'a, K: Key> Iterator<'a, K> {
impl<'a, K: Serializable> Iterator<'a, K> {
fn new(database: &'a Database<K>, options: ReadOptions<'a, K>) -> Iterator<'a, K> {
unsafe {
let c_readoptions = c_readoptions(&options);
Expand All @@ -221,7 +221,7 @@ impl<'a, K: Key> Iterator<'a, K> {
}
}

impl<'a, K: Key> LevelDBIterator<'a, K> for Iterator<'a, K> {
impl<'a, K: Serializable> LevelDBIterator<'a, K> for Iterator<'a, K> {
type RevIter = RevIterator<'a, K>;

#[inline]
Expand Down Expand Up @@ -279,7 +279,7 @@ impl<'a, K: Key> LevelDBIterator<'a, K> for Iterator<'a, K> {
}
}

impl<'a, K: Key> LevelDBIterator<'a, K> for RevIterator<'a, K> {
impl<'a, K: Serializable> LevelDBIterator<'a, K> for RevIterator<'a, K> {
type RevIter = Iterator<'a, K>;

#[inline]
Expand Down Expand Up @@ -337,7 +337,7 @@ impl<'a, K: Key> LevelDBIterator<'a, K> for RevIterator<'a, K> {
}
}

impl<'a, K: Key> KeyIterator<'a, K> {
impl<'a, K: Serializable> KeyIterator<'a, K> {
fn new(database: &'a Database<K>, options: ReadOptions<'a, K>) -> KeyIterator<'a, K> {
KeyIterator {
inner: Iterator::new(database, options),
Expand All @@ -351,7 +351,7 @@ impl<'a, K: Key> KeyIterator<'a, K> {
}
}

impl<'a, K: Key> ValueIterator<'a, K> {
impl<'a, K: Serializable> ValueIterator<'a, K> {
fn new(database: &'a Database<K>, options: ReadOptions<'a, K>) -> ValueIterator<'a, K> {
ValueIterator {
inner: Iterator::new(database, options),
Expand All @@ -367,7 +367,7 @@ impl<'a, K: Key> ValueIterator<'a, K> {

macro_rules! impl_leveldb_iterator {
($T:ty, $RevT:ty) => {
impl<'a, K: Key> LevelDBIterator<'a, K> for $T {
impl<'a, K: Serializable> LevelDBIterator<'a, K> for $T {
type RevIter = $RevT;

#[inline]
Expand Down Expand Up @@ -425,7 +425,7 @@ impl_leveldb_iterator!(RevValueIterator<'a, K>, ValueIterator<'a, K>);

macro_rules! impl_iterator {
($T:ty, $Item:ty, $ItemMethod:ident) => {
impl<'a, K: Key> iter::Iterator for $T {
impl<'a, K: Serializable> iter::Iterator for $T {
type Item = $Item;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
6 changes: 3 additions & 3 deletions src/database/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::Database;

use super::bytes::Bytes;
use super::error::Error;
use crate::database::key::Key;
use crate::database::serializable::Serializable;
use crate::binding::*;
use crate::options::{c_readoptions, c_writeoptions, ReadOptions, WriteOptions};
use libc::{c_char, size_t};
Expand All @@ -13,7 +13,7 @@ use std::ptr;

/// Key-Value-Access to the leveldb database, providing
/// a basic interface.
pub trait KV<K: Key> {
pub trait KV<K: Serializable> {
/// get a value from the database.
///
/// The passed key will be compared using the comparator.
Expand Down Expand Up @@ -53,7 +53,7 @@ pub trait KV<K: Key> {
fn delete<BK: Borrow<K>>(&self, options: WriteOptions, key: BK) -> Result<(), Error>;
}

impl<K: Key> KV<K> for Database<K> {
impl<K: Serializable> KV<K> for Database<K> {
/// put a binary value into the database.
///
/// If the key is already present in the database, it will be overwritten.
Expand Down
12 changes: 6 additions & 6 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ffi::CString;

use std::path::Path;

use self::key::Key;
use self::serializable::Serializable;
use comparator::{create_comparator, Comparator};
use std::ptr;

Expand All @@ -22,7 +22,7 @@ pub mod compaction;
pub mod comparator;
pub mod error;
pub mod iterator;
pub mod key;
pub mod serializable;
pub mod kv;
pub mod management;
pub mod options;
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Drop for RawComparator {
///
/// Multiple Database objects can be kept around, as leveldb synchronises
/// internally.
pub struct Database<K: Key> {
pub struct Database<K: Serializable> {
database: RawDB,
// this holds a reference passed into leveldb
// it is never read from Rust, but must be kept around
Expand All @@ -80,10 +80,10 @@ pub struct Database<K: Key> {
marker: PhantomData<K>,
}

unsafe impl<K: Key> Sync for Database<K> {}
unsafe impl<K: Key> Send for Database<K> {}
unsafe impl<K: Serializable> Sync for Database<K> {}
unsafe impl<K: Serializable> Send for Database<K> {}

impl<K: Key> Database<K> {
impl<K: Serializable> Database<K> {
fn new(
database: *mut leveldb_t,
options: Options,
Expand Down
8 changes: 4 additions & 4 deletions src/database/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::binding::*;

use crate::database::cache::Cache;
use crate::database::key::Key;
use crate::database::serializable::Serializable;
use crate::database::snapshots::Snapshot;
use libc::size_t;

Expand Down Expand Up @@ -93,7 +93,7 @@ impl WriteOptions {

/// The read options to use for any read operation.
#[allow(missing_copy_implementations)]
pub struct ReadOptions<'a, K: Key + 'a> {
pub struct ReadOptions<'a, K: Serializable + 'a> {
/// Whether to verify the saved checksums on read.
///
/// default: false
Expand All @@ -112,7 +112,7 @@ pub struct ReadOptions<'a, K: Key + 'a> {
pub snapshot: Option<&'a Snapshot<'a, K>>,
}

impl<'a, K: Key + 'a> ReadOptions<'a, K> {
impl<'a, K: Serializable + 'a> ReadOptions<'a, K> {
/// Return a `ReadOptions` struct with the default values.
pub fn new() -> ReadOptions<'a, K> {
ReadOptions {
Expand Down Expand Up @@ -164,7 +164,7 @@ pub unsafe fn c_writeoptions(options: WriteOptions) -> *mut leveldb_writeoptions
#[allow(missing_docs)]
pub unsafe fn c_readoptions<'a, K>(options: &ReadOptions<'a, K>) -> *mut leveldb_readoptions_t
where
K: Key,
K: Serializable,
{
let c_readoptions = leveldb_readoptions_create();
leveldb_readoptions_set_verify_checksums(c_readoptions, options.verify_checksums as u8);
Expand Down
Loading

0 comments on commit 16e6e82

Please sign in to comment.