Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: JS: Improve error handling of batch addition #510

Open
wants to merge 4 commits into
base: main-dev
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions javascript/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CompiledIndex : public Napi::ObjectWrap<CompiledIndex> {
Napi::Value Count(Napi::CallbackInfo const& ctx);

std::unique_ptr<index_dense_t> native_;
std::mutex mtx;
};

Napi::Object CompiledIndex::Init(Napi::Env env, Napi::Object exports) {
Expand Down Expand Up @@ -165,13 +166,23 @@ void CompiledIndex::Add(Napi::CallbackInfo const& ctx) {

// Create an instance of the executor with the default number of threads
auto run_parallel = [&](auto vectors) {
std::string error = "";
executor_stl_t executor;
executor.fixed(tasks, [&](std::size_t /*thread_idx*/, std::size_t task_idx) {
add_result_t result = native_->add(static_cast<default_key_t>(keys[task_idx]),
vectors + task_idx * native_->dimensions());
if (!result)
Napi::Error::New(ctx.Env(), result.error.release()).ThrowAsJavaScriptException();
if (!result) {
std::lock_guard<std::mutex> lock(mtx);
error
.append("<key:")
.append(std::to_string(keys[task_idx]))
.append(" message:")
.append(result.error.release())
.append(">");
}
});
if (error.size() > 0)
Napi::Error::New(ctx.Env(), error).ThrowAsJavaScriptException();
};

Napi::TypedArray vectors = ctx[1].As<Napi::TypedArray>();
Expand Down
25 changes: 23 additions & 2 deletions javascript/usearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,28 @@ test('Invalid operations', async (t) => {
() => index.add(42n, new Float32Array([0.2, 0.6, 0.4])),
{
name: 'Error',
message: 'Duplicate keys not allowed in high-level wrappers'
message: '<key:42 message:Duplicate keys not allowed in high-level wrappers>'
}
);
});

await t.test('Batch add containing the same key', () => {
const index = new usearch.Index({
metric: "l2sq",
connectivity: 16,
dimensions: 3,
});
index.add(42n, new Float32Array([0.2, 0.6, 0.4]));
assert.throws(
() => {
index.add(
[41n, 42n, 43n],
[[0.1, 0.6, 0.4], [0.2, 0.6, 0.4], [0.3, 0.6, 0.4]]
);
},
{
name: 'Error',
message: '<key:42 message:Duplicate keys not allowed in high-level wrappers>'
}
);
});
Expand Down Expand Up @@ -232,7 +253,7 @@ test('Serialization', async (t) => {
() => index.add(43n, new Float32Array([0.2, 0.6, 0.4])),
{
name: 'Error',
message: "Can't add to an immutable index"
message: "<key:43 message:Can't add to an immutable index>"
}
);
});
Expand Down
Loading