1
1
'use strict'
2
2
3
- const core = require ( 'datastore-core' )
4
- const ShardingStore = core . ShardingDatastore
3
+ const { shard, ShardingDatastore } = require ( 'datastore-core' )
5
4
const Block = require ( 'ipld-block' )
6
5
const { cidToKey, keyToCid } = require ( './blockstore-utils' )
7
6
const map = require ( 'it-map' )
8
7
const drain = require ( 'it-drain' )
9
8
const pushable = require ( 'it-pushable' )
10
-
9
+ /**
10
+ * @typedef {import("interface-datastore").Query } Query
11
+ * @typedef {import("interface-datastore").Datastore } Datastore
12
+ * @typedef {import("interface-datastore").Options } DatastoreOptions
13
+ * @typedef {import("cids") } CID
14
+ */
15
+
16
+ /**
17
+ *
18
+ * @param {Datastore } filestore
19
+ * @param {* } options
20
+ */
11
21
module . exports = async ( filestore , options ) => {
12
22
const store = await maybeWithSharding ( filestore , options )
13
23
return createBaseStore ( store )
14
24
}
15
25
26
+ /**
27
+ * @param {Datastore } filestore
28
+ * @param {{ sharding: any; } } options
29
+ */
16
30
function maybeWithSharding ( filestore , options ) {
17
31
if ( options . sharding ) {
18
- const shard = new core . shard . NextToLast ( 2 )
19
- return ShardingStore . createOrOpen ( filestore , shard )
32
+ return ShardingDatastore . createOrOpen ( filestore , new shard . NextToLast ( 2 ) )
20
33
}
21
34
return filestore
22
35
}
23
36
37
+ /**
38
+ * @param {Datastore | ShardingDatastore } store
39
+ */
24
40
function createBaseStore ( store ) {
25
41
return {
26
42
/**
27
43
* Query the store
28
44
*
29
- * @param {Object } query
30
- * @param {Object } options
31
- * @returns {AsyncIterator <Block|CID> }
45
+ * @param {Query } query
46
+ * @param {DatastoreOptions } [ options]
47
+ * @returns {AsyncIterable <Block|CID> }
32
48
*/
33
49
async * query ( query , options ) {
34
50
for await ( const { key, value } of store . query ( query , options ) ) {
@@ -45,7 +61,7 @@ function createBaseStore (store) {
45
61
* Get a single block by CID
46
62
*
47
63
* @param {CID } cid
48
- * @param {Object } options
64
+ * @param {DatastoreOptions } [ options]
49
65
* @returns {Promise<Block> }
50
66
*/
51
67
async get ( cid , options ) {
@@ -58,9 +74,9 @@ function createBaseStore (store) {
58
74
/**
59
75
* Like get, but for more
60
76
*
61
- * @param {AsyncIterator <CID> } cids
62
- * @param {Object } options
63
- * @returns {AsyncIterator <Block> }
77
+ * @param {Iterable<CID> | AsyncIterable <CID> } cids
78
+ * @param {DatastoreOptions } [ options]
79
+ * @returns {AsyncIterable <Block> }
64
80
*/
65
81
async * getMany ( cids , options ) {
66
82
for await ( const cid of cids ) {
@@ -72,7 +88,7 @@ function createBaseStore (store) {
72
88
* Write a single block to the store
73
89
*
74
90
* @param {Block } block
75
- * @param {Object } options
91
+ * @param {DatastoreOptions } [ options]
76
92
* @returns {Promise<Block> }
77
93
*/
78
94
async put ( block , options ) {
@@ -94,7 +110,7 @@ function createBaseStore (store) {
94
110
* Like put, but for more
95
111
*
96
112
* @param {AsyncIterable<Block>|Iterable<Block> } blocks
97
- * @param {Object } options
113
+ * @param {DatastoreOptions } [ options]
98
114
* @returns {AsyncIterable<Block> }
99
115
*/
100
116
async * putMany ( blocks , options ) { // eslint-disable-line require-await
@@ -142,41 +158,38 @@ function createBaseStore (store) {
142
158
* Does the store contain block with this CID?
143
159
*
144
160
* @param {CID } cid
145
- * @param {Object } options
146
- * @returns {Promise<bool> }
161
+ * @param {DatastoreOptions } [options]
147
162
*/
148
- async has ( cid , options ) { // eslint-disable-line require-await
163
+ has ( cid , options ) {
149
164
return store . has ( cidToKey ( cid ) , options )
150
165
} ,
151
166
152
167
/**
153
168
* Delete a block from the store
154
169
*
155
170
* @param {CID } cid
156
- * @param {Object } options
171
+ * @param {DatastoreOptions } [ options]
157
172
* @returns {Promise<void> }
158
173
*/
159
- async delete ( cid , options ) { // eslint-disable-line require-await
174
+ delete ( cid , options ) {
160
175
return store . delete ( cidToKey ( cid ) , options )
161
176
} ,
162
177
163
178
/**
164
179
* Delete a block from the store
165
180
*
166
- * @param {AsyncIterable<CID> } cids
167
- * @param {Object } options
168
- * @returns {Promise<void> }
181
+ * @param {AsyncIterable<any> | Iterable<any> } cids
182
+ * @param {DatastoreOptions } [options]
169
183
*/
170
- async * deleteMany ( cids , options ) { // eslint-disable-line require-await
171
- yield * store . deleteMany ( map ( cids , cid => cidToKey ( cid ) ) , options )
184
+ deleteMany ( cids , options ) {
185
+ return store . deleteMany ( map ( cids , cid => cidToKey ( cid ) ) , options )
172
186
} ,
173
187
174
188
/**
175
189
* Close the store
176
190
*
177
- * @returns {Promise<void> }
178
191
*/
179
- async close ( ) { // eslint-disable-line require-await
192
+ close ( ) {
180
193
return store . close ( )
181
194
}
182
195
}
0 commit comments