Skip to content

Commit

Permalink
implment MSET, more work on SORT, still not complete
Browse files Browse the repository at this point in the history
  • Loading branch information
janl committed Dec 4, 2009
1 parent c59d4ef commit 3db4e5d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 32 deletions.
114 changes: 90 additions & 24 deletions awesome.js
Expand Up @@ -302,6 +302,18 @@ var server = tcp.createServer(function(socket) {
}
},

mset: {
bulk: true,
callback: function() {
debug("received MSET command");
var msets = this.multi_data;
for(var idx in msets) {
store.set(idx, msets[idx]);
}
reply.ok();
}
},

ping: {
callback: function() {
debug("received PING command");
Expand Down Expand Up @@ -757,7 +769,23 @@ var server = tcp.createServer(function(socket) {

this.setData = function(data) {
this.data = data.trim();
}
},

this.setMultiBulkData = function(lines) {
var result = {};
var key = null;
for(var idx = 0; idx < lines.length; idx++) {
if(idx % 2) { // skip even lines
if(!key) {
key = lines[idx];
} else {
result[key] = lines[idx];
key = null;
}
}
}
this.multi_data = result;
},

this.exec = function() {
debug("in exec '" + this.cmd + "'");
Expand Down Expand Up @@ -790,42 +818,80 @@ var server = tcp.createServer(function(socket) {
return s.substring(start, s.indexOf(eol, start));
}

function string_count(haystack, needle) {
var regex = new RegExp(needle, "g");
var result = haystack.match(regex);
if(result) {
return result.length - 1;
} else {
return 0;
}
}

var buffer = "";
var in_bulk_request = false;
var in_multi_bulk_request = false;
var cmd = {};
socket.addListener("receive", function(packet) {
buffer += packet;
debug("read: '" + buffer.substr(0, 64) + "'");
var idx;
while(idx = buffer.indexOf(eol) != -1) { // we have a newline
if(in_bulk_request) {
debug("in bulk req");
// later
cmd.setData(buffer);
in_bulk_request = false;
buffer = adjustBuffer(buffer);
cmd.exec();
while(buffer.indexOf(eol) != -1) { // we have a newline
if(in_multi_bulk_request) {
debug("in multi bulk request");
// handle multi bulk requests
if(!cmd_length) {
var cmd_length = cmd.len * 2; // *2 = $len
}
debug("cmd_length: " + cmd_length);
debug("string_count: " + string_count(buffer, eol));
if(string_count(buffer, eol) == cmd_length) {
var lines = buffer.split(eol);
cmd.cmd = lines[2];
lines = lines.slice(2); // chop off *len\n$4\nmget
cmd.setMultiBulkData(lines)
cmd_length++;
while(cmd_length--) {
buffer = adjustBuffer(buffer);
}
in_multi_bulk_request = false;
cmd.exec();

}
} else {
// not a bulk request yet
debug("not in bulk req (yet)");
cmd = Command(buffer);
if(cmd.is_inline()) {
debug("is inline command");
// handle bulk requests
if(in_bulk_request) {
debug("in bulk req");
cmd.setData(buffer);
in_bulk_request = false;
buffer = adjustBuffer(buffer);
cmd.exec();
} else {
if(buffer.indexOf(eol) != buffer.lastIndexOf(eol)) { // two new lines
debug("received a bulk command in a single buffer");
// parse out command line
cmd.setData(parseData(buffer));
in_bulk_request = false;
buffer = adjustBuffer(buffer);
// not a bulk request yet
debug("not in bulk req (yet)");
cmd = Command(buffer);
if(cmd.cmd.charAt(0) == "*") {
cmd.len = cmd.cmd.charAt(1);
in_multi_bulk_request = true;
continue;
}
if(cmd.is_inline()) {
debug("is inline command");
cmd.exec();
} else {
debug("wait for bulk: '" + buffer + "'");
in_bulk_request = true;
if(buffer.indexOf(eol) != buffer.lastIndexOf(eol)) { // two new lines
debug("received a bulk command in a single buffer");
// parse out command line
cmd.setData(parseData(buffer));
in_bulk_request = false;
buffer = adjustBuffer(buffer);
cmd.exec();
} else {
debug("wait for bulk: '" + buffer + "'");
in_bulk_request = true;
}
}
buffer = adjustBuffer(buffer);
}
buffer = adjustBuffer(buffer);
}
}
});
Expand Down
22 changes: 16 additions & 6 deletions sorter.js
Expand Up @@ -51,14 +51,10 @@ exports.parse = function(opts) {
};
};

exports.sort = function(list) {
exports.sort = function(list, key) {
debug("pre list" + list);

debug(options.by);
debug(options.get);
if(!options.by && !options.get) {
debug("no fancy crap");
debug(options.direction);
if(!options.direction || options.direction == "ASC") {
list.sort();
} else if(options.direction == "DESC"){
Expand All @@ -73,7 +69,6 @@ exports.sort = function(list) {
return list;
}


if(options.by) {
var result = [];
// fetch KYES with by as the pattern,
Expand All @@ -96,6 +91,21 @@ exports.sort = function(list) {
if(sort_a == sort_b) { return 0;}
return sort_a < sort_b ? -1 : 1;
});

if(options.get) { // substitute value-keys with object values
debug("options.get: " + options.get);
return list.map(function(elm) {
if(options.get == "#") {
var index = elm - 1;
} else {
var index = options.get.replace("\*", elm);
}
debug("get value for: " + index);
debug("value: " + store.lindex(key, index));
return store.lindex(key, index);
});
}

return list;
}
}
Expand Down
4 changes: 2 additions & 2 deletions store.js
Expand Up @@ -42,7 +42,7 @@ exports.dump = function() {
};

exports.flushdb = function() {
stores.current = [];
stores[current] = {};
};

exports.get = function(key) {
Expand Down Expand Up @@ -536,7 +536,7 @@ exports.sort = function(key, options) {
debug("options in store.js: "+ options)
sorter.parse(options);

return sorter.sort(this.get(key));
return sorter.sort(this.get(key), key);
};

// TODO: make private again
Expand Down

0 comments on commit 3db4e5d

Please sign in to comment.