Skip to content

Commit

Permalink
0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
zh217 committed Nov 5, 2022
1 parent 38a35b2 commit 1c0c480
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 135 deletions.
42 changes: 35 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cozo"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
description = "A general-purpose, transactional, relational database that uses Datalog and focuses on graph data and algorithms"
authors = ["Ziyang Hu"]
Expand Down
7 changes: 4 additions & 3 deletions cozo-lib-c/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cozo_c"
version = "0.1.1"
version = "0.1.3"
edition = "2021"
license = "AGPL-3.0-or-later"
homepage = "https://github.com/cozodb/cozo"
Expand All @@ -19,6 +19,7 @@ io-uring = ["cozo/io-uring"]

[dependencies]
cozo = { version = "0.1.2", path = ".." }
miette = { version = "=5.3.0", features = ["fancy"] }
serde_json = "1.0.81"
lazy_static = "1.4.0"

[build-dependencies]
cbindgen = "0.24.3"
27 changes: 27 additions & 0 deletions cozo-lib-c/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022, The Cozo Project Authors. Licensed under MIT/Apache-2.0/BSD-3-Clause.
*/

use std::env;

use cbindgen::{Config, Language};

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

let mut config = Config::default();
config.cpp_compat = true;
cbindgen::Builder::new()
.with_config(config)
.with_crate(crate_dir)
.with_language(Language::C)
.with_include_guard("COZO_C_H")
.with_autogen_warning(
"/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */",
)
.with_header("/* Copyright 2022, The Cozo Project Authors. Licensed under MIT/Apache-2.0/BSD-3-Clause. */")
.with_documentation(true)
.generate()
.expect("Unable to generate bindings")
.write_to_file("cozo_c.h");
}
6 changes: 0 additions & 6 deletions cozo-lib-c/cbindgen.toml

This file was deleted.

11 changes: 5 additions & 6 deletions cozo-lib-c/cozo_c.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright 2022, The Cozo Project Authors. Licensed under MIT/Apache-2.0/BSD-3-Clause. */

#ifndef cozo_c_h
#define cozo_c_h
#ifndef COZO_C_H
#define COZO_C_H

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

Expand Down Expand Up @@ -49,10 +49,9 @@ bool cozo_close_db(int32_t id);
* `true` if an error occurred.
*
* Returns a UTF-8-encoded C-string that **must** be freed with `cozo_free_str`.
* If `*errored` is false, then the string contains the JSON return value of the query.
* If `*errored` is true, then the string contains the error message.
* The string contains the JSON return value of the query.
*/
char *cozo_run_query(int32_t db_id, const char *script_raw, const char *params_raw, bool *errored);
char *cozo_run_query(int32_t db_id, const char *script_raw, const char *params_raw);

/**
* Free any C-string returned from the Cozo C API.
Expand All @@ -66,4 +65,4 @@ void cozo_free_str(char *s);
} // extern "C"
#endif // __cplusplus

#endif /* cozo_c_h */
#endif /* COZO_C_H */
17 changes: 5 additions & 12 deletions cozo-lib-c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@
#include <stdbool.h>
#include "cozo_c.h"

void run_query(int32_t db_id, const char* query) {
void run_query(int32_t db_id, const char *query) {
const char *empty_params = "{}";
bool errored;
char *res;
res = cozo_run_query(db_id, query, empty_params, &errored);

if (errored) {
printf("encountered an error:\n%s\n\n", res);
} else {
printf("query is successful with result:\n%s\n\n", res);
}
res = cozo_run_query(db_id, query, empty_params);
printf("%s\n", res);
cozo_free_str(res);
}

int main() {
int32_t db_id;
char* err = cozo_open_db("_test_db", &db_id);
char *err = cozo_open_db("_test_db", &db_id);

if (err) {
printf("%s", err);
cozo_free_str(err);
return -1;
}

run_query(db_id, "?[a, b, c] <- [[1, 2, 3]]");
run_query(db_id, "?[a] <- [[1, 2, 3]]");
run_query(db_id, "?[] <- [[1, 2, 3]]");

cozo_close_db(db_id);

Expand Down
57 changes: 15 additions & 42 deletions cozo-lib-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,19 @@ pub unsafe extern "C" fn cozo_close_db(id: i32) -> bool {
/// `true` if an error occurred.
///
/// Returns a UTF-8-encoded C-string that **must** be freed with `cozo_free_str`.
/// If `*errored` is false, then the string contains the JSON return value of the query.
/// If `*errored` is true, then the string contains the error message.
/// The string contains the JSON return value of the query.
#[no_mangle]
pub unsafe extern "C" fn cozo_run_query(
db_id: i32,
script_raw: *const c_char,
params_raw: *const c_char,
errored: &mut bool,
) -> *mut c_char {
let script = match CStr::from_ptr(script_raw).to_str() {
Ok(p) => p,
Err(err) => {
*errored = true;
return CString::new(format!("{}", err)).unwrap().into_raw();
Err(_) => {
return CString::new(r##"{"ok":false,"message":"script is not UTF-8 encoded"}"##)
.unwrap()
.into_raw();
}
};
let db = {
Expand All @@ -100,52 +99,26 @@ pub unsafe extern "C" fn cozo_run_query(
};
match db_ref {
None => {
*errored = true;
return CString::new("database already closed").unwrap().into_raw();
return CString::new(r##"{"ok":false,"message":"database closed"}"##)
.unwrap()
.into_raw();
}
Some(db) => db,
}
};
let params_str = match CStr::from_ptr(params_raw).to_str() {
Ok(p) => p,
Err(err) => {
*errored = true;
return CString::new(format!("{}", err)).unwrap().into_raw();
}
};

let params_map: serde_json::Value = match serde_json::from_str(&params_str) {
Ok(m) => m,
Err(_) => {
*errored = true;
return CString::new("the given params argument is not valid JSON")
.unwrap()
.into_raw();
return CString::new(
r##"{"ok":false,"message":"params argument is not UTF-8 encoded"}"##,
)
.unwrap()
.into_raw();
}
};

let params_arg: BTreeMap<_, _> = match params_map {
serde_json::Value::Object(m) => m.into_iter().collect(),
_ => {
*errored = true;
return CString::new("the given params argument is not a JSON map")
.unwrap()
.into_raw();
}
};
let result = db.run_script(script, &params_arg);
match result {
Ok(json) => {
*errored = false;
let json_str = json.to_string();
CString::new(json_str).unwrap().into_raw()
}
Err(err) => {
let err_str = format!("{:?}", err);
*errored = true;
CString::new(err_str).unwrap().into_raw()
}
}
let result = db.run_script_str(script, &params_str);
CString::new(result).unwrap().into_raw()
}

/// Free any C-string returned from the Cozo C API.
Expand Down
6 changes: 2 additions & 4 deletions cozo-lib-java/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cozo_java"
version = "0.1.1"
version = "0.1.3"
edition = "2021"
license = "AGPL-3.0-or-later"
homepage = "https://github.com/cozodb/cozo"
Expand All @@ -19,7 +19,5 @@ io-uring = ["cozo/io-uring"]

[dependencies]
robusta_jni = "0.2.0"
cozo = { version = "0.1.2", path = ".." }
miette = { version = "=5.3.0", features = ["fancy"] }
serde_json = "1.0.81"
cozo = { version = "0.1.3", path = ".." }
lazy_static = "1.4.0"
18 changes: 1 addition & 17 deletions cozo-lib-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ lazy_static! {

#[bridge]
mod jni {
use std::collections::BTreeMap;
use std::sync::atomic::Ordering;

use robusta_jni::convert::{IntoJavaValue, Signature, TryFromJavaValue, TryIntoJavaValue};
Expand Down Expand Up @@ -73,22 +72,7 @@ mod jni {
let db = db_ref.ok_or_else(|| JniError::from("database already closed"))?;
db
};
let params_map: serde_json::Value = serde_json::from_str(&params_str)
.map_err(|_| JniError::from("the given params argument is not valid JSON"))?;

let params_arg: BTreeMap<_, _> = match params_map {
serde_json::Value::Object(m) => m.into_iter().collect(),
_ => {
return Err(JniError::from(
"the given params argument is not a JSON map",
))
}
};
let result = db.run_script(&script, &params_arg);
match result {
Ok(json) => Ok(json.to_string()),
Err(err) => Err(JniError::from(format!("{:?}", err))),
}
Ok(db.run_script_str(&script, &params_str))
}
}
}
Loading

0 comments on commit 1c0c480

Please sign in to comment.