ydn-db async operation need to assign value to variable - ydn-db

I'm unable to assign variable from async operation using callback . .done didn't assign value to variable. Here my code :
var someGlobalVar = 0;
dbs.count('cfs_init').done(function(x) {
console.log('Total : ' + x);
someGlobalVar = x;
});
console.log(someGlobalVar); // 0

Your assignment works if you manually (or in setTimeout function) execute console.log(someGlobalVar);, after receiving done callback.
You are executing it before done callback and hence it is still 0.

Related

Modifies clause error on a changed object

How can I state (in Dafny) an "ensures" guarantee that the object returned by a method will be "new", i.e., will not be the same as an object used anywhere else (yet)?
The following code shows a minimal example:
method newArray(a:array<int>) returns (b:array<int>)
requires a != null
ensures b != null
ensures a != b
ensures b.Length == a.Length+1
{
b := new int[a.Length+1];
}
class Testing {
var test : array<int>;
method doesnotwork()
requires this.test!=null
requires this.test.Length > 10;
modifies this
{
this.test := newArray(this.test); //change array a with b
this.test[3] := 9; //error modifies clause
}
method doeswork()
requires this.test!=null
requires this.test.Length > 10;
modifies this
{
this.test := new int[this.test.Length+1];
this.test[3] := 9;
}
}
The "doeswork" function compiles (and verifies) correctly, but the other one does not, as the Dafny compiler cannot know that the object returned by the "newArray" function is new, i.e., is not required to be listed as modifiable in the "require" statement of the "doesnotwork" function in order for that function to fulfill the requirement that it only modifies "this". In the "doeswork" function, I simply inserted the definition of the "newArray" function, and then it works.
You can find the example above under https://rise4fun.com/Dafny/hHWwr, where it can also be ran online.
Thanks!
You can say ensures fresh(b) on newArray.
fresh means exactly what you described: the object is not the same as any object that was allocated before the call to newArray.

Removing an object from a dictionary in lua

I am using aerospike for storage and its UDF's in Lua. While executing the udf's through both NodeJS and Python- I need to delete a key-value pair from the dictionary being passed as a parameter.
Below is the code snippets:
function deleteProduct(rec, prod_id, isodate)
map.remove(rec, prod_id)
aerospike:update(rec)
return 0
end
And the rec structure is:
{
meta.num_prod: 4
s.10000006: {
prod_id: "10000006"
qty: "4"
}
I do understand that pythonic dictionary is not same as Lua maps- but I am stuck with this. I get the error message as:
/opt/aerospike/usr/udf/lua/update.lua:14: bad argument #1 to \'remove\' (Map expected, got userdata)
The rec is the aerospike record being invoked in the below manner:
var udf = { module:'update', funcname: 'deleteProductFromCart', args: [prod_key, isoDate]}
sails.aerospike.execute(cart_key, udf, function(err, result) {
if(err.code!=status.AEROSPIKE_OK){
console.log(err)
defer.resolve(false)
}
else{
defer.resolve(true)
}
});
According to the provided error message, you should call it this way (through colon):
map:remove(rec, prod_id)
I am sure you know what is the difference.
Below works just fine!
map[key] = nil

How to call inputTask from within another inputTask?

In a inputTask I'm programmatically calling another inputTask, e.g. testOnly, with parameter string as follows:
val readParams = inputKey[Unit]("reads version")
readParams := {
... // here some Parser code
val a = "*OnlyThisClassPls*"
testOnly.toTask(a)
}
Unfortunately instead of result I get an exception Illegal dynamic reference. Why?
I think I solved my problem.
I created a method which converts testOnly inputTask to dynamic task (taskDyn) with parameter
def testOnlyWithDynamicParams(params: String) = Def.taskDyn {
(testOnly in Test).toTask(params)
}
I defined an dynamic input task (inputTaskDyn) which uses method to convert and evaluates value at the end
readParams := Def.inputTaskDyn {
... // here some Parser code
val paramsForTestOnly = " *OnlyThisClassPls*"
testOnlyWithDynamicParams(paramsForTestOnly)
}.evaluated
I'm not sure if it is a best way but it works for me. If you know the better solution please correct me.

How can I determine end-of-cursor in Rust using sqlite3 Select?

I'm just starting to evaluate Rust. Using Rust and the sqlite3 repo on Github, I'm attempting to determine EOF for a Cursor. I'm not sure how to do that "correctly", I think it may be via the "match" statement.
The 2nd line in the following 2 lines is how I'm currently determining EOF, but this is obviously not the "correct" way:
let oNextResult:sqlite::types::ResultCode = oDbCursor.step();
tDone = (fmt!("%?", oNextResult) == ~"SQLITE_DONE");
The following is the unfinished function containing the above 2 lines. Please excuse the lack of Rust naming-convention, but I will look at implementing that.
/********************
**** Update Data ****
*********************/
fn fUpdateData(oDb1:&sqlite::database::Database, iUpdateMax:int) -> bool {
println(fmt!("Updating %d Rows .......", iUpdateMax));
let sSql:~str = fmt!("Select ikey, sname, iborn, dbal from test LIMIT %d",
iUpdateMax);
let oDbExec = oDb1.exec(sSql);
if oDbExec.is_err() {
println(fmt!("Select Failed! : %?, sql=%s", oDbExec, sSql));
return false;
}
println("Select succeeded. Processing select list .....");
let mut iUpdateCount: int = 0;
let oDbCursor:sqlite::cursor::Cursor = oDb1.prepare(sSql, &None).unwrap();
let mut tDone:bool = false;
while !tDone {
let oNextResult:sqlite::types::ResultCode = oDbCursor.step();
tDone = (fmt!("%?", oNextResult) == ~"SQLITE_DONE");
if !tDone {
let sKey = oDbCursor.get_text(0);
let sName = oDbCursor.get_text(1);
let sBorn = oDbCursor.get_text(2);
let sBal = oDbCursor.get_text(3);
println(fmt!("skey = %s, sname = %s, sBorn = %s, sBal = %s", sKey,
sName, sBorn, sBal));
iUpdateCount += 1;
}
}
println(fmt!("Update succeeded, items updated = %d", iUpdateCount));
return true;
}
I don't know if there is a correct way at the moment but you can also the result codes from the types module:
use sqlite::types::ResultCode;
and then do something like this so there's no need for using fmt!
while cursor.step() == SQLITE_ROW {...}
or this:
while cursor.get_column_count() != 0 {...; cursor.step()}
Function get_column_count returns an int. If there's no data it will return 0. It calls int sqlite3_data_count(sqlite3_stmt *pStmt); under the hood and here's what sqlite docs say about it:
The sqlite3_data_count(P) interface returns the number of columns in
the current row of the result set of prepared statement P. If prepared
statement P does not have results ready to return (via calls to the
sqlite3_column_*() of interfaces) then sqlite3_data_count(P) returns
0. The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. The sqlite3_data_count(P) routine returns 0 if the previous
call to sqlite3_step(P) returned SQLITE_DONE. The
sqlite3_data_count(P) will return non-zero if previous call to
sqlite3_step(P) returned SQLITE_ROW, except in the case of the PRAGMA
incremental_vacuum where it always returns zero since each step of
that multi-step pragma returns 0 columns of data.
As it's mentioned on the readme file rustsqlite interface is not finalized, watch out for changes.

ARC [rewriter] NSInvocation's setArgument is not safe to be used with an object with ownership other than __unsafe_unretained

I been to convert my project to ARC and i m stuck with this error.
&object,&invocation and &callerToRetain is showing me error of "[rewriter] NSInvocation's setArgument is not safe to be used with an object with ownership other than __unsafe_unretained"
+ (void)performSelector:(SEL)selector onTarget:(id *)target withObject:(id)object amount:(void *)amount callerToRetain:(id)callerToRetain{if ([*target respondsToSelector:selector]) {
NSMethodSignature *signature = nil;
signature = [*target methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
int argumentNumber = 2;
// If we got an object parameter, we pass a pointer to the object pointer
if (object) {
[invocation setArgument:&object atIndex:argumentNumber];
argumentNumber++;
}
// For the amount we'll just pass the pointer directly so NSInvocation will call the method using the number itself rather than a pointer to it
if (amount) {
[invocation setArgument:amount atIndex:argumentNumber];
}
SEL callback = #selector(performInvocation:onTarget:releasingObject:);
NSMethodSignature *cbSignature = [ASIHTTPRequest methodSignatureForSelector:callback];
NSInvocation *cbInvocation = [NSInvocation invocationWithMethodSignature:cbSignature];
[cbInvocation setSelector:callback];
[cbInvocation setTarget:self];
[cbInvocation setArgument:&invocation atIndex:2];
[cbInvocation setArgument:&target atIndex:3];
if (callerToRetain) {
[cbInvocation setArgument:&callerToRetain atIndex:4];
}
CFRetain(invocation);
// Used to pass in a request that we must retain until after the call
// We're using CFRetain rather than [callerToRetain retain] so things to avoid earthquakes when using garbage collection
if (callerToRetain) {
CFRetain(callerToRetain);
}
[cbInvocation performSelectorOnMainThread:#selector(invoke) withObject:nil waitUntilDone:[NSThread isMainThread]];
}}
Please help me out.
An NSInvocation by default does not retain or copy given arguments for efficiency, so each object passed as argument must still live when the invocation is invoked. That means the pointers passed to -setArgument:atIndex: are handled as __unsafe_unretained.
If you use NSInvocation in ARC, the simplest way to get it working is
call [invocation retainArguments] after creating the invocation object. That means the invocation will retain the given arguments.
When passing the arguments, cast them to __unsafe_unretained.
There is no step 3.

Resources