Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unlink command support #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions lib/client/redis-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const events = require("events"),
/**
* @deprecated use {@link parsers} instead
*/
const parseArguments = function(args, options) { // eslint-disable-line complexity
const parseArguments = function (args, options) { // eslint-disable-line complexity
var arr,
len = args.length,
callback,
Expand Down Expand Up @@ -39,9 +39,9 @@ const parseArguments = function(args, options) { // eslint-disable-line complexi
// arg1 = {k1: v1, k2: v2,}
// arg2 = callback
arr = [args[0]];
if(options && options.valueIsString) {
if (options && options.valueIsString) {
arr.push(String(args[1]));
} else if(options && options.valueIsBuffer) {
} else if (options && options.valueIsBuffer) {
arr.push(args[1]);
} else {
for (var field in args[1]) {
Expand Down Expand Up @@ -152,7 +152,7 @@ class RedisClient extends events.EventEmitter {

// Emit the message to ALL matching subscriptions
Object.keys(this.psubscriptions).forEach((key) => {
if(this.psubscriptions[key].test(ch)) {
if (this.psubscriptions[key].test(ch)) {
this.emit('pmessage', key, ch, msg);
return true;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ RedisClient.prototype.publish = function (channel, msg, callback) {
/**
* multi
*/
RedisClient.prototype.multi = RedisClient.prototype.batch = function(commands) {
RedisClient.prototype.multi = RedisClient.prototype.batch = function (commands) {
return multi.multi(this, commands, false);
};
RedisClient.prototype.batch = function (commands) {
Expand All @@ -228,26 +228,30 @@ RedisClient.prototype.batch = function (commands) {

const getKeysVarArgs = function (args) {
var keys = [];
var hasCallback = typeof(args[args.length - 1]) === 'function';
var hasCallback = typeof (args[args.length - 1]) === 'function';
for (var i = 0; i < (hasCallback ? args.length - 1 : args.length); i++) {
keys.push(args[i]);
}
var callback = hasCallback ? args[args.length - 1] : undefined;
return {keys: keys, callback: callback};
return { keys: keys, callback: callback };
};

RedisClient.prototype.del = RedisClient.prototype.DEL = function (keys, callback) {
this._selectedDb.del(keys, callback);
};

RedisClient.prototype.unlink = RedisClient.prototype.UNLINK = function (keys, callback) {
this._selectedDb.unlink(keys, callback);
};

RedisClient.prototype.exists = RedisClient.prototype.EXISTS = function (keys, callback) {
const args = getKeysVarArgs(arguments);
keys = args.keys;
callback = args.callback;
this._selectedDb.exists(keys, callback);
};

RedisClient.prototype.type = RedisClient.prototype.TYPE = function(key, callback) {
RedisClient.prototype.type = RedisClient.prototype.TYPE = function (key, callback) {
this._selectedDb.type(key, callback);
};

Expand Down Expand Up @@ -351,7 +355,7 @@ RedisClient.prototype.set = RedisClient.prototype.SET = function (...userArgs) {
);
};

RedisClient.prototype.append = RedisClient.prototype.APPEND = function(...userArgs) {
RedisClient.prototype.append = RedisClient.prototype.APPEND = function (...userArgs) {
exec(parsers.append, userArgs, (args, cb) =>
this._selectedDb.append(args.default.key, args.default.value, cb)
);
Expand Down Expand Up @@ -444,12 +448,12 @@ RedisClient.prototype.hscan = RedisClient.prototype.HSCAN = function () {
let match = '*';
let count = 10;

if(args.length > 0) {
if (args.length > 0) {
for (let i = 0; i < args.length; i++) {
if(typeof args[i] === 'string' && args[i].toLowerCase() === "match") {
match = args[i+1];
} else if(typeof args[i] === 'string' && args[i].toLowerCase() === "count") {
count = args[i+1];
if (typeof args[i] === 'string' && args[i].toLowerCase() === "match") {
match = args[i + 1];
} else if (typeof args[i] === 'string' && args[i].toLowerCase() === "count") {
count = args[i + 1];
}
}
}
Expand Down Expand Up @@ -497,7 +501,7 @@ RedisClient.prototype.rpoplpush = RedisClient.prototype.RPOPLPUSH = function (so

RedisClient.prototype._bpop = function (fn, key, timeout, callback) {
const keys = [];
const hasCallback = typeof(arguments[arguments.length - 1]) === "function";
const hasCallback = typeof (arguments[arguments.length - 1]) === "function";
for (let i = 1; i < (hasCallback ? arguments.length - 2 : arguments.length - 1); i++) {
keys.push(arguments[i]);
}
Expand Down Expand Up @@ -564,7 +568,7 @@ RedisClient.prototype.srandmember = RedisClient.prototype.SRANDMEMBER = function
this._selectedDb.srandmember(key, count, callback);
};

RedisClient.prototype.sscan = RedisClient.prototype.SSCAN = function(...userArgs) {
RedisClient.prototype.sscan = RedisClient.prototype.SSCAN = function (...userArgs) {
exec(parsers.sscan, userArgs, (args, cb) => {
this._selectedDb.sscan(args.default.key, args.default.cursor, args.named.match, args.named.count, cb);
});
Expand Down Expand Up @@ -652,12 +656,12 @@ RedisClient.prototype.zrevrank = RedisClient.prototype.ZREVRANK = function () {
this._selectedDb.zrevrank(...args);
};

RedisClient.prototype.zunionstore = RedisClient.prototype.ZUNIONSTORE = function() {
RedisClient.prototype.zunionstore = RedisClient.prototype.ZUNIONSTORE = function () {
const args = parseArguments(arguments);
this._selectedDb.zunionstore(...args);
};

RedisClient.prototype.zinterstore = RedisClient.prototype.ZINTERSTORE = function() {
RedisClient.prototype.zinterstore = RedisClient.prototype.ZINTERSTORE = function () {
const args = parseArguments(arguments);
this._selectedDb.zinterstore(...args);
};
Expand All @@ -672,7 +676,7 @@ RedisClient.prototype.zscore = RedisClient.prototype.ZSCORE = function () {
*/

RedisClient.prototype.send_command = RedisClient.prototype.SEND_COMMAND = function (callback) {
if (typeof(arguments[arguments.length - 1]) === 'function') {
if (typeof (arguments[arguments.length - 1]) === 'function') {
arguments[arguments.length - 1]();
}
};
Expand Down Expand Up @@ -714,13 +718,13 @@ RedisClient.prototype.script = RedisClient.prototype.SCRIPT = function (...userA
* Mirror all commands for Multi
*/
types.getMethods(RedisClient).public()
.skip('duplicate', 'quit', 'end')
.forEach((methodName) => {
multi.Multi.prototype[methodName] = function (...args) {
this._command(methodName, args);
//Return this for chaining
return this;
};
});
.skip('duplicate', 'quit', 'end')
.forEach((methodName) => {
multi.Multi.prototype[methodName] = function (...args) {
this._command(methodName, args);
//Return this for chaining
return this;
};
});

module.exports = RedisClient;
56 changes: 40 additions & 16 deletions lib/server/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,49 @@ exports.del = function (keys, callback) {
helpers.callCallback(callback, null, keysDeleted);
};

/**
* Unlink
*/
exports.unlink = function (keys, callback) {

if (!(keys instanceof Array)) {
keys = [keys];
}

let keysUnlinked = 0;

for (let i = 0; i < keys.length; i++) {

if (keys[i] in this.storage) {

delete this.storage[keys[i]];
keysUnlinked++;

}
}

helpers.callCallback(callback, null, keysUnlinked);
};

/**
* Exists
*/
exports.exists = function (keys, callback) {
if (!(keys instanceof Array)) {
keys = [keys];
keys = [keys];
}

let result = 0;
for (let i = 0; i < keys.length; i++) {
if( keys[i] in this.storage) {
result++;
}
if (keys[i] in this.storage) {
result++;
}
}

helpers.callCallback(callback, null, result);
};

exports.type = function(key, callback) {
exports.type = function (key, callback) {
const type = key in this.storage
? this.storage[key].type
: "none";
Expand All @@ -61,15 +85,15 @@ exports.expire = function (key, seconds, callback) {

if (obj) {
var now = new Date().getTime();
var milli = Math.min(seconds*1000, Math.pow(2, 31) - 1);
var milli = Math.min(seconds * 1000, Math.pow(2, 31) - 1);

if (this.storage[key]._expire) {
clearTimeout(this.storage[key]._expire);
}

this.storage[key].expires = new Date(now + milli);
var _expire = setTimeout(() => {
delete this.storage[key];
delete this.storage[key];
}, milli);
if (_expire.unref) {
_expire.unref();
Expand All @@ -83,7 +107,7 @@ exports.expire = function (key, seconds, callback) {
};

exports.pexpire = function (key, ms, callback) {
const computedSeconds = ms > 0 ? ms/1000 : ms;
const computedSeconds = ms > 0 ? ms / 1000 : ms;
return this.expire(key, computedSeconds, (err, seconds) => {
helpers.callCallback(callback, err, seconds);
});
Expand Down Expand Up @@ -121,7 +145,7 @@ exports.expireat = function (key, timestamp, callback) {
};

exports.pexpireat = function (key, timestamp, callback) {
return this.expireat(key, timestamp / 1000, (err, result) => {
return this.expireat(key, timestamp / 1000, (err, result) => {
helpers.callCallback(callback, err, result);
});
};
Expand Down Expand Up @@ -206,9 +230,9 @@ exports.scan = function (index, pattern, count, callback) {
if (idx >= index && regex.test(key)) {
keys.push(key);
count--;
if(count === 0) {
resIdx = idx+1;
break;
if (count === 0) {
resIdx = idx + 1;
break;
}
}
idx++;
Expand All @@ -225,10 +249,10 @@ exports.rename = function (key, newKey, callback) {
var err = null;

if (key in this.storage) {
this.storage[newKey] = this.storage[key];
delete this.storage[key];
this.storage[newKey] = this.storage[key];
delete this.storage[key];
} else {
err = new Error("ERR no such key");
err = new Error("ERR no such key");
}

helpers.callCallback(callback, err, "OK");
Expand Down Expand Up @@ -262,7 +286,7 @@ exports.renamenx = function (key, newKey, callback) {
* Dbsize
* http://redis.io/commands/dbsize
*/
exports.dbsize = function(callback) {
exports.dbsize = function (callback) {
var size = Object.keys(this.storage).length || 0;
helpers.callCallback(callback, null, size);
};
Loading