Skip to content

Commit

Permalink
Fix: Reserve after deserialization in JS (#484)
Browse files Browse the repository at this point in the history
I got an error when I loaded and searched with load() or view().

Code Example:

```js
// Saved with `index.save('index.usearch');` in another script.
index.load('index.usearch');
const results = index.search(new Float32Array([0.2, 0.6, 0.4]), 10);
```
  • Loading branch information
abetomo authored Oct 11, 2024
1 parent d6fd1eb commit 16dec63
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions javascript/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ void CompiledIndex::Load(Napi::CallbackInfo const& ctx) {
auto result = native_->load(path.c_str());
if (!result)
Napi::TypeError::New(ctx.Env(), result.error.release()).ThrowAsJavaScriptException();
if (!native_->try_reserve(ceil2(native_->size())))
Napi::Error::New(ctx.Env(), "Failed to reserve memory").ThrowAsJavaScriptException();

} catch (...) {
Napi::TypeError::New(ctx.Env(), "Loading failed").ThrowAsJavaScriptException();
Expand All @@ -144,6 +146,8 @@ void CompiledIndex::View(Napi::CallbackInfo const& ctx) {
auto result = native_->view(path.c_str());
if (!result)
Napi::TypeError::New(ctx.Env(), result.error.release()).ThrowAsJavaScriptException();
if (!native_->try_reserve(ceil2(native_->size())))
Napi::Error::New(ctx.Env(), "Failed to reserve memory").ThrowAsJavaScriptException();

} catch (...) {
Napi::TypeError::New(ctx.Env(), "Memory-mapping failed").ThrowAsJavaScriptException();
Expand Down
50 changes: 50 additions & 0 deletions javascript/usearch.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const test = require('node:test');
const assert = require('node:assert');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const usearch = require('./dist/cjs/usearch.js');

function assertAlmostEqual(actual, expected, tolerance = 1e-6) {
Expand Down Expand Up @@ -122,3 +125,50 @@ test('Invalid operations', async (t) => {
);
});
});


test('Serialization', async (t) => {
const indexPath = path.join(os.tmpdir(), 'usearch.test.index')

t.beforeEach(() => {
const index = new usearch.Index({
metric: "l2sq",
connectivity: 16,
dimensions: 3,
});
index.add(42n, new Float32Array([0.2, 0.6, 0.4]));
index.save(indexPath);
});

t.afterEach(() => {
fs.unlinkSync(indexPath);
});

await t.test('load', () => {
const index = new usearch.Index({
metric: "l2sq",
connectivity: 16,
dimensions: 3,
});
index.load(indexPath);
const results = index.search(new Float32Array([0.2, 0.6, 0.4]), 10);

assert.equal(index.size(), 1);
assert.deepEqual(results.keys, new BigUint64Array([42n]));
assertAlmostEqual(results.distances[0], new Float32Array([0]));
});

await t.test('view', () => {
const index = new usearch.Index({
metric: "l2sq",
connectivity: 16,
dimensions: 3,
});
index.view(indexPath);
const results = index.search(new Float32Array([0.2, 0.6, 0.4]), 10);

assert.equal(index.size(), 1);
assert.deepEqual(results.keys, new BigUint64Array([42n]));
assertAlmostEqual(results.distances[0], new Float32Array([0]));
});
});

0 comments on commit 16dec63

Please sign in to comment.