3
3
const core = require ( 'datastore-core' )
4
4
const ShardingStore = core . ShardingDatastore
5
5
const Block = require ( 'ipld-block' )
6
- const { cidToKey, keyToCid } = require ( './blockstore-utils' )
7
- const map = require ( 'it-map' )
8
- const pipe = require ( 'it-pipe' )
6
+ const { cidToKey } = require ( './blockstore-utils' )
9
7
10
8
module . exports = async ( filestore , options ) => {
11
9
const store = await maybeWithSharding ( filestore , options )
@@ -25,7 +23,7 @@ function createBaseStore (store) {
25
23
/**
26
24
* Query the store.
27
25
*
28
- * @param {Object } query
26
+ * @param {object } query
29
27
* @param {Object } options
30
28
* @returns {AsyncIterator<Block> }
31
29
*/
@@ -41,26 +39,9 @@ function createBaseStore (store) {
41
39
*/
42
40
async get ( cid , options ) {
43
41
const key = cidToKey ( cid )
44
- let blockData
45
- try {
46
- blockData = await store . get ( key , options )
47
- return new Block ( blockData , cid )
48
- } catch ( err ) {
49
- if ( err . code === 'ERR_NOT_FOUND' ) {
50
- const otherCid = cidToOtherVersion ( cid )
42
+ const blockData = await store . get ( key , options )
51
43
52
- if ( ! otherCid ) {
53
- throw err
54
- }
55
-
56
- const otherKey = cidToKey ( otherCid )
57
- const blockData = await store . get ( otherKey , options )
58
- await store . put ( key , blockData )
59
- return new Block ( blockData , cid )
60
- }
61
-
62
- throw err
63
- }
44
+ return new Block ( blockData , cid )
64
45
} ,
65
46
/**
66
47
* Like get, but for more.
@@ -86,15 +67,14 @@ function createBaseStore (store) {
86
67
throw new Error ( 'invalid block' )
87
68
}
88
69
89
- const exists = await this . has ( block . cid )
70
+ const key = cidToKey ( block . cid )
71
+ const exists = await store . has ( key , options )
90
72
91
73
if ( exists ) {
92
- return this . get ( block . cid , options )
74
+ return
93
75
}
94
76
95
- await store . put ( cidToKey ( block . cid ) , block . data , options )
96
-
97
- return block
77
+ return store . put ( key , block . data , options )
98
78
} ,
99
79
100
80
/**
@@ -104,43 +84,32 @@ function createBaseStore (store) {
104
84
* @param {Object } options
105
85
* @returns {AsyncIterable<Block> }
106
86
*/
107
- async * putMany ( blocks , options ) { // eslint-disable-line require-await
108
- yield * pipe (
109
- blocks ,
110
- ( source ) => {
111
- // turn them into a key/value pair
112
- return map ( source , ( block ) => {
113
- return { key : cidToKey ( block . cid ) , value : block . data }
114
- } )
115
- } ,
116
- ( source ) => {
117
- // put them into the datastore
118
- return store . putMany ( source , options )
119
- } ,
120
- ( source ) => {
121
- // map the returned key/value back into a block
122
- return map ( source , ( { key, value } ) => {
123
- return new Block ( value , keyToCid ( key ) )
124
- } )
87
+ async * putMany ( blocks , options ) {
88
+ for await ( const block of blocks ) {
89
+ const key = cidToKey ( block . cid )
90
+
91
+ if ( await store . has ( key ) ) {
92
+ continue
125
93
}
126
- )
94
+
95
+ await store . put ( key , block . data , options )
96
+
97
+ yield block
98
+ }
127
99
} ,
100
+
128
101
/**
129
- * Does the store contain block with this cid ?
102
+ * Does the store contain block with this CID ?
130
103
*
131
104
* @param {CID } cid
132
105
* @param {Object } options
133
106
* @returns {Promise<bool> }
134
107
*/
135
- async has ( cid , options ) {
136
- const exists = await store . has ( cidToKey ( cid ) , options )
137
- if ( exists ) return exists
138
- const otherCid = cidToOtherVersion ( cid )
139
- if ( ! otherCid ) return false
140
- return store . has ( cidToKey ( otherCid ) , options )
108
+ async has ( cid , options ) { // eslint-disable-line require-await
109
+ return store . has ( cidToKey ( cid ) , options )
141
110
} ,
142
111
/**
143
- * Delete a block from the store
112
+ * Delete a CID or multihash from the store
144
113
*
145
114
* @param {CID } cid
146
115
* @param {Object } options
@@ -173,11 +142,3 @@ function createBaseStore (store) {
173
142
}
174
143
}
175
144
}
176
-
177
- function cidToOtherVersion ( cid ) {
178
- try {
179
- return cid . version === 0 ? cid . toV1 ( ) : cid . toV0 ( )
180
- } catch ( err ) {
181
- return null
182
- }
183
- }
0 commit comments