|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { |
| 4 | + createStore, |
| 5 | + findLevelJs |
| 6 | +} = require('../../src/utils') |
| 7 | +const { Key } = require('interface-datastore') |
| 8 | +const fromString = require('uint8arrays/from-string') |
| 9 | +const toString = require('uint8arrays/to-string') |
| 10 | + |
| 11 | +async function keysToBinary (name, store, onProgress = () => {}) { |
| 12 | + let db = findLevelJs(store) |
| 13 | + |
| 14 | + // only interested in level-js |
| 15 | + if (!db) { |
| 16 | + onProgress(`${name} did not need an upgrade`) |
| 17 | + |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + onProgress(`Upgrading ${name}`) |
| 22 | + |
| 23 | + await withEach(db, (key, value) => { |
| 24 | + return [ |
| 25 | + { type: 'del', key: key }, |
| 26 | + { type: 'put', key: fromString(key), value: value } |
| 27 | + ] |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +async function keysToStrings (name, store, onProgress = () => {}) { |
| 32 | + let db = findLevelJs(store) |
| 33 | + |
| 34 | + // only interested in level-js |
| 35 | + if (!db) { |
| 36 | + onProgress(`${name} did not need a downgrade`) |
| 37 | + |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + onProgress(`Downgrading ${name}`) |
| 42 | + |
| 43 | + await withEach(db, (key, value) => { |
| 44 | + return [ |
| 45 | + { type: 'del', key: key }, |
| 46 | + { type: 'put', key: toString(key), value: value } |
| 47 | + ] |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +async function process (repoPath, repoOptions, onProgress, fn) { |
| 52 | + const datastores = Object.keys(repoOptions.storageBackends) |
| 53 | + .filter(key => repoOptions.storageBackends[key].name === 'LevelDatastore') |
| 54 | + .map(name => ({ |
| 55 | + name, |
| 56 | + store: createStore(repoPath, name, repoOptions) |
| 57 | + })) |
| 58 | + |
| 59 | + onProgress(0, `Migrating ${datastores.length} dbs`) |
| 60 | + let migrated = 0 |
| 61 | + |
| 62 | + for (const { name, store } of datastores) { |
| 63 | + await store.open() |
| 64 | + |
| 65 | + try { |
| 66 | + await fn(name, store, (message) => { |
| 67 | + onProgress(parseInt((migrated / datastores.length) * 100), message) |
| 68 | + }) |
| 69 | + } finally { |
| 70 | + migrated++ |
| 71 | + store.close() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + onProgress(100, `Migrated ${datastores.length} dbs`) |
| 76 | +} |
| 77 | + |
| 78 | +module.exports = { |
| 79 | + version: 10, |
| 80 | + description: 'Migrates datastore-level keys to binary', |
| 81 | + migrate: (repoPath, repoOptions, onProgress = () => {}) => { |
| 82 | + return process(repoPath, repoOptions, onProgress, keysToBinary) |
| 83 | + }, |
| 84 | + revert: (repoPath, repoOptions, onProgress = () => {}) => { |
| 85 | + return process(repoPath, repoOptions, onProgress, keysToStrings) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @typedef {Uint8Array|string} Key |
| 91 | + * @typedef {Uint8Array} Value |
| 92 | + * @typedef {{ type: 'del', key: Key } | { type: 'put', key: Key, value: Value }} Operation |
| 93 | + * |
| 94 | + * Uses the upgrade strategy from [email protected] - note we can't call the `.upgrade` command |
| 95 | + * directly because it will be removed in [email protected] and we can't guarantee users will |
| 96 | + * have migrated by then - e.g. they may jump from [email protected] straight to [email protected] |
| 97 | + * so we have to duplicate the code here. |
| 98 | + * |
| 99 | + * @param {import('interface-datastore').Datastore} db |
| 100 | + * @param {function (Key, Value): Operation[]} fn |
| 101 | + */ |
| 102 | +function withEach (db, fn) { |
| 103 | + function batch (operations, next) { |
| 104 | + const store = db.store('readwrite') |
| 105 | + const transaction = store.transaction |
| 106 | + let index = 0 |
| 107 | + let error |
| 108 | + |
| 109 | + transaction.onabort = () => next(error || transaction.error || new Error('aborted by user')) |
| 110 | + transaction.oncomplete = () => next() |
| 111 | + |
| 112 | + function loop () { |
| 113 | + var op = operations[index++] |
| 114 | + var key = op.key |
| 115 | + |
| 116 | + try { |
| 117 | + var req = op.type === 'del' ? store.delete(key) : store.put(op.value, key) |
| 118 | + } catch (err) { |
| 119 | + error = err |
| 120 | + transaction.abort() |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + if (index < operations.length) { |
| 125 | + req.onsuccess = loop |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + loop() |
| 130 | + } |
| 131 | + |
| 132 | + return new Promise((resolve, reject) => { |
| 133 | + const it = db.iterator() |
| 134 | + // raw keys and values only |
| 135 | + it._deserializeKey = it._deserializeValue = (data) => data |
| 136 | + next() |
| 137 | + |
| 138 | + function next () { |
| 139 | + it.next((err, key, value) => { |
| 140 | + if (err || key === undefined) { |
| 141 | + it.end((err2) => { |
| 142 | + if (err2) { |
| 143 | + reject(err2) |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + resolve() |
| 148 | + }) |
| 149 | + |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + batch(fn(key, value), next) |
| 154 | + }) |
| 155 | + } |
| 156 | + }) |
| 157 | +} |
0 commit comments