@@ -12,6 +12,7 @@ const {
12
12
Adapter, Key, Errors
13
13
} = require ( 'interface-datastore' )
14
14
const map = require ( 'it-map' )
15
+ const parallel = require ( 'it-parallel-batch' )
15
16
16
17
const noop = ( ) => { }
17
18
const fsAccess = promisify ( fs . access || noop )
@@ -25,6 +26,11 @@ const fsUnlink = promisify(fs.unlink || noop)
25
26
* @typedef {import('interface-datastore').KeyQuery } KeyQuery
26
27
*/
27
28
29
+ /**
30
+ * @template TEntry
31
+ * @typedef {import('interface-store').AwaitIterable<TEntry> } AwaitIterable
32
+ */
33
+
28
34
/**
29
35
* Write a file atomically
30
36
*
@@ -62,7 +68,7 @@ async function writeFile (path, contents) {
62
68
class FsDatastore extends Adapter {
63
69
/**
64
70
* @param {string } location
65
- * @param {{ createIfMissing?: boolean; errorIfExists?: boolean; extension?: string; } | undefined } [opts]
71
+ * @param {{ createIfMissing?: boolean, errorIfExists?: boolean, extension?: string, putManyConcurrency?: number } | undefined } [opts]
66
72
*/
67
73
constructor ( location , opts ) {
68
74
super ( )
@@ -71,7 +77,10 @@ class FsDatastore extends Adapter {
71
77
this . opts = Object . assign ( { } , {
72
78
createIfMissing : true ,
73
79
errorIfExists : false ,
74
- extension : '.data'
80
+ extension : '.data' ,
81
+ deleteManyConcurrency : 50 ,
82
+ getManyConcurrency : 50 ,
83
+ putManyConcurrency : 50
75
84
} , opts )
76
85
}
77
86
@@ -175,6 +184,23 @@ class FsDatastore extends Adapter {
175
184
}
176
185
}
177
186
187
+ /**
188
+ * @param {AwaitIterable<Pair> } source
189
+ * @returns {AsyncIterable<Pair> }
190
+ */
191
+ async * putMany ( source ) {
192
+ yield * parallel (
193
+ map ( source , ( { key, value } ) => {
194
+ return async ( ) => {
195
+ await this . put ( key , value )
196
+
197
+ return { key, value }
198
+ }
199
+ } ) ,
200
+ this . opts . putManyConcurrency
201
+ )
202
+ }
203
+
178
204
/**
179
205
* Read from the file system without extension.
180
206
*
@@ -215,6 +241,38 @@ class FsDatastore extends Adapter {
215
241
return data
216
242
}
217
243
244
+ /**
245
+ * @param {AwaitIterable<Key> } source
246
+ * @returns {AsyncIterable<Uint8Array> }
247
+ */
248
+ async * getMany ( source ) {
249
+ yield * parallel (
250
+ map ( source , key => {
251
+ return async ( ) => {
252
+ return this . get ( key )
253
+ }
254
+ } ) ,
255
+ this . opts . getManyConcurrency
256
+ )
257
+ }
258
+
259
+ /**
260
+ * @param {AwaitIterable<Key> } source
261
+ * @returns {AsyncIterable<Key> }
262
+ */
263
+ async * deleteMany ( source ) {
264
+ yield * parallel (
265
+ map ( source , key => {
266
+ return async ( ) => {
267
+ await this . delete ( key )
268
+
269
+ return key
270
+ }
271
+ } ) ,
272
+ this . opts . deleteManyConcurrency
273
+ )
274
+ }
275
+
218
276
/**
219
277
* Check for the existence of the given key.
220
278
*
@@ -223,6 +281,7 @@ class FsDatastore extends Adapter {
223
281
*/
224
282
async has ( key ) {
225
283
const parts = this . _encode ( key )
284
+
226
285
try {
227
286
await fsAccess ( parts . file )
228
287
} catch ( err ) {
0 commit comments