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

add keyTypes method. returns an object containing key:datatype pairs, because...

add keyTypes method. returns an object containing key:datatype pairs, because deuce wants an object.
parent f127672e
No related branches found
No related tags found
No related merge requests found
...@@ -197,6 +197,17 @@ function JSONClient(serverAddr,serverPort) { ...@@ -197,6 +197,17 @@ function JSONClient(serverAddr,serverPort) {
return this.wait(); return this.wait();
} }
/* read object keys and key types (lock for reading or writing, blocking) */
this.keys=function(scope,location,lock) {
this.send(scope,"QUERY",{
oper:"KEYTYPES",
location:location,
lock:lock,
timeout:this.settings.TIMEOUT
});
return this.wait();
}
/* 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",{
...@@ -290,18 +301,21 @@ function JSONClient(serverAddr,serverPort) { ...@@ -290,18 +301,21 @@ function JSONClient(serverAddr,serverPort) {
if(!this.socket.data_waiting) if(!this.socket.data_waiting)
return false; return false;
var packet=this.socket.recvJSON(); var packet=this.socket.recvJSON();
switch(packet.func.toUpperCase()) { if(packet != null) {
case "PING": switch(packet.func.toUpperCase()) {
this.socket.pingOut("PONG"); case "PING":
return false; this.socket.pingOut("PONG");
case "PONG": return false;
this.socket.pingIn(packet); case "PONG":
return false; this.socket.pingIn(packet);
case "ERROR": return false;
throw(packet.data.description); case "ERROR":
return false; throw(packet.data.description);
return false;
}
return packet;
} }
return packet; return false;
} }
/* do not return until the expected response is received */ /* do not return until the expected response is received */
......
...@@ -99,7 +99,8 @@ function JSONdb (fileName) { ...@@ -99,7 +99,8 @@ function JSONdb (fileName) {
WHO:10, WHO:10,
STATUS:11, STATUS:11,
KEYS:12, KEYS:12,
SLICE:13 SLICE:13,
KEYTYPES:14
} }
/* error constants */ /* error constants */
...@@ -429,7 +430,33 @@ function JSONdb (fileName) { ...@@ -429,7 +430,33 @@ function JSONdb (fileName) {
return false; return false;
} }
} }
/* retrieve a list of object keys */
this.keyTypes = function(request,record) {
var client = request.client;
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.property]) {
var type = typeof record.data[record.property][k];
if(record.data[record.property][k] instanceof Array)
type = "array";
keys[k] = type;
}
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(request,record) { this.remove = function(request,record) {
var client = request.client; var client = request.client;
...@@ -678,6 +705,9 @@ function JSONdb (fileName) { ...@@ -678,6 +705,9 @@ function JSONdb (fileName) {
case "KEYS": case "KEYS":
result=this.keys(request,record); result=this.keys(request,record);
break; break;
case "KEYTYPES":
result=this.keys(request,record);
break;
case "PUSH": case "PUSH":
result=this.push(request,record); result=this.push(request,record);
break; break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment