Skip to content

Commit 6c5e171

Browse files
AuHauachingbrain
andcommitted
feat: integration of js-ipfs-repo-migrations
Integration of js-ipfs-repo-migrations brings automatic repo migrations to ipfs-repo (both in-browser and fs). It is possible to control the automatic migration using either config's setting 'repoDisableAutoMigration' or IPFSRepo's option 'disableAutoMigration'. BREAKING CHANGE: repo.blocks.query() now returns multihashes as a key instead of CID. If you want to have CID returned call it as query({}, true), which will constructs CIDv1 using IPLD's RAW codec. This means that this constructed CID might not equal to the one that the block was originally saved. Related to ipfs/js-ipfs#2415 Co-authored-by: achingbrain <[email protected]>
1 parent 4692a47 commit 6c5e171

File tree

11 files changed

+125
-102
lines changed

11 files changed

+125
-102
lines changed

README.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ This is the implementation of the [IPFS repo spec](https://github.com/ipfs/specs
4040
- [`Promise<Buffer> repo.get(key)`](#promisebuffer-repogetkey)
4141
- [Blocks](#blocks)
4242
- [`Promise<Block> repo.blocks.put(block:Block)`](#promiseblock-repoblocksputblockblock)
43-
- [`AsyncIterator<Block> repo.blocks.putMany(source)`](#asynciteratorblock-repoblocksputmanysource)
44-
- [`Promise<Buffer> repo.blocks.get(cid)`](#promisebuffer-repoblocksgetcid)
45-
- [`AsyncIterable<Buffer> repo.blocks.getMany(source)`](#asynciterablebuffer-repoblocksgetmanysource)
43+
- [`AsyncIterator<Block> repo.blocks.putMany(source:AsyncIterable<Block>)`](#asynciteratorblock-repoblocksputmanysourceasynciterableblock)
44+
- [`Promise<Block> repo.blocks.get(cid:CID)`](#promiseblock-repoblocksgetcidcid)
45+
- [`AsyncIterable<Block> repo.blocks.getMany(source:AsyncIterable<CID>)`](#asynciterableblock-repoblocksgetmanysourceasynciterablecid)
46+
- [`Promise<boolean> repo.blocks.has (cid:CID)`](#promiseboolean-repoblockshas-cidcid)
47+
- [`Promise<boolean> repo.blocks.delete (cid:CID)`](#promiseboolean-repoblocksdelete-cidcid)
48+
- [`Promise<Array<Object>> repo.blocks.query (query)`](#promisearrayobject-repoblocksquery-query)
4649
- [`Promise<CID> repo.blocks.delete(cid:CID)`](#promisecid-repoblocksdeletecidcid)
47-
- [`AsyncIterator<CID> repo.blocks.deleteMany(source)`](#asynciteratorcid-repoblocksdeletemanysource)
50+
- [`AsyncIterator<CID> repo.blocks.deleteMany(source:AsyncIterable<CID>)`](#asynciteratorcid-repoblocksdeletemanysourceasynciterablecid)
4851
- [Datastore](#datastore)
4952
- [`repo.datastore`](#repodatastore)
5053
- [Config](#config)
51-
- [`Promise repo.config.set(key:string, value)`](#promise-repoconfigsetkeystring-value)
52-
- [`Promise repo.config.replace(value)`](#promise-repoconfigreplacevalue)
53-
- [`Promise<?> repo.config.get(key:string)`](#promise-repoconfiggetkeystring)
54+
- [`Promise repo.config.set(key:String, value:Object)`](#promise-repoconfigsetkeystring-valueobject)
55+
- [`Promise repo.config.replace(value:Object)`](#promise-repoconfigreplacevalueobject)
56+
- [`Promise<?> repo.config.get(key:String)`](#promise-repoconfiggetkeystring)
5457
- [`Promise<Object> repo.config.getAll()`](#promiseobject-repoconfiggetall)
5558
- [`Promise<boolean> repo.config.exists()`](#promiseboolean-repoconfigexists)
5659
- [Version](#version)
@@ -229,31 +232,51 @@ Get a value at the root of the repo
229232

230233
* `block` should be of type [Block][]
231234

232-
#### `AsyncIterator<Block> repo.blocks.putMany(source)`
235+
#### `AsyncIterator<Block> repo.blocks.putMany(source:AsyncIterable<Block>)`
233236

234237
Put many blocks.
235238

236239
* `source` should be an AsyncIterable that yields entries of type [Block][]
237240

238-
#### `Promise<Buffer> repo.blocks.get(cid)`
241+
#### `Promise<Block> repo.blocks.get(cid:CID)`
239242

240243
Get block.
241244

242245
* `cid` is the content id of type [CID][]
243246

244-
#### `AsyncIterable<Buffer> repo.blocks.getMany(source)`
247+
#### `AsyncIterable<Block> repo.blocks.getMany(source:AsyncIterable<CID>)`
245248

246-
Get block.
249+
Get many blocks
247250

248251
* `source` should be an AsyncIterable that yields entries of type [CID][]
249252

253+
#### `Promise<boolean> repo.blocks.has (cid:CID)`
254+
255+
Indicate if a block is present for the passed CID
256+
257+
* `cid` should be of the type [CID][]
258+
259+
#### `Promise<boolean> repo.blocks.delete (cid:CID)`
260+
261+
Deletes a block
262+
263+
* `cid` should be of the type [CID][]
264+
265+
#### `Promise<Array<Object>> repo.blocks.query (query)`
266+
267+
Query what blocks are available in blockstore.
268+
269+
* `query` is a object as specified in [interface-datastore](https://github.com/ipfs/interface-datastore#query).
270+
271+
Datastore:
272+
250273
#### `Promise<CID> repo.blocks.delete(cid:CID)`
251274

252275
* `cid` should be of the type [CID][]
253276

254277
Delete a block
255278

256-
#### `AsyncIterator<CID> repo.blocks.deleteMany(source)`
279+
#### `AsyncIterator<CID> repo.blocks.deleteMany(source:AsyncIterable<CID>)`
257280

258281
* `source` should be an Iterable or AsyncIterable that yields entries of the type [CID][]
259282

@@ -269,7 +292,7 @@ This contains a full implementation of [the `interface-datastore` API](https://g
269292

270293
Instead of using `repo.set('config')` this exposes an API that allows you to set and get a decoded config object, as well as, in a safe manner, change any of the config values individually.
271294

272-
#### `Promise repo.config.set(key:string, value)`
295+
#### `Promise repo.config.set(key:String, value:Object)`
273296

274297
Set a config value. `value` can be any object that is serializable to JSON.
275298

@@ -281,11 +304,11 @@ const config = await repo.config.get()
281304
assert.equal(config.a.b.c, 'c value')
282305
```
283306

284-
#### `Promise repo.config.replace(value)`
307+
#### `Promise repo.config.replace(value:Object)`
285308

286309
Set the whole config value. `value` can be any object that is serializable to JSON.
287310

288-
#### `Promise<?> repo.config.get(key:string)`
311+
#### `Promise<?> repo.config.get(key:String)`
289312

290313
Get a config value. Returned promise resolves to the same type that was set before.
291314

@@ -379,7 +402,7 @@ Returned promise resolves to a `boolean` indicating the existence of the lock.
379402

380403
### Migrations
381404

382-
When there is a new repo migration and the version of repo is increased, don't
405+
When there is a new repo migration and the version of the repo is increased, don't
383406
forget to propagate the changes into the test repo (`test/test-repo`).
384407

385408
**For tools that run mainly in the browser environment, be aware that disabling automatic

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@
6969
"debug": "^4.1.0",
7070
"err-code": "^2.0.0",
7171
"interface-datastore": "^1.0.2",
72-
"ipfs-repo-migrations": "^0.2.1",
72+
"ipfs-repo-migrations": "github:ipfs/js-ipfs-repo-migrations#migration/8-multihash_and_keys",
7373
"ipfs-utils": "^2.2.0",
7474
"ipld-block": "^0.9.1",
7575
"it-map": "^1.0.2",
76-
"it-pipe": "^1.1.0",
76+
"it-pushable": "^1.4.0",
7777
"just-safe-get": "^2.0.0",
7878
"just-safe-set": "^2.1.0",
7979
"multibase": "^0.7.0",

src/blockstore-utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ exports.cidToKey = cid => {
1616
throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID')
1717
}
1818

19-
return new Key('/' + multibase.encode('base32', cid.buffer).toString().slice(1).toUpperCase(), false)
19+
return new Key('/' + multibase.encode('base32', cid.multihash).toString().slice(1).toUpperCase(), false)
2020
}
2121

2222
/**
2323
* Transform a datastore Key instance to a CID
24+
* As Key is a multihash of the CID, it is reconstructed using IPLD's RAW codec.
25+
* Hence it is highly probable that stored CID will differ from a CID retrieved from blockstore.
2426
*
2527
* @param {Key} key
2628
* @returns {CID}
2729
*/
2830
exports.keyToCid = key => {
29-
return new CID(multibase.decode('b' + key.toString().slice(1).toLowerCase()))
31+
// Block key is of the form /<base32 encoded string>
32+
return new CID(1, 'raw', multibase.decode('b' + key.toString().slice(1).toLowerCase()))
3033
}

src/blockstore.js

Lines changed: 62 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
const core = require('datastore-core')
44
const ShardingStore = core.ShardingDatastore
55
const Block = require('ipld-block')
6-
const { cidToKey, keyToCid } = require('./blockstore-utils')
6+
const { cidToKey } = require('./blockstore-utils')
77
const map = require('it-map')
8-
const pipe = require('it-pipe')
8+
const drain = require('it-drain')
9+
const pushable = require('it-pushable')
910

1011
module.exports = async (filestore, options) => {
1112
const store = await maybeWithSharding(filestore, options)
@@ -23,7 +24,7 @@ function maybeWithSharding (filestore, options) {
2324
function createBaseStore (store) {
2425
return {
2526
/**
26-
* Query the store.
27+
* Query the store
2728
*
2829
* @param {Object} query
2930
* @param {Object} options
@@ -32,38 +33,23 @@ function createBaseStore (store) {
3233
async * query (query, options) { // eslint-disable-line require-await
3334
yield * store.query(query, options)
3435
},
36+
3537
/**
36-
* Get a single block by CID.
38+
* Get a single block by CID
3739
*
3840
* @param {CID} cid
3941
* @param {Object} options
4042
* @returns {Promise<Block>}
4143
*/
4244
async get (cid, options) {
4345
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)
51-
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-
}
46+
const blockData = await store.get(key, options)
6147

62-
throw err
63-
}
48+
return new Block(blockData, cid)
6449
},
50+
6551
/**
66-
* Like get, but for more.
52+
* Like get, but for more
6753
*
6854
* @param {AsyncIterator<CID>} cids
6955
* @param {Object} options
@@ -74,8 +60,9 @@ function createBaseStore (store) {
7460
yield this.get(cid, options)
7561
}
7662
},
63+
7764
/**
78-
* Write a single block to the store.
65+
* Write a single block to the store
7966
*
8067
* @param {Block} block
8168
* @param {Object} options
@@ -86,59 +73,75 @@ function createBaseStore (store) {
8673
throw new Error('invalid block')
8774
}
8875

89-
const exists = await this.has(block.cid)
76+
const key = cidToKey(block.cid)
77+
const exists = await store.has(key, options)
9078

91-
if (exists) {
92-
return this.get(block.cid, options)
79+
if (!exists) {
80+
await store.put(key, block.data, options)
9381
}
9482

95-
await store.put(cidToKey(block.cid), block.data, options)
96-
9783
return block
9884
},
9985

10086
/**
101-
* Like put, but for more.
87+
* Like put, but for more
10288
*
10389
* @param {AsyncIterable<Block>|Iterable<Block>} blocks
10490
* @param {Object} options
10591
* @returns {AsyncIterable<Block>}
10692
*/
10793
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-
})
94+
// we cannot simply chain to `store.putMany` because we convert a CID into
95+
// a key based on the multihash only, so we lose the version & codec and
96+
// cannot give the user back the CID they used to create the block, so yield
97+
// to `store.putMany` but return the actual block the user passed in.
98+
//
99+
// nb. we want to use `store.putMany` here so bitswap can control batching
100+
// up block HAVEs to send to the network - if we use multiple `store.put`s
101+
// it will not be able to guess we are about to `store.put` more blocks
102+
const output = pushable()
103+
104+
// process.nextTick runs on the microtask queue, setImmediate runs on the next
105+
// event loop iteration so is slower. Use process.nextTick if it is available.
106+
const runner = process && process.nextTick ? process.nextTick : setImmediate
107+
108+
runner(async () => {
109+
try {
110+
await drain(store.putMany(async function * () {
111+
for await (const block of blocks) {
112+
const key = cidToKey(block.cid)
113+
const exists = await store.has(key, options)
114+
115+
if (!exists) {
116+
yield { key, value: block.data }
117+
}
118+
119+
// there is an assumption here that after the yield has completed
120+
// the underlying datastore has finished writing the block
121+
output.push(block)
122+
}
123+
}()))
124+
125+
output.end()
126+
} catch (err) {
127+
output.end(err)
125128
}
126-
)
129+
})
130+
131+
yield * output
127132
},
133+
128134
/**
129-
* Does the store contain block with this cid?
135+
* Does the store contain block with this CID?
130136
*
131137
* @param {CID} cid
132138
* @param {Object} options
133139
* @returns {Promise<bool>}
134140
*/
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)
141+
async has (cid, options) { // eslint-disable-line require-await
142+
return store.has(cidToKey(cid), options)
141143
},
144+
142145
/**
143146
* Delete a block from the store
144147
*
@@ -149,6 +152,7 @@ function createBaseStore (store) {
149152
async delete (cid, options) { // eslint-disable-line require-await
150153
return store.delete(cidToKey(cid), options)
151154
},
155+
152156
/**
153157
* Delete a block from the store
154158
*
@@ -157,12 +161,9 @@ function createBaseStore (store) {
157161
* @returns {Promise<void>}
158162
*/
159163
async * deleteMany (cids, options) { // eslint-disable-line require-await
160-
yield * store.deleteMany((async function * () {
161-
for await (const cid of cids) {
162-
yield cidToKey(cid)
163-
}
164-
}()), options)
164+
yield * store.deleteMany(map(cids, cid => cidToKey(cid)), options)
165165
},
166+
166167
/**
167168
* Close the store
168169
*
@@ -173,11 +174,3 @@ function createBaseStore (store) {
173174
}
174175
}
175176
}
176-
177-
function cidToOtherVersion (cid) {
178-
try {
179-
return cid.version === 0 ? cid.toV1() : cid.toV0()
180-
} catch (err) {
181-
return null
182-
}
183-
}

src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
22

33
module.exports = {
4-
repoVersion: 7
4+
repoVersion: 8
55
}

0 commit comments

Comments
 (0)