|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | +const CID = require('cids') |
| 5 | +const Key = require('interface-datastore').Key |
| 6 | +const core = require('datastore-core') |
| 7 | +const ShardingStore = core.ShardingDatastore |
| 8 | +const mb = require('multibase') |
| 9 | +const utils = require('../../src/utils') |
| 10 | +const log = require('debug')('ipfs-repo-migrations:migration-8') |
| 11 | + |
| 12 | +// This function in js-ipfs-repo defaults to not using sharding |
| 13 | +// but the default value of the options.sharding is true hence this |
| 14 | +// function defaults to use sharding. |
| 15 | +async function maybeWithSharding (filestore, options) { |
| 16 | + if (options.sharding === false) { |
| 17 | + return filestore |
| 18 | + } |
| 19 | + |
| 20 | + const shard = new core.shard.NextToLast(2) |
| 21 | + |
| 22 | + return ShardingStore.createOrOpen(filestore, shard) |
| 23 | +} |
| 24 | + |
| 25 | +function keyToMultihash (key) { |
| 26 | + const buf = mb.decode(`b${key.toString().slice(1)}`) |
| 27 | + |
| 28 | + // Extract multihash from CID |
| 29 | + let multihash = new CID(buf).multihash |
| 30 | + |
| 31 | + // Encode and slice off multibase codec |
| 32 | + multihash = mb.encode('base32', multihash).slice(1) |
| 33 | + |
| 34 | + // Should be uppercase for interop with go |
| 35 | + multihash = multihash.toString().toUpperCase() |
| 36 | + |
| 37 | + return new Key(`/${multihash}`, false) |
| 38 | +} |
| 39 | + |
| 40 | +function keyToCid (key) { |
| 41 | + const buf = mb.decode(`b${key.toString().slice(1)}`) |
| 42 | + |
| 43 | + // CID to Key |
| 44 | + const multihash = mb.encode('base32', new CID(1, 'raw', buf).buffer).slice(1) |
| 45 | + |
| 46 | + return new Key(`/${multihash}`.toUpperCase(), false) |
| 47 | +} |
| 48 | + |
| 49 | +async function process (repoPath, options, keyFunction){ |
| 50 | + const { StorageBackend, storageOptions } = utils.getDatastoreAndOptions(options, 'blocks') |
| 51 | + |
| 52 | + const baseStore = new StorageBackend(path.join(repoPath, 'blocks'), storageOptions) |
| 53 | + await baseStore.open() |
| 54 | + const store = await maybeWithSharding(baseStore, storageOptions) |
| 55 | + await store.open() |
| 56 | + |
| 57 | + try { |
| 58 | + let counter = 0 |
| 59 | + |
| 60 | + for await (const block of store.query({})) { |
| 61 | + const newKey = keyFunction(block.key) |
| 62 | + |
| 63 | + // If the Key is base32 CIDv0 then there's nothing to do |
| 64 | + if(newKey.toString() !== block.key.toString()) { |
| 65 | + counter += 1 |
| 66 | + |
| 67 | + log(`Migrating Block from ${block.key.toString()} to ${newKey.toString()}`) |
| 68 | + await store.delete(block.key) |
| 69 | + await store.put(newKey, block.value) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + log(`Changed ${ counter } blocks`) |
| 74 | + } finally { |
| 75 | + await store.close() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +module.exports = { |
| 80 | + version: 8, |
| 81 | + description: 'Transforms key names into base32 encoding and converts Block store to use bare multihashes encoded as base32', |
| 82 | + migrate: (repoPath, options = {}) => { |
| 83 | + return process(repoPath, options, keyToMultihash) |
| 84 | + }, |
| 85 | + revert: (repoPath, options = {}) => { |
| 86 | + return process(repoPath, options, keyToCid) |
| 87 | + } |
| 88 | +} |
0 commit comments