Permalink
Browse files

popByte fix

  • Loading branch information...
1 parent c005b86 commit d24495aa8a2ec4a89e52cbb9598bb367fd4a6c65 @apb2006 committed Jul 19, 2012
Showing with 182 additions and 102 deletions.
  1. +2 −1 changelog.md
  2. +2 −2 examples/EventBasics.js
  3. +4 −6 examples/QueryBindExample.js
  4. +4 −10 examples/QueryExample.js
  5. +18 −0 examples/RawExample.js
  6. +0 −22 examples/npm-debug.log
  7. +1 −19 index.js
  8. +53 −20 lib/parser.js
  9. +67 −14 lib/query.js
  10. +7 −7 readme.md
  11. +24 −1 tests/test-stress.js
View
@@ -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
View
@@ -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){
@@ -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();
View
@@ -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();
View
@@ -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();
View
@@ -1,22 +0,0 @@
-info it worked if it ends with ok
-verbose cli [ 'node', '/usr/bin/npm', 'test' ]
-info using npm@1.0.105
-info using node@v0.6.10
-verbose /usr/bin/node node symlink
-verbose config file /home/andy/.npmrc
-verbose config file /usr/etc/npmrc
-verbose config file /usr/lib/node_modules/npm/npmrc
-ERR! Error: ENOENT, no such file or directory '/home/andy/workspace/basex-node/examples/package.json'
-ERR! Report this *entire* log at:
-ERR! <http://github.com/isaacs/npm/issues>
-ERR! or email it to:
-ERR! <npm-@googlegroups.com>
-ERR!
-ERR! System Linux 3.0.0-19-generic
-ERR! command "node" "/usr/bin/npm" "test"
-ERR! cwd /home/andy/workspace/basex-node/examples
-ERR! node -v v0.6.10
-ERR! npm -v 1.0.105
-ERR! path /home/andy/workspace/basex-node/examples/package.json
-ERR! code ENOENT
-verbose exit [ 1, true ]
View
@@ -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) {
View
@@ -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;
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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);
}
Oops, something went wrong.

0 comments on commit d24495a

Please sign in to comment.