Permalink
Browse files

Added .mutates prop to commands

  • Loading branch information...
1 parent 7a16cca commit 9cae1835dbcdebe1b9498b99458655393377432b @tj committed Jan 10, 2011
Showing with 42 additions and 44 deletions.
  1. +2 −2 lib/commands/connection.js
  2. +2 −2 lib/commands/hash.js
  3. +4 −4 lib/commands/keys.js
  4. +2 −2 lib/commands/server.js
  5. +26 −20 lib/commands/string.js
  6. +6 −14 lib/database.js
@@ -39,9 +39,9 @@ exports.quit = function(client){
* SELECT <index>
*/
-exports.select = function(client, index){
+(exports.select = function(client, index){
index = +utils.string(index);
if (isNaN(index) || index < 0) return client.error('invalid DB index');
this.selectDB(index);
client.ok();
-};
+}).mutates = true;
View
@@ -15,7 +15,7 @@ var utils = require('../utils');
* HSET <key> <field> <val>
*/
-exports.hset = function(client, key, field, val){
+(exports.hset = function(client, key, field, val){
var key = utils.string(key)
, field = utils.string(field)
, hash = this.data[key];
@@ -31,7 +31,7 @@ exports.hset = function(client, key, field, val){
hash[field] = val;
this.data[key] = hash;
client.bool(true);
-};
+}).mutates = true;
/**
* HGET <key> <field>
View
@@ -56,7 +56,7 @@ exports.randomkey = function(client){
* DEL <key>
*/
-exports.del = function(client, key){
+(exports.del = function(client, key){
// TODO: varg
key = utils.string(key);
if (this.data[key]) {
@@ -65,21 +65,21 @@ exports.del = function(client, key){
} else {
client.int(0);
}
-};
+}).mutates = true;
/**
* RENAME <from> <to>
*/
-exports.rename = function(client, from, to){
+(exports.rename = function(client, from, to){
from = utils.string(from);
if (null == this.data[from]) return client.error('no such key');
to = utils.string(to);
if (from == to) return client.error('source and destination objects are the same');
this.data[to] = this.data[from];
this.data[from] = null;
client.ok();
-};
+}).mutates = true;
/**
* KEYS <pattern>
View
@@ -15,10 +15,10 @@ var utils = require('../utils');
* FLUSHDB
*/
-exports.flushdb = function(client){
+(exports.flushdb = function(client){
this.data = {};
client.ok();
-};
+}).mutates = true;
/**
* INFO
View
@@ -44,41 +44,41 @@ exports.getset = function(client, key, val){
* SET <key> <str>
*/
-exports.set = function(client, key, str){
+(exports.set = function(client, key, str){
key = utils.string(key);
this.data[key] = str;
client.ok();
-};
+}).mutates = true;
/**
* SETNX <key> <val>
*/
-exports.setnx = function(client, key, val){
+(exports.setnx = function(client, key, val){
key = utils.string(key);
if (null != this.data[key]) return client.bool(false);
this.data[key] = val;
client.bool(true);
-};
+}).mutates = true;
/**
* INCR <key>
*/
-exports.incr = function(client, key){
+(exports.incr = function(client, key){
var key = utils.string(key)
, val = this.data[key] || '0'
, n = +val.toString();
// TODO: typecheck
client.int(this.data[key] = ++n);
-};
+}).mutates = true;
/**
* INCRBY <key> <num>
*/
-exports.incrby = function(client, key, num){
+(exports.incrby = function(client, key, num){
var key = utils.string(key)
, val = this.data[key] || '0'
, n = +val.toString()
@@ -88,13 +88,13 @@ exports.incrby = function(client, key, num){
// TODO: typecheck
client.int(this.data[key] = n + num);
-};
+}).mutates = true;
/**
* DECRBY <key> <num>
*/
-exports.decrby = function(client, key, num){
+(exports.decrby = function(client, key, num){
var key = utils.string(key)
, val = this.data[key] || '0'
, n = +val.toString()
@@ -104,20 +104,20 @@ exports.decrby = function(client, key, num){
// TODO: typecheck
client.int(this.data[key] = n - num);
-};
+}).mutates = true;
/**
* DECR <key>
*/
-exports.decr = function(client, key){
+(exports.decr = function(client, key){
var key = utils.string(key)
, val = this.data[key] || '0'
, n = +val.toString();
// TODO: typecheck
client.int(this.data[key] = --n);
-};
+}).mutates = true;
/**
* STRLEN <key>
@@ -139,7 +139,7 @@ exports.strlen = function(client, key){
* APPEND <key> <str>
*/
-exports.append = function(client, key, str){
+(exports.append = function(client, key, str){
var key = utils.string(key)
, val = null != this.data[key]
? this.data[key]
@@ -157,13 +157,13 @@ exports.append = function(client, key, str){
} else {
client.typeError();
}
-};
+}).mutates = true;
/**
* SETRANGE <key> <offset> <str>
*/
-exports.setrange = function(client, key, offset, str){
+(exports.setrange = function(client, key, offset, str){
var key = utils.string(key)
, val = this.data[key]
, offset = +utils.string(offset);
@@ -174,7 +174,7 @@ exports.setrange = function(client, key, offset, str){
str.copy(val, offset);
client.int(val.length);
}
-};
+}).mutates;
/**
* GETRANGE <key> <from> <to>
@@ -231,7 +231,7 @@ exports.substr = exports.getrange;
* MSET (<key> <val>)+
*/
-(exports.mset = function(client, strs){
+exports.mset = function(client, strs){
var key
, val
, len = strs.length;
@@ -241,13 +241,16 @@ exports.substr = exports.getrange;
this.data[key] = val;
}
client.ok();
-}).multiple = 2;
+};
+
+exports.mset.multiple = 2;
+exports.mset.mutates = true;
/**
* MSETNX (<key> <val>)+
*/
-(exports.msetnx = function(client, strs){
+exports.msetnx = function(client, strs){
var key
, val
, len = strs.length
@@ -269,4 +272,7 @@ exports.substr = exports.getrange;
}
client.bool(true);
-}).multiple = 2;
+};
+
+exports.msetnx.multiple = 2;
+exports.msetnx.mutates = true;
View
@@ -24,20 +24,12 @@ var commands = require('./commands')
* optimized, and wipe out the AOF all together.
*/
-var mutate = [
- 'set'
- , 'del'
- , 'mset'
- , 'incr'
- , 'incrby'
- , 'decr'
- , 'decrby'
- , 'rename'
- , 'flushdb'
- , 'append'
- , 'setrange'
- , 'select'
-];
+var mutate = [];
+
+Object.keys(commands).forEach(function(cmd){
+ var fn = commands[cmd];
+ if (fn.mutates) mutate.push(cmd);
+});
/**
* Initialize a new `Database` with the given `server` and `options`.

0 comments on commit 9cae183

Please sign in to comment.