Skip to content

Commit

Permalink
popByte fix
Browse files Browse the repository at this point in the history
  • Loading branch information
apb2006 committed Jul 19, 2012
1 parent c005b86 commit d24495a
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 102 deletions.
3 changes: 2 additions & 1 deletion changelog.md
@@ -1,8 +1,9 @@
## v0.5.0 - 2012-07-15

- Major bugs fixed
- Support for BaseX events
- Rewrite of parser
- still buggy


## v0.4.1 - 2011-11-09

Expand Down
4 changes: 2 additions & 2 deletions examples/EventBasics.js
Expand Up @@ -12,8 +12,8 @@ var session1 = new basex.Session("localhost", 1984, "admin", "admin");
basex.debug_mode = true;
session1.execute("create event testevent",d.print);
session1.watch("testevent",watchCallback,d.print);
session1.unwatch("testevent",null,d.print);
session1.execute("drop event messenger",d.printMsg("S1:drop event"));
session1.unwatch("testevent",d.print);
session1.execute("drop event testevent",d.printMsg("S1:drop event"));
session1.close(d.print);

function watchCallback(name,msg){
Expand Down
10 changes: 4 additions & 6 deletions examples/QueryBindExample.js
Expand Up @@ -9,18 +9,16 @@ var log = require("../debug");
var session = new basex.Session("localhost", 1984, "admin", "admin");

// create query instance
var input = "declare variable $name external; for $i in 1 to 10 return element { $name } { $i }";
var input = "declare variable $name external; for $i in 1 to 1000 return element { $name } { $i }";
var query = session.query(input);

// bind variable
query.bind("name", "nodex","",log.print);

query.info(log.print);
// print results
query.execute(log.print);
// do it again
query.bind("name", "again","",log.print);
query.execute(log.print);
query.info(log.print);



// close query instance
query.close();
Expand Down
14 changes: 4 additions & 10 deletions examples/QueryExample.js
Expand Up @@ -6,21 +6,15 @@
*/
var basex = require("../index");
var log = require("../debug");
basex.debug_mode = false;
// create session
var session = new basex.Session("localhost", 1984, "admin", "admin");

// create session
var session = new basex.Session("localhost", 1984, "admin", "admin");
basex.debug_mode = false;
// create query instance
var input = 'for $i in 1 to 10 return <xml>Text { $i }</xml>';
var input = 'for $i in 1 to 100 return <xml>Text { $i }</xml>';
var query = session.query(input);

query.results(log.print);
//query.info(basex.print);
//query.options(basex.print);
// loop through all results
//while(query.more()) {
// console.log(query->next());
//}

// close query instance
query.close();
Expand Down
18 changes: 18 additions & 0 deletions examples/RawExample.js
@@ -0,0 +1,18 @@
/*
* This example shows the use of binary data .
*/
var basex = require("../index");
var client = new basex.Session();
basex.debug_mode = true;

function print(err, reply) {
if (err) {
console.log("Error: " + err);
} else {
console.log(reply);
}
};
var xq="let $av:= (0 to 127,-128 to -1)!xs:byte(.)" +
" return convert:bytes-to-hex($av)";
client.execute("xquery declare option output:method 'raw';"+xq,print);
client.close();
22 changes: 0 additions & 22 deletions examples/npm-debug.log

This file was deleted.

20 changes: 1 addition & 19 deletions index.js
Expand Up @@ -201,25 +201,7 @@ var BaseXStream = function(host, port, username, password) {
}

};
// Iterator: returns array of items:
this.readiter = function() {
if (-1 != self.buffer.indexOf("\0")) {
var items = [];
while (!self.ok()) {
items.push(self.readline());
}
var ok = self.ok();
var r = {
ok : ok
};
if (ok) {
r.result = items
} else {
r.info = self.readline()
}
return r
}
};


// add command and returns the result:
this.execute = function(cmd, callback) {
Expand Down
73 changes: 53 additions & 20 deletions lib/parser.js
Expand Up @@ -5,39 +5,72 @@
* parse incoming messages from basex server
* functions expect bufferobj to have a buffer property
* that is yet unprocessed msg stream as string
*/
*/

// take fields named in array flds delimited by \0 from buffer
// @param bufferObj an Object with a buffer property. The buffer is modified
// @param popStatus boolean if true read 1byte status 0/1 into ok:
// @return object or empty if message not fully present in buffer.
function popmsg(bufferObj,flds,popStatus){
var i=0,last=0, buf=bufferObj.buffer,res={};
popStatus = (typeof popStatus == 'undefined')?true:popStatus;
function popmsg(bufferObj, flds, popStatus) {
var i = 0, last = 0, buf = bufferObj.buffer, res = {};
popStatus = (typeof popStatus == 'undefined') ? true : popStatus;

while(i<buf.length && flds.length){
if(buf[i]=="\0"){
res[flds.shift()]=buf.substring(i,last)
last=i+1;
while (i < buf.length && flds.length) {
if (buf[i] == "\0") {
res[flds.shift()] = buf.substring(i, last)
last = i + 1;
}
i++;
}
if(flds.length>0 ||(popStatus && i==buf.length)){
if (flds.length > 0 || (popStatus && i == buf.length)) {
return
}
if(popStatus){
if("\0"==buf[i]){
res["ok"]=true;
}else if("\1"==buf[i]){
res["ok"]=false;
}else{
throw "expected status marker at"+i+"="+buf.charCodeAt(i)+ ":"+buf;
}
if (popStatus) {
if ("\0" == buf[i]) {
res["ok"] = true;
} else if ("\1" == buf[i]) {
res["ok"] = false;
} else {
throw "expected status marker at" + i + "=" + buf.charCodeAt(i)
+ ":" + buf;
}
i++;
}
bufferObj.buffer=buf.substring(i)
// console.log("msg extracted, remaining: ",bufferObj.buffer.length,bufferObj.buffer)
}
bufferObj.buffer = buf.substring(i)
// console.log("msg extracted, remaining:
// ",bufferObj.buffer.length,bufferObj.buffer)
return res
};

// @param bufferObj an Object with a buffer property. The buffer is modified
function popByte(bufferObj) {
var buff = bufferObj.buffer
if (0==buff.length) {
return
}
bufferObj.buffer = buff.substring(1)
return {
data : buff[0]
}
}

// @param bufferObj an Object with a buffer property. The buffer is modified
function popLine(bufferObj) {
var buff = bufferObj.buffer, line = ""
for ( var i = 0; i < buff.length; i++) {
var c = buff[i]
if (c != "\xFF") {
if (c == "\0") {
bufferObj.buffer = buff.substring(i+1)
return {
data : line
}
} else {
line += c
}
}
}
};
exports.popmsg = popmsg;
exports.popLine = popLine;
exports.popByte = popByte;
81 changes: 67 additions & 14 deletions lib/query.js
@@ -1,17 +1,21 @@
/* BaseX Node.js client query handler
* http://docs.basex.org/wiki/Server_Protocol
* andy bunce 2011-2012
*/
* andy bunce 2011-2012
*/
parser = require("./parser");

function Query(session, query) {
this.session = session;
this.query = query;
this.id = null;
this.cache = [];
this.state = "type";
var self = this;

this.close = function(callback) {
self.session.send_command({
send : function() { // send is function as needs id
return "\x02" + self.id +"\0"
return "\x02" + self.id + "\0"
},
parser : self.session.parser2,
callback : callback
Expand All @@ -22,8 +26,8 @@ function Query(session, query) {
var type = "";
self.session.send_command({
send : function() {
return "\x03" + self.id + "\0" + name + "\0" + value
+ "\0" + type +"\0"
return "\x03" + self.id + "\0" + name + "\0" + value + "\0"
+ type + "\0"
},
parser : self.session.parser2,
callback : callback
Expand All @@ -34,17 +38,66 @@ function Query(session, query) {
this.results = function(callback) {
self.session.send_command({
send : function() {
return "\x04" + self.id +"\0"
return "\x04" + self.id + "\0"
},
parser : self.session.readiter,
parser : self.parseResults,
callback : callback
});
};

// Iterator: returns array of items:
this.parseResults = function() {
do {
// console.log("state",self.state,self.cache)
progress = false;
switch (self.state) {
case "type":
var r = parser.popByte(session);
if (r) {
self.state = (r.data == "\0") ? "status" : "item";
progress = true;
}
break
case "item":
var r = parser.popLine(session);
if (r) {
self.state = "type";
self.cache.push(r.data);
progress = true;
}
break
case "status":
var r = parser.popByte(session);
if (r) {
if (r.data == "\0") {
return {
ok : true,
result : self.cache
}
} else {
self.state = "error"
}
progress = true;
}
break
case "error":
var r = parser.popLine(session)
if (r) {
return {
ok : false,
result : self.cache,
info : r.result
}
}
break
}
} while (progress)
};

this.execute = function(callback) {
self.session.send_command({
send : function() {
return "\x05" + self.id +"\0"
return "\x05" + self.id + "\0"
},
parser : self.session.parser2,
callback : callback
Expand All @@ -54,7 +107,7 @@ function Query(session, query) {
this.info = function(callback) {
self.session.send_command({
send : function() {
return "\x06" + self.id +"\0"
return "\x06" + self.id + "\0"
},
parser : self.session.parser2,
callback : callback
Expand All @@ -64,7 +117,7 @@ function Query(session, query) {
this.options = function(callback) {
self.session.send_command({
send : function() {
return "\x07" + self.id +"\0"
return "\x07" + self.id + "\0"
},
parser : self.session.parser2,
callback : callback
Expand All @@ -74,13 +127,13 @@ function Query(session, query) {
// request id
session.send_command({
blocking : true,
send : "\0" + query +"\0",
send : "\0" + query + "\0",
parser : self.session.parser2,
callback : function(err, reply) {
self.id = reply.result;
// if (exports.debug_mode) {
// console.log("Query id: ", self.id, ", query: ", query);
// }
// if (exports.debug_mode) {
// console.log("Query id: ", self.id, ", query: ", query);
// }
;
self.session.setBlock(false);
}
Expand Down

0 comments on commit d24495a

Please sign in to comment.