8.0 KiB
name: MemoryDb
summary: Creates a MemoryDb database extending TransactionDb
description: Create an instance of a Memory database using MemoryDb and extending TransactionDb. options argument (optional) used to specifies whether to use compression.
- Enable compression when storing "blocks" since they compress well.
- Disable compression when storing "state" Transactions Trie using @polkadot/trie-db or @polkadot/db/src/engines/TransactionDb.ts (potentially LevelDb or RocksDb implementation of a Merkle Patricia Trie) data structure serialised with Recursive Length Prefix (RLP) encoding
example:
import stringToU8a from '@polkadot/util/string/toU8a';
import MemoryDb from '@polkadot/db/Memory';
import TransactionDb from '@polkadot/db/engines/TransactionDb';
const memoryDb = new MemoryDb();
const txDb = new TransactionDb(memoryDb);
// Open the memory database
memoryDb.open();
// Declare key/value pair to allocate to store under a the key
const key = stringToU8a('key');
const value = stringToU8a('some value');
// Store key/value pair in memory db
memoryDb.put(key, value);
// Retrieve value for key from memory db
valueRetrieved = memoryDb.get(key);
// Delete key/value pair from memory db
memoryDb.del(key);
// Transaction to Store key/value pair in transaction db
const isTxSuccess = txDb.transaction(() => {
txDb.put(key, value);
// Boolean to indicate whether transaction was successful or not
return true;
});
console.log(`Transaction successful?: ${isTxSuccess}`);
// Retrieve transaction value from memory db
console.log(`Transfered value: ${memoryDb.get(key)}`);
// Close the memory database
memoryDb.close();
Hierarchy
↳ Memory
Implements
Constructors
constructor
⊕ new Memory(options?: BaseDbOptions): Memory
Overrides TransactionDb.constructor
Defined in Memory.ts:66
Parameters:
| Name | Type |
|---|---|
Optional options |
BaseDbOptions |
Returns: Memory
Methods
close
▸ close(): void
Inherited from TransactionDb.close
Defined in engines/TransactionDb.ts:51
Returns: void
del
▸ del(key: Uint8Array): void
Inherited from TransactionDb.del
Defined in engines/TransactionDb.ts:81
Parameters:
| Name | Type |
|---|---|
| key | Uint8Array |
Returns: void
drop
▸ drop(): void
Inherited from TransactionDb.drop
Defined in engines/TransactionDb.ts:59
Returns: void
empty
▸ empty(): void
Inherited from TransactionDb.empty
Defined in engines/TransactionDb.ts:63
Returns: void
get
▸ get(key: Uint8Array): Uint8Array | null
Inherited from TransactionDb.get
Defined in engines/TransactionDb.ts:93
Parameters:
| Name | Type |
|---|---|
| key | Uint8Array |
Returns: Uint8Array | null
maintain
▸ maintain(fn: ProgressCb): void
Implementation of TxDb.maintain
Inherited from TransactionDb.maintain
Defined in engines/TransactionDb.ts:71
Parameters:
| Name | Type |
|---|---|
| fn | ProgressCb |
Returns: void
open
▸ open(): void
Inherited from TransactionDb.open
Defined in engines/TransactionDb.ts:55
Returns: void
put
▸ put(key: Uint8Array, value: Uint8Array): void
Inherited from TransactionDb.put
Defined in engines/TransactionDb.ts:107
Parameters:
| Name | Type |
|---|---|
| key | Uint8Array |
| value | Uint8Array |
Returns: void
rename
▸ rename(base: string, file: string): void
Inherited from TransactionDb.rename
Defined in engines/TransactionDb.ts:67
Parameters:
| Name | Type |
|---|---|
| base | string |
| file | string |
Returns: void
size
▸ size(): number
Inherited from TransactionDb.size
Defined in engines/TransactionDb.ts:77
Returns: number
transaction
▸ transaction(fn: function): boolean
Inherited from TransactionDb.transaction
Defined in engines/TransactionDb.ts:29
Parameters:
| Name | Type |
|---|---|
| fn | function |
Returns: boolean