Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 5e7858e

Browse files
authored
feat: add streaming/cancellable API (#23)
Upgrades to the latest interface-datastore which includes streaming APIs and passing AbortControllers. Uses the new Adapter class to implement these with minimal code changes.
1 parent a19da65 commit 5e7858e

File tree

5 files changed

+69
-61
lines changed

5 files changed

+69
-61
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
},
3939
"homepage": "https://github.com/ipfs/js-datastore-core#readme",
4040
"devDependencies": {
41-
"aegir": "^21.4.5",
41+
"aegir": "^22.0.0",
4242
"async-iterator-all": "^1.0.0",
4343
"chai": "^4.2.0",
4444
"dirty-chai": "^2.0.1"
4545
},
4646
"dependencies": {
4747
"buffer": "^5.5.0",
4848
"debug": "^4.1.1",
49-
"interface-datastore": "^0.8.2"
49+
"interface-datastore": "^1.0.2"
5050
},
5151
"engines": {
5252
"node": ">=6.0.0",

src/keytransform.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict'
22

3-
const utils = require('interface-datastore').utils
3+
const { Adapter, utils } = require('interface-datastore')
44
const map = utils.map
55

66
/**
77
* A datastore shim, that wraps around a given datastore, changing
88
* the way keys look to the user, for example namespacing
99
* keys, reversing them, etc.
1010
*/
11-
class KeyTransformDatastore {
11+
class KeyTransformDatastore extends Adapter {
1212
constructor (child, transform) {
13+
super()
14+
1315
this.child = child
1416
this.transform = transform
1517
}
@@ -18,20 +20,20 @@ class KeyTransformDatastore {
1820
return this.child.open()
1921
}
2022

21-
put (key, val) {
22-
return this.child.put(this.transform.convert(key), val)
23+
put (key, val, options) {
24+
return this.child.put(this.transform.convert(key), val, options)
2325
}
2426

25-
get (key) {
26-
return this.child.get(this.transform.convert(key))
27+
get (key, options) {
28+
return this.child.get(this.transform.convert(key), options)
2729
}
2830

29-
has (key) {
30-
return this.child.has(this.transform.convert(key))
31+
has (key, options) {
32+
return this.child.has(this.transform.convert(key), options)
3133
}
3234

33-
delete (key) {
34-
return this.child.delete(this.transform.convert(key))
35+
delete (key, options) {
36+
return this.child.delete(this.transform.convert(key), options)
3537
}
3638

3739
batch () {
@@ -43,14 +45,14 @@ class KeyTransformDatastore {
4345
delete: (key) => {
4446
b.delete(this.transform.convert(key))
4547
},
46-
commit: () => {
47-
return b.commit()
48+
commit: (options) => {
49+
return b.commit(options)
4850
}
4951
}
5052
}
5153

52-
query (q) {
53-
return map(this.child.query(q), e => {
54+
query (q, options) {
55+
return map(this.child.query(q, options), e => {
5456
e.key = this.transform.invert(e.key)
5557
return e
5658
})

src/mount.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
/* @flow */
22
'use strict'
33

4-
const Key = require('interface-datastore').Key
5-
const Errors = require('interface-datastore').Errors
6-
const utils = require('interface-datastore').utils
7-
const filter = utils.filter
8-
const take = utils.take
9-
const sortAll = utils.sortAll
10-
const replaceStartWith = utils.replaceStartWith
4+
const {
5+
Adapter, Key, Errors, utils: {
6+
filter,
7+
take,
8+
sortAll,
9+
replaceStartWith
10+
}
11+
} = require('interface-datastore')
1112

1213
const Keytransform = require('./keytransform')
1314

1415
/**
1516
* A datastore that can combine multiple stores inside various
1617
* key prefixs.
1718
*/
18-
class MountDatastore {
19+
class MountDatastore extends Adapter {
1920
constructor (mounts) {
21+
super()
22+
2023
this.mounts = mounts.slice()
2124
}
2225

@@ -44,38 +47,38 @@ class MountDatastore {
4447
}
4548
}
4649

47-
put (key, value) {
50+
put (key, value, options) {
4851
const match = this._lookup(key)
4952
if (match == null) {
5053
throw Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
5154
}
5255

53-
return match.datastore.put(match.rest, value)
56+
return match.datastore.put(match.rest, value, options)
5457
}
5558

56-
get (key) {
59+
get (key, options) {
5760
const match = this._lookup(key)
5861
if (match == null) {
5962
throw Errors.notFoundError(new Error('No datastore mounted for this key'))
6063
}
61-
return match.datastore.get(match.rest)
64+
return match.datastore.get(match.rest, options)
6265
}
6366

64-
has (key) {
67+
has (key, options) {
6568
const match = this._lookup(key)
6669
if (match == null) {
6770
return false
6871
}
69-
return match.datastore.has(match.rest)
72+
return match.datastore.has(match.rest, options)
7073
}
7174

72-
delete (key) {
75+
delete (key, options) {
7376
const match = this._lookup(key)
7477
if (match == null) {
7578
throw Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
7679
}
7780

78-
return match.datastore.delete(match.rest)
81+
return match.datastore.delete(match.rest, options)
7982
}
8083

8184
close () {
@@ -112,13 +115,13 @@ class MountDatastore {
112115
const match = lookup(key)
113116
match.batch.delete(match.rest)
114117
},
115-
commit: () => {
116-
return Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit()))
118+
commit: (options) => {
119+
return Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit(options)))
117120
}
118121
}
119122
}
120123

121-
query (q) {
124+
query (q, options) {
122125
const qs = this.mounts.map(m => {
123126
const ks = new Keytransform(m.datastore, {
124127
convert: (key) => {
@@ -138,7 +141,7 @@ class MountDatastore {
138141
prefix: prefix,
139142
filters: q.filters,
140143
keysOnly: q.keysOnly
141-
})
144+
}, options)
142145
})
143146

144147
let it = _many(qs)

src/sharding.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict'
22

33
const { Buffer } = require('buffer')
4-
const Key = require('interface-datastore').Key
5-
4+
const { Adapter, Key } = require('interface-datastore')
65
const sh = require('./shard')
76
const KeytransformStore = require('./keytransform')
87

@@ -15,8 +14,10 @@ const shardReadmeKey = new Key(sh.README_FN)
1514
* Wraps another datastore such that all values are stored
1615
* sharded according to the given sharding function.
1716
*/
18-
class ShardingDatastore {
17+
class ShardingDatastore extends Adapter {
1918
constructor (store, shard) {
19+
super()
20+
2021
this.child = new KeytransformStore(store, {
2122
convert: this._convertKey.bind(this),
2223
invert: this._invertKey.bind(this)
@@ -75,27 +76,27 @@ class ShardingDatastore {
7576
throw new Error('datastore exists')
7677
}
7778

78-
put (key, val) {
79-
return this.child.put(key, val)
79+
put (key, val, options) {
80+
return this.child.put(key, val, options)
8081
}
8182

82-
get (key) {
83-
return this.child.get(key)
83+
get (key, options) {
84+
return this.child.get(key, options)
8485
}
8586

86-
has (key) {
87-
return this.child.has(key)
87+
has (key, options) {
88+
return this.child.has(key, options)
8889
}
8990

90-
delete (key) {
91-
return this.child.delete(key)
91+
delete (key, options) {
92+
return this.child.delete(key, options)
9293
}
9394

9495
batch () {
9596
return this.child.batch()
9697
}
9798

98-
query (q) {
99+
query (q, options) {
99100
const tq = {
100101
keysOnly: q.keysOnly,
101102
offset: q.offset,
@@ -130,7 +131,7 @@ class ShardingDatastore {
130131
})
131132
}
132133

133-
return this.child.query(tq)
134+
return this.child.query(tq, options)
134135
}
135136

136137
close () {

src/tiered.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const Errors = require('interface-datastore').Errors
3+
const { Adapter, Errors } = require('interface-datastore')
44
const log = require('debug')('datastore:core:tiered')
55

66
/**
@@ -10,8 +10,10 @@ const log = require('debug')('datastore:core:tiered')
1010
* last one first.
1111
*
1212
*/
13-
class TieredDatastore {
13+
class TieredDatastore extends Adapter {
1414
constructor (stores) {
15+
super()
16+
1517
this.stores = stores.slice()
1618
}
1719

@@ -31,10 +33,10 @@ class TieredDatastore {
3133
}
3234
}
3335

34-
async get (key) {
36+
async get (key, options) {
3537
for (const store of this.stores) {
3638
try {
37-
const res = await store.get(key)
39+
const res = await store.get(key, options)
3840
if (res) return res
3941
} catch (err) {
4042
log(err)
@@ -43,19 +45,19 @@ class TieredDatastore {
4345
throw Errors.notFoundError()
4446
}
4547

46-
async has (key) {
48+
async has (key, options) {
4749
for (const s of this.stores) {
48-
if (await s.has(key)) {
50+
if (await s.has(key, options)) {
4951
return true
5052
}
5153
}
5254

5355
return false
5456
}
5557

56-
async delete (key) {
58+
async delete (key, options) {
5759
try {
58-
await Promise.all(this.stores.map(store => store.delete(key)))
60+
await Promise.all(this.stores.map(store => store.delete(key, options)))
5961
} catch (err) {
6062
throw Errors.dbDeleteFailedError()
6163
}
@@ -75,16 +77,16 @@ class TieredDatastore {
7577
delete: (key) => {
7678
batches.forEach(b => b.delete(key))
7779
},
78-
commit: async () => {
80+
commit: async (options) => {
7981
for (const batch of batches) {
80-
await batch.commit()
82+
await batch.commit(options)
8183
}
8284
}
8385
}
8486
}
8587

86-
query (q) {
87-
return this.stores[this.stores.length - 1].query(q)
88+
query (q, options) {
89+
return this.stores[this.stores.length - 1].query(q, options)
8890
}
8991
}
9092

0 commit comments

Comments
 (0)