Skip to content
Snippets Groups Projects
Commit 39e8ed20 authored by mcmlxxix's avatar mcmlxxix
Browse files

new db query "KEYS" returns an array of object keys

parent caae4a1f
No related branches found
No related tags found
No related merge requests found
...@@ -14,19 +14,25 @@ load("json-sock.js"); ...@@ -14,19 +14,25 @@ load("json-sock.js");
- JSONClient.cycle(); - JSONClient.cycle();
- JSONClient.connect(); - JSONClient.connect();
- JSONClient.disconnect(); - JSONClient.disconnect();
- JSONClient.read(); - JSONClient.read(scope,location,lock);
- JSONClient.pop(); - JSONClient.pop(scope,location,lock);
- JSONClient.shift(); - JSONClient.shift(scope,location,lock);
- JSONClient.write(); - JSONClient.write(scope,location,lock);
- JSONClient.push(); - JSONClient.push(scope,location,lock);
- JSONClient.unshift(); - JSONClient.unshift(scope,location,lock);
- JSONClient.lock(); - JSONClient.lock(scope,location,lock);
- JSONClient.unlock(); - JSONClient.unlock(scope,location);
- JSONClient.subscribe(); - JSONClient.subscribe(scope,location);
- JSONClient.unsubscribe(); - JSONClient.unsubscribe(scope,location);
- JSONClient.status(); - JSONClient.status(scope,location);
- JSONClient.who(); - JSONClient.who(scope,location);
- JSONClient.ident(); - JSONClient.ident(scope,username,password);
NOTE: scope is the module or root service you wish to send the command to,
location is a dot-notated object property, and lock is one of the following:
LOCK_READ = 1
LOCK_WRITE = 2
LOCK_UNLOCK = -1
indirect methods: these will generally be called automatically by the other methods indirect methods: these will generally be called automatically by the other methods
and you will not typically need to use them and you will not typically need to use them
...@@ -43,22 +49,14 @@ load("json-sock.js"); ...@@ -43,22 +49,14 @@ load("json-sock.js");
sample usage: sample usage:
var LOCK_READ = 1;
var LOCK_WRITE = 2;
var UNLOCK = -1;
load("json-client.js"); load("json-client.js");
var client=new JSONClient(myServer,myPort); var client=new JSONClient(myServer,myPort);
function callback(data) {
myData = data;
}
while(1) { while(1) {
doSomething(); doSomething();
client.lock("mydatabase.dong",LOCK_READ); client.lock("myscript","mydatabase.dong",LOCK_READ);
var dong=client.read("mydatabase.dong"); var dong=client.read("myscript","mydatabase.dong");
client.unlock("mydatabase.dong"); client.unlock("myscript","mydatabase.dong");
print("look at my " + dong); print("look at my " + dong);
client.cycle(); client.cycle();
} }
...@@ -143,6 +141,16 @@ function JSONClient(serverAddr,serverPort) { ...@@ -143,6 +141,16 @@ function JSONClient(serverAddr,serverPort) {
return this.wait("RESPONSE"); return this.wait("RESPONSE");
} }
/* read object keys (lock for reading or writing, blocking) */
this.keys=function(scope,location,lock) {
this.send(scope,"QUERY",{
oper:"KEYS",
location:location,
lock:lock
});
return this.wait("RESPONSE");
}
/* shift object data (lock for reading or writing, blocking) */ /* shift object data (lock for reading or writing, blocking) */
this.shift=function(scope,location,lock) { this.shift=function(scope,location,lock) {
this.send(scope,"QUERY",{ this.send(scope,"QUERY",{
......
...@@ -87,7 +87,8 @@ function JSONdb (fileName) { ...@@ -87,7 +87,8 @@ function JSONdb (fileName) {
SUBSCRIBE:8, SUBSCRIBE:8,
UNSUBSCRIBE:9, UNSUBSCRIBE:9,
WHO:10, WHO:10,
STATUS:11 STATUS:11,
KEYS:12
} }
/* error constants */ /* error constants */
...@@ -358,6 +359,27 @@ function JSONdb (fileName) { ...@@ -358,6 +359,27 @@ function JSONdb (fileName) {
} }
}; };
/* retrieve a list of object keys */
this.keys = function(client,record) {
var keys=[];
/* if the requested data does not exist, result is undefined */
if(record.data === undefined) {
send_packet(client,undefined,"RESPONSE");
return true;
}
/* if this client has this record locked, read */
if(record.info.lock[client.id]) {
for(var k in record.data[record.child_name])
keys.push(k);
send_packet(client,keys,"RESPONSE");
return true;
}
/* if there is no lock for this client, error */
else {
return false;
}
}
/* remove a record from the database (requires WRITE_LOCK) */ /* remove a record from the database (requires WRITE_LOCK) */
this.remove = function(client,record) { this.remove = function(client,record) {
/* if the requested data does not exist, do nothing */ /* if the requested data does not exist, do nothing */
...@@ -619,6 +641,9 @@ function JSONdb (fileName) { ...@@ -619,6 +641,9 @@ function JSONdb (fileName) {
case "WRITE": case "WRITE":
result=this.write(request.client,record,request.data); result=this.write(request.client,record,request.data);
break; break;
case "KEYS":
result=this.keys(request.client,record);
break;
case "PUSH": case "PUSH":
result=this.push(request.client,record,request.data); result=this.push(request.client,record,request.data);
break; break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment