Pizza Connections

IT Technology Blog

Menu Close

OrientDB functions by samples – 1

It's time to share...Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Pin on Pinterest

In this post we will see in details the OrientDB functions. OrientDB functions allow to store the business logic directly in the database similarly to RDBMS stored procedures. Functions can be written in SQL, Javascript or Groovy language but in this post I’ll use Javascript only. I’ve been using the Functions tab of OrientDB Studio to develop all functions in this post.

Functions tab

Hello world function

Create or choose your database then go to the Functions tab. Then type helloWorld into name text input field and choose javascript language.

This is the code of your first function :

return "Hello world";

Now save the function and execute it.

Hello world function

 

Database class

To explore the database class you can write a simple function, orientClass, without parameter.

return orient.getClass();

The result of the function execution is:

[
    {
        "@type": "d",
        "@version": 0,
        "value": "class com.orientechnologies.orient.graph.script.OScriptGraphOrientWrapper"
    }
]

As you see, the functions is referencing a Java class directly. The java class that the orient variable wraps is OScriptGraphOrientWrapper. Thus, in case you want to know more about its methods you can invoke just view the OScriptGraphOrientWrapper javadoc.

Use the getClass method whenever you need to know which class wraps a javascript variable.

Vertex and transactions

Now let’s create our first vertex.

The function createPerson has two parameters that can be added using the parameter button.

var mydb = orient.getGraph();

//check if exists Person class
//If the Person class does not exist I create it
if(mydb.getVertexType('Person')==null){
 mydb.createVertexType('Person'); 
}

//The var v wraps OrientVertex class
var v=mydb.addVertex('class:Person');

v.setProperty('name',name);
v.setProperty('surname',surname);

//Commit
mydb.commit();

return v;

This function creates a Person vertex. The Person class is created the first time the function is invoked.

3

The result is :

[
    {
        "@type": "d",
        "@rid": "#12:0",
        "@version": 1,
        "@class": "Person",
        "name": "Gianpaolo",
        "surname": "Altamura"
    }
]

But what about transactions? What happens if you omit the sixteenth line (mydb.commit) what happens?

The default behavior is that the transactions is committed and the record is stored but you get this result:

[
    {
        "@type": "d",
        "@rid": "#12:-2",
        "@version": 1,
        "@class": "Person",
        "name": "Gianpaolo",
        "surname": "Altamura"
    }
]

Note that @rid is equal to # 12: -2 but the record has @rid equal to # 12: 0. Then use the commit.

Now we are going to create a rollback function as a variation of the previous one, but with the same input parameters:

var mydb = orient.getGraph();

if(mydb.getVertexType('Person')==null){
  mydb.createVertexType('Person'); 
}
var v=mydb.addVertex('class:Person');

v.setProperty('name',name);
v.setProperty('surname',surname);

var v1=mydb.addVertex('class:Person');

v1.setProperty('name',name+'1');
v1.setProperty('surname',surname+'1');


mydb.rollback();

return v;

The result is that no record is stored because the rollback statement has prevented the insertion of data.

Modify the function code as shown below :

//TRANSACTIONS NOT ENABLED
var mydb = orient.getGraphNoTx();

if(mydb.getVertexType('Person')==null){
  mydb.createVertexType('Person'); 
}
var v=mydb.addVertex('class:Person');

v.setProperty('name',name);
v.setProperty('surname',surname);

var v1=mydb.addVertex('class:Person');

v1.setProperty('name',name+'1');
v1.setProperty('surname',surname+'1');


mydb.rollback();

return v;

By running this function we’ll now create  two records because the getGraphNoTx() disables the transaction system.

Query

Now let’s create functions to search data.

This first function (queryPerson) gets all records of the Person class. No parameters are required.

var mydb = orient.getGraph();

var result=mydb.getVerticesOfClass('Person');

if(result!=null){
  result=result.iterator();
}
return result;

The result var is an Iterator<OrientVertex> then you can use all methods of iterator class (next(), hasNext(), etc…).

Now modify the function to execute a sql query

var mydb = orient.getGraph();

try{
  var results = mydb.command('sql','select from Person');
}catch(err){
  return err;  
}

return results;

This version of the function returns an array of OrientVertex.

Functions as Rest services

As described in the OrientDB documentation, each function is exposed as a REST service allowing the receiving of parameters. Parameters are passed by position.

Now we can try to call the createPerson function with Chrome app “Postman – REST Client”. The result is shown in the next image.

4

 

It's time to share...Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Pin on Pinterest

© 2017 Pizza Connections. All rights reserved.

Theme by Anders Norén.