Search in sources :

Example 16 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OSchedulerTest method eventBySQL.

@Test(enabled = false)
public void eventBySQL() throws Exception {
    final ODatabaseDocumentTx db = initDatabase();
    try {
        OFunction func = createFunction(db);
        // CREATE NEW EVENT
        db.command(new OCommandSQL("insert into oschedule set name = 'test', function = ?, rule = \"0/1 * * * * ?\"")).execute(func.getId());
        Thread.sleep(4500);
        long count = getLogCounter(db);
        Assert.assertTrue(count >= 4);
        // UPDATE
        db.command(new OCommandSQL("update oschedule set rule = \"0/2 * * * * ?\" where name = 'test'")).execute(func.getId());
        Thread.sleep(4000);
        long newCount = getLogCounter(db);
        Assert.assertTrue(newCount - count > 1);
        Assert.assertTrue(newCount - count <= 2);
        // DELETE
        db.command(new OCommandSQL("delete from oschedule where name = 'test'")).execute(func.getId());
        Thread.sleep(3000);
        count = newCount;
        newCount = getLogCounter(db);
        Assert.assertTrue(newCount - count <= 1);
    } finally {
        db.drop();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction) Test(org.testng.annotations.Test)

Example 17 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OFunctionSqlTest method functionSqlWithInnerFunctionJs.

@Test
public void functionSqlWithInnerFunctionJs() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:functionSqlWithInnerFunctionJs");
    db.create();
    // ODatabaseRecordThreadLocal.INSTANCE.set(db);
    ODocument doc1 = new ODocument("Test");
    doc1.field("name", "Enrico");
    db.save(doc1);
    doc1.reset();
    doc1.setClassName("Test");
    doc1.field("name", "Luca");
    db.save(doc1);
    OFunction function = new OFunction();
    function.setName("test");
    function.setCode("select name from Test where name = :name and hello(:name) = 'Hello Enrico'");
    function.setParameters(new ArrayList<String>() {

        {
            add("name");
        }
    });
    function.save();
    OFunction function1 = new OFunction();
    function1.setName("hello");
    function1.setLanguage("javascript");
    function1.setCode("return 'Hello ' + name");
    function1.setParameters(new ArrayList<String>() {

        {
            add("name");
        }
    });
    function1.save();
    Object result = function.executeInContext(new OBasicCommandContext(), "Enrico");
    System.out.println(result);
    Assert.assertEquals(((OResultSet) result).size(), 1);
    db.drop();
}
Also used : OBasicCommandContext(com.orientechnologies.orient.core.command.OBasicCommandContext) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction) Test(org.testng.annotations.Test)

Example 18 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OSQLFunctionAstarTest method setUpDatabase.

private void setUpDatabase() {
    dbCounter++;
    graph = new OrientGraph("memory:OSQLFunctionAstarTest" + dbCounter);
    graph.createEdgeType("has_path");
    OFunction cf = graph.getRawGraph().getMetadata().getFunctionLibrary().createFunction("myCustomHeuristic");
    cf.setCode("return 1;");
    v0 = graph.addVertex(null);
    v1 = graph.addVertex(null);
    v2 = graph.addVertex(null);
    v3 = graph.addVertex(null);
    v4 = graph.addVertex(null);
    v5 = graph.addVertex(null);
    v6 = graph.addVertex(null);
    // Tabriz
    v0.setProperty("node_id", "Z");
    v0.setProperty("name", "Tabriz");
    v0.setProperty("lat", 31.746512f);
    v0.setProperty("lon", 51.427002f);
    v0.setProperty("alt", 2200);
    // Tehran
    v1.setProperty("node_id", "A");
    v1.setProperty("name", "Tehran");
    v1.setProperty("lat", 35.746512f);
    v1.setProperty("lon", 51.427002f);
    v1.setProperty("alt", 1800);
    // Mecca
    v2.setProperty("node_id", "B");
    v2.setProperty("name", "Mecca");
    v2.setProperty("lat", 21.371244f);
    v2.setProperty("lon", 39.847412f);
    v2.setProperty("alt", 1500);
    // Bejin
    v3.setProperty("node_id", "C");
    v3.setProperty("name", "Bejin");
    v3.setProperty("lat", 39.904041f);
    v3.setProperty("lon", 116.408011f);
    v3.setProperty("alt", 1200);
    // London
    v4.setProperty("node_id", "D");
    v4.setProperty("name", "London");
    v4.setProperty("lat", 51.495065f);
    v4.setProperty("lon", -0.120850f);
    v4.setProperty("alt", 900);
    // NewYork
    v5.setProperty("node_id", "E");
    v5.setProperty("name", "NewYork");
    v5.setProperty("lat", 42.779275f);
    v5.setProperty("lon", -74.641113f);
    v5.setProperty("alt", 1700);
    // Los Angles
    v6.setProperty("node_id", "F");
    v6.setProperty("name", "Los Angles");
    v6.setProperty("lat", 34.052234f);
    v6.setProperty("lon", -118.243685f);
    v6.setProperty("alt", 400);
    Edge e1 = graph.addEdge(null, v1, v2, "has_path");
    e1.setProperty("weight", 250.0f);
    e1.setProperty("ptype", "road");
    Edge e2 = graph.addEdge(null, v2, v3, "has_path");
    e2.setProperty("weight", 250.0f);
    e2.setProperty("ptype", "road");
    Edge e3 = graph.addEdge(null, v1, v3, "has_path");
    e3.setProperty("weight", 1000.0f);
    e3.setProperty("ptype", "road");
    Edge e4 = graph.addEdge(null, v3, v4, "has_path");
    e4.setProperty("weight", 250.0f);
    e4.setProperty("ptype", "road");
    Edge e5 = graph.addEdge(null, v2, v4, "has_path");
    e5.setProperty("weight", 600.0f);
    e5.setProperty("ptype", "road");
    Edge e6 = graph.addEdge(null, v4, v5, "has_path");
    e6.setProperty("weight", 400.0f);
    e6.setProperty("ptype", "road");
    Edge e7 = graph.addEdge(null, v5, v6, "has_path");
    e7.setProperty("weight", 300.0f);
    e7.setProperty("ptype", "road");
    Edge e8 = graph.addEdge(null, v3, v6, "has_path");
    e8.setProperty("weight", 200.0f);
    e8.setProperty("ptype", "road");
    Edge e9 = graph.addEdge(null, v4, v6, "has_path");
    e9.setProperty("weight", 900.0f);
    e9.setProperty("ptype", "road");
    Edge e10 = graph.addEdge(null, v2, v6, "has_path");
    e10.setProperty("weight", 2500.0f);
    e10.setProperty("ptype", "road");
    Edge e11 = graph.addEdge(null, v1, v5, "has_path");
    e11.setProperty("weight", 100.0f);
    e11.setProperty("ptype", "road");
    Edge e12 = graph.addEdge(null, v4, v1, "has_path");
    e12.setProperty("weight", 200.0f);
    e12.setProperty("ptype", "road");
    Edge e13 = graph.addEdge(null, v5, v3, "has_path");
    e13.setProperty("weight", 800.0f);
    e13.setProperty("ptype", "road");
    Edge e14 = graph.addEdge(null, v5, v2, "has_path");
    e14.setProperty("weight", 500.0f);
    e14.setProperty("ptype", "road");
    Edge e15 = graph.addEdge(null, v6, v5, "has_path");
    e15.setProperty("weight", 250.0f);
    e15.setProperty("ptype", "road");
    Edge e16 = graph.addEdge(null, v3, v1, "has_path");
    e16.setProperty("weight", 550.0f);
    e16.setProperty("ptype", "road");
    graph.commit();
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) Edge(com.tinkerpop.blueprints.Edge) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Aggregations

OFunction (com.orientechnologies.orient.core.metadata.function.OFunction)18 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)4 OBasicCommandContext (com.orientechnologies.orient.core.command.OBasicCommandContext)3 Test (org.testng.annotations.Test)3 OException (com.orientechnologies.common.exception.OException)2 OCommandScriptException (com.orientechnologies.orient.core.command.script.OCommandScriptException)2 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)2 ArrayList (java.util.ArrayList)2 OPartitionedObjectPool (com.orientechnologies.common.concur.resource.OPartitionedObjectPool)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OFunctionLibrary (com.orientechnologies.orient.core.metadata.function.OFunctionLibrary)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)1