Skip to content

Commit

Permalink
feat: update hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz committed Feb 26, 2024
1 parent 26a42d6 commit 549bcaa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-grapes-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@koopjs/winnow": minor
---

- swap stringhash for murmurhash
21 changes: 12 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/winnow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
"js-sql-parser": "^1.5.0",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"murmurhash": "^2.0.1",
"ngeohash": "^0.6.3",
"proj4": "^2.10.0",
"simple-statistics": "^7.8.3",
"string-hash": "^1.1.3",
"wkt-parser": "^1.3.3"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const hashFixture = require('./hash-fixture');
const USE_JAVASCRIPT_HASHING = process.env.OBJECTID_FEATURE_HASH === 'javascript';
const stringHash = require('string-hash');
const murmurhash = require('murmurhash');

function getHashFunction () {
if (USE_JAVASCRIPT_HASHING) return stringHash;
if (USE_JAVASCRIPT_HASHING) {
return murmurhash;
}

// Try to use farmhash, fallback to JavaScript only hashing library
// Try to use farmhash, fallback to native JavaScript hashing library
try {
const hashFunction = require('farmhash').hash32;
hashFunction(JSON.stringify(hashFixture));
return hashFunction;
} catch (e) {
return stringHash;
return murmurhash;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ const test = require('tape');
const proxyquire = require('proxyquire');
const modulePath = './hash-function';
const stub = {
'string-hash': () => { return 'string-hash'; },
'murmurhash': () => { return 'murmurhash'; },
'farmhash': { // eslint-disable-line
hash32: () => { return 'farmhash'; }
}
};

test('hashFunction: ENV variable forces use of string-hash', t => {
test('hashFunction: ENV variable forces use of murmurhash', t => {
t.plan(1);
process.env.OBJECTID_FEATURE_HASH = 'javascript';
const hashFunction = proxyquire(modulePath, stub);
t.equals(hashFunction(), 'string-hash');
t.equals(hashFunction(), 'murmurhash');
process.env.OBJECTID_FEATURE_HASH = undefined;
});

Expand All @@ -22,14 +22,14 @@ test('hashFunction: default use of farmhash', t => {
t.equals(hashFunction(), 'farmhash');
});

test('hashFunction: fallback use of string-hash', t => {
test('hashFunction: fallback use of murmurhash', t => {
t.plan(1);
const stub = {
'string-hash': () => { return 'string-hash'; },
'murmurhash': () => { return 'murmurhash'; },
'farmhash': { // eslint-disable-line
hash32: () => { throw new Error(); }
}
};
const hashFunction = proxyquire(modulePath, stub);
t.equals(hashFunction(), 'string-hash');
t.equals(hashFunction(), 'murmurhash');
});

0 comments on commit 549bcaa

Please sign in to comment.