Files
common/docs/db/classes/_memory_.memory.md
T
2019-02-19 15:11:49 +00:00

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

TransactionDb

↳ 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

Implementation of TxDb.close

Inherited from TransactionDb.close

Defined in engines/TransactionDb.ts:51

Returns: void


del

▸ del(key: Uint8Array): void

Implementation of TxDb.del

Inherited from TransactionDb.del

Defined in engines/TransactionDb.ts:81

Parameters:

Name Type
key Uint8Array

Returns: void


drop

▸ drop(): void

Implementation of TxDb.drop

Inherited from TransactionDb.drop

Defined in engines/TransactionDb.ts:59

Returns: void


empty

▸ empty(): void

Implementation of TxDb.empty

Inherited from TransactionDb.empty

Defined in engines/TransactionDb.ts:63

Returns: void


get

▸ get(key: Uint8Array): Uint8Array | null

Implementation of TxDb.get

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

Implementation of TxDb.open

Inherited from TransactionDb.open

Defined in engines/TransactionDb.ts:55

Returns: void


put

▸ put(key: Uint8Array, value: Uint8Array): void

Implementation of TxDb.put

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

Implementation of TxDb.rename

Inherited from TransactionDb.rename

Defined in engines/TransactionDb.ts:67

Parameters:

Name Type
base string
file string

Returns: void


size

▸ size(): number

Implementation of TxDb.size

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