Class: DTable

Clusterluck.DTable(opts)

new DTable(opts)

In-memory key/value storage. Uses a combination of an AOF file and storage snapshot for persistence. For every "write" action, a corresponding write to the AOF file LATEST.LOG occurs. On an interval, this file is fsynced to disk (by default, 1000 milliseconds). In addition, a storage snapshot is kept and rewritten based on usage of the table. If the table remains inactive for a configurable amount of time (default 180000 milliseconds) or receives a configurable number of write operations before this idle time occurs (default 100 writes), the storage snapshot is resaved based on current state at that point in time. The algorithm is as follows:

  • Close the file descriptor on the AOF file LATEST.LOG
  • Append contents of LATEST.LOG (AOF file) to PREV.LOG (backup AOF file)
  • Delete LATEST.LOG and reopen with a new file descriptor
  • Dump state of table to DATA_PREV.SNAP (storage snapshot)
  • If succesful, move this to DATA.SNAP, else delete DATA_PREV.SNAP
  • Delete PREV.LOG

In the time that the AOF file LATEST.LOG is closed, log writes are stored in an internal queue. Once the file descriptor is reopened, the queue is flushed.

Parameters:
Name Type Description
opts Object
Properties
Name Type Attributes Description
path String
writeThreshold Number <optional>
autoSave Number <optional>
fsyncInterval Number <optional>
compress Boolean <optional>
encodeFn function <optional>
decodeFn function <optional>
name String <optional>
Source:

Methods

(static) decodeValue(value) → {Map|Set|JSON}

Parameters:
Name Type Description
value Object
Properties
Name Type Description
type String
data JSON
Source:
Returns:
Type
Map | Set | JSON

(static) encodeValue(value) → {Object}

Parameters:
Name Type Description
value Map | Set | JSON
Source:
Returns:
Type
Object

(static) invalidTypeError(command, key, type) → {Error}

Parameters:
Name Type Description
command String
key String
type String
Source:
Returns:
Type
Error

clear() → {Clusterluck.DTable}

Clears the contents of this table.

Source:
Returns:

This isntance.

Type
Clusterluck.DTable

del(key) → {Clusterluck.DTable}

Removes key key from this table.

Parameters:
Name Type Description
key String

Key to remove from this table.

Source:
Returns:

This instance.

Type
Clusterluck.DTable

forEach(cb, fin)

Asynchronously iterates over each key/value pair stored in this table at the point of this call.

Parameters:
Name Type Description
cb function

Function to call on each key/value pair. Has the signature function (key, val, next) {...}.

fin function

Finishing callback to call once iteration has completed. Hash the signature function (err) {...}, where err is populated if passed into the next callback at any point of iteration..

Source:

forEachSync(cb) → {Clusterluck.DTable}

Synchronously iterates over each key/value pair stored in this table.

Parameters:
Name Type Description
cb function

Function call on each key/value pair. Has the signature function (key, val) {...}.

Source:
Returns:

This instance.

Type
Clusterluck.DTable

get(key) → {Map|Set|JSON}

Retrieves value stored at key, returning undefined if no such data exists.

Parameters:
Name Type Description
key String

Key to retrieve data from.

Source:
Returns:

Value stored at key.

Type
Map | Set | JSON

hdel(key, hkey) → {Clusterluck.DTable}

Removes the hash key hkey from the hash map stored under key.

Parameters:
Name Type Description
key String

Key which holds the hash map that hkey will be removed from.

hkey String

The hash key to remove from the hash map.

Source:
Returns:

This instance.

Type
Clusterluck.DTable

hget(key, hkey) → {JSON}

Retrieves value stored at hash key hkey under storage key key, returning undefined if no such data exists.

Parameters:
Name Type Description
key String

Key to retrieve hash map from.

hkey String

Hash key to retrieve data from.

Source:
Returns:
  • Value stored under hash key hkey at the hash map stored under key.
Type
JSON

hset(key, hkey, val) → {Map}

Sets value under the hash key hkey in the hash map stored at key.

Parameters:
Name Type Description
key String

Key which holds the hash map.

hkey String

Hash key to insert val under.

val JSON

Value to set under hkey in the hash map.

Source:
Returns:

The map stored at key.

Type
Map

idle() → {Boolean}

Returns whether this table is in an idle state or not.

Source:
Returns:

Whether this table is idle or not.

Type
Boolean

load(cb)

Loads state from disk. The algorithm is as follows:

  • DATA.SNAP is loaded and read into the current state of table.
  • PREV.LOG is loaded and rerun against the current state.
  • LATEST.LOG is loaded and also rerun against the current state.
Parameters:
Name Type Description
cb function

Callback called once the snapshot and AOF files have been loaded and rerun.

Source:

raw() → {Map}

Returns the underlying in-memory map this table writes to.

Source:
Returns:

Underlying in-memory map this table writes to.

Type
Map

sdel(key, val) → {Clusterluck.DTable}

Deletes val from the set stored under key.

Parameters:
Name Type Description
key String

Key which olds the set to remove val from.

val String

Value to remove from the set.

Source:
Returns:

This instance.

Type
Clusterluck.DTable

set(key, val) → {Map|Set|JSON}

Sets value value under key key.

Parameters:
Name Type Description
key String
val Map | Set | JSON
Source:
Returns:
Type
Map | Set | JSON

smember(key, val) → {Boolean}

Returns whether val is a member of the set stored at key.

Parameters:
Name Type Description
key String

Key to retrieve set from.

val String

Value to check existence of in the set.

Source:
Returns:

Whether val is a member of the set stored at key.

Type
Boolean

sset(key, val) → {String}

Inserts val into the set stored at key key.

Parameters:
Name Type Description
key String

Key which holds the set to insert val under.

val String

Value to insert into the set.

Source:
Returns:

The set stored at key.

Type
String

start(name) → {Clusterluck.DTable}

Starts dtable instance, which triggers an fopen call to LATEST.LOG, the fsync interval for this log file, as well as other internal intervals to check for storage snapshot flush conditions.

Parameters:
Name Type Description
name String

Name of table, meant for debugging purposes.

Source:
Returns:

This instance.

Type
Clusterluck.DTable

stop(cb)

Stops the table, including all internal disk-based logic. If the table is idle and has an open file descriptor against LATEST.LOG, it will close immediately. If it's idle but the file descriptor against LATEST.LOG has been closed, this call will wait for a file descriptor to open again before continuing. Otherwise, the table isn't idle and therefore we wait for this condition.

Parameters:
Name Type Description
cb function

Callback called once the table has been stopped.

Source: