@@ -5,7 +5,7 @@ const ShardingStore = core.ShardingDatastore
5
5
const Block = require ( 'ipfs-block' )
6
6
const CID = require ( 'cids' )
7
7
const errcode = require ( 'err-code' )
8
- const { cidToKey } = require ( './blockstore-utils' )
8
+ const { cidToKey, keyToCidKey } = require ( './blockstore-utils' )
9
9
10
10
module . exports = async ( filestore , options ) => {
11
11
const store = await maybeWithSharding ( filestore , options )
@@ -26,10 +26,15 @@ function createBaseStore (store) {
26
26
* Query the store.
27
27
*
28
28
* @param {object } query
29
+ * @param {boolean } reconstructsCids - Defines if Keys are converted to a reconstructed CID using IPLD_RAW codec
29
30
* @return {Iterable }
30
31
*/
31
- async * query ( query ) {
32
+ async * query ( query , reconstructsCids = false ) {
32
33
for await ( const block of store . query ( query ) ) {
34
+ if ( reconstructsCids ) {
35
+ block . key = keyToCidKey ( block . key )
36
+ }
37
+
33
38
yield block
34
39
}
35
40
} ,
@@ -44,26 +49,8 @@ function createBaseStore (store) {
44
49
throw errcode ( new Error ( 'Not a valid cid' ) , 'ERR_INVALID_CID' )
45
50
}
46
51
const key = cidToKey ( cid )
47
- let blockData
48
- try {
49
- blockData = await store . get ( key )
50
- return new Block ( blockData , cid )
51
- } catch ( err ) {
52
- if ( err . code === 'ERR_NOT_FOUND' ) {
53
- const otherCid = cidToOtherVersion ( cid )
54
-
55
- if ( ! otherCid ) {
56
- throw err
57
- }
58
-
59
- const otherKey = cidToKey ( otherCid )
60
- const blockData = await store . get ( otherKey )
61
- await store . put ( key , blockData )
62
- return new Block ( blockData , cid )
63
- }
64
-
65
- throw err
66
- }
52
+ const blockData = await store . get ( key )
53
+ return new Block ( blockData , cid )
67
54
} ,
68
55
/**
69
56
* Write a single block to the store.
@@ -112,18 +99,14 @@ function createBaseStore (store) {
112
99
* Does the store contain block with this cid?
113
100
*
114
101
* @param {CID } cid
115
- * @returns {Promise<bool > }
102
+ * @returns {Promise<boolean > }
116
103
*/
117
- async has ( cid ) {
104
+ has ( cid ) {
118
105
if ( ! CID . isCID ( cid ) ) {
119
106
throw errcode ( new Error ( 'Not a valid cid' ) , 'ERR_INVALID_CID' )
120
107
}
121
108
122
- const exists = await store . has ( cidToKey ( cid ) )
123
- if ( exists ) return exists
124
- const otherCid = cidToOtherVersion ( cid )
125
- if ( ! otherCid ) return false
126
- return store . has ( cidToKey ( otherCid ) )
109
+ return store . has ( cidToKey ( cid ) )
127
110
} ,
128
111
/**
129
112
* Delete a block from the store
@@ -147,11 +130,3 @@ function createBaseStore (store) {
147
130
}
148
131
}
149
132
}
150
-
151
- function cidToOtherVersion ( cid ) {
152
- try {
153
- return cid . version === 0 ? cid . toV1 ( ) : cid . toV0 ( )
154
- } catch ( err ) {
155
- return null
156
- }
157
- }
0 commit comments