For anyone reading this, I found out that the key to using the javascript
functions in OrientDB is that the object you retrieve from either the graph
or document databases, are in fact the same object as you would get in the
Java API.
So the Java API can be used as reference for writing javascript functions.
The current function I'm working with is as follows:
Arguments className, limit and skip.
var log = [];
if(className == undefined) return "ERROR: missing className";
if(limit == undefined) return "ERROR: missing limit";
if(skip == undefined) return "ERROR: missing skip";
limit = parseInt(limit);
skip = parseInt(skip);
if(limit <= 0 || limit > 1000) return "ERROR: limit out of bounds";
if(skip < 0) return "ERROR: skip out of bounds";
var graph = orient.getGraph();
var products = graph.command("sql", "select from "+className+" limit
"+limit+" skip "+skip);
for(var i=0;i<products.length;i++){
var product = products[i];
var record = product.getRecord();
var propertyKeys = product.getPropertyKeys();
var iterator = propertyKeys.iterator();
while(iterator.hasNext()){
var key = iterator.next();
var type = product.getRecord().fieldType(key);
if(type != null && type.getId() == 13){
if(product.getProperty(key) != null){
product.addEdge(key, product.getProperty(key));
}
product.removeProperty(key);
}else if(type != null && (type.getId() == 14 || type.getId() == 15)){
var list = product.getProperty(key);
var listIterator = list.iterator();
while(listIterator.hasNext()){
var linkTo = listIterator.next();
if(linkTo != null){
product.addEdge(key, linkTo);
}
}
product.removeProperty(key);
}
}
}
return log;
The log variable is not used in the current version, but it's a way I use
to give some feedback back from the script.
It is mostly working, though I'm still having an issue with some records,
where the links give a ODocument back instead of a Vertex, I'll need to
detect and fix that automatically.
The error I get: "java.lang.ClassCastException: Cannot cast
com.orientechnologies.orient.core.record.impl.ODocument to
com.tinkerpop.blueprints.Vertex"
Regards,
Sem
On Monday, October 26, 2015 at 11:03:19 PM UTC+1, Sem van der Wal wrote:
>
> Hi Luigi,
>
> Thanks for your reply, and I'm sorry to take so long to respond.
> Since the application I'm working on is both live and in active
> development, I've had to postpone the conversion of the database, but I am
> now working on that.
>
> In your last post you've mentioned using a simple js function to convert
> links to edges, and I've been trying this out with some code in the
> Functions tab of the OrientDB studio, but can't seem to grasp how to
> manipulate the database records using js exactly.
>
> Do you by any change have an example of such a script laying around which
> I could use as starting point to create my own conversion script?
>
> Also, the docs seem to be a little meager on the subject, or maybe I am
> missing some part of it, I found these two pages:
> http://orientdb.com/docs/2.1/Functions.html
> http://orientdb.com/docs/2.1/Javascript-Command.html
>
> Thanks,
>
> Sem
>
> On Wednesday, September 23, 2015 at 10:22:39 AM UTC+2, Luigi Dell'Aquila
> wrote:
>>
>> Hi Sem,
>>
>> Document and Graph APIs can be used together, so I suggest you to do it
>> step by step, starting from a small part of your domain and replacing links
>> with edges.
>> The migration can be made with a simple js function that scans a class
>> and creates edges based on links, my advice is to do it in batch commits of
>> 500-1000 elements
>>
>> Luigi
>>
>>
>> 2015-09-21 12:03 GMT+02:00 Sem van der Wal <[email protected]>:
>>
>>> Hi Luigi,
>>>
>>> Thank you for your response, I was afraid that might be the outcome :)
>>> I've started converting my application to the Graph api a while back and
>>> put it 'on hold' since it seems to be a lot of work to convert all my
>>> 'plain' object to the Vertex and Edge model which the Graph api uses.
>>>
>>> Do you have any advice on the best way to make the transition from
>>> Object to Graph api?
>>>
>>> Regards,
>>>
>>> Sem
>>>
>>> On Monday, September 21, 2015 at 11:52:42 AM UTC+2, Luigi Dell'Aquila
>>> wrote:
>>>>
>>>> Hi Sem,
>>>>
>>>> I strongly suggest you to switch to Graph API, it can manage your use
>>>> case in a much more efficient and flexible way.
>>>> Document API is lighter, but you have to manually manage "broken"
>>>> links, and as you can see sometimes it's a big trouble.
>>>>
>>>> Thanks
>>>>
>>>> Luigi
>>>>
>>>>
>>>> 2015-09-21 11:03 GMT+02:00 Sem van der Wal <[email protected]>:
>>>>
>>>>> Hi All,
>>>>>
>>>>> I'm having some trouble with the ObjectDatabase in combination with 1
>>>>> to N links / references to other objects.
>>>>> In a number of objects I'm using List<OtherObject> in order to keep
>>>>> references to related objects.
>>>>> This works perfectly for the most part, since I'm able to use the
>>>>> references in queries and quickly access these referenced objects from my
>>>>> application.
>>>>>
>>>>> However, when I delete an object from the database which is referenced
>>>>> in one of these lists, I'm starting to see a whole lot of these errors:
>>>>>
>>>>> The field 'Company.companies' has been declared as LINKLIST but
>>>>> contains a null record (probably a deleted record?))
>>>>>
>>>>>
>>>>> Deleting a single object can cause a chain of issues, because objects
>>>>> referencing the deleted object cannot be loaded, but also objects with
>>>>> references to an object with an reference to the deleted object, because
>>>>> the intermediate cannot be loaded, etc etc
>>>>>
>>>>> So I need a way to prevent these deleted object from causing these
>>>>> issues in my database.
>>>>>
>>>>>
>>>>> What I could do, is before deleting an object, search for all other
>>>>> objects referencing the record to be deleted, and removing all references.
>>>>>
>>>>> I think that would work, but I'm not sure if that's the best way to do
>>>>> it, I'm thinking that there is something wrong in my design which causes
>>>>> this to be such an issue.
>>>>>
>>>>>
>>>>> Also, I am aware that this issue could be solved by using the Graph
>>>>> interface, since links are two-way and automatically managed / updated in
>>>>> that case, maybe it's the best approach to switch to the Graph interface?
>>>>>
>>>>>
>>>>> I would very much like to hear what the best or commonly accepted way
>>>>> to approach this issue is.
>>>>>
>>>>>
>>>>> Thanks in advance for your replies!
>>>>>
>>>>>
>>>>> Regards,
>>>>>
>>>>>
>>>>> Sem
>>>>>
>>>>> --
>>>>>
>>>>> ---
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "OrientDB" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> --
>>>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "OrientDB" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
--
---
You received this message because you are subscribed to the Google Groups
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.