Skip to content

Commit

Permalink
add rust
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-yan committed Oct 9, 2021
1 parent 278fac9 commit 5876f68
Show file tree
Hide file tree
Showing 14 changed files with 756 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "rust/ejdb-sys/ejdb-upstream"]
path = rust/ejdb-sys/ejdb-upstream
url = https://github.com/Softmotions/ejdb.git
2 changes: 1 addition & 1 deletion EJDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ EJDBPP::EJDBError EJDBPP::close()
return EJDBPP::EJDBError::None;
}

cpp::result<int64_t, EJDBPP::EJDBError> EJDBPP::put(const std::string &collection, const std::string &json)
cpp::result<int64_t, EJDBPP::EJDBError> EJDBPP::putNew(const std::string &collection, const std::string &json)
{
JBL jbl = 0;
iwrc rc = jbl_from_json(&jbl, json.c_str());
Expand Down
2 changes: 1 addition & 1 deletion EJDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EJDBPP

EJDBPP::EJDBError close();

cpp::result<int64_t, EJDBError> put(const std::string &collection, const std::string &json);
cpp::result<int64_t, EJDBError> putNew(const std::string &collection, const std::string &json);

EJDBPP::EJDBError patch(const std::string &collection, const std::string &json, uint64_t id);

Expand Down
34 changes: 22 additions & 12 deletions EJDBQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class EJDBPP;


class EJDBQuery
{
friend class EJDBPP;
friend class EJDBPP;

private:
JQL m_q;
std::string m_query;
Expand All @@ -34,26 +34,27 @@ friend class EJDBPP;

cpp::result<int64_t, EJDBQueryError> limit();

EJDBQuery(const std::string &collection,const std::string &query);
EJDBQuery(const std::string &collection, const std::string &query);

EJDBQueryError init();

template <typename T>
EJDBQueryError setPlaceholder(const std::string &placeholder, int index, const T &val)
{ std::cout << "set" << std::endl;
{
std::cout << "set" << std::endl;

if constexpr(std::is_same<T, int64_t>::value)
if constexpr (std::is_same<T, int64_t>::value)
{
iwrc rc = jql_set_i64(m_q, placeholder.c_str(), index, val);
std::cout << "set2" << std::endl;
if (rc)
{
iwlog_ecode_error3(rc);
return EJDBQueryError::FailedToSetPlaceholder;
}
return EJDBQueryError::None;
}
else if constexpr(std::is_same<T, bool>::value){
else if constexpr (std::is_same<T, bool>::value)
{
iwrc rc = jql_set_bool(m_q, placeholder.c_str(), index, val);
if (rc)
{
Expand All @@ -62,13 +63,25 @@ friend class EJDBPP;
}
return EJDBQueryError::None;
}
else if constexpr(std::is_same<T, bool> ::value) {
else if constexpr (std::is_same<T, char *>::value)
{
iwrc rc = jql_set_str(m_q, placeholder.c_str(), index, val);
if (rc) {
if (rc)
{

iwlog_ecode_error3(rc);
return EJDBQueryError::FailedToSetPlaceholder;
}
return EJDBQueryError::None;
}
else if constexpr (std::is_same<T, _Float64>::value)
{
iwrc rc = jql_set_f64(m_q, placeholder.c_str(), index, val);

if (rc)
{
iwlog_ecode_error3(rc);
return EJDBQueryError::FailedToSetPlaceholder;
}
return EJDBQueryError::None;
}
Expand All @@ -77,16 +90,13 @@ friend class EJDBPP;
static_assert("Unsupported Placeholder Type.");
return EJDBQueryError::UnsupportedPlaceholderType;
}


}

EJDBQueryError setPlaceholderJSON(const std::string &placeholder, int index, const std::string &val);

EJDBQueryError setRegexp(const std::string &placeholder, int index, const std::string &regexp);

EJDBQueryError setNull(const std::string &placeholder, int index);

};

#endif
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main() {
EJDB_OPTS opts = {
.kv = {
.path = "example.db",
// .oflags = IWKV_TRUNC
.oflags = 0
}
};
EJDB db; // EJDB2 storage handle
Expand Down
27 changes: 27 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "ejdb"
version = "0.4.1"
authors = ["Vladimir Matveev <[email protected]>"]
description = "Bindings for EJDB, and embedded JSON database"
repository = "https://github.com/netvl/ejdb.rs"
documentation = "https://netvl.github.io/ejdb.rs/"
readme = "Readme.md"
keywords = ["ejdb", "database", "json", "bson", "ffi"]
license = "MIT"

[badges]
maintenance = { status = "looking-for-maintainer" }

[exe]
name = "ejdb"

[dependencies]
ejdb-sys = { path = "ejdb-sys", version = "0.3" }
bson = "0.13"
bitflags = "1.0"
quick-error = "1.2"
libc = "0.2"
itertools = "0.8"

[dev-dependencies]
tempdir = "0.3"
21 changes: 21 additions & 0 deletions rust/ejdb-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "ejdb-sys"
version = "0.3.0"
authors = ["Vladimir Matveev <[email protected]>"]
repository = "https://github.com/netvl/ejdb.rs"
description = "Native bindings for libejdb"
keywords = ["ejdb", "database", "ffi"]
license = "MIT"
links = "ejdb"
build = "build.rs"

[build-dependencies]
pkg-config = "0.3"
cmake = "0.1"
bindgen = "0.59"

[dependencies]
libc = "0.2"

[lib]
doctest = false
55 changes: 55 additions & 0 deletions rust/ejdb-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
extern crate bindgen;
extern crate cmake;
extern crate pkg_config;

use cmake::Config;
use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
pkg_config::Config::new().probe("zlib").unwrap();

let dst = Config::new("ejdb-upstream")
.cflag("-w")
.profile("Debug")
.define("BUILD_SAMPLES", "OFF")
.define("BUILD_SHARED_LIBS", "OFF")
.build();

Command::new("make").status().expect("failed to make!");

println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib64").display()
);
println!("cargo:rustc-link-lib=static=ejdb2-2");
println!("cargo:rustc-link-lib=static=facilio-1");
println!("cargo:rustc-link-lib=static=iowow-1");

println!("cargo:include={}",dst.join("include/ejdb2").display());
println!("debug={}",format!("-I{}", dst.join("include").as_path().to_str().unwrap()));

let bindings = bindgen::Builder::default()
.header(dst.join("include/ejdb2/ejdb2.h").as_path().to_str().unwrap())
// Hide duplicated types
.clang_arg( format!("-I{}", dst.join("include").as_path().to_str().unwrap()) )
.blacklist_item("FP_NAN")
.blacklist_item("FP_INFINITE")
.blacklist_item("FP_ZERO")
.blacklist_item("FP_SUBNORMAL")
.blacklist_item("FP_NORMAL")
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
1 change: 1 addition & 0 deletions rust/ejdb-sys/ejdb-upstream
Submodule ejdb-upstream added at 171233
5 changes: 5 additions & 0 deletions rust/ejdb-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
Loading

0 comments on commit 5876f68

Please sign in to comment.