Preliminary cache migration code

cache loading test

this.attemptMigration

Migration data and cleanup

Linting

IPFS data

Revert "Linting"

This reverts commit e41bc4a9ec2011716300134f985c7ec749743177.

Revert "IPFS data"

This reverts commit 299e0b7b72d74cdbaec80ad0796211790404e4c3.

Better fixtures

package-lock.json

Test for directory options

directory option working

Fixing eventlog tests

Safer migration

Moving to migrations folder

Linting
This commit is contained in:
Mark Henderson
2019-08-31 23:01:17 -04:00
parent 9b58f429cd
commit e793edf9cf
42 changed files with 532 additions and 13 deletions

View File

@@ -18,6 +18,7 @@ const exchangeHeads = require('./exchange-heads')
const { isDefined, io } = require('./utils')
const Storage = require('orbit-db-storage-adapter')
const leveldown = require('leveldown')
const migrations = require('./migrations')
const Logger = require('logplease')
const logger = Logger.create('orbit-db')
@@ -303,6 +304,8 @@ class OrbitDB {
if (haveDB && !options.overwrite) { throw new Error(`Database '${dbAddress}' already exists!`) }
await this._migrate(options, dbAddress)
// Save the database locally
await this._addManifestToCache(options.cache, dbAddress)
@@ -395,10 +398,21 @@ class OrbitDB {
if (!cache) {
return false
}
const data = await cache.get(path.join(dbAddress.toString(), '_manifest'))
const addr = dbAddress.toString()
const data = await cache.get(path.join(addr, '_manifest'))
return data !== undefined && data !== null
}
/**
* Runs all migrations inside the src/migration folder
* @param Object options Options to pass into the migration
* @param OrbitDBAddress dbAddress Address of database in OrbitDBAddress format
*/
async _migrate (options, dbAddress) {
await migrations.run(this, options, dbAddress)
}
/**
* Returns supported database types as an Array of strings
* Eg. [ 'counter', 'eventlog', 'feed', 'docstore', 'keyvalue']

View File

@@ -0,0 +1,44 @@
const path = require('path')
const fs = require('fs')
const Cache = require('orbit-db-cache')
const Logger = require('logplease')
const logger = Logger.create('orbit-db')
Logger.setLogLevel('ERROR')
async function migrate (OrbitDB, options, dbAddress) {
let oldCache = OrbitDB.caches[options.directory]; let oldStore
if (!oldCache) {
const addr = path.join(OrbitDB.directory, dbAddress.root, dbAddress.path)
if (fs && fs.existsSync && !fs.existsSync(addr)) return
oldStore = await OrbitDB.storage.createStore(addr)
oldCache = new Cache(oldStore)
}
const _localHeads = await oldCache.get('_localHeads')
if (!_localHeads) return
const keyRoot = dbAddress.toString()
logger.debug('Attempting to migrate from old cache location')
const migrationKeys = [
'_remoteHeads',
'_localHeads',
'snapshot',
'queue'
]
for (let i in migrationKeys) {
try {
const key = path.join(keyRoot, migrationKeys[i])
const val = await oldCache.get(migrationKeys[i])
if (val) await options.cache.set(key, val)
} catch (e) {
logger.debug(e.message)
}
}
await options.cache.set(path.join(keyRoot, '_manifest'), dbAddress.root)
if (oldStore) await oldStore.close()
}
module.exports = migrate

11
src/migrations/index.js Normal file
View File

@@ -0,0 +1,11 @@
const from021To022 = require('./0.21-0.22')
const migrations = [from021To022]
async function run (OrbitDB, options, dbAddress) {
for (let i = 0; i < migrations.length; i++) {
await migrations[i](OrbitDB, options, dbAddress)
}
}
module.exports = { run }