diff --git a/lib/consts.js b/lib/consts.js index 4a286d4..33f46db 100644 --- a/lib/consts.js +++ b/lib/consts.js @@ -63,6 +63,8 @@ module.exports = { dlmOpts: Object.freeze(_.extend({ rquorum: 0.51, wquorum: 0.51, + minWaitTimeout: 10, + maxWaitTimeout: 100, disk: false }, dtableOpts)) }; diff --git a/lib/dtable.js b/lib/dtable.js index 48af88d..00bfc1f 100644 --- a/lib/dtable.js +++ b/lib/dtable.js @@ -16,6 +16,15 @@ const defaults = consts.dtableOpts; class DTable extends EventEmitter { /** + * + * 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.SNAP (storage snapshot) + * - 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. * * @class DTable * @memberof Clusterluck @@ -49,12 +58,14 @@ class DTable extends EventEmitter { } /** + * + * 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. * * @method start * @memberof Clusterluck.DTable * @instance * - * @param {String} name + * @param {String} name - Name of table, meant for debugging purposes. * * @return {Clusterluck.DTable} This instance. * @@ -67,12 +78,14 @@ class DTable extends EventEmitter { } /** + * + * 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. * * @method stop * @memberof Clusterluck.DTable * @instance * - * @param {Function} cb + * @param {Function} cb - Callback called once the table has been stopped. * */ stop(cb) { @@ -87,12 +100,17 @@ class DTable extends EventEmitter { } /** + * + * 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. * * @method load * @memberof Clusterluck.DTable * @instance * - * @param {Function} cb + * @param {Function} cb - Callback called once the snapshot and AOF files have been loaded and rerun. * */ load(cb) { @@ -104,12 +122,14 @@ class DTable extends EventEmitter { } /** + * + * Returns whether this table is in an idle state or not. * * @method idle * @memberof Clusterluck.DTable * @instance * - * @return {Boolean} + * @return {Boolean} Whether this table is idle or not. * */ idle() { @@ -117,14 +137,16 @@ class DTable extends EventEmitter { } /** + * + * Retrieves value stored at `key`, returning `undefined` if no such data exists. * * @method get * @memberof Clusterluck.DTable * @instance * - * @param {String} key + * @param {String} key - Key to retrieve data from. * - * @return {Any} + * @return {Map|Set|JSON} Value stored at `key`. * */ get(key) { @@ -132,15 +154,17 @@ class DTable extends EventEmitter { } /** + * + * Returns whether `val` is a member of the set stored at `key`. * * @method smember * @memberof Clusterluck.DTable * @instance * - * @param {String} key - * @param {String} val + * @param {String} key - Key to retrieve set from. + * @param {String} val - Value to check existence of in the set. * - * @return {Boolean} + * @return {Boolean} Whether `val` is a member of the set stored at `key`. * */ smember(key, val) { @@ -152,15 +176,17 @@ class DTable extends EventEmitter { } /** + * + * Retrieves value stored at hash key `hkey` under storage key `key`, returning `undefined` if no such data exists. * * @method hget * @memberof Clusterluck.DTable * @instance * - * @param {String} key - * @param {String} hkey + * @param {String} key - Key to retrieve hash map from. + * @param {String} hkey - Hash key to retrieve data from. * - * @return {JSON} + * @return {JSON} - Value stored under hash key `hkey` at the hash map stored under `key`. * */ hget(key, hkey) { @@ -172,15 +198,17 @@ class DTable extends EventEmitter { } /** + * + * Sets value `value` under key `key`. * * @method set * @memberof Clusterluck.DTable * @instance * * @param {String} key - * @param {Any} val + * @param {Map|Set|JSON} val * - * @return {Any} + * @return {Map|Set|JSON} * */ set(key, val) { @@ -191,15 +219,17 @@ class DTable extends EventEmitter { } /** + * + * Inserts `val` into the set stored at key `key`. * * @method sset * @memberof Clusterluck.DTable * @instance * - * @param {String} key - * @param {String} val + * @param {String} key - Key which holds the set to insert `val` under. + * @param {String} val - Value to insert into the set. * - * @return {String} + * @return {String} The set stored at `key`. * */ sset(key, val) { @@ -215,16 +245,18 @@ class DTable extends EventEmitter { } /** + * + * Sets `value` under the hash key `hkey` in the hash map stored at `key`. * * @method hset * @memberof Clusterluck.DTable * @instance * - * @param {String} key - * @param {String} hkey - * @param {JSON} val + * @param {String} key - Key which holds the hash map. + * @param {String} hkey - Hash key to insert `val` under. + * @param {JSON} val - Value to set under `hkey` in the hash map. * - * @return {JSON} + * @return {Map} The map stored at `key`. * */ hset(key, hkey, val) { @@ -240,14 +272,16 @@ class DTable extends EventEmitter { } /** + * + * Removes key `key` from this table. * * @method del * @memberof Clusterluck.DTable * @instance * - * @param {String} key + * @param {String} key - Key to remove from this table. * - * @return {Clusterluck.DTable} + * @return {Clusterluck.DTable} This instance. * */ del(key) { @@ -258,15 +292,17 @@ class DTable extends EventEmitter { } /** + * + * Deletes `val` from the set stored under `key`. * * @method sdel * @memberof Clusterluck.DTable * @instance * - * @param {String} key - * @param {String} val + * @param {String} key - Key which olds the set to remove `val` from. + * @param {String} val - Value to remove from the set. * - * @return {Clusterluck.DTable} + * @return {Clusterluck.DTable} This instance. * */ sdel(key, val) { @@ -282,15 +318,17 @@ class DTable extends EventEmitter { } /** + * + * Removes the hash key `hkey` from the hash map stored under `key`. * * @method hdel * @memberof Clusterluck.DTable * @instance * - * @param {String} key - * @param {String} hkey + * @param {String} key - Key which holds the hash map that `hkey` will be removed from. + * @param {String} hkey - The hash key to remove from the hash map. * - * @return {Clusterluck.DTable} + * @return {Clusterluck.DTable} This instance. * */ hdel(key, hkey) { @@ -306,13 +344,15 @@ class DTable extends EventEmitter { } /** + * + * Asynchronously iterates over each key/value pair stored in this table at the point of this call. * * @method forEach * @memberof Clusterluck.DTable * @instance * - * @param {Function} cb - * @param {Function} fin + * @param {Function} cb - Function to call on each key/value pair. Has the signature `function (key, val, next) {...}`. + * @param {Function} fin -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.. * */ forEach(cb, fin) { @@ -331,14 +371,16 @@ class DTable extends EventEmitter { } /** + * + * Synchronously iterates over each key/value pair stored in this table. * * @method forEachSync * @memberof Clusterluck.DTable * @instance * - * @param {Function} cb + * @param {Function} cb - Function call on each key/value pair. Has the signature `function (key, val) {...}`. * - * @return {Clusterluck.DTable} + * @return {Clusterluck.DTable} This instance. * */ forEachSync(cb) {