🚧 add Neon glue code
This commit is contained in:
parent
53b7f4b556
commit
62f1929502
2 changed files with 79 additions and 9 deletions
|
|
@ -42,7 +42,6 @@ pub fn text_to_embedding(text: String) -> Vec<f32> {
|
|||
.collect::<Vec<f32>>();
|
||||
}
|
||||
|
||||
// Database interactions
|
||||
fn get_conn() -> Connection {
|
||||
let path = dirs::home_dir()
|
||||
.unwrap()
|
||||
|
|
|
|||
|
|
@ -4,11 +4,6 @@ mod gitignore;
|
|||
mod sync;
|
||||
mod utils;
|
||||
|
||||
// Vec<(String, String)>,
|
||||
// Vec<(String, String)>,
|
||||
// Vec<(String, String)>,
|
||||
// Vec<(String, String)>,
|
||||
|
||||
use neon::prelude::*;
|
||||
|
||||
fn build_js_array<'a>(
|
||||
|
|
@ -52,13 +47,89 @@ fn sync_results(mut cx: FunctionContext) -> JsResult<JsObject> {
|
|||
Ok(final_object)
|
||||
}
|
||||
|
||||
fn get_num_cpus(mut cx: FunctionContext) -> JsResult<JsNumber> {
|
||||
Ok(cx.number(2.0 as f64))
|
||||
fn db_add_chunk(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let hash = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
let content = cx.argument::<JsString>(1)?.value(&mut cx);
|
||||
|
||||
let tags_vec = cx.argument::<JsArray>(2)?.to_vec(&mut cx).unwrap();
|
||||
let mut tags: Vec<String> = Vec::new();
|
||||
for item in tags_vec {
|
||||
let tag = item
|
||||
.downcast::<JsString, _>(&mut cx)
|
||||
.unwrap()
|
||||
.value(&mut cx);
|
||||
tags.push(tag);
|
||||
}
|
||||
|
||||
let embedding_vec = cx.argument::<JsArray>(3)?.to_vec(&mut cx).unwrap();
|
||||
let mut embedding: Vec<f32> = Vec::new();
|
||||
for item in embedding_vec {
|
||||
let float = item
|
||||
.downcast::<JsNumber, _>(&mut cx)
|
||||
.unwrap()
|
||||
.value(&mut cx) as f32;
|
||||
embedding.push(float);
|
||||
}
|
||||
db::add_chunk(hash, content, tags, embedding);
|
||||
|
||||
return Ok(JsUndefined::new(&mut cx));
|
||||
}
|
||||
|
||||
fn db_remove_chunk(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let hash = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
db::remove_chunk(hash);
|
||||
|
||||
return Ok(JsUndefined::new(&mut cx));
|
||||
}
|
||||
fn db_add_tag(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let hash = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
let tag = cx.argument::<JsString>(1)?.value(&mut cx);
|
||||
db::add_tag(hash, tag);
|
||||
|
||||
return Ok(JsUndefined::new(&mut cx));
|
||||
}
|
||||
fn db_remove_tag(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let hash = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
let tag = cx.argument::<JsString>(1)?.value(&mut cx);
|
||||
db::remove_tag(hash, tag);
|
||||
|
||||
return Ok(JsUndefined::new(&mut cx));
|
||||
}
|
||||
fn db_retrieve(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let n = cx.argument::<JsNumber>(0)?.value(&mut cx) as usize;
|
||||
|
||||
let tags_vec = cx.argument::<JsArray>(1)?.to_vec(&mut cx).unwrap();
|
||||
let mut tags: Vec<String> = Vec::new();
|
||||
for item in tags_vec {
|
||||
let tag = item
|
||||
.downcast::<JsString, _>(&mut cx)
|
||||
.unwrap()
|
||||
.value(&mut cx);
|
||||
tags.push(tag);
|
||||
}
|
||||
|
||||
let v_vec = cx.argument::<JsArray>(2)?.to_vec(&mut cx).unwrap();
|
||||
let mut v: Vec<f32> = Vec::new();
|
||||
for item in v_vec {
|
||||
let float = item
|
||||
.downcast::<JsNumber, _>(&mut cx)
|
||||
.unwrap()
|
||||
.value(&mut cx) as f32;
|
||||
v.push(float);
|
||||
}
|
||||
|
||||
db::retrieve(n, tags, v);
|
||||
|
||||
return Ok(JsUndefined::new(&mut cx));
|
||||
}
|
||||
|
||||
#[neon::main]
|
||||
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
||||
cx.export_function("get", get_num_cpus)?;
|
||||
cx.export_function("sync_results", sync_results)?;
|
||||
cx.export_function("add_chunk", db_add_chunk);
|
||||
cx.export_function("remove_chunk", db_remove_chunk);
|
||||
cx.export_function("add_tag", db_add_tag);
|
||||
cx.export_function("remove_tag", db_remove_tag);
|
||||
cx.export_function("retrieve", db_retrieve);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue