New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
atomic key-value commands #1664
Comments
Unless I'm missing something, this can easily be done using AQL:
What's your concern? Performance? Flexibility? I guess an AQL function along the lines of ...
... could be useful. |
@pluma interesting. I guess your HINCR example could be realized with a user defined AQL function. Still... ;) |
Based on my example the implementation of HINCR (or MongoDB-style function HINCR(obj, diff) {
for (const key of Object.keys(diff)) {
if (!obj[key]) {
obj[key] = diff[key];
} else {
obj[key] += diff[key];
}
}
return obj;
} I'm not sure what the performance implications of this are and whether it would be as easy to implement natively. @jsteemann ? |
@mchacki has been doing all those javascript to c++ conversions. |
FYI: this is what I settled on function HINCR(obj, key) {
let result = {};
result[key] = (obj[key] || 0) + 1;
return result;
} which is sufficient when doing an Anyways, my main point is that all these actions make Redis useful (http://try.redis.io) and that great artists steal ;) |
You can actually use const in a for-of like I did. Its scope is limited to each iteration, it's not reassigned. |
@pluma: Do you know what difference it makes to V8? (var vs. let vs. const vs.) I wonder why |
vs.
|
@pekeler only outside strict-mode. Prefix the line with // execute each line individually in a fresh node shell
// to avoid raising a TypeError when re-declaring const
'use strict'; console.log(typeof v); var v = 5; // logs "undefined"
'use strict'; console.log(typeof c); const c = 5; // throws ReferenceError
'use strict'; console.log(typeof x); let x = 5; // throws ReferenceError
console.log(typeof v); var v = 5; // logs "undefined"
console.log(typeof c); const c = 5; // logs "undefined"
console.log(typeof x); let x = 5; // throws ReferenceError @CoDEmanX some people have argued that it could be optimized but even if it were the difference would likely be insignificant. It's mostly about signalling intent: this variable will not be reassigned throughout this block of code. The reason Object.keys is required is that objects aren't dictionaries. Maps are dictionaries. And maps provide three generators: keys, values and entries (pairs of keys and values). Because Object.keys preceded the new collection types it will continue to return an array instead of a generator (and arrays can be treated as generators, i.e. used in for-of loops). The additional functions Object.values and Object.entries are currently in stage 3 and will likely land in ES2016 or ES2017 but they will behave like Object.keys (i.e. return arrays) rather than like the Map or Set equivalents (which is okay: they're also functions on the Object constructor like Object.keys rather than methods like the Map and Set equivalents). If any of you want to discuss this further I would suggest taking it to the mailing list or IRC. Let's keep the issue focussed on its original topic: atomic key-value commands, not JS syntax. 😄 EDIT: Another more direct proof that const outside strict mode uses function scope rather than block scope: // again, remember to run each line a fresh node shell
'use strict'; {const c = 5;} console.log(c); // throws ReferenceError
{const c = 5;} console.log(c); // logs 5 |
Thanks @pluma, I wasn't aware of the differences between objects and maps (complete result array vs. iterator returned). BTW: AQL is mostly a declarative language. Mostly, because there are also many functions you can call. If I'm not mistaken, that makes it also a functional programming language. @pekeler's example of LET doc = DOCUMENT("coll/1234")
UPDATE doc WITH {phoneCalls: INCREMENT(OLD.phoneCalls)} It would be more convenient, if something like the following was allowed: UPDATE "coll/1234" INCREMENT OLD.phoneCalls Or even: INCREMENT DOCUMENT("coll/1234").phoneCalls Note the change of
As function, the signature with support for both aforementioned options could look like |
I have a document that has a hash as one if its values. The values of that hash are numbers and I need to increment them from remote. Now I wish ArangoDB had Redis'
HINCRBY
and I could use it from the arangojs adapter.That's just one example. Another example for a command I could use right now would be
LPUSH
to insert a new value at the beginning of an array in a document.I think it would be awesome if there were equivalents for almost all of these in ArangoDB and if they were usable from the ReST api, local JavaScript, and maybe even straight in AQL.
http://redis.io/commands#hash
http://redis.io/commands#generic
http://redis.io/commands#list
http://redis.io/commands#set
http://redis.io/commands#sorted_set
http://redis.io/commands#string
The text was updated successfully, but these errors were encountered: