You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Move pub sub command into individual commands and use call_on_write
This commit is contained in:
86
lib/multi.js
86
lib/multi.js
@@ -20,45 +20,8 @@ function Multi (client, args) {
|
||||
}
|
||||
}
|
||||
|
||||
Multi.prototype.hmset = Multi.prototype.HMSET = function hmset () {
|
||||
var arr,
|
||||
len = 0,
|
||||
callback,
|
||||
i = 0;
|
||||
if (Array.isArray(arguments[0])) {
|
||||
arr = arguments[0];
|
||||
callback = arguments[1];
|
||||
} else if (Array.isArray(arguments[1])) {
|
||||
len = arguments[1].length;
|
||||
arr = new Array(len + 1);
|
||||
arr[0] = arguments[0];
|
||||
for (; i < len; i += 1) {
|
||||
arr[i + 1] = arguments[1][i];
|
||||
}
|
||||
callback = arguments[2];
|
||||
} else if (typeof arguments[1] === 'object' && (typeof arguments[2] === 'function' || typeof arguments[2] === 'undefined')) {
|
||||
arr = [arguments[0]];
|
||||
for (var field in arguments[1]) { // jshint ignore: line
|
||||
arr.push(field, arguments[1][field]);
|
||||
}
|
||||
callback = arguments[2];
|
||||
} else {
|
||||
len = arguments.length;
|
||||
// The later should not be the average use case
|
||||
if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
|
||||
len--;
|
||||
callback = arguments[len];
|
||||
}
|
||||
arr = new Array(len);
|
||||
for (; i < len; i += 1) {
|
||||
arr[i] = arguments[i];
|
||||
}
|
||||
}
|
||||
this.queue.push(['hmset', arr, callback]);
|
||||
return this;
|
||||
};
|
||||
|
||||
function pipeline_transaction_command (self, command, args, index, cb) {
|
||||
function pipeline_transaction_command (self, command, args, index, cb, call_on_write) {
|
||||
// Queueing is done first, then the commands are executed
|
||||
self._client.send_command(command, args, function (err, reply) {
|
||||
if (err) {
|
||||
if (cb) {
|
||||
@@ -131,20 +94,22 @@ Multi.prototype.exec_transaction = function exec_transaction (callback) {
|
||||
pipeline_transaction_command(self, 'multi', []);
|
||||
// Drain queue, callback will catch 'QUEUED' or error
|
||||
for (var index = 0; index < len; index++) {
|
||||
var args = self.queue.get(index);
|
||||
var command = args[0];
|
||||
var cb = args[2];
|
||||
// The commands may not be shifted off, since they are needed in the result handler
|
||||
var command_obj = self.queue.get(index);
|
||||
var command = command_obj[0];
|
||||
var cb = command_obj[2];
|
||||
var call_on_write = command_obj.length === 4 ? command_obj[3] : undefined;
|
||||
// Keep track of who wants buffer responses:
|
||||
if (self._client.options.detect_buffers) {
|
||||
self.wants_buffers[index] = false;
|
||||
for (var i = 0; i < args[1].length; i += 1) {
|
||||
if (args[1][i] instanceof Buffer) {
|
||||
for (var i = 0; i < command_obj[1].length; i += 1) {
|
||||
if (command_obj[1][i] instanceof Buffer) {
|
||||
self.wants_buffers[index] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pipeline_transaction_command(self, command, args[1], index, cb);
|
||||
pipeline_transaction_command(self, command, command_obj[1], index, cb, call_on_write);
|
||||
}
|
||||
|
||||
self._client.internal_send_command('exec', [], function (err, replies) {
|
||||
@@ -171,7 +136,18 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
|
||||
var self = this;
|
||||
var len = self.queue.length;
|
||||
var index = 0;
|
||||
var args;
|
||||
var command_obj;
|
||||
self._client.cork();
|
||||
if (!callback) {
|
||||
while (command_obj = self.queue.shift()) {
|
||||
self._client.internal_send_command(command_obj[0], command_obj[1], command_obj[2], (command_obj.length === 4 ? command_obj[3] : undefined));
|
||||
}
|
||||
self._client.uncork();
|
||||
return !self._client.should_buffer;
|
||||
} else if (len === 0) {
|
||||
utils.reply_in_order(self._client, callback, null, []);
|
||||
return !self._client.should_buffer;
|
||||
}
|
||||
var callback_without_own_cb = function (err, res) {
|
||||
if (err) {
|
||||
self.results.push(err);
|
||||
@@ -190,26 +166,20 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
|
||||
callback(null, self.results);
|
||||
};
|
||||
};
|
||||
if (len === 0) {
|
||||
if (callback) {
|
||||
utils.reply_in_order(self._client, callback, null, []);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
self.results = [];
|
||||
self._client.cork();
|
||||
while (args = self.queue.shift()) {
|
||||
var command = args[0];
|
||||
while (command_obj = self.queue.shift()) {
|
||||
var command = command_obj[0];
|
||||
var call_on_write = command_obj.length === 4 ? command_obj[3] : undefined;
|
||||
var cb;
|
||||
if (typeof args[2] === 'function') {
|
||||
cb = batch_callback(self, args[2], index);
|
||||
if (typeof command_obj[2] === 'function') {
|
||||
cb = batch_callback(self, command_obj[2], index);
|
||||
} else {
|
||||
cb = callback_without_own_cb;
|
||||
}
|
||||
if (typeof callback === 'function' && index === len - 1) {
|
||||
cb = last_callback(cb);
|
||||
}
|
||||
self._client.internal_send_command(command, args[1], cb);
|
||||
this._client.internal_send_command(command, command_obj[1], cb, call_on_write);
|
||||
index++;
|
||||
}
|
||||
self._client.uncork();
|
||||
|
Reference in New Issue
Block a user