Skip to content

Commit

Permalink
relocate build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rim99 committed Sep 22, 2024
1 parent 66d0f69 commit 6504e01
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ readme = "README.md"
homepage = "https://github.com/rim99/leveldb-rs-binding"
repository = "https://github.com/rim99/leveldb-rs-binding"

build = "src/sys/build.rs"
build = "src/build.rs"
links = "leveldb"
autotests = false

Expand Down
23 changes: 9 additions & 14 deletions src/sys/build.rs → src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ fn build_snappy() -> PathBuf {
let libdir = Path::new(&outdir).join(LIBDIR);

env::set_var("NUM_JOBS", num_cpus::get().to_string());
let dest_prefix =
cmake::Config::new(Path::new("deps").join("google-snappy"))
.define("BUILD_SHARED_LIBS", "OFF")
.define("SNAPPY_BUILD_TESTS", "OFF")
.define("SNAPPY_BUILD_BENCHMARKS", "OFF")
.define("HAVE_LIBZ", "OFF")
.define("CMAKE_INSTALL_LIBDIR", &libdir)
.build();
let dest_prefix = cmake::Config::new(Path::new("deps").join("google-snappy"))
.define("BUILD_SHARED_LIBS", "OFF")
.define("SNAPPY_BUILD_TESTS", "OFF")
.define("SNAPPY_BUILD_BENCHMARKS", "OFF")
.define("HAVE_LIBZ", "OFF")
.define("CMAKE_INSTALL_LIBDIR", &libdir)
.build();

assert_eq!(
dest_prefix.join(LIBDIR),
Expand All @@ -41,8 +40,7 @@ fn build_leveldb(snappy_prefix: Option<PathBuf>) {
let libdir = Path::new(&outdir).join(LIBDIR);

env::set_var("NUM_JOBS", num_cpus::get().to_string());
let mut config =
cmake::Config::new(Path::new("deps").join("google-leveldb"));
let mut config = cmake::Config::new(Path::new("deps").join("google-leveldb"));
config
.define("LEVELDB_BUILD_TESTS", "OFF")
.define("LEVELDB_BUILD_BENCHMARKS", "OFF")
Expand All @@ -53,10 +51,7 @@ fn build_leveldb(snappy_prefix: Option<PathBuf>) {
#[cfg(not(target_env = "msvc"))]
let ldflags = format!("-L{}", snappy_prefix.join(LIBDIR).display());

env::set_var(
"LDFLAGS",
ldflags
);
env::set_var("LDFLAGS", ldflags);

config
.define("HAVE_SNAPPY", "ON")
Expand Down
2 changes: 1 addition & 1 deletion src/database/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::error::Error;
use super::Database;
use crate::database::key::from_u8;
use crate::database::key::Key;
use crate::leveldb::*;
use crate::options::{c_writeoptions, WriteOptions};
use crate::sys::leveldb::*;
use libc::{c_char, c_void, size_t};
use std::marker::PhantomData;
use std::ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/database/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Drop for Bytes {
unsafe {
use libc::c_void;

crate::sys::leveldb::leveldb_free(self.bytes as *mut u8 as *mut c_void);
crate::leveldb::leveldb_free(self.bytes as *mut u8 as *mut c_void);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Structs and traits to work with the leveldb cache.
use crate::sys::leveldb::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t};
use crate::leveldb::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t};
use libc::size_t;

#[allow(missing_docs)]
Expand Down
2 changes: 1 addition & 1 deletion src/database/compaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Compaction
use super::key::Key;
use super::Database;
use crate::sys::leveldb::leveldb_compact_range;
use crate::leveldb::leveldb_compact_range;
use libc::{c_char, size_t};

pub trait Compaction<'a, K: Key + 'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/database/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Databases written with one Comparator cannot be opened with another.
use crate::database::key::from_u8;
use crate::database::key::Key;
use crate::sys::leveldb::*;
use crate::leveldb::*;
use libc::{c_char, c_void, size_t};
use std::cmp::Ordering;
use std::marker::PhantomData;
Expand Down
2 changes: 1 addition & 1 deletion src/database/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The module defining custom leveldb error type.
use crate::sys::leveldb::leveldb_free;
use crate::leveldb::leveldb_free;
use libc::c_char;
use libc::c_void;
use std;
Expand Down
2 changes: 1 addition & 1 deletion src/database/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::key::{from_u8, Key};
use super::options::{c_readoptions, ReadOptions};
use super::Database;
use crate::sys::leveldb::{
use crate::leveldb::{
leveldb_create_iterator, leveldb_iter_destroy, leveldb_iter_key, leveldb_iter_next,
leveldb_iter_prev, leveldb_iter_seek, leveldb_iter_seek_to_first, leveldb_iter_seek_to_last,
leveldb_iter_valid, leveldb_iter_value, leveldb_iterator_t, leveldb_readoptions_destroy,
Expand Down
2 changes: 1 addition & 1 deletion src/database/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use super::Database;
use super::bytes::Bytes;
use super::error::Error;
use crate::database::key::Key;
use crate::leveldb::*;
use crate::options::{c_readoptions, c_writeoptions, ReadOptions, WriteOptions};
use crate::sys::leveldb::*;
use libc::{c_char, size_t};
use std::borrow::Borrow;
use std::ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/database/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ffi::CString;
use std::path::Path;
use std::ptr;

use crate::sys::leveldb::{leveldb_destroy_db, leveldb_repair_db};
use crate::leveldb::{leveldb_destroy_db, leveldb_repair_db};

/// destroy a database. You shouldn't hold a handle on the database anywhere at that time.
pub fn destroy(name: &Path, options: Options) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The main database module, allowing to interface with leveldb on
//! a key-value basis.
use crate::sys::leveldb::*;
use crate::leveldb::*;

use self::error::Error;
use self::options::{c_options, Options};
Expand Down
2 changes: 1 addition & 1 deletion src/database/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! * `Options`: used when opening a database
//! * `ReadOptions`: used when reading from leveldb
//! * `WriteOptions`: used when writng to leveldb
use crate::sys::leveldb::*;
use crate::leveldb::*;

use crate::database::cache::Cache;
use crate::database::key::Key;
Expand Down
4 changes: 2 additions & 2 deletions src/database/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! Snapshots give you a reference to the database at a certain
//! point in time and won't change while you work with them.
use crate::sys::leveldb::{leveldb_create_snapshot, leveldb_release_snapshot};
use crate::sys::leveldb::{leveldb_snapshot_t, leveldb_t};
use crate::leveldb::{leveldb_create_snapshot, leveldb_release_snapshot};
use crate::leveldb::{leveldb_snapshot_t, leveldb_t};

use crate::database::key::Key;
use crate::database::kv::KV;
Expand Down
130 changes: 101 additions & 29 deletions src/sys/mod.rs → src/leveldb.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
#![warn(missing_docs)]
/// Low level binding of LevelDB
pub mod leveldb {
#![allow(
non_camel_case_types,
non_upper_case_globals,
non_snake_case,
missing_docs
)]

use libc::{c_char, c_int, c_uchar, c_void};
use libc::size_t;
use libc::{c_char, c_int, c_uchar, c_void};
// These are opaque types that LevelDB uses.
opaque!{
opaque! {
/// Opaque handle representing an opened database. The handle is thread-safe.
pub struct leveldb_t;
pub struct leveldb_cache_t;
Expand Down Expand Up @@ -35,10 +37,10 @@ opaque!{
}

#[repr(C)]
#[derive(Copy,Clone)]
#[derive(Copy, Clone)]
pub enum Compression {
No = 0,
Snappy = 1
No = 0,
Snappy = 1,
}

extern "C" {
Expand All @@ -50,27 +52,82 @@ extern "C" {
/// If this operation fails,
/// - `leveldb_t` is a nullpointer
/// - `errptr` contains more information about the error reason
pub fn leveldb_open(options: *const leveldb_options_t, name: *const c_char, errptr: *mut *mut c_char) -> *mut leveldb_t;
pub fn leveldb_open(
options: *const leveldb_options_t,
name: *const c_char,
errptr: *mut *mut c_char,
) -> *mut leveldb_t;
/// Close the database represented by a `leveldb_t` handle
///
/// Note that this operation cannot fail.
pub fn leveldb_close(db: *mut leveldb_t);
pub fn leveldb_put(db: *mut leveldb_t, options: *const leveldb_writeoptions_t, key: *const c_char, keylen: size_t, val: *const c_char, vallen: size_t, errptr: *mut *mut c_char);
pub fn leveldb_delete(db: *mut leveldb_t, options: *const leveldb_writeoptions_t, key: *const c_char, keylen: size_t, errptr: *mut *mut c_char);
pub fn leveldb_write(db: *mut leveldb_t, options: *const leveldb_writeoptions_t, batch: *mut leveldb_writebatch_t, errptr: *mut *mut c_char);
pub fn leveldb_get(db: *mut leveldb_t, options: *const leveldb_readoptions_t, key: *const c_char, keylen: size_t, vallen: *mut size_t, errptr: *mut *mut c_char) -> *mut c_char;
pub fn leveldb_create_iterator(db: *mut leveldb_t, options: *const leveldb_readoptions_t) -> *mut leveldb_iterator_t;
pub fn leveldb_put(
db: *mut leveldb_t,
options: *const leveldb_writeoptions_t,
key: *const c_char,
keylen: size_t,
val: *const c_char,
vallen: size_t,
errptr: *mut *mut c_char,
);
pub fn leveldb_delete(
db: *mut leveldb_t,
options: *const leveldb_writeoptions_t,
key: *const c_char,
keylen: size_t,
errptr: *mut *mut c_char,
);
pub fn leveldb_write(
db: *mut leveldb_t,
options: *const leveldb_writeoptions_t,
batch: *mut leveldb_writebatch_t,
errptr: *mut *mut c_char,
);
pub fn leveldb_get(
db: *mut leveldb_t,
options: *const leveldb_readoptions_t,
key: *const c_char,
keylen: size_t,
vallen: *mut size_t,
errptr: *mut *mut c_char,
) -> *mut c_char;
pub fn leveldb_create_iterator(
db: *mut leveldb_t,
options: *const leveldb_readoptions_t,
) -> *mut leveldb_iterator_t;
pub fn leveldb_create_snapshot(db: *mut leveldb_t) -> *mut leveldb_snapshot_t;
pub fn leveldb_release_snapshot(db: *mut leveldb_t, snapshot: *const leveldb_snapshot_t);
pub fn leveldb_property_value(db: *mut leveldb_t, propname: *const c_char) -> *mut c_char;

// TODO: const'ness of pointers here is in question
pub fn leveldb_approximate_sizes(db: *mut leveldb_t, num_ranges: c_int, range_start_key: *const *const c_char, range_start_key_len: *const size_t, range_limit_key: *const *const c_char, range_limit_key_len: *const size_t, sizes: *mut u64);
pub fn leveldb_compact_range(db: *mut leveldb_t, start_key: *const c_char, start_key_len: size_t, limit_key: *const c_char, limit_key_len: size_t);
pub fn leveldb_approximate_sizes(
db: *mut leveldb_t,
num_ranges: c_int,
range_start_key: *const *const c_char,
range_start_key_len: *const size_t,
range_limit_key: *const *const c_char,
range_limit_key_len: *const size_t,
sizes: *mut u64,
);
pub fn leveldb_compact_range(
db: *mut leveldb_t,
start_key: *const c_char,
start_key_len: size_t,
limit_key: *const c_char,
limit_key_len: size_t,
);

// Management operations
pub fn leveldb_destroy_db(options: *const leveldb_options_t, name: *const c_char, errptr: *mut *mut c_char);
pub fn leveldb_repair_db(options: *const leveldb_options_t, name: *const c_char, errptr: *mut *mut c_char);
pub fn leveldb_destroy_db(
options: *const leveldb_options_t,
name: *const c_char,
errptr: *mut *mut c_char,
);
pub fn leveldb_repair_db(
options: *const leveldb_options_t,
name: *const c_char,
errptr: *mut *mut c_char,
);

// Iterator
pub fn leveldb_iter_destroy(it: *mut leveldb_iterator_t);
Expand All @@ -88,13 +145,23 @@ extern "C" {
pub fn leveldb_writebatch_create() -> *mut leveldb_writebatch_t;
pub fn leveldb_writebatch_destroy(b: *mut leveldb_writebatch_t);
pub fn leveldb_writebatch_clear(b: *mut leveldb_writebatch_t);
pub fn leveldb_writebatch_put(b: *mut leveldb_writebatch_t, key: *const c_char, keylen: size_t, val: *const c_char, vallen: size_t);
pub fn leveldb_writebatch_delete(b: *mut leveldb_writebatch_t, key: *const c_char, keylen: size_t);
pub fn leveldb_writebatch_put(
b: *mut leveldb_writebatch_t,
key: *const c_char,
keylen: size_t,
val: *const c_char,
vallen: size_t,
);
pub fn leveldb_writebatch_delete(
b: *mut leveldb_writebatch_t,
key: *const c_char,
keylen: size_t,
);
pub fn leveldb_writebatch_iterate(
b: *mut leveldb_writebatch_t,
state: *mut c_void,
put: extern fn(*mut c_void, *const c_char, size_t, *const c_char, size_t),
deleted: extern fn(*mut c_void, *const c_char, size_t)
put: extern "C" fn(*mut c_void, *const c_char, size_t, *const c_char, size_t),
deleted: extern "C" fn(*mut c_void, *const c_char, size_t),
);

// Options
Expand All @@ -103,7 +170,10 @@ extern "C" {
/// Deallocate a `leveldb_options_t` handle (not the database!)
pub fn leveldb_options_destroy(o: *mut leveldb_options_t);
pub fn leveldb_options_set_comparator(o: *mut leveldb_options_t, c: *mut leveldb_comparator_t);
pub fn leveldb_options_set_filter_policy(o: *mut leveldb_options_t, c: *mut leveldb_filterpolicy_t);
pub fn leveldb_options_set_filter_policy(
o: *mut leveldb_options_t,
c: *mut leveldb_filterpolicy_t,
);
/// Modify `o` to specify whether a new database should be created if none exists yet
///
/// - If `val` is != 0, new database creation is enabled
Expand All @@ -123,9 +193,9 @@ extern "C" {
// Comparator
pub fn leveldb_comparator_create(
state: *mut c_void,
destructor: extern fn(*mut c_void),
compare: extern fn(*mut c_void, *const c_char, size_t, *const c_char, size_t) -> c_int,
name: extern fn(*mut c_void) -> *const c_char
destructor: extern "C" fn(*mut c_void),
compare: extern "C" fn(*mut c_void, *const c_char, size_t, *const c_char, size_t) -> c_int,
name: extern "C" fn(*mut c_void) -> *const c_char,
) -> *mut leveldb_comparator_t;
pub fn leveldb_comparator_destroy(c: *mut leveldb_comparator_t);

Expand Down Expand Up @@ -154,7 +224,10 @@ extern "C" {
pub fn leveldb_readoptions_destroy(o: *mut leveldb_readoptions_t);
pub fn leveldb_readoptions_set_verify_checksums(o: *mut leveldb_readoptions_t, val: c_uchar);
pub fn leveldb_readoptions_set_fill_cache(o: *mut leveldb_readoptions_t, val: c_uchar);
pub fn leveldb_readoptions_set_snapshot(o: *mut leveldb_readoptions_t, snapshot: *const leveldb_snapshot_t);
pub fn leveldb_readoptions_set_snapshot(
o: *mut leveldb_readoptions_t,
snapshot: *const leveldb_snapshot_t,
);

// Write options
pub fn leveldb_writeoptions_create() -> *mut leveldb_writeoptions_t;
Expand All @@ -176,4 +249,3 @@ extern "C" {
pub fn leveldb_major_version() -> c_int;
pub fn leveldb_minor_version() -> c_int;
}
} // mod leveldb
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pub use crate::database::kv;
pub use crate::database::management;
pub use crate::database::options;
pub use crate::database::snapshots;
pub use crate::sys::leveldb::{leveldb_major_version, leveldb_minor_version};
pub use crate::leveldb::{leveldb_major_version, leveldb_minor_version};

#[allow(missing_docs)]
pub mod database;
pub mod sys;
pub mod leveldb;

/// Library version information
///
Expand Down
2 changes: 1 addition & 1 deletion src/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod database;
pub mod sys;
pub mod leveldb;

0 comments on commit 6504e01

Please sign in to comment.