Skip to content

Commit

Permalink
refactor: make array to store error message (#1)
Browse files Browse the repository at this point in the history
* clang format

* refactor: make array to store error message

* run tests

* fix ci

* timeout
  • Loading branch information
jiacai2050 authored Nov 5, 2024
1 parent 834ff0a commit 7e7922a
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 284 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI

on:
workflow_dispatch:
pull_request:
paths-ignore:
- '**.md'
push:
branches:
- main
- master
paths-ignore:
- '**.md'

env:
CC: gcc

jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install deps
run: |
sudo apt install -y gfortran python3-dev libomp-15-dev lcov intel-mkl
- uses: Swatinem/rust-cache@v2
- name: fmt
run: |
make fmt
- name: test
run: |
make test
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.PHONY: fmt
fmt:
find src include -iname "*.h" -o -iname "*.cpp" | xargs clang-format -i

.PHONY: test
test:
cargo test
89 changes: 37 additions & 52 deletions include/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,45 @@

extern "C" {

struct CError {
int type_;
const char* message;
#define VSAG_WRAPPER_MAX_ERROR_MESSAGE_LENGTH 256

CError(int type, const char* message) : type_(type), message(message) {}
struct CError {
int type_;
char message[VSAG_WRAPPER_MAX_ERROR_MESSAGE_LENGTH];
};

const CError* create_index(
const char* in_index_type,
const char* in_parameters,

void** out_index_ptr
);

const CError* build_index(
void* in_index_ptr,
size_t in_num_vectors,
size_t in_dim,
const int64_t* in_ids,
const float* in_vectors,

const int64_t** out_failed_ids,
size_t* out_num_failed
);

const CError* knn_search_index(
void* in_index_ptr,
size_t in_dim,
const float* in_query_vector,
size_t in_k,
const char* in_search_parameters,

const int64_t** out_ids,
const float** out_distances,
size_t* out_num_results
);

const CError* dump_index(
void* in_index_ptr,
const char* in_file_path
);

const CError* load_index(
const char* in_file_path,
const char* in_index_type,
const char* in_parameters,

void** out_index_ptr
);

void free_error(const CError*);
void free_index(void* index_ptr);
void free_i64_vector(int64_t* vector);
void free_f32_vector(float* vector);
CError *new_error(int type_, const char *msg);
void free_error(const CError *);

const CError *create_index(const char *in_index_type, const char *in_parameters,

void **out_index_ptr);

const CError *build_index(void *in_index_ptr, size_t in_num_vectors,
size_t in_dim, const int64_t *in_ids,
const float *in_vectors,

const int64_t **out_failed_ids,
size_t *out_num_failed);

const CError *knn_search_index(void *in_index_ptr, size_t in_dim,
const float *in_query_vector, size_t in_k,
const char *in_search_parameters,

const int64_t **out_ids,
const float **out_distances,
size_t *out_num_results);

const CError *dump_index(void *in_index_ptr, const char *in_file_path);

const CError *load_index(const char *in_file_path, const char *in_index_type,
const char *in_parameters,

void **out_index_ptr);

void free_index(void *index_ptr);
void free_i64_vector(int64_t *vector);
void free_f32_vector(float *vector);
} // extern "C"

#endif // WRAPPER_H
#endif // WRAPPER_H
12 changes: 8 additions & 4 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,20 @@ extern "C" {
#[repr(C)]
pub struct CError {
pub type_: c_int,
pub message: *const c_char,
// should be same length with CError defined in wrapper.h
pub message: [u8; 256],
}

pub fn from_c_error(err: *const CError) -> crate::error::Error {
let error = crate::error::Error {
error_type: unsafe { std::mem::transmute::<i32, crate::error::ErrorType>((*err).type_) },
message: unsafe {
std::ffi::CStr::from_ptr((*err).message)
.to_string_lossy()
.into_owned()
let null_pos = (*err)
.message
.iter()
.position(|&x| x == 0)
.unwrap_or((*err).message.len());
String::from_utf8_lossy(&(*err).message[..null_pos]).into_owned()
},
};
unsafe {
Expand Down
Loading

0 comments on commit 7e7922a

Please sign in to comment.