Permalink
Browse files

Bug fixes code cleanup

  • Loading branch information...
1 parent 4f3b3b4 commit 61451716353742e051011f4fefdf429d18ecf44b Doug Martin committed May 3, 2011
Showing with 33 additions and 130 deletions.
  1. +1 −1 benchmark/hive.benckmark.js
  2. +29 −126 lib/server.js
  3. +2 −2 lib/socket.js
  4. +1 −1 lib/utility/{parser.js → helper.js}
View
2 benchmark/hive.benckmark.js
@@ -52,6 +52,7 @@ var testDeletion = function(hive) {
for (var i = l - 1; i > 0; i--) {
var w = permuttedWords[i];
hive.remove(w);
+
}
var end = new Date();
printStats("HIVE", "DELETION", start, end);
@@ -60,7 +61,6 @@ var testDeletion = function(hive) {
var test = function() {
var arr = [], hive = new Hive();
-
console.log("STARTING TEST....");
testInserts(hive);
testLookUps(hive);
View
155 lib/server.js
@@ -1,65 +1,23 @@
var express = require("express"),
comb = require("comb"),
- Hive = require("./hive");
+ format = comb.string.format,
+ Hive = require("./hive"),
+ helper = require("./utility/helper");
var hive = new Hive();
var app = express.createServer();
//app.use(express.bodyParser());
-app.get('/get/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- res.send(hive.get(key));
- } else {
- res.send({error : "Key Required"});
- }
-});
-
-app.get('/getGte/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- res.send(hive.getKeyGte(key));
- } else {
- res.send({error : "Key Required"});
- }
-});
-
-app.get('/getGt/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- res.send(hive.getKeyGt(key));
- } else {
- res.send({error : "Key Required"});
- }
-});
-
-app.get('/getLt/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- res.send(hive.getKeyLt(key));
- } else {
- res.send({error : "Key Required"});
- }
-});
-
-app.get('/getLte/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- res.send(hive.getKeyLte(key));
- } else {
- res.send({error : "Key Required"});
- }
-});
-
-app.get('/remove/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- hive.remove(key);
- res.send({removed : true});
- } else {
- res.send({error : "Key Required"});
- }
+["get", "getGte", "getGt", "getLt", "getLte", "remove", "incr", "decr"].forEach(function(action) {
+ app.get(format("/%s/:key", action), function(req, res) {
+ var key = req.params.key;
+ if (key) {
+ res.send(hive[action](key));
+ } else {
+ res.send({error : format("Key Required when calling %s", action)});
+ }
+ })
});
app.get('/flushAll', function(req, res) {
@@ -72,86 +30,31 @@ app.get('/flushAll', function(req, res) {
}
});
-
-app.get('/incr/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- res.send(hive.incr(key));
- } else {
- res.send({error : "Key Required"});
- }
-});
-
-app.get('/decr/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- res.send(hive.decr(key));
- } else {
- res.send({error : "Key Required"});
- }
- return null;
-});
-
-
-app.post('/set/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- var body = req.body;
- if (comb.isObject(body) && body.value) {
- res.send({ set : hive.set(key, body.value, body.expires)});
- } else {
- console.log(req.body);
- res.send({error : "the value is required in the form of an object containing a value."});
- }
- } else {
- res.send({error : "Key Required"});
- }
- return null;
-});
-
-app.get('/replace/:key', function(req, res) {
- var key = req.params.key;
+app.get("/stats", function(req, res){
+ var key = req.params.key;
if (key) {
- var body = req.body;
- if (comb.isObject(body) && body.value) {
- res.send({ replaced : hive.replace(key, body.value)});
- } else {
- res.send({error : "the value is required in the form of an object containing a value."});
- }
- } else {
- res.send({error : "Key Required"});
- }
- return null;
-});
-
-app.post('/append/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- var body = req.body;
- if (comb.isObject(body) && body.value) {
- res.send({ set : hive.append(key, body.value)});
- } else {
- res.send({error : "the value is required in the form of an object containing a value."});
- }
+ hive.flushAll();
+ res.send({stats : helper.stats()});
} else {
res.send({error : "Key Required"});
}
- return null;
});
-app.post('/prepend/:key', function(req, res) {
- var key = req.params.key;
- if (key) {
- var body = req.body;
- if (comb.isObject(body) && body.value) {
- res.send({ set : hive.prepend(key, body.value)});
+["set", "replace", "append", "prepend"].forEach(function(action) {
+ app.post(format("/%s/:key", action), function(req, res) {
+ var key = req.params.key;
+ if (key) {
+ var body = req.body;
+ if (comb.isObject(body) && body.value) {
+ res.send({ set : hive[action](key, body.value, body.expires)});
+ } else {
+ console.log(req.body);
+ res.send({error : "the value is required in the form of an object containing a value."});
+ }
} else {
- res.send({error : "the value is required in the form of an object containing a value."});
+ res.send({error : "Key Required"});
}
- } else {
- res.send({error : "Key Required"});
- }
- return null;
+ });
});
app.listen(3000);
View
4 lib/socket.js
@@ -1,4 +1,4 @@
-var net = require("net"), hiveUtil = require("./utility/parser"), comb = require("comb"), Hive = require("./hive");
+var net = require("net"), helper = require("./utility/helper"), comb = require("comb"), Hive = require("./hive");
var HOST = "localhost";
var PORT = 8124;
@@ -10,7 +10,7 @@ var server = net.createServer(function (c) {
if (comb.isInstanceOf(data, Buffer)) {
data = data.toString();
}
- hiveUtil.execute(data, hive, c);
+ helper.execute(data, hive, c);
});
});
server.listen(PORT, HOST);
View
2 lib/utility/parser.js → lib/utility/helper.js
@@ -61,7 +61,7 @@ var parseCommand = function(command) {
return ret;
};
-var stats = function() {
+var stats = exports.stats = function() {
return JSON.stringify(process.memoryUsage(), null, 4);
};

0 comments on commit 6145171

Please sign in to comment.