Permalink
Browse files

Updated to work with callbacks rather than promises in node.js 0.1.30 +

Also applied a patch from Florent for non-existing records - thanks!
  • Loading branch information...
1 parent 41c7733 commit 79e30b5f0c5a24354695003dfdece6f35cb2be35 @waveto committed Feb 23, 2010
Showing with 68 additions and 35 deletions.
  1. +5 −4 README
  2. +45 −17 demo.js
  3. +18 −14 tyrant.js
View
9 README
@@ -3,6 +3,9 @@ node-tyrant
This is an implementation of the Tokyo Tyrant network protocol for the Node javascript/v8 language.
+Update: v0.2
+updated to move from promises to function(err, value) callbacks in node.js
+
Update: v0.1.3
updated to use the updated module structure of node.
@@ -28,10 +31,9 @@ var sys = require('sys');
var c = tyrant.connect();
c.addListener("connect", function (){
- c.genuid().addCallback(function(v) {sys.puts(v);});
-
+ c.genuid(function(err, v) {sys.puts(v);});
c.put('0', 'town', 'Bangor € 1');
- c.get('0').addCallback(function(values) {
+ c.get('0', function(err, values) {
var r=tyrant.dict(values);
sys.puts(r.town);
c.quit();
@@ -42,4 +44,3 @@ c.addListener("connect", function (){
-
View
62 demo.js
@@ -34,7 +34,7 @@ function simpleStoreAndRetreive() {
}
sys.puts('Reading 10 records :');
for (var i=0; i<10; i++) {
- tyrant.get('val_'+i).addCallback(function(value) {
+ tyrant.get('val_'+i, function(err, value) {
sys.puts('Result '+i+' : '+value);
});
}
@@ -44,11 +44,14 @@ function simpleStoreAndRetreive() {
function appendToRecord() {
tyrant.put('key1', 'First Value');
tyrant.putcat('key1', '+Second Value');
- tyrant.get('key1').addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
+ tyrant.get('key1', function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
}
function addToCounter() {
- tyrant.addint('counter', 1).addCallback(function(value) {sys.puts('Result '+value);});
+ tyrant.addint('counter', 1, function(err, value) {sys.puts('Result '+value);});
}
@@ -59,7 +62,7 @@ function tableStoreAndRetreive() {
}
sys.puts('Reading 10 table records :');
for (var i=0; i<10; i++) {
- tyrant.get('k_'+i).addCallback(function(values) {
+ tyrant.get('k_'+i, function(err, values) {
var r=tyrant.dict(values);
sys.puts('Result '+i+' : name = '+r.name); // is always 10....
});
@@ -74,7 +77,7 @@ function tableStoreAndRetreiveWithClosure() {
sys.puts('Reading 10 table records :');
for (var i=0; i<10; i++) {
(function(i) {
- tyrant.get('k_'+i).addCallback(function(values) {
+ tyrant.get('k_'+i, function(err, values) {
var r=tyrant.dict(values);
sys.puts('Result '+i+' : name = '+r.name); // is now the expected value for i
});
@@ -88,16 +91,31 @@ function tableSearchExample() {
tyrant.put('k1', 'name', 'Joe Blogs');
tyrant.put('k2', 'name', 'Joe Smith');
tyrant.put('k3', 'name', 'Bill Smith');
- tyrant.setindex('name', tyrant.ITLEXICAL).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
+ tyrant.setindex('name', tyrant.ITLEXICAL, function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
// Do a test get
- tyrant.get('k1').addCallback(function(value) {value=tyrant.dict(value); sys.puts('Name = '+value.name);});
+ tyrant.get('k1', function(err, value) {value=tyrant.dict(value); sys.puts('Name = '+value.name);});
// Do some searches
- tyrant.search(tyrant.starts('name', 'Joe'), tyrant.sort('name', 'asc')).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
- tyrant.search(tyrant.starts('name', 'Joe'), tyrant.sort('name', 'desc')).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
- tyrant.search(tyrant.ends('name', 'Smith')).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
- tyrant.search(tyrant.starts('name', 'Joe'),tyrant.ends('name', 'Smith')).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
+ tyrant.search(tyrant.starts('name', 'Joe'), tyrant.sort('name', 'asc'), function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
+ tyrant.search(tyrant.starts('name', 'Joe'), tyrant.sort('name', 'desc'), function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
+ tyrant.search(tyrant.ends('name', 'Smith'), function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
+ tyrant.search(tyrant.starts('name', 'Joe'),tyrant.ends('name', 'Smith'), function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
// Add a few more records
for (var i=0; i<20; i++) {
@@ -106,20 +124,30 @@ function tableSearchExample() {
// Do some more searches
- tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'asc')).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
- tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'desc')).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
+ tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'asc'), function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
+ tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'desc'), function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
// Test limit and offset
- tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'asc'), tyrant.limit(5, 5)).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
+ tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'asc'), tyrant.limit(5, 5), function(err, value) {
+ if (err) {sys.puts('Error : '+error);}
+ sys.puts('Result : '+value);
+ });
+
- tyrant.getlist(['ki0', 'ki1']).addCallback(function(v) {
+ tyrant.getlist(['ki0', 'ki1'], function(err, v) {
for (var i=0; i<v.length; i++) {
sys.puts('Name : '+v[i]);
}
});
// Test some helper functions
- tyrant.get('ki0').addCallback(function(value) {value=tyrant.dict(value); sys.puts('Name = '+value.name);});
+ tyrant.get('ki0', function(err, value) {value=tyrant.dict(value); sys.puts('Name = '+value.name);});
}
function generalDemo() {
@@ -137,7 +165,7 @@ function tableDemo(){
function runDemo() {
- tyrant.status().addCallback(function(s) {
+ tyrant.status(function(err, s) {
sys.puts('Tokyo Tyrant Server Status');
sys.puts('--------------------------');
sys.puts(s);
View
@@ -1,9 +1,9 @@
// node-tyrant.js
//
// A node.js network inerface for Tokyo Tyrant
-// Version 0.1.3
-// Requires node 0.1.18 or later
-// Rhys Jones, Acknack Ltd 2009
+// Version 0.2
+// Requires node 0.1.30 or later
+// Rhys Jones, Acknack Ltd 2010
//
// Copyright 2009, Acknack Ltd. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -50,7 +50,7 @@ exports.addListener = function(event, listener) {
exports.connect = function(port, hostname) {
conn=tcp.createConnection(port || 1978, hostname || '127.0.0.1');
conn.addListener("connect", onConnect);
- conn.addListener("receive", onReceive);
+ conn.addListener("data", onData);
conn.addListener("disconnect", onDisconnect);
return this;
}
@@ -230,7 +230,7 @@ function responseSingle(data) {
}
function responseMisc(data) {
- if (data.charCodeAt(0)!=0) return [null, 1, 'Tyrant Error : '+data.charCodeAt(0)];
+ if (data.charCodeAt(0)!=0) return [null, 5, 'Tyrant Error : '+data.charCodeAt(0)];
if (data.length<9) return [null, -1, null];
var r=[];
var c=1;
@@ -250,19 +250,24 @@ function createCommandSender(commandName) {
throw "connection is not open";
}
+ var callback = null;
var numArgs = arguments.length;
+
+ if (typeof(arguments[arguments.length-1])=='function') {
+ callback = arguments[arguments.length-1];
+ numArgs=arguments.length-1;
+ }
+
var cmd;
- if (commands[commandName]) {
+ if (commands[commandName]) {
cmd = commands[commandName][0](commandName, arguments, numArgs);
} else {
throw 'unknown command '+commandName;
}
- var promise = new process.Promise;
- callbacks.push( { 'cmd':commandName, 'promise':promise });
- conn.send(cmd, "binary");
- return promise;
+ callbacks.push( { cmd:commandName, cb:callback });
+ conn.write(cmd, "binary");
}
}
@@ -311,7 +316,7 @@ exports.dict = function (r) {
-function onReceive(data) {
+function onData(data) {
//sys.puts('Received: '+data.length+', response : '+response.length);
//pprint(data);
response += data;
@@ -327,9 +332,8 @@ function onReceive(data) {
}
if ( offset>=0 || result || err ) {
var callback = callbacks.shift();
- if (callback && callback.promise) {
- if (result) {callback.promise.emitSuccess(result);}
- if (err) {callback.promise.emitError(err);}
+ if (callback && callback.cb) {
+ callback.cb(err, result);
}
}
}

0 comments on commit 79e30b5

Please sign in to comment.