Skip to content

Commit

Permalink
refactor: use submodule for unqlite source
Browse files Browse the repository at this point in the history
  • Loading branch information
zitsen committed Jan 26, 2018
1 parent d2f69a4 commit 4b2409f
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
*.bk
src/ffi.rs
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "unqlite"]
path = unqlite
url = https://github.com/symisc/unqlite.git
19 changes: 11 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
[package]
name = "unqlite"
version = "1.3.2"
version = "1.4.0"
authors = ["Huo Linhe <[email protected]>"]
license = "MIT/Apache-2.0"
readme = "README.md"
homepage = "https://github.com/zitsen/unqlite.rs"
repository = "https://github.com/zitsen/unqlite.rs.git"
documentation = "http://zitsen.github.io/unqlite.rs"
documentation = "https://docs.rs/unqlite"
description = "Rust `unqlite` library wrapper."
keywords = ["unqlite", "kv", "nosql"]

[build-dependencies]
bindgen = "0.32"
cc = "1.0"

[dependencies]
libc = "0.2"
unqlite-sys = "^1.1.0"

[dev-dependencies]
tempfile = "2.1.3"

[features]
default = ["enable-threads"]
# For thread-safe
enable-threads = [ "unqlite-sys/enable-threads" ]
enable-threads = []
# Other features
jx9-disable-builtin-func = [ "unqlite-sys/jx9-disable-builtin-func" ]
jx9-enable-math-func = [ "unqlite-sys/jx9-enable-math-func" ]
jx9-disable-disk-io = [ "unqlite-sys/jx9-disable-disk-io" ]
enable-jx9-hash-io = [ "unqlite-sys/enable-jx9-hash-io" ]
jx9-disable-builtin-func = []
jx9-enable-math-func = []
jx9-disable-disk-io = []
enable-jx9-hash-io = []
70 changes: 70 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
extern crate bindgen;
extern crate cc;

fn main() {
use bindgen::builder;
// Configure and generate bindings.
let bindings = builder()
.header("unqlite/unqlite.h")
.with_codegen_config(bindgen::CodegenConfig {
vars: false,
..Default::default()
})
.generate()
.expect("generate unqlite bindings");
// Write the generated bindings to an output file.
bindings
.write_to_file("src/ffi.rs")
.expect("write to source file");

cc::Build::new()
.file("unqlite/unqlite.c")
.warnings(false)
.if_enable_threads()
.if_jx9_diable_builtin_func()
.if_jx9_enable_math_func()
.if_jx9_disable_disk_io()
.if_enable_jx9_hash_io()
.compile("libunqlite.a");
}
trait ConfigExt {
fn if_enable_threads(&mut self) -> &mut Self {
self
}
fn if_jx9_diable_builtin_func(&mut self) -> &mut Self {
self
}
fn if_jx9_enable_math_func(&mut self) -> &mut Self {
self
}
fn if_jx9_disable_disk_io(&mut self) -> &mut Self {
self
}
fn if_enable_jx9_hash_io(&mut self) -> &mut Self {
self
}
}

impl ConfigExt for cc::Build {
#[cfg(feature = "enable-threads")]
fn if_enable_threads(&mut self) -> &mut Self {
self.define("UNQLITE_ENABLE_THREADS", None)
.flag("-lpthread")
}
#[cfg(feature = "jx9-disable-builtin-func")]
fn if_jx9_diable_builtin_func(&mut self) -> &mut Self {
self.define("JX9_DISABLE_BUILTIN_FUNC", None)
}
#[cfg(feature = "jx9-enable-math-func")]
fn if_jx9_enable_math_func(&mut self) -> &mut Self {
self.define("JX9_ENABLE_MATH_FUNC", None)
}
#[cfg(feature = "jx9-disable-disk-io")]
fn if_jx9_disable_disk_io(&mut self) -> &mut Self {
self.define("JX9_DISABLE_DISK_IO", None)
}
#[cfg(feature = "enable-jx9-hash-io")]
fn if_enable_jx9_hash_io(&mut self) -> &mut Self {
self.define("UNQLITE_ENABLE_JX9_HASH_IO", None)
}
}
6 changes: 2 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use UnQLite;
use error::Wrap;
use ffi::constants::{UNQLITE_CONFIG_JX9_ERR_LOG, UNQLITE_CONFIG_DISABLE_AUTO_COMMIT,
UNQLITE_CONFIG_ERR_LOG, UNQLITE_CONFIG_GET_KV_NAME, UNQLITE_CONFIG_KV_ENGINE,
UNQLITE_CONFIG_MAX_PAGE_CACHE};
use ffi::unqlite_config;
use libc::strlen;
use std::ffi::CString;
use std::mem;
use std::os::raw::c_char;
use std::ptr;
use vars::{UNQLITE_CONFIG_JX9_ERR_LOG, UNQLITE_CONFIG_DISABLE_AUTO_COMMIT, UNQLITE_CONFIG_ERR_LOG,
UNQLITE_CONFIG_GET_KV_NAME, UNQLITE_CONFIG_KV_ENGINE, UNQLITE_CONFIG_MAX_PAGE_CACHE};

/// A `Trait` for configuration.
///
Expand Down Expand Up @@ -153,7 +152,6 @@ fn from_chars_to_string(p: *mut c_char) -> String {
from_chars_to_cstring(p).into_string().unwrap()
}


#[cfg(test)]
#[cfg(feature = "enable-threads")]
mod tests {
Expand Down
13 changes: 5 additions & 8 deletions src/document/doc_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ use ffi::{unqlite_array_add_strkey_elem, unqlite_compile, unqlite_compile_file,
unqlite_vm, unqlite_vm_config, unqlite_vm_dump, unqlite_vm_exec,
unqlite_vm_extract_variable, unqlite_vm_new_array, unqlite_vm_new_scalar,
unqlite_vm_release, unqlite_vm_release_value, unqlite_vm_reset, unqlite_value_int64};
use ffi::constants::{UNQLITE_OK, UNQLITE_VM_CONFIG_ARGV_ENTRY, UNQLITE_VM_CONFIG_CREATE_VAR,
UNQLITE_VM_CONFIG_ENV_ATTR, UNQLITE_VM_CONFIG_ERR_REPORT,
UNQLITE_VM_CONFIG_EXEC_VALUE, UNQLITE_VM_CONFIG_EXTRACT_OUTPUT,
UNQLITE_VM_CONFIG_IMPORT_PATH, UNQLITE_VM_CONFIG_OUTPUT,
UNQLITE_VM_CONFIG_RECURSION_DEPTH, UNQLITE_VM_OUTPUT_LENGTH};
use std::cell::RefCell;
use std::ffi::CString;
use std::os::raw::c_void;
use std::ptr::{null, null_mut, NonNull};
use std::rc::Rc;
use std::slice;
use std::sync::mpsc;
use vars::{UNQLITE_OK, UNQLITE_VM_CONFIG_ARGV_ENTRY, UNQLITE_VM_CONFIG_CREATE_VAR,
UNQLITE_VM_CONFIG_ENV_ATTR, UNQLITE_VM_CONFIG_ERR_REPORT, UNQLITE_VM_CONFIG_EXEC_VALUE,
UNQLITE_VM_CONFIG_EXTRACT_OUTPUT, UNQLITE_VM_CONFIG_IMPORT_PATH,
UNQLITE_VM_CONFIG_OUTPUT, UNQLITE_VM_CONFIG_RECURSION_DEPTH, UNQLITE_VM_OUTPUT_LENGTH};

/// Jx9 script compiler Interface.
///
Expand Down Expand Up @@ -148,9 +147,7 @@ impl UnQLiteVm {
UNQLITE_VM_CONFIG_EXTRACT_OUTPUT,
&ptr,
&mut len
).map(|_| unsafe {
slice::from_raw_parts(ptr as *const u8, len as usize)
})
).map(|_| unsafe { slice::from_raw_parts(ptr as *const u8, len as usize) })
}

/// Return the total number of bytes that have been outputted by the Virtual Machine
Expand Down
5 changes: 2 additions & 3 deletions src/document/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ fn output_extract() {
db.compile(prog)
.and_then(|mut vm| {
vm.exec().and_then(|_| {
vm.extract_output().map(|msg| {
assert_eq!("hello world", String::from_utf8_lossy(msg))
})
vm.extract_output()
.map(|msg| assert_eq!("hello world", String::from_utf8_lossy(msg)))
})
})
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/document/vm_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use ffi::{unqlite_array_count, unqlite_array_walk, unqlite_value, unqlite_value_
unqlite_value_is_json_object, unqlite_value_is_null, unqlite_value_is_string,
unqlite_value_to_bool, unqlite_value_to_double, unqlite_value_to_string,
unqlite_value_to_int64};
use ffi::constants::{UNQLITE_ABORT, UNQLITE_OK};
use std::collections::HashMap;
use std::os::raw::{c_int, c_void};
use std::slice;
use vars::{UNQLITE_ABORT, UNQLITE_OK};

/// Map of Values
pub type Map = HashMap<String, Value>;
Expand Down Expand Up @@ -153,7 +153,7 @@ unsafe extern "C" fn map_walk(
match both {
Some((k, v)) => {
(*collection).insert(k, v);
UNQLITE_OK
UNQLITE_OK as _
}
None => UNQLITE_ABORT,
}
Expand Down
23 changes: 6 additions & 17 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ffi::constants::*;
use std::error;
use std::fmt;
use std::result;
use vars::*;

/// Custom `Result` type.
pub type Result<T> = result::Result<T, Error>;
Expand Down Expand Up @@ -155,16 +155,7 @@ impl From<i32> for ErrorKind {
/// ```
///
/// This should be nice for functional programming style.
///
/// NOTE: Do not use this trait out of the crate
///
/// TODO: [`pub_restricted` feature][rfc_1422] is for the case,
/// but [rsutfmt] will cause a panic if we use it now,
/// we would apply this feature after [rustfmt][rustfmt] support it.
///
/// [rfc_1422]: https://github.com/rust-lang/rfcs/blob/master/text/1422-pub-restricted.md
/// [rustfmt]: https://github.com/rust-lang-nursery/rustfmt
pub trait Wrap {
pub(crate) trait Wrap {
fn wrap(self) -> Result<()>;
}

Expand All @@ -179,12 +170,10 @@ impl Custom {
let kind = ErrorKind::from(result);
match kind {
ErrorKind::OK => Ok(()),
_ => Err(
Custom {
kind: kind,
raw: result,
}.into(),
),
_ => Err(Custom {
kind: kind,
raw: result,
}.into()),
}
}

Expand Down
22 changes: 8 additions & 14 deletions src/kv_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use ffi::{unqlite, unqlite_kv_cursor, unqlite_kv_cursor_data, unqlite_kv_cursor_
unqlite_kv_cursor_key, unqlite_kv_cursor_key_callback, unqlite_kv_cursor_last_entry,
unqlite_kv_cursor_next_entry, unqlite_kv_cursor_prev_entry, unqlite_kv_cursor_release,
unqlite_kv_cursor_reset, unqlite_kv_cursor_seek, unqlite_kv_cursor_valid_entry};
use ffi::constants::{UNQLITE_CURSOR_MATCH_EXACT, UNQLITE_CURSOR_MATCH_GE, UNQLITE_CURSOR_MATCH_LE};
use std::mem;
use std::os::raw::c_void;
use std::ptr::{self, NonNull};
use vars::{UNQLITE_CURSOR_MATCH_EXACT, UNQLITE_CURSOR_MATCH_GE, UNQLITE_CURSOR_MATCH_LE};

/// Cursor iterator interfaces.
///
Expand Down Expand Up @@ -150,7 +150,6 @@ impl Entry {
self.0.value_callback(func, data)
}


/// Goto next entry.
///
/// Returns `None` if there's no valid cursors.
Expand Down Expand Up @@ -208,11 +207,9 @@ impl RawCursor {
/// Opening Database Cursors
pub fn init(unqlite: &UnQLite) -> Result<Self> {
let mut cursor: *mut unqlite_kv_cursor = unsafe { mem::uninitialized() };
wrap!(init, unqlite.as_raw_mut_ptr(), &mut cursor).map(|_| {
RawCursor {
engine: unqlite.engine,
cursor: unsafe { NonNull::new_unchecked(cursor) },
}
wrap!(init, unqlite.as_raw_mut_ptr(), &mut cursor).map(|_| RawCursor {
engine: unqlite.engine,
cursor: unsafe { NonNull::new_unchecked(cursor) },
})
}

Expand Down Expand Up @@ -278,9 +275,8 @@ impl RawCursor {

self.key_len().and_then(|mut len| {
let ptr = unsafe { ::libc::malloc(len as _) };
wrap!(key, self.cursor(), ptr as _, &mut len).map(|_| unsafe {
Vec::from_raw_parts(ptr as _, len as _, len as _)
})
wrap!(key, self.cursor(), ptr as _, &mut len)
.map(|_| unsafe { Vec::from_raw_parts(ptr as _, len as _, len as _) })
})
}

Expand All @@ -297,9 +293,8 @@ impl RawCursor {

self.value_len().and_then(|mut len| {
let ptr = unsafe { ::libc::malloc(len as _) };
wrap!(data, self.cursor(), ptr as _, &mut len).map(|_| unsafe {
Vec::from_raw_parts(ptr as _, len as _, len as _)
})
wrap!(data, self.cursor(), ptr as _, &mut len)
.map(|_| unsafe { Vec::from_raw_parts(ptr as _, len as _, len as _) })
})
}

Expand All @@ -316,7 +311,6 @@ impl RawCursor {
.and_then(|key| self.value().map(|value| (key, value)))
}


/// Deleting Records using Database Cursors
pub fn delete(self) -> Result<Self> {
wrap_in_place!(self, delete_entry)
Expand Down
2 changes: 1 addition & 1 deletion src/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use ffi::{unqlite_kv_append,
unqlite_kv_fetch,
unqlite_kv_fetch_callback,
unqlite_kv_store};
use ffi::constants::{UNQLITE_KV_CONFIG_CMP_FUNC, UNQLITE_KV_CONFIG_HASH_FUNC};
use std::mem;
use std::os::raw::c_void;
use std::ptr;
use vars::{UNQLITE_KV_CONFIG_CMP_FUNC, UNQLITE_KV_CONFIG_HASH_FUNC};

/// Key-Value Store Interface
pub trait KV {
Expand Down
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@
#![feature(concat_idents)]

extern crate libc;
extern crate unqlite_sys as ffi;

#[cfg(test)]
extern crate tempfile;

pub use error::{Error, Result};
use error::Wrap;

use ffi::{unqlite_close, unqlite_open};
use std::ffi::CString;
use std::mem;
Expand Down Expand Up @@ -119,7 +119,6 @@ macro_rules! wrap {
($i: ident, $($e: expr),*) => (eval!($i, $($e),*).wrap());
}


macro_rules! wrap_raw {
($self_:ident, $i: ident) => (
wrap!($i, $self_.as_raw_mut_ptr())
Expand All @@ -141,10 +140,8 @@ impl UnQLite {
let mut db: *mut ::ffi::unqlite = unsafe { mem::uninitialized() };
let filename = filename.as_ref();
let filename = try!(CString::new(filename));
wrap!(open, &mut db, filename.as_ptr(), mode.into()).map(|_| {
UnQLite {
engine: unsafe { NonNull::new_unchecked(db) },
}
wrap!(open, &mut db, filename.as_ptr(), mode.into()).map(|_| UnQLite {
engine: unsafe { NonNull::new_unchecked(db) },
})
}

Expand Down Expand Up @@ -270,6 +267,11 @@ impl Drop for UnQLite {
}
}

#[allow(dead_code, non_snake_case, non_camel_case_types)]
mod ffi;
#[allow(dead_code)]
mod vars;

mod error;
mod openmode;
mod config;
Expand Down
Loading

0 comments on commit 4b2409f

Please sign in to comment.