isCompressed option to database

This commit is contained in:
Jaco Greeff
2018-09-03 12:10:07 +02:00
parent 8e4c2b066a
commit 67895df61b
5 changed files with 29 additions and 16 deletions
+4 -2
View File
@@ -2,15 +2,17 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { BaseDbOptions } from './types';
import FileFlatDb from './engines/FileFlatDb';
import LruDb from './engines/LruDb';
import TransactionDb from './engines/TransactionDb';
export default class DiskDb extends TransactionDb {
constructor (base: string, name: string) {
constructor (base: string, name: string, options?: BaseDbOptions) {
super(
new LruDb(
new FileFlatDb(base, name)
new FileFlatDb(base, name, options)
)
);
}
+4 -2
View File
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { BaseDbOptions } from './types';
import MemoryDb from './engines/MemoryDb';
import TransactionDb from './engines/TransactionDb';
export default class Memory extends TransactionDb {
constructor () {
super(new MemoryDb());
constructor (options?: BaseDbOptions) {
super(new MemoryDb(options));
}
}
+15 -10
View File
@@ -2,7 +2,7 @@
// 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 { BaseDb, BaseDbOptions, ProgressCb } from '../types';
import fs from 'fs';
import mkdirp from 'mkdirp';
@@ -10,6 +10,7 @@ import path from 'path';
import snappy from 'snappy';
import assert from '@polkadot/util/assert';
import logger from '@polkadot/util/logger';
import isUndefined from '@polkadot/util/is/undefined';
import bufferToU8a from '@polkadot/util/buffer/toU8a';
import u8aToBuffer from '@polkadot/util/u8a/toBuffer';
import u8aToHex from '@polkadot/util/u8a/toHex';
@@ -57,10 +58,14 @@ const l = logger('db/flat');
export default class FileFlatDb implements BaseDb {
private _fd: number;
private _file: string;
private _isCompressed: boolean;
constructor (base: string, file: string = DEFAULT_FILE) {
constructor (base: string, file: string = DEFAULT_FILE, options: BaseDbOptions = {}) {
this._fd = -1;
this._file = path.join(base, file);
this._isCompressed = options && !isUndefined(options.isCompressed)
? options.isCompressed
: false;
mkdirp.sync(base);
}
@@ -141,19 +146,19 @@ export default class FileFlatDb implements BaseDb {
}
private _deserializeValue (value: Buffer): Uint8Array | null {
// return bufferToU8a(value);
return bufferToU8a(
snappy.uncompressSync(value)
this._isCompressed
? snappy.uncompressSync(value)
: value
);
}
private _serializeValue (value: Uint8Array): Buffer {
// return u8aToBuffer(value);
return snappy.compressSync(
u8aToBuffer(value)
);
return this._isCompressed
? snappy.compressSync(
u8aToBuffer(value)
)
: u8aToBuffer(value);
}
private _serializeKey (key: Uint8Array): Buffer {
+2 -2
View File
@@ -2,7 +2,7 @@
// 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 { BaseDb, BaseDbOptions, ProgressCb } from '../types';
// import logger from '@polkadot/util/logger';
// import u8aToHex from '@polkadot/util/u8a/toHex';
@@ -16,7 +16,7 @@ type Storage = {
export default class MemoryDb implements BaseDb {
private storage: Storage;
constructor () {
constructor (options?: BaseDbOptions) {
this.storage = {};
}
+4
View File
@@ -10,6 +10,10 @@ export type ProgressValue = {
export type ProgressCb = (progress: ProgressValue) => void;
export type BaseDbOptions = {
isCompressed?: boolean,
};
export interface BaseDb {
close (): void;
open (): void;