Skip to content

Commit

Permalink
Merge pull request #17 from S4N0I/master
Browse files Browse the repository at this point in the history
Make node module context-aware to allow usage in worker threads
  • Loading branch information
jimkang committed Aug 8, 2023
2 parents 3e9ecb9 + fe8f450 commit 39eaca2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build-wrapper:

test: tests/data/text8-vector.json
node tests/smalltest.js
node tests/worker-thread-test.js
node tests/smalltest-manhattan.js
node tests/basictests.js basic-config.js

Expand Down
4 changes: 3 additions & 1 deletion addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ void InitAll(v8::Local<v8::Object> exports) {
AnnoyIndexWrapper::Init(exports);
}

NODE_MODULE(addon, InitAll)
NODE_MODULE_INIT() {
InitAll(exports);
}
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"bindings": "^1.2.1",
"level": "^6.0.0",
"nan": "^2.14.0"
"nan": "^2.14.0",
"node-worker-threads-pool": "^1.5.1"
}
}
18 changes: 18 additions & 0 deletions tests/testworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { parentPort } = require('worker_threads');
var Annoy = require('../index');

var annoyPath = __dirname + '/data/test.annoy';

const annoyIndex = new Annoy(10, 'Angular')

if(!annoyIndex.load(annoyPath)) {
throw new Error('Loading annoy index failed!')
} else {
console.log('Successfully loaded annoy index.')
}

parentPort.on('message', async (id) => {
// return the result to the main thread
const result = annoyIndex.getNNsByItem(id, 10, -1, false)
parentPort.postMessage(result);
});
19 changes: 19 additions & 0 deletions tests/worker-thread-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var test = require('tape');
const { StaticPool } = require('node-worker-threads-pool')

var workerPath = __dirname + '/testworker.js';

test('Worker thread test', workerThreadTest);

async function workerThreadTest(t) {
const workerPool = new StaticPool({
size: 2,
task: workerPath
});

const idsToLookUp = [0, 1]
const indexLookups = idsToLookUp.map(id => workerPool.exec(id))
console.log("Lookup results: ", (await Promise.all(indexLookups)))
workerPool.destroy()
t.end()
}

0 comments on commit 39eaca2

Please sign in to comment.