Skip to content

Commit

Permalink
utilizing lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Apr 15, 2011
1 parent 01332a9 commit 1c2af26
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 57 deletions.
39 changes: 19 additions & 20 deletions lib/commands/hash.js
Expand Up @@ -9,14 +9,15 @@
* Module dependencies.
*/

var utils = require('../utils');
var utils = require('../utils')
, string = utils.string;

/**
* HLEN <key>
*/

exports.hlen = function(client, key){
var obj = this.lookup(utils.string(key));
var obj = this.lookup(string(key));

if (!obj) {
client.int(0);
Expand All @@ -32,8 +33,7 @@ exports.hlen = function(client, key){
*/

exports.hvals = function(client, key){
var key = utils.string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (!obj) {
client.emptyList();
Expand All @@ -51,8 +51,7 @@ exports.hvals = function(client, key){
*/

exports.hkeys = function(client, key){
var key = utils.string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (!obj) {
client.emptyList();
Expand All @@ -68,9 +67,9 @@ exports.hkeys = function(client, key){
*/

(exports.hset = function(client, key, field, val){
var key = utils.string(key)
, field = utils.string(field)
, obj = this.db.data[key];
var key = string(key)
, field = string(field)
, obj = this.lookup(key);

if (obj && 'hash' != obj.type) return client.typeError();
obj = obj || (this.db.data[key] = { type: 'hash', val: {} });
Expand All @@ -85,16 +84,16 @@ exports.hkeys = function(client, key){

(exports.hmset = function(client, data){
var len = data.length
, key = utils.string(data[0])
, obj = this.db.data[key]
, key = string(data[0])
, obj = this.lookup(key)
, field
, val;

if (obj && 'hash' != obj.type) return client.typeError();
obj = obj || (this.db.data[key] = { type: 'hash', val: {} });

for (var i = 1; i < len; ++i) {
field = utils.string(data[i++]);
field = string(data[i++]);
val = data[i];
obj.val[field] = val;
}
Expand All @@ -110,9 +109,9 @@ exports.hmset.skip = 1;
*/

exports.hget = function(client, key, field){
var key = utils.string(key)
, field = utils.string(field)
, obj = this.db.data[key]
var key = string(key)
, field = string(field)
, obj = this.lookup(key)
, val;

if (!obj) {
Expand All @@ -133,8 +132,8 @@ exports.hget = function(client, key, field){
*/

exports.hgetall = function(client, key){
var key = utils.string(key)
, obj = this.db.data[key]
var key = string(key)
, obj = this.lookup(key)
, list = [];

if (!obj) {
Expand All @@ -154,9 +153,9 @@ exports.hgetall = function(client, key){
*/

exports.hexists = function(client, key, field){
var key = utils.string(key)
, field = utils.string(field)
, obj = this.db.data[key]
var key = string(key)
, field = string(field)
, obj = this.lookup(key);

if (obj) {
if ('hash' == obj.type) {
Expand Down
21 changes: 8 additions & 13 deletions lib/commands/keys.js
Expand Up @@ -17,8 +17,7 @@ var utils = require('../utils')
*/

exports.expire = function(client, key, seconds){
var key = string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (obj) {
obj.expires = Date.now() + Number(string(seconds));
Expand All @@ -33,8 +32,7 @@ exports.expire = function(client, key, seconds){
*/

exports.expireat = function(client, key, seconds){
var key = string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (obj) {
obj.expires = +string(seconds);
Expand All @@ -49,8 +47,7 @@ exports.expireat = function(client, key, seconds){
*/

exports.persist = function(client, key){
var key = string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (obj && 'number' == typeof obj.expires) {
delete obj.expires;
Expand All @@ -65,8 +62,7 @@ exports.persist = function(client, key){
*/

exports.ttl = function(client, key){
var key = string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (obj && 'number' == typeof obj.expires) {
client.int(obj.expires - Date.now());
Expand All @@ -80,8 +76,7 @@ exports.ttl = function(client, key){
*/

exports.type = function(client, key){
var key = string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (obj) {
client.write(obj.type);
Expand All @@ -95,7 +90,7 @@ exports.type = function(client, key){
*/

exports.exists = function(client, key){
client.bool(this.db.data[string(key)]);
client.bool(this.lookup(string(key)));
};

/**
Expand All @@ -122,7 +117,7 @@ exports.randomkey = function(client){
// TODO: varg
// TODO: del count ?
key = string(key);
if (this.db.data[key]) {
if (this.lookup(key)) {
delete this.db.data[key];
client.bool(true);
} else {
Expand All @@ -139,7 +134,7 @@ exports.randomkey = function(client){

// Fail if attempting to rename a non-existant key
from = string(from);
if (null == data[from]) return client.error('no such key');
if (!this.lookup(from)) return client.error('no such key');

// Fail on same keys
to = string(to);
Expand Down
43 changes: 19 additions & 24 deletions lib/commands/string.js
Expand Up @@ -17,8 +17,7 @@ var utils = require('../utils')
*/

exports.get = function(client, key){
var key = string(key)
, obj = this.db.data[key];
var obj = this.lookup(string(key));

if (!obj) {
client.nil();
Expand All @@ -35,16 +34,14 @@ exports.get = function(client, key){

exports.getset = function(client, key, val){
var key = string(key)
, prev = this.data[key]
, prevType = this.keyType(key);
, obj = this.lookup(key);

this.data[key] = val;
this.keyType(key, 'string');
this.db.data[key] = { type: 'string', val: val };

if (null == prev) {
if (!obj) {
client.nil();
} else if ('string' == prevType) {
client.send(prev);
} else if ('string' == obj.type) {
client.send(obj.val);
} else {
client.typeError();
}
Expand All @@ -66,9 +63,8 @@ exports.getset = function(client, key, val){

(exports.setnx = function(client, key, val){
key = string(key);
if (null != this.data[key]) return client.bool(false);
this.data[key] = val;
this.keyType(key, 'string');
if (this.lookup(key)) return client.bool(false);
this.db.data[key] = { type: 'string', val: val };
client.bool(true);
}).mutates = true;

Expand All @@ -78,7 +74,7 @@ exports.getset = function(client, key, val){

(exports.incr = function(client, key){
var key = string(key)
, obj = this.db.data[key];
, obj = this.lookup(key);

if (!obj) {
this.db.data[key] = { type: 'string', val: 1 };
Expand All @@ -98,7 +94,7 @@ exports.getset = function(client, key, val){

(exports.incrby = function(client, key, num){
var key = string(key)
, obj = this.db.data[key]
, obj = this.lookup(key)
, num = +string(num);

if (isNaN(num)) return client.rangeError();
Expand All @@ -121,7 +117,7 @@ exports.getset = function(client, key, val){

(exports.decrby = function(client, key, num){
var key = string(key)
, obj = this.db.data[key]
, obj = this.lookup(key)
, num = +string(num);

if (isNaN(num)) return client.rangeError();
Expand All @@ -144,7 +140,7 @@ exports.getset = function(client, key, val){

(exports.decr = function(client, key){
var key = string(key)
, obj = this.db.data[key];
, obj = this.lookup(key);

if (!obj) {
this.db.data[key] = { type: 'string', val: -1 };
Expand All @@ -164,7 +160,7 @@ exports.getset = function(client, key, val){

exports.strlen = function(client, key){
var key = string(key)
, val = this.data[key];
, val = this.lookup(key);

if (val) {
client.int(val.length);
Expand All @@ -181,19 +177,18 @@ exports.strlen = function(client, key){

(exports.append = function(client, key, str){
var key = string(key)
, val = null != this.data[key]
? this.data[key]
: new Buffer(0);
, obj = this.lookup(key)
|| { type: 'string', val: new Buffer(0) };

if ('string' != this.keyType(key)) return client.typeError();
if (obj && 'string' != obj.type) return client.typeError();

if (Buffer.isBuffer(val)) {
var offset = val.length
if (Buffer.isBuffer(obj.val)) {
var offset = obj.val.length
, len = offset + str.length
, buf = new Buffer(len);
val.copy(buf);
str.copy(buf, offset);
this.data[key] = buf;
this.db.data[key] = obj;
client.int(len);
} else {
client.typeError();
Expand Down

0 comments on commit 1c2af26

Please sign in to comment.