Files
common/docs/db/classes/_disk_.diskdb.md
T
2019-01-04 13:55:24 +00:00

8.1 KiB

name: DiskDb

summary: Creates DiskDb database using LruDb for caching with FileFlatDb and extending TransactionDb.

description: Create an instance of a Disk database using LruDb (for caching with FileFlatDb) and extending TransactionDb. Note that LruDB uses the Least Recently Used (LRU) cache algorithm

example:

import stringToU8a from '@polkadot/util/string/toU8a';
import DiskDb from '@polkadot/db/Disk';
import TransactionDb from '@polkadot/db/engines/TransactionDb';

// Creat diskDb instance that wraps LruDb with backing and Lru cache
const diskDb = new DiskDb();

// Creat txDb instance that uses diskDb
const txDb = new TransactionDb(diskDb);

// Open the disk db backing database. Clears the Lru cache
diskDb.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 disk db backing and also in Lru cache
diskDb.put(key, value);

// Retrieve value for key from disk db. Returns cached value if key
// in cache, otherwise returns backing value for key and stores
// this latest retrieved key/value pair in Lru cache
valueRetrieved = diskDb.get(key);

// Delete key/value pair from disk db backing and set the key to null in Lru cache
diskDb.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 diskDb db
console.log(`Transfered value: ${diskDb.get(key)}`);

// Close the diskDb database and clear Lru cache
diskDb.close();

Hierarchy

TransactionDb

↳ DiskDb

Implements

Constructors

constructor

⊕ new DiskDb(base: string, name: string, options?: BaseDbOptions): DiskDb

Overrides TransactionDb.constructor

Defined in Disk.ts:66

Parameters:

Name Type
base string
name string
Optional options BaseDbOptions

Returns: DiskDb


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