Skip to content

Commit

Permalink
implement TYPE, fix LRANGE & RPOPLPUSH
Browse files Browse the repository at this point in the history
  • Loading branch information
janl committed Nov 28, 2009
1 parent c1555bd commit 8f45962
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
13 changes: 13 additions & 0 deletions awesome.js
Expand Up @@ -73,6 +73,10 @@ var server = tcp.createServer(function(socket) {
reply_function(value);
}
},

status: function(s) {
reply.send("+" + s);
}
};

function Command(line) {
Expand Down Expand Up @@ -396,6 +400,8 @@ var server = tcp.createServer(function(socket) {
if(store.lpush(dst, value)) {
reply.bulk(value);
} else {
// restore src
store.rpush(src, value);
reply.error(E_LIST_VALUE);
}
}
Expand All @@ -416,6 +422,13 @@ var server = tcp.createServer(function(socket) {
}
},

type: {
callback: function() {
var key = that.args[1];
reply.status(store.type(key));
}
},

// for debugging
dump: {
callback: function() {
Expand Down
22 changes: 20 additions & 2 deletions store.js
Expand Up @@ -226,13 +226,31 @@ exports.lrange = function(key, start, end) {
var start = parseInt(start);
var end = parseInt(end);

if(end < 0) {
end = value.length + end + 1;
if(end < 0) { // With Array.slice(start, end), end is non-inclusive
end = value.length + end + 1; // we need inclusive
}
var slice = value.slice(start, end);
return slice;
};

exports.type = function(key) {
if(!this.has(key)) {
return "none";
}

var value = this.get(key);
if(this.is_array(value)) {
return "list";
}

// TODO
// if(this.is_set(value)) {
// return "set";
// }

return "string";
};

// TODO: make private again
exports.is_array = is_array;

Expand Down

0 comments on commit 8f45962

Please sign in to comment.