Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
add function for geting key table from arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
hunts committed Aug 17, 2015
1 parent ac54be3 commit 46d140a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
24 changes: 15 additions & 9 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var EventEmitter = require('events').EventEmitter;
var util = require('util');
var HashMap = require('hashmap');
var redis = require('redis');
var commands = require('./commands');
var Pool = require('./pool');
Expand Down Expand Up @@ -436,8 +437,8 @@ function init_commands(self) {
function bindCommand(self, cmd) {
ClusterClient.prototype[cmd] = function () {
var argsAndFn = extractArgsAndCallback(arguments);
var keys = this.findKeysFromArgs(commands[cmd], argsAndFn[0]);

var keyTable = this.findKeyTable(cmd, argsAndFn[0]);
var keys = keyTable == null ? null : keyTable.values();
try {
var slotProxy = self.getProxy(keys);
if(!slotProxy) {
Expand Down Expand Up @@ -555,41 +556,46 @@ function isFunction(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
}



/**
* Finds keys from command args.
*
* @param {Object} commandMeta
* @param {String} command name
* @param {Array} args Command args
* @returns {Array|String} A list of string.
*/
ClusterClient.prototype.findKeysFromArgs = function(commandMeta, args) {
ClusterClient.prototype.findKeyTable = function(cmd, args) {
var commandMeta = commands[cmd];
var keyPosition = commandMeta[2];

// command do not need a key
if(!keyPosition) {
return null;
}

// init key array with first key.
var table = new HashMap();

// store key position and its value.
// command name is not included in args, so -1.
var keys = [args[keyPosition - 1]];
table.set(keyPosition - 1, args[keyPosition - 1]);

var lastKeyPosition = commandMeta[3];
var keyStep = commandMeta[4];

// only have one key
if(keyPosition === lastKeyPosition || !keyStep) {
return keys;
return table;
}

// command name is not included in args, so -1.
keyPosition += keyStep - 1;
while(args[keyPosition]) {
keys.push(args[keyPosition]);
table.set(keyPosition, args[keyPosition]);
keyPosition += keyStep;
}

return keys;
return table;
};

module.exports = ClusterClient;
8 changes: 4 additions & 4 deletions lib/pipelining.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ function Pipelining(clusterClient) {
*
*/
this.enqueue = function(cmd, args) {
var keys = this.client.findKeysFromArgs(commands[cmd], args);
var keyTable = this.client.findKeyTable(cmd, args);
var slot = null;
if (!keys) {
if (!keyTable || keyTable.count() === 0) {
slot = -1; // solt:-1 for commands which do not target to a particular slot.
} else if (keys.length > 1) {
} else if (keyTable.count() > 1) {
throw new Error('transaction do not support mset/mget and etc with multi keys assigned');
} else {
slot = calculateSlot(keys[0]);
slot = calculateSlot(keyTable.values()[0]);
}

if (!slotMap[slot]) {
Expand Down
8 changes: 4 additions & 4 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ function Transaction(clusterClient) {
*
*/
this.enqueue = function(cmd, args) {
var keys = this.client.findKeysFromArgs(commands[cmd], args);
var keyTable = this.client.findKeyTable(cmd, args);
var slot = null;
if (!keys) {
if (!keyTable || keyTable.count() === 0) {
slot = -1; // solt:-1 for commands which do not target to a particular slot.
} else if (keys.length > 1) {
} else if (keyTable.count() > 1) {
throw new Error('transaction do not support mset/mget and etc with multi keys assigned');
} else {
slot = calculateSlot(keys[0]);
slot = calculateSlot(keyTable.values()[0]);
}

if (!slotMap[slot]) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clustedis",
"version": "0.0.5",
"version": "0.0.6",
"description": "Node.js Redis cluster client for the official cluster implementation (by Redis 3.0)",
"scripts": {
"test": "nyc gulp test",
Expand Down Expand Up @@ -29,7 +29,8 @@
},
"dependencies": {
"generic-pool": "2.2.0",
"redis": "^0.12.1"
"redis": "^0.12.1",
"hashmap": "^2.0.3"
},
"devDependencies": {
"gulp": "^3.8.11",
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ describe('cluster command tests: ', function() {
done();
});
});

describe('client helper functions', function() {
it('should get key table from args', function(done) {
var kt = client.findKeyTable('mset', ['key1', 'value1', 'key2', 'value2']);
expect(kt.count()).to.be.equal(2);
expect(kt.get(0)).to.be.equal('key1');
expect(kt.get(2)).to.be.equal('key2');
done();
});
});

describe('redis server info', function() {

Expand Down

0 comments on commit 46d140a

Please sign in to comment.