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
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 8, 2018
1 parent d1869ed commit 29bbf7e
Show file tree
Hide file tree
Showing 3 changed files with 36 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 callback(err)
}
}

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

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

it('put - get using key with no selector', 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 with update', function (done) {
this.timeout(20 * 1000)
const tdht = new TestDHT()
Expand Down

0 comments on commit 29bbf7e

Please sign in to comment.