@polkadot/db transaction updates (#388)
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
# 0.76.1
|
||||
|
||||
- Swap DB interfaces to optional LRU, removing transactionAsync (client-only changes)
|
||||
|
||||
# 0.75.1
|
||||
|
||||
- Start journey to 1.0
|
||||
|
||||
@@ -65,13 +65,15 @@ import TransactionDb from './engines/TransactionDb';
|
||||
* ```
|
||||
*/
|
||||
export default class DiskDb extends TransactionDb {
|
||||
constructor (base: string, name: string, options?: BaseDbOptions) {
|
||||
constructor (base: string, name: string, options: BaseDbOptions = {}) {
|
||||
const backing = options.isNative
|
||||
? new FileFlatDb(base, name, options)
|
||||
: new LmDb(base, name, options);
|
||||
|
||||
super(
|
||||
options && options.isNative
|
||||
? new LruDb(
|
||||
new FileFlatDb(base, name, options)
|
||||
)
|
||||
: new LmDb(base, name, options)
|
||||
options.isLru
|
||||
? new LruDb(backing)
|
||||
: backing
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ const ONE_K = 1024;
|
||||
// 16 for nibbles (key -> asNibbles), 256 for bytes (key -> asU8a)
|
||||
// There is a tradeof here:
|
||||
// - with nibbles there are more writes and more read branching traversals
|
||||
// - with bytes there are less writes, however more (initial) disk space
|
||||
const ENTRY_NUM: 16 | 256 = 256;
|
||||
// - with bytes there are less writes, however more disk space
|
||||
const ENTRY_NUM: 16 | 256 = 16;
|
||||
const ENTRY_SIZE = 1 + UINT_SIZE;
|
||||
|
||||
// the size of a branch entry, 96 for nibbles, 1,536 for bytes
|
||||
|
||||
@@ -26,28 +26,6 @@ export default class TransactionDb implements TxDb {
|
||||
this.txStarted = false;
|
||||
}
|
||||
|
||||
async transactionAsync<T> (fn: () => Promise<T>): Promise<T> {
|
||||
l.debug(() => ['transactionAsync']);
|
||||
|
||||
try {
|
||||
this.createTx();
|
||||
|
||||
const result = await fn();
|
||||
|
||||
if (result) {
|
||||
this.commitTx();
|
||||
} else {
|
||||
this.revertTx();
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.revertTx();
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
transaction<T> (fn: () => T): T {
|
||||
l.debug(() => ['transaction']);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ export type ProgressCb = (progress: ProgressValue) => void;
|
||||
|
||||
export type BaseDbOptions = {
|
||||
isCompressed?: boolean,
|
||||
isLru?: boolean,
|
||||
isNative?: boolean
|
||||
};
|
||||
|
||||
@@ -34,6 +35,5 @@ export interface BaseDb {
|
||||
}
|
||||
|
||||
export interface TxDb extends BaseDb {
|
||||
transactionAsync <T> (fn: () => Promise<T>): Promise<T>;
|
||||
transaction <T> (fn: () => T): T;
|
||||
}
|
||||
|
||||
@@ -31,26 +31,6 @@ export default class Trie extends Impl implements TrieDb {
|
||||
l.log(`Created with ${this.codec.type} codec, root ${u8aToHex(this.rootHash, 64)}`);
|
||||
}
|
||||
|
||||
async transactionAsync<T> (fn: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
this.createCheckpoint();
|
||||
|
||||
const result = await this.db.transactionAsync(fn);
|
||||
|
||||
if (result) {
|
||||
this.commitCheckpoint();
|
||||
} else {
|
||||
this.revertCheckpoint();
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.revertCheckpoint();
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
transaction<T> (fn: () => T): T {
|
||||
try {
|
||||
this.createCheckpoint();
|
||||
|
||||
Reference in New Issue
Block a user