Permalink
Browse files

utilizing lookup

  • Loading branch information...
1 parent 01332a9 commit 1c2af262d613318fa1125e882f3693b23fc2adcc @tj committed Apr 15, 2011
Showing with 46 additions and 57 deletions.
  1. +19 −20 lib/commands/hash.js
  2. +8 −13 lib/commands/keys.js
  3. +19 −24 lib/commands/string.js
View
@@ -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);
@@ -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();
@@ -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();
@@ -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: {} });
@@ -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;
}
@@ -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) {
@@ -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) {
@@ -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) {
View
@@ -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));
@@ -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);
@@ -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;
@@ -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());
@@ -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);
@@ -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)));
};
/**
@@ -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 {
@@ -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);
View
@@ -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();
@@ -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();
}
@@ -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;
@@ -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 };
@@ -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();
@@ -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();
@@ -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 };
@@ -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);
@@ -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();

0 comments on commit 1c2af26

Please sign in to comment.