33const core = require ( 'datastore-core' )
44const ShardingStore = core . ShardingDatastore
55const 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' )
97
108module . exports = async ( filestore , options ) => {
119 const store = await maybeWithSharding ( filestore , options )
@@ -25,7 +23,7 @@ function createBaseStore (store) {
2523 /**
2624 * Query the store.
2725 *
28- * @param {Object } query
26+ * @param {object } query
2927 * @param {Object } options
3028 * @returns {AsyncIterator<Block> }
3129 */
@@ -41,26 +39,9 @@ function createBaseStore (store) {
4139 */
4240 async get ( cid , options ) {
4341 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 )
5143
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 )
6445 } ,
6546 /**
6647 * Like get, but for more.
@@ -86,15 +67,14 @@ function createBaseStore (store) {
8667 throw new Error ( 'invalid block' )
8768 }
8869
89- const exists = await this . has ( block . cid )
70+ const key = cidToKey ( block . cid )
71+ const exists = await store . has ( key , options )
9072
9173 if ( exists ) {
92- return this . get ( block . cid , options )
74+ return
9375 }
9476
95- await store . put ( cidToKey ( block . cid ) , block . data , options )
96-
97- return block
77+ return store . put ( key , block . data , options )
9878 } ,
9979
10080 /**
@@ -104,43 +84,32 @@ function createBaseStore (store) {
10484 * @param {Object } options
10585 * @returns {AsyncIterable<Block> }
10686 */
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
12593 }
126- )
94+
95+ await store . put ( key , block . data , options )
96+
97+ yield block
98+ }
12799 } ,
100+
128101 /**
129- * Does the store contain block with this cid ?
102+ * Does the store contain block with this CID ?
130103 *
131104 * @param {CID } cid
132105 * @param {Object } options
133106 * @returns {Promise<bool> }
134107 */
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 )
141110 } ,
142111 /**
143- * Delete a block from the store
112+ * Delete a CID or multihash from the store
144113 *
145114 * @param {CID } cid
146115 * @param {Object } options
@@ -173,11 +142,3 @@ function createBaseStore (store) {
173142 }
174143 }
175144}
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