OrientDB Hooks are only triggers in the data set phrasing that empower inner occasions when every CRUD activities in the client applications. You can utilize snares to compose custom approval rules, to implement security, or to mastermind outside occasions like repeating against a social DBMS.
OrientDB upholds two sorts of snares −
Dynamic Hook − Triggers, which can be worked at class level or potentially Document level.
Java (Native) Hook − Triggers, which can be assembled utilizing Java classes.
Dynamic Hooks
Dynamic snares are more adaptable than Java snares, since they can be changed at runtime and can run per report if necessary, however are more slow than Java snares.
To execute snares against your reports, first permit your classes to broaden OTriggered base class. Afterward, characterize a custom property for the intrigued occasion. Following are the accessible occasions.
- onBeforeCreate − Called prior to making another record.
- onAfterCreate − Called subsequent to making another archive.
- onBeforeRead − Called prior to perusing a report.
- onAfterRead − Called subsequent to perusing a report.
- onBeforeUpdate − Called prior to refreshing a report.
- onAfterUpdate − Called subsequent to refreshing a report.
- onBeforeDelete − Called prior to erasing a report.
- onAfterDelete − Called subsequent to erasing an archive.
Dynamic Hooks can call −
- Capacities, written in SQL, Javascript or any language upheld by OrientDB and JVM.
- Java static techniques.
Class Level Hooks
Class level snares are characterized for all the reports that identify with a class. Following is a guide to set up a snare that demonstrations at class level against Invoice reports.
CREATE CLASS Invoice EXTENDS OTriggered
ALTER CLASS Invoice CUSTOM onAfterCreate = invoiceCreated
How about we make the capacity invoiceCreated in Javascript that prints in the worker reassure the receipt number made.
CREATE FUNCTION invoiceCreated "print('\\nInvoice created: ' + doc.field ('number'));"
LANGUAGE Javascript
Presently attempt the snare by making another Invoice record.
INSERT INTO Invoice CONTENT {number: 100, notes: 'This is a test}
In the event that this order is executed effectively, you will get the accompanying yield.
Invoice created: 100
Document Level Hook
You can characterize a unique activity just against at least one archives. To do this, permit your class to broaden OTriggered class.
For instance let us execute a trigger, as Javascript work, against an existent Profile class, for all the archives with property account = 'Premium'. The trigger will be called to forestall cancellation of reports.
ALTER CLASS Profile SUPERCLASS OTriggered UPDATE Profile
SET onBeforeDelete = 'preventDeletion' WHERE account = 'Premium'
How about we make the preventDeletion() Javascript work.
CREATE FUNCTION preventDeletion "throw new java.lang.RuntimeException('Cannot
delete Premium profile ' + doc)" LANGUAGE Javascript
And afterward test the snare by attempting to erase a 'Superior' account.
DELETE FROM #12:1
java.lang.RuntimeException: Cannot delete Premium profile
profile#12:1{onBeforeDelete:preventDeletion,account:Premium,name:Jill} v-1
(<Unknown source>#2) in <Unknown source> at line number 2
JAVA Hooks
One normal use case for OrientDB Hooks (triggers) is to oversee made and refreshed dates for any or all classes. For instance, you can set a CreatedDate field at whatever point a record is made and set an UpdatedDate field at whatever point a record is refreshed, and do it in a way where you actualize the rationale once at the data set layer and never need to stress over it again at the application layer.
Prior to making, you should download orientdb-core.jar record by visit the accompanying connection download OrientDB center. Furthermore, later duplicate that container record into the organizer where you need to store the Java source document.
Create Hook File
Make a Java record named HookTest.java, which will test the Hook instrument utilizing Java language.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import com.orientechnologies.orient.core.hook.ODocumentHookAbstract;
import com.orientechnologies.orient.core.hook.ORecordHook;
import com.orientechnologies.orient.core.hook.ORecordHookAbstract;
import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener;
import com.orientechnologies.orient.core.db.ODatabase;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
public class HookTest extends ODocumentHookAbstract implements ORecordHook {
public HookTest() {
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return DISTRIBUTED_EXECUTION_MODE.BOTH;
}
public RESULT onRecordBeforeCreate( ODocument iDocument ) {
System.out.println("Ran create hook");
return ORecordHook.RESULT.RECORD_NOT_CHANGED;
}
public RESULT onRecordBeforeUpdate( ODocument iDocument ) {
System.out.println("Ran update hook");
return ORecordHook.RESULT.RECORD_NOT_CHANGED;
}
}
The above example code prints the suitable remark each time you make or update a record of that class.
We should add one more snare document setCreatedUpdatedDates.java as follows −
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import com.orientechnologies.orient.core.hook.ODocumentHookAbstract;
import com.orientechnologies.orient.core.hook.ORecordHook;
import com.orientechnologies.orient.core.hook.ORecordHookAbstract;
import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener;
import com.orientechnologies.orient.core.db.ODatabase;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
public class setCreatedUpdatedDates extends ODocumentHookAbstract implements ORecordHook {
public setCreatedUpdatedDates() {
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return DISTRIBUTED_EXECUTION_MODE.BOTH;
}
public RESULT onRecordBeforeCreate( ODocument iDocument ) {
if ((iDocument.getClassName().charAt(0) == 't') || (iDocument.getClassName().charAt(0)=='r')) {
iDocument.field("CreatedDate", System.currentTimeMillis() / 1000l);
iDocument.field("UpdatedDate", System.currentTimeMillis() / 1000l);
return ORecordHook.RESULT.RECORD_CHANGED;
} else {
return ORecordHook.RESULT.RECORD_NOT_CHANGED;
}
}
public RESULT onRecordBeforeUpdate( ODocument iDocument ) {
if ((iDocument.getClassName().charAt(0) == 't') || (iDocument.getClassName().charAt(0)=='r')) {
iDocument.field("UpdatedDate", System.currentTimeMillis() / 1000l);
return ORecordHook.RESULT.RECORD_CHANGED;
} else {
return ORecordHook.RESULT.RECORD_NOT_CHANGED;
}
}
}
What the above code does is search for any class that begins with the letters 'r' or 't' and sets CreatedDate and UpdatedDate when the record gets made and sets just UpdatedDate each time the record gets refreshed.
Compile Java Hooks
Incorporate Java code by utilizing the accompanying order. Note: Keep the downloaded container document and these Java records into a similar envelope.
$ jar cf hooks-1.0-SNAPSHOT.jar *.java
Move Compiled Code to Where OrientDB Server Can Find It
You need to duplicate the completed .container record to the index where your OrientDB worker will search for them. This implies the './lib' envelope under your OrientDB Server root registry will resemble this −
$ cp hooks-1.0-SNAPSHOT.jar "$ORIENTDB_HOME/lib"
Empower Test Hook in the OrientDB Server Configuration File
Alter $ORIENTDB_HOME/config/orientdb-worker config.xml and add the accompanying segment close to the furthest limit of the document.
<hooks>
<hook class = "HookTest" position = "REGULAR"/>
</hooks>
...
</orient-server>
Restart OrientDB Server
When you restart OrientDB Server, the snare you characterized in orientdb-worker config.xml is presently dynamic. Dispatch an OrientDB reassure, interface it to your information base, and run the accompanying order −
INSERT INTO V SET ID = 1;
In the event that this order is executed effectively, you will get the accompanying yield.
Ran create hook
Presently run the accompanying order −
UPDATE V SET ID = 2 WHERE ID = 1;
In the event that this order is executed effectively, you will get the accompanying yield.
Ran update hook
Empower Real Hook in the OrientDB Server Configuration File
Alter $ORIENTDB_HOME/config/orientdb-worker config.xml and change the snares area as follows −
<hooks>
<hook class="setCreatedUpdatedDates" position="REGULAR"/>
</hooks>
...
</orient-server>
Restart OrientDB Server
Make another class that begins with the letter 'r' or 't' −
CREATE CLASS tTest EXTENDS V;
Presently embed a record −
UPDATE tTest SET ID = 2 WHERE ID = 1;
SELECT FROM tTest;
On the off chance that this order is executed effectively, you will get the accompanying yield.
----+-----+------+----+-----------+-----------
# |@RID |@CLASS|ID |CreatedDate|UpdatedDate
----+-----+------+----+-----------+-----------
0 |#19:0|tTest |2 |1427597275 |1427597306
----+-----+------+----+-----------+-----------
Despite the fact that you didn't indicate qualities to set for CreatedDate and UpdatedDate, OrientDB has set these fields naturally for you.
UPDATE tTest SET ID = 2 WHERE ID = 1;
SELECT FROM tTest;
On the off chance that this order is executed effectively, you will get the accompanying yield.
----+-----+------+----+-----------+-----------
# |@RID |@CLASS|ID |CreatedDate|UpdatedDate
----+-----+------+----+-----------+-----------
0 |#19:0|tTest |2 |1427597275 |1427597306
----+-----+------+----+-----------+-----------
You can see that OrientDB has changed the UpdatedDate yet has allowed the CreatedDate to stay unaltered.
OrientDB Java Hooks can be an incredibly significant device to help computerize work you would some way or another need to do in application code. As numerous DBAs are not generally Java specialists, ideally the data contained in this instructional exercise will give you a head start and cause you to feel great with the innovation, enabling you to effectively make data set triggers as the need emerges.