Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion cpp/vector_engine_ffi.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#include "vector_engine_ffi.h"
#include "vector_engine.h"

struct NativeVectorEngine {};
#include <string>
#include <vector>

struct NativeVectorEngine {
VectorEngine engine;
};

struct NativeSearchResults {
std::vector<SearchResult> results;
};

extern "C" NativeVectorEngine* native_vector_engine_new() {
return new NativeVectorEngine();
Expand All @@ -9,3 +19,33 @@ extern "C" NativeVectorEngine* native_vector_engine_new() {
extern "C" void native_vector_engine_free(NativeVectorEngine* engine) {
delete engine;
}

extern "C" size_t native_search_results_len(const NativeSearchResults* results) {
return results->results.size();
}

extern "C" const char* native_search_results_id_at(const NativeSearchResults* results, size_t index) {
return results->results[index].id.c_str();
}

extern "C" float native_search_results_score_at(const NativeSearchResults* results, size_t index) {
return results->results[index].score;
}

extern "C" void native_search_results_free(NativeSearchResults* results) {
delete results;
}

extern "C" void native_vector_engine_insert(NativeVectorEngine* engine, const char* id, const float* vector, size_t len) {
engine->engine.insert(std::string(id), vector, len);
}

extern "C" bool native_vector_engine_delete(NativeVectorEngine* engine, const char* id) {
return engine->engine.erase(std::string(id));
}

extern "C" NativeSearchResults* native_vector_engine_search(const NativeVectorEngine* engine, const float* query, size_t len, size_t k) {
auto* results = new NativeSearchResults();
results->results = engine->engine.search(query, len, k);
return results;
}
22 changes: 3 additions & 19 deletions cpp/vector_engine_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,13 @@ struct NativeSearchResults;

extern "C" {

// subject to change
NativeVectorEngine* native_vector_engine_new();
void native_vector_engine_free(NativeVectorEngine* engine);

// insert / delete / search
void native_vector_engine_insert(
NativeVectorEngine* engine,
const char* id,
const float* vector,
size_t len
);

bool native_vector_engine_delete(
NativeVectorEngine* engine,
const char* id
);

NativeSearchResults* native_vector_engine_search(
const NativeVectorEngine* engine,
const float* query,
size_t len,
size_t k
);
void native_vector_engine_insert(NativeVectorEngine* engine, const char* id, const float* vector, size_t len);
bool native_vector_engine_delete(NativeVectorEngine* engine, const char* id);
NativeSearchResults* native_vector_engine_search(NativeVectorEngine* engine, const float* query, size_t len, size_t k);

// working with pointers to send info back to rust
size_t native_search_results_len(const NativeSearchResults* results);
Expand Down
17 changes: 17 additions & 0 deletions crates/vdb-ffi/Cargo.lock

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

1 change: 1 addition & 0 deletions crates/vdb-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ path = "src/lib.rs"

[build-dependencies]
bindgen = "0.72"
cc = "1"
15 changes: 13 additions & 2 deletions crates/vdb-ffi/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use std::{env, path::PathBuf};
use std::path::PathBuf;

fn main() {
println!("cargo:rerun-if-changed=../../cpp/vector_engine_ffi.h");
// linking the compiled cpp files
let cpp_dir = PathBuf::from("../../cpp");
let mut native_build = cc::Build::new();
native_build
.cpp(true)
.std("c++17")
.include(&cpp_dir)
.file(cpp_dir.join("vector_engine.cpp"))
.file(cpp_dir.join("vector_engine_ffi.cpp"));

native_build.compile("vector_engine_native");

// generates rust declarations based of cpp ffi
let bindings = bindgen::Builder::default()
.header("../../cpp/vector_engine_ffi.h")
.clang_arg("-xc++")
Expand Down
3 changes: 1 addition & 2 deletions crates/vdb-ffi/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ pub struct FfiSearchResults {
handle: *mut NativeSearchResults,
}

// The wrapper owns the native handle and is always accessed behind higher-level
// synchronization in the API layer.
// Send is built into Rust as a trait, allows FfiVectorEngine to be moved between threads safely.
unsafe impl Send for FfiVectorEngine {}

impl FfiVectorEngine {
Expand Down
Loading