mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-05-20 13:56:39 +00:00

Add tests to make sure a database can be saved and loaded multiple times in a row Add a test to make sure the correct replication progress is emitted Add mem-store for faster development testing Improve tests Improve logging Fix replication benchmark Update packages and build
96 lines
2.0 KiB
JavaScript
96 lines
2.0 KiB
JavaScript
'use strict'
|
|
|
|
const multihashing = require('multihashing-async')
|
|
const mh = require('multihashes')
|
|
|
|
const defaultHashAlg = 'sha2-256'
|
|
|
|
// 'use strict'
|
|
|
|
// const ImmutableDB = require('./immutabledb-interface')
|
|
|
|
const defaultFormat = { format: 'dag-cbor', hashAlg: 'sha2-256' }
|
|
|
|
/* ImmutableDB using IPLD (through IPFS) */
|
|
class IPLDStore {
|
|
constructor (ipfs) {
|
|
// super()
|
|
this._ipfs = ipfs
|
|
}
|
|
|
|
async put (value) {
|
|
const cid = await this._ipfs.dag.put(value, defaultFormat)
|
|
return cid.toBaseEncodedString()
|
|
}
|
|
|
|
async get (key) {
|
|
const result = await this._ipfs.dag.get(key)
|
|
return result.value
|
|
}
|
|
}
|
|
|
|
const createMultihash = (data, hashAlg) => {
|
|
return new Promise((resolve, reject) => {
|
|
multihashing(data, hashAlg || defaultHashAlg, (err, multihash) => {
|
|
if (err)
|
|
return reject(err)
|
|
|
|
resolve(mh.toB58String(multihash))
|
|
})
|
|
})
|
|
}
|
|
|
|
// const LRU = require('lru')
|
|
// const ImmutableDB = require('./immutabledb-interface')
|
|
// const createMultihash = require('./create-multihash')
|
|
|
|
/* Memory store using an LRU cache */
|
|
class MemStore {
|
|
constructor () {
|
|
this._store = {}//new LRU(1000)
|
|
}
|
|
|
|
async put (value) {
|
|
const data = value//new Buffer(JSON.stringify(value))
|
|
const hash = await createMultihash(data)
|
|
// console.log(this._store)
|
|
// this._store.set(hash, data)
|
|
if (!this._store) this._store = {}
|
|
// console.log(this._store)
|
|
// console.log(hash, data)
|
|
this._store[hash] = data
|
|
// return hash
|
|
return {
|
|
toJSON: () => {
|
|
return {
|
|
data: value,
|
|
multihash: hash,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async get (key) {
|
|
// const data = this._store.get(key)
|
|
const data = this._store[key]
|
|
|
|
// if (data) {
|
|
// const value = JSON.parse(data)
|
|
// return value
|
|
// }
|
|
|
|
// return data
|
|
return {
|
|
toJSON: () => {
|
|
return {
|
|
data: this._store[key],
|
|
multihash: key,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = MemStore
|