Add RocksDb interface (#208)
* Add RocksDb interface * Move binary deps to optional * LevelDb skip * Travis config for rocksdb * Use pushd to change directory * Don't forget to run yarn * CFLAGS for gcc? * Library should compress * Update dependencies
This commit is contained in:
@@ -9,7 +9,7 @@ import toU8a from '@polkadot/util/u8a/fromString';
|
||||
|
||||
import LevelDb from './LevelDb';
|
||||
|
||||
describe('LevelDb', () => {
|
||||
describe.skip('LevelDb', () => {
|
||||
const base = path.join(os.tmpdir(), '--test--');
|
||||
const name = 'LevelDb';
|
||||
const location = path.join(base, name);
|
||||
|
||||
@@ -7,10 +7,11 @@ import { BaseDb, ProgressCb } from '../types';
|
||||
import leveldb, { LevelDb } from 'nosql-leveldb';
|
||||
import mkdirp from 'mkdirp';
|
||||
import path from 'path';
|
||||
import snappy from 'snappy';
|
||||
// import snappy from 'snappy';
|
||||
import logger from '@polkadot/util/logger';
|
||||
import bufferToU8a from '@polkadot/util/buffer/toU8a';
|
||||
import u8aToBuffer from '@polkadot/util/u8a/toBuffer';
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
|
||||
// import assert from '@polkadot/util/assert';
|
||||
// import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
@@ -38,7 +39,6 @@ export default class Level implements BaseDb {
|
||||
l.debug(() => ['open']);
|
||||
|
||||
this.db.openSync({
|
||||
compression: false,
|
||||
createIfMissing: true
|
||||
});
|
||||
}
|
||||
@@ -79,19 +79,22 @@ export default class Level implements BaseDb {
|
||||
|
||||
private _deserializeValue (value: Buffer): Uint8Array | null {
|
||||
return value
|
||||
? bufferToU8a(
|
||||
snappy.uncompressSync(value)
|
||||
)
|
||||
? bufferToU8a(value)
|
||||
// ? bufferToU8a(
|
||||
// snappy.uncompressSync(value)
|
||||
// )
|
||||
: null;
|
||||
}
|
||||
|
||||
private _serializeKey (key: Uint8Array): Buffer {
|
||||
return u8aToBuffer(key);
|
||||
private _serializeKey (key: Uint8Array): string {
|
||||
return u8aToHex(key);
|
||||
}
|
||||
|
||||
private _serializeValue (value: Uint8Array): Buffer {
|
||||
return snappy.compressSync(
|
||||
u8aToBuffer(value)
|
||||
);
|
||||
return u8aToBuffer(value);
|
||||
|
||||
// return snappy.compressSync(
|
||||
// u8aToBuffer(value)
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright 2017-2018 @polkadot/db authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import mkdirp from 'mkdirp';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import toU8a from '@polkadot/util/u8a/fromString';
|
||||
|
||||
import RocksDb from './RocksDb';
|
||||
|
||||
describe.skip('RocksDb', () => {
|
||||
const base = path.join(os.tmpdir(), '--test--');
|
||||
const name = 'RocksDb';
|
||||
const location = path.join(base, name);
|
||||
let db;
|
||||
|
||||
beforeAll(() => {
|
||||
db = new RocksDb(base, name);
|
||||
db.open();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
db.close();
|
||||
rimraf.sync(location);
|
||||
});
|
||||
|
||||
it('sets & retrieves', () => {
|
||||
const key = toU8a('key0');
|
||||
const value = toU8a('value0');
|
||||
|
||||
db.put(key, value);
|
||||
|
||||
expect(db.get(key)).toEqual(value);
|
||||
});
|
||||
|
||||
it('retrieves null for non-existing', () => {
|
||||
const key = toU8a('key1');
|
||||
|
||||
expect(db.get(key)).toEqual(null);
|
||||
});
|
||||
|
||||
it('overrides values', () => {
|
||||
const key = toU8a('key2');
|
||||
const value = toU8a('value1');
|
||||
const replace = toU8a('value2');
|
||||
|
||||
db.put(key, value);
|
||||
|
||||
expect(db.get(key)).toEqual(value);
|
||||
|
||||
db.put(key, replace);
|
||||
|
||||
expect(db.get(key)).toEqual(replace);
|
||||
});
|
||||
|
||||
it('deletes values', () => {
|
||||
const key = toU8a('key2');
|
||||
|
||||
db.del(key);
|
||||
|
||||
expect(db.get(key)).toEqual(null);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,98 @@
|
||||
// Copyright 2017-2018 @polkadot/db authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { BaseDb, ProgressCb } from '../types';
|
||||
|
||||
import rocksdb, { RocksDb } from 'rocksdb-node';
|
||||
import mkdirp from 'mkdirp';
|
||||
import path from 'path';
|
||||
// import snappy from 'snappy';
|
||||
import logger from '@polkadot/util/logger';
|
||||
import bufferToU8a from '@polkadot/util/buffer/toU8a';
|
||||
import u8aToBuffer from '@polkadot/util/u8a/toBuffer';
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
|
||||
// import assert from '@polkadot/util/assert';
|
||||
// import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
|
||||
const l = logger('db/rocksdb');
|
||||
|
||||
export default class Rocks implements BaseDb {
|
||||
private db: RocksDb;
|
||||
|
||||
constructor (base: string, name: string) {
|
||||
const location = path.join(base, name);
|
||||
|
||||
mkdirp.sync(location);
|
||||
|
||||
this.db = rocksdb.open({ create_if_missing: true }, location);
|
||||
}
|
||||
|
||||
close (): void {
|
||||
l.debug(() => ['close']);
|
||||
|
||||
this.db.close();
|
||||
}
|
||||
|
||||
open (): void {
|
||||
l.debug(() => ['open']);
|
||||
}
|
||||
|
||||
maintain (fn: ProgressCb): void {
|
||||
fn({
|
||||
isCompleted: true,
|
||||
keys: 0,
|
||||
percent: 100
|
||||
});
|
||||
}
|
||||
|
||||
del (key: Uint8Array): void {
|
||||
this.db.del(
|
||||
{},
|
||||
this._serializeKey(key)
|
||||
);
|
||||
}
|
||||
|
||||
get (key: Uint8Array): Uint8Array | null {
|
||||
try {
|
||||
return this._deserializeValue(
|
||||
this.db.get(
|
||||
{ buffer: true },
|
||||
this._serializeKey(key)
|
||||
) as Buffer
|
||||
);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
put (key: Uint8Array, value: Uint8Array): void {
|
||||
this.db.put(
|
||||
{},
|
||||
this._serializeKey(key),
|
||||
this._serializeValue(value)
|
||||
);
|
||||
}
|
||||
|
||||
private _deserializeValue (value: Buffer): Uint8Array | null {
|
||||
return value
|
||||
? bufferToU8a(value)
|
||||
// ? bufferToU8a(
|
||||
// snappy.uncompressSync(value)
|
||||
// )
|
||||
: null;
|
||||
}
|
||||
|
||||
private _serializeKey (key: Uint8Array): string {
|
||||
return u8aToHex(key);
|
||||
}
|
||||
|
||||
private _serializeValue (value: Uint8Array): Buffer {
|
||||
return u8aToBuffer(value);
|
||||
|
||||
// return snappy.compressSync(
|
||||
// u8aToBuffer(value)
|
||||
// );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user