Skip to content

Commit

Permalink
Made some callbacks optional (mainly for add/remove ops)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed May 12, 2011
1 parent 2c25fb0 commit 4b48029
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 44 deletions.
1 change: 1 addition & 0 deletions lib/Structure.js
Expand Up @@ -163,6 +163,7 @@ Structure.prototype.getKeys = function (structures, which) {
*/ */


Structure.prototype.namespaceKey = function (key) { Structure.prototype.namespaceKey = function (key) {
key = key || '';
if (this.namespace.length) { if (this.namespace.length) {
key = this.namespace + ':' + key; key = this.namespace + ':' + key;
} }
Expand Down
16 changes: 10 additions & 6 deletions lib/advanced_structures/CappedList.js
Expand Up @@ -44,12 +44,13 @@ CappedList.prototype.init = function (length) {
* *
* @param {int} pivot * @param {int} pivot
* @param {string} value * @param {string} value
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


CappedList.prototype.insertBefore = function (pivot, value, callback) { CappedList.prototype.insertBefore = function (pivot, value, callback) {
callback = callback || function () {};
var multi = this.client.multi() var multi = this.client.multi()
multi.linsert(this.key, 'BEFORE', pivot, value); multi.linsert(this.key, 'BEFORE', pivot, value);
multi.ltrim(this.key, -1 * this.len, -1); multi.ltrim(this.key, -1 * this.len, -1);
Expand All @@ -62,12 +63,13 @@ CappedList.prototype.insertBefore = function (pivot, value, callback) {
* *
* @param {int} pivot * @param {int} pivot
* @param {string} value * @param {string} value
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


CappedList.prototype.insertAfter = function (pivot, value, callback) { CappedList.prototype.insertAfter = function (pivot, value, callback) {
callback = callback || function () {};
var multi = this.client.multi() var multi = this.client.multi()
multi.linsert(this.key, 'AFTER', pivot, value); multi.linsert(this.key, 'AFTER', pivot, value);
multi.ltrim(this.key, -1 * this.len, -1); multi.ltrim(this.key, -1 * this.len, -1);
Expand All @@ -78,13 +80,14 @@ CappedList.prototype.insertAfter = function (pivot, value, callback) {
/** /**
* Add one or more elements to the start of the list. * Add one or more elements to the start of the list.
* *
* @param {string|array} values * @param {string|array} value(s)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


CappedList.prototype.unshift = CappedList.prototype.lpush = function (values, callback) { CappedList.prototype.unshift = CappedList.prototype.lpush = function (values, callback) {
callback = callback || function () {};
var multi = this.client.multi(); var multi = this.client.multi();
if (Array.isArray(values)) { if (Array.isArray(values)) {
var key = this.key; var key = this.key;
Expand All @@ -102,13 +105,14 @@ CappedList.prototype.unshift = CappedList.prototype.lpush = function (values, ca
/** /**
* Add one or more elements to the end of the list. * Add one or more elements to the end of the list.
* *
* @param {string|array} values * @param {string|array} value(s)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


CappedList.prototype.push = CappedList.prototype.add = function (values, callback) { CappedList.prototype.push = CappedList.prototype.add = function (values, callback) {
callback = callback || function () {};
var multi = this.client.multi(); var multi = this.client.multi();
if (Array.isArray(values)) { if (Array.isArray(values)) {
var key = this.key; var key = this.key;
Expand Down
10 changes: 6 additions & 4 deletions lib/advanced_structures/DensitySet.js
Expand Up @@ -20,10 +20,10 @@ var SortedSet = require('../base_structures/SortedSet').SortedSet;
* `redback.createDensitySet(key);` * `redback.createDensitySet(key);`
* *
* Reference: * Reference:
* http://redis.io/topics/data-types#sorted-sets * http://redis.io/topics/data-types#sorted-sets
* *
* Redis Structure: * Redis Structure:
* `(namespace:)key = zset(count => element)` * `(namespace:)key = zset(count => element)`
*/ */


var DensitySet = exports.DensitySet = SortedSet.prototype.extend(); var DensitySet = exports.DensitySet = SortedSet.prototype.extend();
Expand All @@ -32,12 +32,13 @@ var DensitySet = exports.DensitySet = SortedSet.prototype.extend();
* Add one or more elements to the set. * Add one or more elements to the set.
* *
* @param {string|Array} element(s) * @param {string|Array} element(s)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


DensitySet.prototype.add = function (element, callback) { DensitySet.prototype.add = function (element, callback) {
callback = callback || function () {};
if (Array.isArray(element)) { if (Array.isArray(element)) {
return this.addAll(element, callback); return this.addAll(element, callback);
} }
Expand All @@ -49,12 +50,13 @@ DensitySet.prototype.add = function (element, callback) {
* Remove one or more elements from the set. * Remove one or more elements from the set.
* *
* @param {string|Array} element(s) * @param {string|Array} element(s)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


DensitySet.prototype.remove = function (element, callback) { DensitySet.prototype.remove = function (element, callback) {
callback = callback || function () {};
if (Array.isArray(element)) { if (Array.isArray(element)) {
return this.removeAll(element, callback); return this.removeAll(element, callback);
} }
Expand Down
6 changes: 4 additions & 2 deletions lib/advanced_structures/KeyPair.js
Expand Up @@ -225,12 +225,13 @@ KeyPair.prototype.idExists = function (id, callback) {
* Deletes a unique value and its associated id. * Deletes a unique value and its associated id.
* *
* @param {string} value * @param {string} value
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


KeyPair.prototype.delete = function (value, callback) { KeyPair.prototype.delete = function (value, callback) {
callback = callback || function () {};
var self = this, value = this.hashValue(value); var self = this, value = this.hashValue(value);
this.client.hget(this.idkey, value, function (err, id) { this.client.hget(this.idkey, value, function (err, id) {
if (err || value == null) return callback(err); if (err || value == null) return callback(err);
Expand All @@ -243,12 +244,13 @@ KeyPair.prototype.delete = function (value, callback) {
* Deletes an id and its associated unique value. * Deletes an id and its associated unique value.
* *
* @param {int} id * @param {int} id
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


KeyPair.prototype.deleteById = function (id, callback) { KeyPair.prototype.deleteById = function (id, callback) {
callback = callback || function () {};
var self = this; var self = this;
this.client.hget(this.key, id, function (err, value) { this.client.hget(this.key, id, function (err, value) {
if (err || value == null) return callback(err); if (err || value == null) return callback(err);
Expand Down
2 changes: 1 addition & 1 deletion lib/advanced_structures/SocialGraph.js
Expand Up @@ -34,7 +34,7 @@ var SocialGraph = exports.SocialGraph = Structure.new();
*/ */


SocialGraph.prototype.init = function (prefix) { SocialGraph.prototype.init = function (prefix) {
this.key_prefix = this.namespace.length ? this.namespace + ':' : ''; this.key_prefix = this.namespaceKey();
if (prefix) { if (prefix) {
this.key_prefix += prefix + ':'; this.key_prefix += prefix + ':';
} }
Expand Down
6 changes: 4 additions & 2 deletions lib/base_structures/Bitfield.js
Expand Up @@ -29,12 +29,13 @@ var Bitfield = exports.Bitfield = Structure.new();
* Get a single bit * Get a single bit
* *
* @param {int} bit * @param {int} bit
* @param {Function} callback * @param {Function} callback (optional)
* @return this; * @return this;
* @api public * @api public
*/ */


Bitfield.prototype.get = function (bit, callback) { Bitfield.prototype.get = function (bit, callback) {
callback = callback || function () {};
this.client.getbit(this.key, bit, callback); this.client.getbit(this.key, bit, callback);
return this; return this;
} }
Expand All @@ -44,12 +45,13 @@ Bitfield.prototype.get = function (bit, callback) {
* *
* @param {int} bit * @param {int} bit
* @param {bool} value * @param {bool} value
* @param {Function} callback * @param {Function} callback (optional)
* @return this; * @return this;
* @api public * @api public
*/ */


Bitfield.prototype.set = function (bit, value, callback) { Bitfield.prototype.set = function (bit, value, callback) {
callback = callback || function () {};
this.client.setbit(this.key, bit, value ? 1 : 0, callback); this.client.setbit(this.key, bit, value ? 1 : 0, callback);
return this; return this;
} }
17 changes: 11 additions & 6 deletions lib/base_structures/Hash.js
Expand Up @@ -68,12 +68,13 @@ Hash.prototype.length = function (callback) {
* Delete a hash key. * Delete a hash key.
* *
* @param {string} hash_key * @param {string} hash_key
* @param {Function} callback * @param {Function} callback (optional)
* @return this; * @return this;
* @api public * @api public
*/ */


Hash.prototype.delete = Hash.prototype.del = function (hash_key, callback) { Hash.prototype.delete = Hash.prototype.del = function (hash_key, callback) {
callback = callback || function () {};
this.client.hdel(this.key, hash_key, callback); this.client.hdel(this.key, hash_key, callback);
return this; return this;
} }
Expand Down Expand Up @@ -103,16 +104,17 @@ Hash.prototype.exists = function (hash_key, callback) {
* *
* @param {string|Object} hash_key * @param {string|Object} hash_key
* @param {string} value (optional) * @param {string} value (optional)
* @param {Function} callback * @param {Function} callback (optional)
* @return this; * @return this;
* @api public * @api public
*/ */


Hash.prototype.set = function (hash_key, value, callback) { Hash.prototype.set = function (hash_key, value, callback) {
if (typeof value === 'function') { if (typeof hash_key === 'object') {
callback = value; callback = value || function () {};
this.client.hmset(this.key, hash_key, callback); this.client.hmset(this.key, hash_key, callback);
} else { } else {
callback = callback || function () {};
this.client.hset(this.key, hash_key, value, callback); this.client.hset(this.key, hash_key, value, callback);
} }
return this; return this;
Expand All @@ -129,6 +131,7 @@ Hash.prototype.set = function (hash_key, value, callback) {
*/ */


Hash.prototype.add = function (hash_key, value, callback) { Hash.prototype.add = function (hash_key, value, callback) {
callback = callback || function () {};
this.client.hsetnx(this.key, hash_key, value, callback); this.client.hsetnx(this.key, hash_key, value, callback);
return this; return this;
} }
Expand Down Expand Up @@ -166,13 +169,14 @@ Hash.prototype.get = function (hash_key, callback) {
* *
* @param {string} hash_key * @param {string} hash_key
* @param {int} amount (optional - default is 1) * @param {int} amount (optional - default is 1)
* @param {Function} callback * @param {Function} callback (optional)
* @return this; * @return this;
* @api public * @api public
*/ */


Hash.prototype.increment = Hash.prototype.increment =
Hash.prototype.incrBy = function (hash_key, amount, callback) { Hash.prototype.incrBy = function (hash_key, amount, callback) {
callback = callback || function () {};
if (typeof amount === 'function') { if (typeof amount === 'function') {
callback = amount; callback = amount;
amount = 1; amount = 1;
Expand All @@ -186,13 +190,14 @@ Hash.prototype.incrBy = function (hash_key, amount, callback) {
* *
* @param {string} hash_key * @param {string} hash_key
* @param {int} amount (optional - default is 1) * @param {int} amount (optional - default is 1)
* @param {Function} callback * @param {Function} callback (optional)
* @return this; * @return this;
* @api public * @api public
*/ */


Hash.prototype.decrement = Hash.prototype.decrement =
Hash.prototype.decrBy = function (hash_key, amount, callback) { Hash.prototype.decrBy = function (hash_key, amount, callback) {
callback = callback || function () {};
if (typeof amount === 'function') { if (typeof amount === 'function') {
callback = amount; callback = amount;
amount = 1; amount = 1;
Expand Down
31 changes: 20 additions & 11 deletions lib/base_structures/List.js
Expand Up @@ -82,12 +82,13 @@ List.prototype.get = function (index, count, callback) {
* *
* @param {int} length * @param {int} length
* @param {bool} keep_earliest (optional - defaults to false) * @param {bool} keep_earliest (optional - defaults to false)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.cap = function (length, keep_earliest, callback) { List.prototype.cap = function (length, keep_earliest, callback) {
callback = callback || function () {};
var start = 0, end = -1; var start = 0, end = -1;
if (typeof keep_earliest === 'function') { if (typeof keep_earliest === 'function') {
//Keep the last `length` elements //Keep the last `length` elements
Expand All @@ -106,12 +107,13 @@ List.prototype.cap = function (length, keep_earliest, callback) {
* *
* @param {string} value * @param {string} value
* @param {bool} count (optional - defaults to 1) * @param {bool} count (optional - defaults to 1)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.remove = function (value, count, callback) { List.prototype.remove = function (value, count, callback) {
callback = callback || function () {};
if (typeof count === 'function') { if (typeof count === 'function') {
callback = count; callback = count;
count = 1; count = 1;
Expand All @@ -125,12 +127,13 @@ List.prototype.remove = function (value, count, callback) {
* *
* @param {int} start * @param {int} start
* @param {int} end * @param {int} end
* @param {Function} callback * @param {Function} callback (optional_
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.trim = function (start, end, callback) { List.prototype.trim = function (start, end, callback) {
callback = callback || function () {};
this.client.ltrim(this.key, start, end, callback); this.client.ltrim(this.key, start, end, callback);
return this; return this;
} }
Expand All @@ -140,12 +143,13 @@ List.prototype.trim = function (start, end, callback) {
* *
* @param {int} pivot * @param {int} pivot
* @param {string} value * @param {string} value
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.insertBefore = function (pivot, value, callback) { List.prototype.insertBefore = function (pivot, value, callback) {
callback = callback || function () {};
this.client.linsert(this.key, 'BEFORE', pivot, value, callback); this.client.linsert(this.key, 'BEFORE', pivot, value, callback);
return this; return this;
} }
Expand All @@ -155,12 +159,13 @@ List.prototype.insertBefore = function (pivot, value, callback) {
* *
* @param {int} pivot * @param {int} pivot
* @param {string} value * @param {string} value
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.insertAfter = function (pivot, value, callback) { List.prototype.insertAfter = function (pivot, value, callback) {
callback = callback || function () {};
this.client.linsert(this.key, 'AFTER', pivot, value, callback); this.client.linsert(this.key, 'AFTER', pivot, value, callback);
return this; return this;
} }
Expand All @@ -170,12 +175,13 @@ List.prototype.insertAfter = function (pivot, value, callback) {
* *
* @param {int} index * @param {int} index
* @param {string} value * @param {string} value
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.set = function (index, value, callback) { List.prototype.set = function (index, value, callback) {
callback = callback || function () {};
this.client.lset(this.key, index, value, callback); this.client.lset(this.key, index, value, callback);
return this; return this;
} }
Expand Down Expand Up @@ -234,13 +240,14 @@ List.prototype.pop = function (wait, callback) {
/** /**
* Add one or more elements to the start of the list. * Add one or more elements to the start of the list.
* *
* @param {string|array} values * @param {string|array} value(s)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.unshift = List.prototype.lpush = function (values, callback) { List.prototype.unshift = List.prototype.lpush = function (values, callback) {
callback = callback || function () {};
if (Array.isArray(values)) { if (Array.isArray(values)) {
var multi = this.client.multi(), key = this.key; var multi = this.client.multi(), key = this.key;
values.reverse().forEach(function (value) { values.reverse().forEach(function (value) {
Expand All @@ -256,13 +263,14 @@ List.prototype.unshift = List.prototype.lpush = function (values, callback) {
/** /**
* Add one or more elements to the end of the list. * Add one or more elements to the end of the list.
* *
* @param {string|array} values * @param {string|array} value(s)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.push = List.prototype.add = function (values, callback) { List.prototype.push = List.prototype.add = function (values, callback) {
callback = callback || function () {};
if (Array.isArray(values)) { if (Array.isArray(values)) {
var multi = this.client.multi(), key = this.key; var multi = this.client.multi(), key = this.key;
values.forEach(function (value) { values.forEach(function (value) {
Expand All @@ -281,12 +289,13 @@ List.prototype.push = List.prototype.add = function (values, callback) {
* *
* @param {String|List} list * @param {String|List} list
* @param {bool} wait (optional) * @param {bool} wait (optional)
* @param {Function} callback * @param {Function} callback (optional)
* @return this * @return this
* @api public * @api public
*/ */


List.prototype.popShift = function (list, wait, callback) { List.prototype.popShift = function (list, wait, callback) {
callback = callback || function () {};
list = this.getKey(list); list = this.getKey(list);
if (typeof wait === 'function') { if (typeof wait === 'function') {
callback = wait; callback = wait;
Expand Down

0 comments on commit 4b48029

Please sign in to comment.