Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
stop trying to guess how to handle the response to messages - we know…
Browse files Browse the repository at this point in the history
… what the message type was, so map that directly to a response handler
  • Loading branch information
iamcal committed Aug 27, 2010
1 parent 48b83d6 commit 5d100c4
Showing 1 changed file with 40 additions and 65 deletions.
105 changes: 40 additions & 65 deletions lib/memcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ var crlf_len = crlf.length;

var error_replies = ['ERROR', 'NOT_FOUND', 'CLIENT_ERROR', 'SERVER_ERROR'];

var reply_indicators = {
'get' : ['VALUE', 'END'],
'set' : ['STORED', 'NOT_STORED', 'EXISTS', 'NOT_FOUND'],
'stats' : ['STATS'],
'delete' : ['DELETED'],
'version' : ['VERSION']
};

var Client = exports.Client = function(port, host) {
this.port = port || 11211;
this.host = host || 'localhost';
Expand Down Expand Up @@ -87,22 +79,22 @@ Client.prototype.dispatchHandles = function() {
};

Client.prototype.query = function(query, type, callback) {
var self = this;
var data = query + crlf;
this.callbacks.push({ type: type, fun: callback });
self.sends += 1;
this.conn.write(data);
var self = this;
var data = query + crlf;
this.callbacks.push({ type: type, fun: callback });
self.sends += 1;
this.conn.write(data);
};

Client.prototype.close = function(idx) {
if (this.conn && this.conn.readyState === "open") {
this.conn.end();
this.conn = null;
this.conn = null;
}
};

Client.prototype.get = function(key, callback) {
return this.query('get ' + key, 'get', callback);
return this.query('get ' + key, 'get', callback);
};


Expand All @@ -124,7 +116,7 @@ Client.prototype.store = function(cmd, key, value, callback, lifetime, flags)
var value_len = value.length || 0;
var query = [cmd, key, set_flags, exp_time, value_len];

return this.query(query.join(' ') + crlf + value, 'set', callback);
return this.query(query.join(' ') + crlf + value, 'simple', callback);
};

// "cas" is a store op that takes an extra "unique" argument
Expand All @@ -140,7 +132,7 @@ Client.prototype.cas = function(key, value, unique, callback, lifetime, flags) {
var value_len = value.length || 0;
var query = ['cas', key, set_flags, exp_time, value_len, unique];

return this.query(query.join(' ') + crlf + value, 'set', callback);
return this.query(query.join(' ') + crlf + value, 'simple', callback);
};


Expand All @@ -150,7 +142,7 @@ Client.prototype.del = function(key, callback){
};

Client.prototype.delete = function(key, callback){
return this.query('delete ' + key, 'delete', callback);
return this.query('delete ' + key, 'simple', callback);
};

Client.prototype.version = function(callback) {
Expand All @@ -165,7 +157,7 @@ Client.prototype.increment = function(key, value, callback) {
}

value = value || 1;
return this.query('incr ' + key + ' ' + value, 'incr', callback);
return this.query('incr ' + key + ' ' + value, 'simple', callback);
};

Client.prototype.decrement = function(key, value, callback) {
Expand All @@ -176,7 +168,7 @@ Client.prototype.decrement = function(key, value, callback) {
}

value = value || 1;
return this.query('decr ' + key + ' ' + value, 'decr', callback);
return this.query('decr ' + key + ' ' + value, 'simple', callback);
};

Client.prototype.handle_received_data = function () {
Expand Down Expand Up @@ -208,35 +200,28 @@ Client.prototype.handle_received_data = function () {
};

Client.prototype.determine_reply_handler = function (buffer) {

// check we have a whole line in the buffer
var crlf_at = buffer.indexOf(crlf);
if (crlf_at == -1) {
return null;
}

// determine errors
for (var error_idx in error_replies) {
var error_indicator = error_replies[error_idx];
if (buffer.indexOf(error_indicator) == 0) {
return this.handle_error(buffer);
}
}

var first_line = buffer.substr(0, crlf_at);
if (parseInt(first_line) == first_line) {
return this.handle_integer(buffer, crlf_at);
// call the handler for the current message type
var type = this.callbacks[0].type;
if (type){
return this['handle_' + type](buffer);
}

// determine errors
for (var error_idx in error_replies) {
var error_indicator = error_replies[error_idx];
if (buffer.indexOf(error_indicator) == 0) {
return this.handle_error(buffer);
}
}

// determine normal reply handler
for (var method in reply_indicators) {
for (var indicator in reply_indicators[method]) {
var current_indicator = reply_indicators[method][indicator];
if (buffer.indexOf(current_indicator) == 0) {
return this['handle_' + method](buffer, current_indicator.length);
}
}
}

return null;

return null;
};

Client.prototype.handle_get = function(buffer) {
Expand All @@ -255,35 +240,25 @@ Client.prototype.handle_get = function(buffer) {
}
};

Client.prototype.handle_delete = function(buffer, line_len){
var value = buffer.substr(0, line_len);
return [value, line_len + crlf_len];
Client.prototype.handle_simple = function(buffer){
var line = readLine(buffer);
return [line, (line.length + crlf_len), null];
};

Client.prototype.handle_set = function(buffer, line_len) {
var value = buffer.substr(0, line_len);
return [value, line_len + crlf_len];
};

Client.prototype.handle_version = function(buffer) {
Client.prototype.handle_version = function(buffer){
var line_len = buffer.indexOf(crlf);
var indicator_len = 'VERSION '.length;
var result_value = buffer.substr(indicator_len, (line_len - indicator_len));
return [result_value, line_len + crlf_len, null];
};

Client.prototype.handle_integer = function(buffer, line_len) {
var result_value = buffer.substr(0, line_len);
return [result_value, line_len + crlf_len, null];
var result_value = buffer.substr(indicator_len, (line_len - indicator_len));
return [result_value, line_len + crlf_len, null];
};

Client.prototype.handle_error = function(buffer) {
line = readLine(buffer);
return [null, (line.length + crlf_len), line];
Client.prototype.handle_error = function(buffer){
line = readLine(buffer);
return [null, (line.length + crlf_len), line];
};

readLine = function(string) {
var line_len = string.indexOf(crlf);
return string.substr(0, line_len);
readLine = function(string){
var line_len = string.indexOf(crlf);
return string.substr(0, line_len);
};

0 comments on commit 5d100c4

Please sign in to comment.