Functions in OrientDB Studio - correct syntax

182 views
Skip to first unread message

Joao Barcia

unread,
May 25, 2015, 1:21:30 PM5/25/15
to orient-...@googlegroups.com
Hi everyone,

First of all, thanks for the great work on OrientDB! We've been really amazed with it. The only major roadblock we have found is functions.

We've spent some time trying to implement simple functions in the studio and the lack of documentation mixed with seemingly changing syntax makes things quite difficult. i.e.: orient.getGraph() function seems to work on 2.0.3 but not on 2.0.9 or 2.1-rc2.

Here is the code we have so far running on 2.0.3:
"
var mydb = orient.getGraph();

var nearbyNodes = mydb.command('sql',"select in('Contributes_To') from " +originNode);

return nearbyNodes;
"

I would like to iterate through each of the nodes returned from the query. Inside of each node I would like to apply a javascript function "propagation()" to the parameter "activation" from that node and change it accordingly.

So,

QUESTION 1 - Is there an updated and reliable documentation source for JavaScript Functions in OrientDB?
QUESTION 2 - What is the correct way of doing an SQL command for newer versions (2.0.9 and 2.1-rc2)? The example above does not seem to work in new versions.
QUESTION 3 - What is the correct way of handling the returned objects? As it was explained here does not seem to work

Thank you very much


Cheers,
Joao

Ziink A

unread,
May 25, 2015, 2:37:47 PM5/25/15
to orient-...@googlegroups.com
Answer 1: Nope. No good docs.
Answer 2: The following worked in 2.1 rc3
orient.getGraph().command( "sql", "update Item set l_tags = '' where @rid in ?", [tagged] );

So you could try
var nearbyNodes = mydb.command('sql',"select in('Contributes_To') from " +originNode, []);

Notice the the empty array. Maybe it's a required parameter.

Answer 3:
Check out 
https://github.com/orientechnologies/orientdb/issues/2390

Here's what I'm using (from above link without some logging code).
toJSObject = """
// This function should return a javascript object when you give it java object like List, Map, OResultSet etc. //
//function toJSObject(obj) {
function mapToJSObject(m) {
var ret = {};
// log("" + m + " IS A MAP (" + m.getClass() + ")");
var it = m.keySet().iterator();
while (it.hasNext()) {
var key = it.next();
ret[key] = toJSObject(m.get(key));
}

return ret;
};

function collectionToJSObject(c) {
var ret = [];
// log("" + c + " IS A COLLECTION (" + c.getClass() + ")");
var it = c.iterator();
while (it.hasNext()) {
ret.push(toJSObject(it.next()));
}

return ret;
};

if (obj == null) {
return "";
} else if (typeof obj == 'object') {
if (obj.getClass) {
//it's a java object !!!
var objClass = obj.getClass();
if (objClass == "class java.lang.String") {
return new String(obj);
} else if (objClass == "class java.lang.Integer" || objClass == "class java.lang.Long") {
return parseInt(obj);
} else if (objClass == "class java.lang.Float" || objClass == "class java.lang.Double") {
return parseFloat(obj);
} else if (obj.iterator) {
//Java COLLECTION (example ArrayList)
return collectionToJSObject(obj);
} else if (obj.keySet) {
//it's a Map (class java.util.HashMap for example)
return mapToJSObject(obj);
} else if (obj.getKey) {
// class java.util.LinkedHashMap
var ret = {};
ret[obj.getKey()] = toJSObject(obj.getValue());
return ret;
} else {
var ret = "[" + obj.getClass() + " Fields] ";
for (var key in obj) {
ret += key + ", ";
}
return ret;
}
} else {
var ret = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
ret[key] = toJSObject(obj[key]);
}
}
return ret;
}
} else { //return as-is
return obj;
}
//}
"""


Other tips.
1. Hook functions provide the 'doc' variable that you can use. For example.
var val = doc.field('propName')

2. See the following file for other methods provided by 'doc'
core/src/main/java/com/orientechnologies/orient/core/record/impl/ODocument.java 

3. If you are making changes in the hook function, be sure to 
return 'RECORD_CHANGED';

4. Where java function requires a vararg (...), provide an array, even for single values. For example, to save an array to a field.
var OType = com.orientechnologies.orient.core.metadata.schema.OType
doc.field('prop1', arrayValue, [OType.EMBEDDEDSET])



Joao Barcia

unread,
May 26, 2015, 10:50:09 AM5/26/15
to orient-...@googlegroups.com
Hi Ziink,

Thanks for the answers! It's a bit strange that something like this is so complex to code.

I have managed to do the query we want with:
var mydb = orient.getGraph();

var nearbyNodes = mydb.command('sql',"select in('Contributes_To') from " +originNode, []);

var nearbyNodesID = nearbyNodes[0].getRecord().field('in');

What is the best way of now applying a mathematical javascript function to a parameter from the given nodes? What we want is something like:
  1. Iterate through the child nodes of a select originNode
  2. For each one of these nodes:
    • apply a propagation() function to the node parameter "activation"
    • update the value of that parameter in the database
We are trying to do it by:
  1. Making a query for the child nodes of originNode and assign it to nearbyNodes
  2. Extracting the @rid's from the returned object and assigning them to a variable nearbyNodesID
  3. Update the parameter "activation" from every node with an @rid present in nearbyNodesID with a value returned from our propagation() function

Does this make sense? Do you have any other pointers?

If I try to use your function toJSObject for nearbyNodes, I get an error. If I use for nearbyNodesID I get JSON Object with some wrong parameters:
[
   
{
       
"@type": "d",
       
"@version": 0,
       
"value": [
           
[
               
{
                   
"name": "artigos indefinidos"
               
},
               
{
                   
"in_Contributes_To": [
                       
[
                           
{
                               
"out": "[class com.orientechnologies.orient.core.id.ORecordId Fields] valid, clusterPosition, next, temporary, hashCode, fromStream, wait, fromString, toStream, identity, isPersistent, notify, reset, copyFrom, getIdentity, notifyAll, copy, getClass, isTemporary, unlock, equals, isNew, compare, getClusterPosition, getClusterId, lock, class, compareTo, record, new, persistent, nextRid, toString, clusterId, isValid, getRecord, "
                           
},
                           
{
                               
"in": "[class com.orientechnologies.orient.core.id.ORecordId Fields] valid, clusterPosition, next, temporary, hashCode, fromStream, wait, fromString, toStream, identity, isPersistent, notify, reset, copyFrom, getIdentity, notifyAll, copy, getClass, isTemporary, unlock, equals, isNew, compare, getClusterPosition, getClusterId, lock, class, compareTo, record, new, persistent, nextRid, toString, clusterId, isValid, getRecord, "
                           
}
                       
],
                       
[
                           
{
                               
"out": "[class com.orientechnologies.orient.core.id.ORecordId Fields] valid, clusterPosition, next, temporary, hashCode, fromStream, wait, fromString, toStream, identity, isPersistent, notify, reset, copyFrom, getIdentity, notifyAll, copy, getClass, isTemporary, unlock, equals, isNew, compare, getClusterPosition, getClusterId, lock, class, compareTo, record, new, persistent, nextRid, toString, clusterId, isValid, getRecord, "
                           
},
                           
{
                               
"in": "[class com.orientechnologies.orient.core.id.ORecordId Fields] valid, clusterPosition, next, temporary, hashCode, fromStream, wait, fromString, toStream, identity, isPersistent, notify, reset, copyFrom, getIdentity, notifyAll, copy, getClass, isTemporary, unlock, equals, isNew, compare, getClusterPosition, getClusterId, lock, class, compareTo, record, new, persistent, nextRid, toString, clusterId, isValid, getRecord, "
                           
}
                       
]
                   
]
               
},
               
{
                   
"out_Contributes_To": [
                       
[
                           
{
                               
"out": "[class com.orientechnologies.orient.core.id.ORecordId Fields] valid, clusterPosition, next, temporary, hashCode, fromStream, wait, fromString, toStream, identity, isPersistent, notify, reset, copyFrom, getIdentity, notifyAll, copy, getClass, isTemporary, unlock, equals, isNew, compare, getClusterPosition, getClusterId, lock, class, compareTo, record, new, persistent, nextRid, toString, clusterId, isValid, getRecord, "
                           
},
                           
{
                               
"in": "[class com.orientechnologies.orient.core.id.ORecordId Fields] valid, clusterPosition, next, temporary, hashCode, fromStream, wait, fromString, toStream, identity, isPersistent, notify, reset, copyFrom, getIdentity, notifyAll, copy, getClass, isTemporary, unlock, equals, isNew, compare, getClusterPosition, getClusterId, lock, class, compareTo, record, new, persistent, nextRid, toString, clusterId, isValid, getRecord, "
                           
}
                       
]
                   
]
               
},
               
{
                   
"activation": 0
               
}
           
],
            ....
etc.

Am I using the function incorrectlyy?

Thanks
Reply all
Reply to author
Forward
0 new messages