add method import_from_backup

main
Ziyang Hu 2 years ago
parent 3b80b02906
commit dcee278e28

2
Cargo.lock generated

@ -587,7 +587,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "cozo_py_module" name = "cozo_py"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"cozo", "cozo",

@ -97,6 +97,16 @@ char *cozo_backup(int32_t db_id,
char *cozo_restore(int32_t db_id, char *cozo_restore(int32_t db_id,
const char *in_path); const char *in_path);
/**
* Import data into a relation
* `db_id`: the ID representing the database.
* `json_payload`: a UTF-8 encoded JSON payload, see the manual for the expected fields.
*
* Returns a UTF-8-encoded C-string indicating the result that **must** be freed with `cozo_free_str`.
*/
char *import_from_backup(int32_t db_id,
const char *json_payload);
/** /**
* Free any C-string returned from the Cozo C API. * Free any C-string returned from the Cozo C API.
* Must be called exactly once for each returned C-string. * Must be called exactly once for each returned C-string.

@ -264,6 +264,41 @@ pub unsafe extern "C" fn cozo_restore(db_id: i32, in_path: *const c_char) -> *mu
.into_raw() .into_raw()
} }
#[no_mangle]
/// Import data into a relation
/// `db_id`: the ID representing the database.
/// `json_payload`: a UTF-8 encoded JSON payload, see the manual for the expected fields.
///
/// Returns a UTF-8-encoded C-string indicating the result that **must** be freed with `cozo_free_str`.
pub unsafe extern "C" fn import_from_backup(
db_id: i32,
json_payload: *const c_char,
) -> *mut c_char {
let db = {
let db_ref = {
let dbs = HANDLES.dbs.lock().unwrap();
dbs.get(&db_id).cloned()
};
match db_ref {
None => {
return CString::new(r##"{"ok":false,"message":"database closed"}"##)
.unwrap()
.into_raw();
}
Some(db) => db,
}
};
let data = match CStr::from_ptr(json_payload).to_str() {
Ok(p) => p,
Err(err) => return CString::new(format!("{}", err)).unwrap().into_raw(),
};
CString::new(db.import_from_backup_str(data))
.unwrap()
.into_raw()
}
/// Free any C-string returned from the Cozo C API. /// Free any C-string returned from the Cozo C API.
/// Must be called exactly once for each returned C-string. /// Must be called exactly once for each returned C-string.
/// ///

@ -8,6 +8,7 @@ public class CozoJavaBridge {
private static native String importRelations(int id, String data); private static native String importRelations(int id, String data);
private static native String backup(int id, String file); private static native String backup(int id, String file);
private static native String restore(int id, String file); private static native String restore(int id, String file);
private static native String importFromBackup(int id, String data);
static { static {
System.loadLibrary("cozo_java"); System.loadLibrary("cozo_java");

@ -63,6 +63,14 @@ JNIEXPORT jstring JNICALL Java_org_cozodb_CozoJavaBridge_backup
JNIEXPORT jstring JNICALL Java_org_cozodb_CozoJavaBridge_restore JNIEXPORT jstring JNICALL Java_org_cozodb_CozoJavaBridge_restore
(JNIEnv *, jclass, jint, jstring); (JNIEnv *, jclass, jint, jstring);
/*
* Class: org_cozodb_CozoJavaBridge
* Method: importFromBackup
* Signature: (ILjava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_cozodb_CozoJavaBridge_importFromBackup
(JNIEnv *, jclass, jint, jstring);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -159,3 +159,20 @@ pub extern "system" fn Java_org_cozodb_CozoJavaBridge_restore(
} }
} }
} }
#[no_mangle]
pub extern "system" fn Java_org_cozodb_CozoJavaBridge_importFromBackup(
env: JNIEnv,
_class: JClass,
id: jint,
data: JString,
) -> jstring {
let data: String = env.get_string(data).unwrap().into();
match get_db(id) {
None => env.new_string(DB_NOT_FOUND).unwrap().into_raw(),
Some(db) => {
let res = db.import_from_backup_str(&data);
env.new_string(res).unwrap().into_raw()
}
}
}

@ -236,6 +236,43 @@ fn import_relations(mut cx: FunctionContext) -> JsResult<JsUndefined> {
Ok(cx.undefined()) Ok(cx.undefined())
} }
fn import_from_backup(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let id = cx.argument::<JsNumber>(0)?.value(&mut cx) as u32;
let db = {
let db_ref = {
let dbs = HANDLES.dbs.lock().unwrap();
dbs.get(&id).cloned()
};
match db_ref {
None => {
let s = cx.string("database already closed");
cx.throw(s)?
}
Some(db) => db,
}
};
let data = cx.argument::<JsString>(1)?.value(&mut cx);
let callback = cx.argument::<JsFunction>(2)?.root(&mut cx);
let channel = cx.channel();
std::thread::spawn(move || {
let result = db.import_from_backup_str(&data);
channel.send(move |mut cx| {
let callback = callback.into_inner(&mut cx);
let this = cx.undefined();
let json_str = cx.string(result);
callback.call(&mut cx, this, vec![json_str.upcast()])?;
Ok(())
});
});
Ok(cx.undefined())
}
#[neon::main] #[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> { fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("open_db", open_db)?; cx.export_function("open_db", open_db)?;
@ -245,5 +282,6 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("restore_db", restore_db)?; cx.export_function("restore_db", restore_db)?;
cx.export_function("export_relations", export_relations)?; cx.export_function("export_relations", export_relations)?;
cx.export_function("import_relations", import_relations)?; cx.export_function("import_relations", import_relations)?;
cx.export_function("import_from_backup", import_from_backup)?;
Ok(()) Ok(())
} }

@ -1,5 +1,5 @@
[package] [package]
name = "cozo_py_module" name = "cozo_py"
version = "0.2.0" version = "0.2.0"
edition = "2021" edition = "2021"
description = "Cozo database for python" description = "Cozo database for python"

@ -62,6 +62,13 @@ impl CozoDbPy {
DB_CLOSED_MSG.to_string() DB_CLOSED_MSG.to_string()
} }
} }
pub fn import_from_backup(&self, py: Python<'_>, data: &str) -> String {
if let Some(db) = &self.db {
py.allow_threads(|| db.import_from_backup_str(data))
} else {
DB_CLOSED_MSG.to_string()
}
}
pub fn close(&mut self) -> bool { pub fn close(&mut self) -> bool {
self.db.take().is_some() self.db.take().is_some()
} }

@ -21,6 +21,7 @@ mod ffi {
fn import_relations_str(&self, data: &str) -> String; fn import_relations_str(&self, data: &str) -> String;
fn backup_db_str(&self, out_file: &str) -> String; fn backup_db_str(&self, out_file: &str) -> String;
fn restore_backup_str(&self, in_file: &str) -> String; fn restore_backup_str(&self, in_file: &str) -> String;
fn import_from_backup_str(&self, data: &str) -> String;
} }
} }

Loading…
Cancel
Save