Convert MemoryDb & Transaction DB overlays to Map (#422)
* Convert MemoryDb & Transaction DB overlays to Map * Remove unused options
This commit is contained in:
@@ -65,6 +65,6 @@ import TransactionDb from './engines/TransactionDb';
|
||||
*/
|
||||
export default class Memory extends TransactionDb {
|
||||
constructor (options?: BaseDbOptions) {
|
||||
super(new MemoryDb(options));
|
||||
super(new MemoryDb());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,22 +2,14 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BaseDb, BaseDbOptions, ProgressCb } from '../types';
|
||||
import { BaseDb, ProgressCb } from '../types';
|
||||
|
||||
import { logger } from '@polkadot/util';
|
||||
|
||||
type Storage = {
|
||||
[index: string]: Uint8Array
|
||||
};
|
||||
|
||||
const l = logger('db/memory');
|
||||
|
||||
export default class MemoryDb implements BaseDb {
|
||||
private storage: Storage;
|
||||
|
||||
constructor (options?: BaseDbOptions) {
|
||||
this.storage = {};
|
||||
}
|
||||
private _storage: Map<string, Uint8Array> = new Map();
|
||||
|
||||
close (): void {
|
||||
this.empty();
|
||||
@@ -32,7 +24,7 @@ export default class MemoryDb implements BaseDb {
|
||||
}
|
||||
|
||||
empty (): void {
|
||||
this.storage = {};
|
||||
this._storage.clear();
|
||||
}
|
||||
|
||||
rename (base: string, file: string): void {
|
||||
@@ -42,32 +34,36 @@ export default class MemoryDb implements BaseDb {
|
||||
maintain (fn: ProgressCb): void {
|
||||
fn({
|
||||
isCompleted: true,
|
||||
keys: Object.keys(this.storage).length,
|
||||
keys: this._storage.size,
|
||||
percent: 100
|
||||
});
|
||||
}
|
||||
|
||||
size (): number {
|
||||
return Object
|
||||
.entries(this.storage)
|
||||
.reduce((size, value) => size + value.length, 0);
|
||||
let size = 0;
|
||||
|
||||
this._storage.forEach((value) =>
|
||||
size += value.length
|
||||
);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
del (key: Uint8Array): void {
|
||||
// l.debug(() => ['del', { key }]);
|
||||
|
||||
delete this.storage[key.toString()];
|
||||
this._storage.delete(key.toString());
|
||||
}
|
||||
|
||||
get (key: Uint8Array): Uint8Array | null {
|
||||
// l.debug(() => ['get', { key }]);
|
||||
|
||||
return this.storage[key.toString()] || null;
|
||||
return this._storage.get(key.toString()) || null;
|
||||
}
|
||||
|
||||
put (key: Uint8Array, value: Uint8Array): void {
|
||||
// l.debug(() => ['put', { key, value }]);
|
||||
|
||||
this.storage[key.toString()] = value;
|
||||
this._storage.set(key.toString(), value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,23 +6,20 @@ import { BaseDb, TxDb, ProgressCb } from '../types';
|
||||
|
||||
import { assert, isNull, logger } from '@polkadot/util';
|
||||
|
||||
type Overlay = {
|
||||
[index: string]: {
|
||||
key: Uint8Array,
|
||||
value: Uint8Array | null
|
||||
}
|
||||
type OverlayItem = {
|
||||
key: Uint8Array,
|
||||
value: Uint8Array | null
|
||||
};
|
||||
|
||||
const l = logger('db/transact');
|
||||
|
||||
export default class TransactionDb implements TxDb {
|
||||
private backing: BaseDb;
|
||||
private txOverlay: Overlay;
|
||||
private txOverlay: Map<string, OverlayItem> = new Map();
|
||||
private txStarted: boolean;
|
||||
|
||||
constructor (backing: BaseDb) {
|
||||
this.backing = backing;
|
||||
this.txOverlay = {};
|
||||
this.txStarted = false;
|
||||
}
|
||||
|
||||
@@ -80,10 +77,7 @@ export default class TransactionDb implements TxDb {
|
||||
|
||||
del (key: Uint8Array): void {
|
||||
if (this.txStarted) {
|
||||
this.txOverlay[key.toString()] = {
|
||||
key,
|
||||
value: null
|
||||
};
|
||||
this.txOverlay.set(key.toString(), { key, value: null });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -94,7 +88,7 @@ export default class TransactionDb implements TxDb {
|
||||
// l.debug(() => ['get', u8aToHex(key)]);
|
||||
|
||||
if (this.txStarted) {
|
||||
const value = this.txOverlay[key.toString()];
|
||||
const value = this.txOverlay.get(key.toString());
|
||||
|
||||
if (value) {
|
||||
return value.value;
|
||||
@@ -108,10 +102,7 @@ export default class TransactionDb implements TxDb {
|
||||
// l.debug(() => ['put', u8aToHex(key), u8aToHex(value)]);
|
||||
|
||||
if (this.txStarted) {
|
||||
this.txOverlay[key.toString()] = {
|
||||
key,
|
||||
value
|
||||
};
|
||||
this.txOverlay.set(key.toString(), { key, value });
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -124,7 +115,7 @@ export default class TransactionDb implements TxDb {
|
||||
|
||||
assert(!this.txStarted, 'Cannot create a transaction when one is already active');
|
||||
|
||||
this.txOverlay = {};
|
||||
this.txOverlay.clear();
|
||||
this.txStarted = true;
|
||||
}
|
||||
|
||||
@@ -137,7 +128,7 @@ export default class TransactionDb implements TxDb {
|
||||
this.backing.txStart();
|
||||
}
|
||||
|
||||
Object.values(this.txOverlay).forEach(({ key, value }) => {
|
||||
this.txOverlay.forEach(({ key, value }) => {
|
||||
if (isNull(value)) {
|
||||
this.backing.del(key);
|
||||
} else {
|
||||
@@ -149,7 +140,7 @@ export default class TransactionDb implements TxDb {
|
||||
this.backing.txCommit();
|
||||
}
|
||||
|
||||
this.txOverlay = {};
|
||||
this.txOverlay.clear();
|
||||
this.txStarted = false;
|
||||
}
|
||||
|
||||
@@ -158,7 +149,7 @@ export default class TransactionDb implements TxDb {
|
||||
|
||||
assert(this.txStarted, 'Cannot revert when not in transaction');
|
||||
|
||||
this.txOverlay = {};
|
||||
this.txOverlay.clear();
|
||||
this.txStarted = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user