Permalink
Browse files

Use updated module system

  • Loading branch information...
1 parent dd9a6d3 commit 98b6c1148471718697977059f27f731b71f3f8c8 @waveto committed Nov 27, 2009
Showing with 58 additions and 45 deletions.
  1. +13 −4 README
  2. +30 −29 demo.js
  3. +3 −2 simple.js
  4. +12 −10 tyrant.js
View
17 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.1.3
+updated to use the updated module structure of node.
+
Update: v0.1.1
node-tyrant has been updated to use the new binary stream representation, and now uses promises for callbacks.
@@ -19,13 +22,18 @@ Quick Example
------------
-var tyrant = require("tyrant.js");
+var tyrant = require("./tyrant");
+var sys = require('sys');
var c = tyrant.connect();
+
c.addListener("connect", function (){
- c.put('town', 'Bangor');
- c.get('town').addCallback(function(value) {
- puts('Town : '+value);
+ c.genuid().addCallback(function(v) {sys.puts(v);});
+
+ c.put('0', 'town', 'Bangor € 1');
+ c.get('0').addCallback(function(values) {
+ var r=tyrant.dict(values);
+ sys.puts(r.town);
c.quit();
});
});
@@ -34,3 +42,4 @@ c.addListener("connect", function (){
+
View
59 demo.js
@@ -1,8 +1,8 @@
// demo.js
//
// A demo for node-tyrant
-// Version 0.1.1
-// Requires node 0.1.13 or later
+// Version 0.1.3
+// Requires node 0.1.18 or later
// Rhys Jones, Acknack Ltd 2009
//
// Copyright 2009, Acknack Ltd. All rights reserved.
@@ -24,17 +24,18 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
-var tyrant = require("tyrant.js");
+var tyrant = require("./tyrant");
+var sys = require('sys');
function simpleStoreAndRetreive() {
- puts('Writing 10 records..');
+ sys.puts('Writing 10 records..');
for (var i=0; i<10; i++) {
tyrant.put('val_'+i, 'Record number '+i);
}
- puts('Reading 10 records :');
+ sys.puts('Reading 10 records :');
for (var i=0; i<10; i++) {
tyrant.get('val_'+i).addCallback(function(value) {
- puts('Result '+i+' : '+value);
+ sys.puts('Result '+i+' : '+value);
});
}
}
@@ -43,39 +44,39 @@ function simpleStoreAndRetreive() {
function appendToRecord() {
tyrant.put('key1', 'First Value');
tyrant.putcat('key1', '+Second Value');
- tyrant.get('key1').addCallback(function(value) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
+ tyrant.get('key1').addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
}
function addToCounter() {
- tyrant.addint('counter', 1).addCallback(function(value) {puts('Result '+value);});
+ tyrant.addint('counter', 1).addCallback(function(value) {sys.puts('Result '+value);});
}
function tableStoreAndRetreive() {
- puts('Writing 10 table records..');
+ sys.puts('Writing 10 table records..');
for (var i=0; i<10; i++) {
tyrant.put('k_'+i, 'name', 'Mr. Number '+i);
}
- puts('Reading 10 table records :');
+ sys.puts('Reading 10 table records :');
for (var i=0; i<10; i++) {
tyrant.get('k_'+i).addCallback(function(values) {
var r=tyrant.dict(values);
- puts('Result '+i+' : name = '+r.name); // is always 10....
+ sys.puts('Result '+i+' : name = '+r.name); // is always 10....
});
}
}
function tableStoreAndRetreiveWithClosure() {
- puts('Writing 10 table records..');
+ sys.puts('Writing 10 table records..');
for (var i=0; i<10; i++) {
tyrant.put('k_'+i, 'name', 'Mr. Number '+i);
}
- puts('Reading 10 table records :');
+ sys.puts('Reading 10 table records :');
for (var i=0; i<10; i++) {
(function(i) {
tyrant.get('k_'+i).addCallback(function(values) {
var r=tyrant.dict(values);
- puts('Result '+i+' : name = '+r.name); // is now the expected value for i
+ sys.puts('Result '+i+' : name = '+r.name); // is now the expected value for i
});
})(i); // Stick i into the anonymous function, so it has it at this value, not the overall variable in the enlarged scope
}
@@ -87,16 +88,16 @@ 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) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
+ tyrant.setindex('name', tyrant.ITLEXICAL).addCallback(function(value) {sys.puts('Result : '+value);}).addErrback(function(error) {sys.puts('Error : '+error);});
// Do a test get
- tyrant.get('k1').addCallback(function(value) {value=tyrant.dict(value); puts('Name = '+value.name);});
+ tyrant.get('k1').addCallback(function(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) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
- tyrant.search(tyrant.starts('name', 'Joe'), tyrant.sort('name', 'desc')).addCallback(function(value) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
- tyrant.search(tyrant.ends('name', 'Smith')).addCallback(function(value) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
- tyrant.search(tyrant.starts('name', 'Joe'),tyrant.ends('name', 'Smith')).addCallback(function(value) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
+ 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);});
// Add a few more records
for (var i=0; i<20; i++) {
@@ -105,20 +106,20 @@ function tableSearchExample() {
// Do some more searches
- tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'asc')).addCallback(function(value) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
- tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'desc')).addCallback(function(value) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
+ 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);});
// Test limit and offset
- tyrant.search(tyrant.starts('name', 'Mr'), tyrant.sort('name', 'asc'), tyrant.limit(5, 5)).addCallback(function(value) {puts('Result : '+value);}).addErrback(function(error) {puts('Error : '+error);});
+ 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.getlist(['ki0', 'ki1']).addCallback(function(v) {
for (var i=0; i<v.length; i++) {
- puts('Name : '+v[i]);
+ sys.puts('Name : '+v[i]);
}
});
// Test some helper functions
- tyrant.get('ki0').addCallback(function(value) {value=tyrant.dict(value); puts('Name = '+value.name);});
+ tyrant.get('ki0').addCallback(function(value) {value=tyrant.dict(value); sys.puts('Name = '+value.name);});
}
function generalDemo() {
@@ -137,10 +138,10 @@ function tableDemo(){
function runDemo() {
tyrant.status().addCallback(function(s) {
- puts('Tokyo Tyrant Server Status');
- puts('--------------------------');
- puts(s);
- puts('\n');
+ sys.puts('Tokyo Tyrant Server Status');
+ sys.puts('--------------------------');
+ sys.puts(s);
+ sys.puts('\n');
if (s.indexOf('type\ttable')>=0) {
tableDemo();
} else {
View
@@ -1,10 +1,11 @@
-var tyrant = require("tyrant.js");
+var tyrant = require("./tyrant");
+var sys = require('sys');
var c = tyrant.connect();
c.addListener("connect", function (){
c.put('town', 'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch');
c.get('town').addCallback(function(value) {
- puts('Town : '+value);
+ sys.puts('Town : '+value);
c.quit();
});
});
View
@@ -1,8 +1,8 @@
// node-tyrant.js
//
// A node.js network inerface for Tokyo Tyrant
-// Version 0.1.2
-// Requires node 0.1.13 or later
+// Version 0.1.3
+// Requires node 0.1.18 or later
// Rhys Jones, Acknack Ltd 2009
//
// Copyright 2009, Acknack Ltd. All rights reserved.
@@ -24,13 +24,15 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
-include('/tcp.js');
-include('/utils.js');
+var tcp = require('tcp');
+//process.mixin(GLOBAL, 'tcp');
+
+var sys = require('sys');
var conn;
var callbacks=[];
var response='';
-var emitter=new node.EventEmitter();
+var emitter=new process.EventEmitter();
var TCMD=String.fromCharCode(0xC8);
exports.ITLEXICAL = '0';
@@ -46,7 +48,7 @@ exports.addListener = function(event, listener) {
exports.connect = function(port, hostname) {
- conn=createConnection(port || 1978, hostname || '127.0.0.1');
+ conn=tcp.createConnection(port || 1978, hostname || '127.0.0.1');
conn.addListener("connect", onConnect);
conn.addListener("receive", onReceive);
conn.addListener("disconnect", onDisconnect);
@@ -165,8 +167,8 @@ function packInt(i) {
function pprint(s) {
for (var i=0; i<s.length; i++) {
- if (s.charCodeAt(i)<32) {puts(s.charCodeAt(i)+' : ');}
- else {puts(s.charCodeAt(i)+' : '+ s.charAt(i));}
+ if (s.charCodeAt(i)<32) {sys.puts(s.charCodeAt(i)+' : ');}
+ else {sys.puts(s.charCodeAt(i)+' : '+ s.charAt(i));}
}
}
@@ -257,7 +259,7 @@ function createCommandSender(commandName) {
throw 'unknown command '+commandName;
}
- var promise = new node.Promise;
+ var promise = new process.Promise;
callbacks.push( { 'cmd':commandName, 'promise':promise });
conn.send(cmd, "binary");
return promise;
@@ -310,7 +312,7 @@ exports.dict = function (r) {
function onReceive(data) {
- //puts('Received: '+data.length+', response : '+response.length);
+ //sys.puts('Received: '+data.length+', response : '+response.length);
//pprint(data);
response += data;
var offset=0;

0 comments on commit 98b6c11

Please sign in to comment.