Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: select first record when no selector function (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Nov 9, 2018
1 parent d1869ed commit 683a903
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"interface-datastore": "~0.6.0",
"k-bucket": "^4.0.1",
"libp2p-crypto": "~0.13.0",
"libp2p-record": "~0.6.0",
"libp2p-record": "~0.6.1",
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.1",
"peer-id": "~0.11.0",
Expand Down
12 changes: 11 additions & 1 deletion src/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,17 @@ module.exports = (dht) => ({
(cb) => dht.getMany(key, 16, options.maxTimeout, cb),
(vals, cb) => {
const recs = vals.map((v) => v.val)
const i = libp2pRecord.selection.bestRecord(dht.selectors, key, recs)
let i = 0

try {
i = libp2pRecord.selection.bestRecord(dht.selectors, key, recs)
} catch (err) {
// Assume the first record if no selector available
if (err.code !== 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY') {
return cb(err)
}
}

const best = recs[i]
dht._log('GetValue %b %s', key, best)

Expand Down
45 changes: 45 additions & 0 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,51 @@ describe('KadDHT', () => {
})
})

it('put - get using key with no prefix (no selector available)', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()

tdht.spawn(2, (err, dhts) => {
expect(err).to.not.exist()
const dhtA = dhts[0]
const dhtB = dhts[1]

waterfall([
(cb) => connect(dhtA, dhtB, cb),
(cb) => dhtA.put(Buffer.from('hello'), Buffer.from('world'), cb),
(cb) => dhtB.get(Buffer.from('hello'), { maxTimeout: 1000 }, cb),
(res, cb) => {
expect(res).to.eql(Buffer.from('world'))
cb()
}
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})
})

it('put - get should fail if unrecognized key prefix in get', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()

tdht.spawn(2, (err, dhts) => {
expect(err).to.not.exist()
const dhtA = dhts[0]
const dhtB = dhts[1]

waterfall([
(cb) => connect(dhtA, dhtB, cb),
(cb) => dhtA.put(Buffer.from('/v2/hello'), Buffer.from('world'), cb),
(cb) => dhtB.get(Buffer.from('/v2/hello'), { maxTimeout: 1000 }, cb)
], (err) => {
expect(err).to.exist()
expect(err.code).to.eql('ERR_UNRECOGNIZED_KEY_PREFIX')
tdht.teardown(done)
})
})
})

it('put - get with update', function (done) {
this.timeout(20 * 1000)
const tdht = new TestDHT()
Expand Down
2 changes: 2 additions & 0 deletions test/utils/test-dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class TestDHT {
sign: false
}

dht.validators.v2 = dht.validators.v // added to simulate just validators available

dht.selectors.v = (k, records) => 0

series([
Expand Down

0 comments on commit 683a903

Please sign in to comment.