Search in sources :

Example 1 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project titan by thinkaurelius.

the class TitanGraphTest method simpleLogTest.

public void simpleLogTest(final boolean withLogFailure) throws InterruptedException {
    final String userlogName = "test";
    final Serializer serializer = graph.getDataSerializer();
    final EdgeSerializer edgeSerializer = graph.getEdgeSerializer();
    final TimestampProvider times = graph.getConfiguration().getTimestampProvider();
    final Instant startTime = times.getTime();
    clopen(option(SYSTEM_LOG_TRANSACTIONS), true, option(LOG_BACKEND, USER_LOG), (withLogFailure ? TestMockLog.class.getName() : LOG_BACKEND.getDefaultValue()), option(TestMockLog.LOG_MOCK_FAILADD, USER_LOG), withLogFailure, option(KCVSLog.LOG_READ_LAG_TIME, USER_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, USER_LOG), Duration.ofMillis(250), option(LOG_SEND_DELAY, USER_LOG), Duration.ofMillis(100), option(KCVSLog.LOG_READ_LAG_TIME, TRANSACTION_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, TRANSACTION_LOG), Duration.ofMillis(250), option(MAX_COMMIT_TIME), Duration.ofSeconds(1));
    final String instanceid = graph.getConfiguration().getUniqueGraphId();
    PropertyKey weight = tx.makePropertyKey("weight").dataType(Float.class).cardinality(Cardinality.SINGLE).make();
    EdgeLabel knows = tx.makeEdgeLabel("knows").make();
    TitanVertex n1 = tx.addVertex("weight", 10.5);
    newTx();
    final Instant[] txTimes = new Instant[4];
    //Transaction with custom userlog name
    txTimes[0] = times.getTime();
    TitanTransaction tx2 = graph.buildTransaction().logIdentifier(userlogName).start();
    TitanVertex v1 = tx2.addVertex("weight", 111.1);
    v1.addEdge("knows", v1);
    tx2.commit();
    final long v1id = getId(v1);
    txTimes[1] = times.getTime();
    tx2 = graph.buildTransaction().logIdentifier(userlogName).start();
    TitanVertex v2 = tx2.addVertex("weight", 222.2);
    v2.addEdge("knows", getV(tx2, v1id));
    tx2.commit();
    final long v2id = getId(v2);
    //Only read tx
    tx2 = graph.buildTransaction().logIdentifier(userlogName).start();
    v1 = getV(tx2, v1id);
    assertEquals(111.1, v1.<Float>value("weight").doubleValue(), 0.01);
    assertEquals(222.2, getV(tx2, v2).<Float>value("weight").doubleValue(), 0.01);
    tx2.commit();
    //Deleting transaction
    txTimes[2] = times.getTime();
    tx2 = graph.buildTransaction().logIdentifier(userlogName).start();
    v2 = getV(tx2, v2id);
    assertEquals(222.2, v2.<Float>value("weight").doubleValue(), 0.01);
    v2.remove();
    tx2.commit();
    //Edge modifying transaction
    txTimes[3] = times.getTime();
    tx2 = graph.buildTransaction().logIdentifier(userlogName).start();
    v1 = getV(tx2, v1id);
    assertEquals(111.1, v1.<Float>value("weight").doubleValue(), 0.01);
    Edge e = getOnlyElement(v1.query().direction(Direction.OUT).labels("knows").edges());
    assertFalse(e.property("weight").isPresent());
    e.property("weight", 44.4);
    tx2.commit();
    close();
    final Instant endTime = times.getTime();
    final ReadMarker startMarker = ReadMarker.fromTime(startTime);
    Log txlog = openTxLog();
    Log userLog = openUserLog(userlogName);
    final EnumMap<LogTxStatus, AtomicInteger> txMsgCounter = new EnumMap<LogTxStatus, AtomicInteger>(LogTxStatus.class);
    for (LogTxStatus status : LogTxStatus.values()) txMsgCounter.put(status, new AtomicInteger(0));
    final AtomicInteger userlogMeta = new AtomicInteger(0);
    txlog.registerReader(startMarker, new MessageReader() {

        @Override
        public void read(Message message) {
            Instant msgTime = message.getTimestamp();
            assertTrue(msgTime.isAfter(startTime) || msgTime.equals(startTime));
            assertNotNull(message.getSenderId());
            TransactionLogHeader.Entry txEntry = TransactionLogHeader.parse(message.getContent(), serializer, times);
            TransactionLogHeader header = txEntry.getHeader();
            //                System.out.println(header.getTimestamp(TimeUnit.MILLISECONDS));
            assertTrue(header.getTimestamp().isAfter(startTime) || header.getTimestamp().equals(startTime));
            assertTrue(header.getTimestamp().isBefore(msgTime) || header.getTimestamp().equals(msgTime));
            assertNotNull(txEntry.getMetadata());
            assertNull(txEntry.getMetadata().get(LogTxMeta.GROUPNAME));
            LogTxStatus status = txEntry.getStatus();
            if (status == LogTxStatus.PRECOMMIT) {
                assertTrue(txEntry.hasContent());
                Object logid = txEntry.getMetadata().get(LogTxMeta.LOG_ID);
                if (logid != null) {
                    assertTrue(logid instanceof String);
                    assertEquals(userlogName, logid);
                    userlogMeta.incrementAndGet();
                }
            } else if (withLogFailure) {
                assertTrue(status.isPrimarySuccess() || status == LogTxStatus.SECONDARY_FAILURE);
                if (status == LogTxStatus.SECONDARY_FAILURE) {
                    TransactionLogHeader.SecondaryFailures secFail = txEntry.getContentAsSecondaryFailures(serializer);
                    assertTrue(secFail.failedIndexes.isEmpty());
                    assertTrue(secFail.userLogFailure);
                }
            } else {
                assertFalse(txEntry.hasContent());
                assertTrue(status.isSuccess());
            }
            txMsgCounter.get(txEntry.getStatus()).incrementAndGet();
        }
    });
    final EnumMap<Change, AtomicInteger> userChangeCounter = new EnumMap<Change, AtomicInteger>(Change.class);
    for (Change change : Change.values()) userChangeCounter.put(change, new AtomicInteger(0));
    final AtomicInteger userLogMsgCounter = new AtomicInteger(0);
    userLog.registerReader(startMarker, new MessageReader() {

        @Override
        public void read(Message message) {
            Instant msgTime = message.getTimestamp();
            assertTrue(msgTime.isAfter(startTime) || msgTime.equals(startTime));
            assertNotNull(message.getSenderId());
            StaticBuffer content = message.getContent();
            assertTrue(content != null && content.length() > 0);
            TransactionLogHeader.Entry txentry = TransactionLogHeader.parse(content, serializer, times);
            Instant txTime = txentry.getHeader().getTimestamp();
            assertTrue(txTime.isBefore(msgTime) || txTime.equals(msgTime));
            assertTrue(txTime.isAfter(startTime) || txTime.equals(msgTime));
            long txid = txentry.getHeader().getId();
            assertTrue(txid > 0);
            for (TransactionLogHeader.Modification modification : txentry.getContentAsModifications(serializer)) {
                assertTrue(modification.state == Change.ADDED || modification.state == Change.REMOVED);
                userChangeCounter.get(modification.state).incrementAndGet();
            }
            userLogMsgCounter.incrementAndGet();
        }
    });
    Thread.sleep(4000);
    assertEquals(5, txMsgCounter.get(LogTxStatus.PRECOMMIT).get());
    assertEquals(4, txMsgCounter.get(LogTxStatus.PRIMARY_SUCCESS).get());
    assertEquals(1, txMsgCounter.get(LogTxStatus.COMPLETE_SUCCESS).get());
    assertEquals(4, userlogMeta.get());
    if (withLogFailure)
        assertEquals(4, txMsgCounter.get(LogTxStatus.SECONDARY_FAILURE).get());
    else
        assertEquals(4, txMsgCounter.get(LogTxStatus.SECONDARY_SUCCESS).get());
    //User-Log
    if (withLogFailure) {
        assertEquals(0, userLogMsgCounter.get());
    } else {
        assertEquals(4, userLogMsgCounter.get());
        assertEquals(7, userChangeCounter.get(Change.ADDED).get());
        assertEquals(4, userChangeCounter.get(Change.REMOVED).get());
    }
    clopen(option(VERBOSE_TX_RECOVERY), true);
    /*
        Transaction Recovery
         */
    TransactionRecovery recovery = TitanFactory.startTransactionRecovery(graph, startTime);
    /*
        Use user log processing framework
         */
    final AtomicInteger userLogCount = new AtomicInteger(0);
    LogProcessorFramework userlogs = TitanFactory.openTransactionLog(graph);
    userlogs.addLogProcessor(userlogName).setStartTime(startTime).setRetryAttempts(1).addProcessor(new ChangeProcessor() {

        @Override
        public void process(TitanTransaction tx, TransactionId txId, ChangeState changes) {
            assertEquals(instanceid, txId.getInstanceId());
            //Just some reasonable upper bound
            assertTrue(txId.getTransactionId() > 0 && txId.getTransactionId() < 100);
            final Instant txTime = txId.getTransactionTime();
            assertTrue(String.format("tx timestamp %s not between start %s and end time %s", txTime, startTime, endTime), //Times should be within a second
            (txTime.isAfter(startTime) || txTime.equals(startTime)) && (txTime.isBefore(endTime) || txTime.equals(endTime)));
            assertTrue(tx.containsRelationType("knows"));
            assertTrue(tx.containsRelationType("weight"));
            EdgeLabel knows = tx.getEdgeLabel("knows");
            PropertyKey weight = tx.getPropertyKey("weight");
            Instant txTimeMicro = txId.getTransactionTime();
            int txNo;
            if (txTimeMicro.isBefore(txTimes[1])) {
                txNo = 1;
                //v1 addition transaction
                assertEquals(1, Iterables.size(changes.getVertices(Change.ADDED)));
                assertEquals(0, Iterables.size(changes.getVertices(Change.REMOVED)));
                assertEquals(1, Iterables.size(changes.getVertices(Change.ANY)));
                assertEquals(2, Iterables.size(changes.getRelations(Change.ADDED)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, knows)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, weight)));
                assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
                assertEquals(0, Iterables.size(changes.getRelations(Change.REMOVED)));
                TitanVertex v = Iterables.getOnlyElement(changes.getVertices(Change.ADDED));
                assertEquals(v1id, getId(v));
                VertexProperty<Float> p = Iterables.getOnlyElement(changes.getProperties(v, Change.ADDED, "weight"));
                assertEquals(111.1, p.value().doubleValue(), 0.01);
                assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, OUT)));
                assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, BOTH)));
            } else if (txTimeMicro.isBefore(txTimes[2])) {
                txNo = 2;
                //v2 addition transaction
                assertEquals(1, Iterables.size(changes.getVertices(Change.ADDED)));
                assertEquals(0, Iterables.size(changes.getVertices(Change.REMOVED)));
                assertEquals(2, Iterables.size(changes.getVertices(Change.ANY)));
                assertEquals(2, Iterables.size(changes.getRelations(Change.ADDED)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, knows)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED, weight)));
                assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
                assertEquals(0, Iterables.size(changes.getRelations(Change.REMOVED)));
                TitanVertex v = Iterables.getOnlyElement(changes.getVertices(Change.ADDED));
                assertEquals(v2id, getId(v));
                VertexProperty<Float> p = Iterables.getOnlyElement(changes.getProperties(v, Change.ADDED, "weight"));
                assertEquals(222.2, p.value().doubleValue(), 0.01);
                assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, OUT)));
                assertEquals(1, Iterables.size(changes.getEdges(v, Change.ADDED, BOTH)));
            } else if (txTimeMicro.isBefore(txTimes[3])) {
                txNo = 3;
                //v2 deletion transaction
                assertEquals(0, Iterables.size(changes.getVertices(Change.ADDED)));
                assertEquals(1, Iterables.size(changes.getVertices(Change.REMOVED)));
                assertEquals(2, Iterables.size(changes.getVertices(Change.ANY)));
                assertEquals(0, Iterables.size(changes.getRelations(Change.ADDED)));
                assertEquals(2, Iterables.size(changes.getRelations(Change.REMOVED)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED, knows)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED, weight)));
                assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
                TitanVertex v = Iterables.getOnlyElement(changes.getVertices(Change.REMOVED));
                assertEquals(v2id, getId(v));
                VertexProperty<Float> p = Iterables.getOnlyElement(changes.getProperties(v, Change.REMOVED, "weight"));
                assertEquals(222.2, p.value().doubleValue(), 0.01);
                assertEquals(1, Iterables.size(changes.getEdges(v, Change.REMOVED, OUT)));
                assertEquals(0, Iterables.size(changes.getEdges(v, Change.ADDED, BOTH)));
            } else {
                txNo = 4;
                //v1 edge modification
                assertEquals(0, Iterables.size(changes.getVertices(Change.ADDED)));
                assertEquals(0, Iterables.size(changes.getVertices(Change.REMOVED)));
                assertEquals(1, Iterables.size(changes.getVertices(Change.ANY)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.ADDED)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED)));
                assertEquals(1, Iterables.size(changes.getRelations(Change.REMOVED, knows)));
                assertEquals(2, Iterables.size(changes.getRelations(Change.ANY)));
                TitanVertex v = Iterables.getOnlyElement(changes.getVertices(Change.ANY));
                assertEquals(v1id, getId(v));
                TitanEdge e = Iterables.getOnlyElement(changes.getEdges(v, Change.REMOVED, Direction.OUT, "knows"));
                assertFalse(e.property("weight").isPresent());
                assertEquals(v, e.vertex(Direction.IN));
                e = Iterables.getOnlyElement(changes.getEdges(v, Change.ADDED, Direction.OUT, "knows"));
                assertEquals(44.4, e.<Float>value("weight").doubleValue(), 0.01);
                assertEquals(v, e.vertex(Direction.IN));
            }
            //See only current state of graph in transaction
            TitanVertex v1 = getV(tx, v1id);
            assertNotNull(v1);
            assertTrue(v1.isLoaded());
            if (txNo != 2) {
                //In the transaction that adds v2, v2 will be considered "loaded"
                assertMissing(tx, v2id);
            //                    assertTrue(txNo + " - " + v2, v2 == null || v2.isRemoved());
            }
            assertEquals(111.1, v1.<Float>value("weight").doubleValue(), 0.01);
            assertCount(1, v1.query().direction(Direction.OUT).edges());
            userLogCount.incrementAndGet();
        }
    }).build();
    //wait
    Thread.sleep(22000L);
    recovery.shutdown();
    long[] recoveryStats = ((StandardTransactionLogProcessor) recovery).getStatistics();
    if (withLogFailure) {
        assertEquals(1, recoveryStats[0]);
        assertEquals(4, recoveryStats[1]);
    } else {
        assertEquals(5, recoveryStats[0]);
        assertEquals(0, recoveryStats[1]);
    }
    userlogs.removeLogProcessor(userlogName);
    userlogs.shutdown();
    assertEquals(4, userLogCount.get());
}
Also used : TransactionRecovery(com.thinkaurelius.titan.core.log.TransactionRecovery) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) ChangeState(com.thinkaurelius.titan.core.log.ChangeState) Message(com.thinkaurelius.titan.diskstorage.log.Message) MessageReader(com.thinkaurelius.titan.diskstorage.log.MessageReader) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) ChangeProcessor(com.thinkaurelius.titan.core.log.ChangeProcessor) EdgeSerializer(com.thinkaurelius.titan.graphdb.database.EdgeSerializer) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) TransactionLogHeader(com.thinkaurelius.titan.graphdb.database.log.TransactionLogHeader) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) EnumMap(java.util.EnumMap) LogTxStatus(com.thinkaurelius.titan.graphdb.database.log.LogTxStatus) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) EdgeSerializer(com.thinkaurelius.titan.graphdb.database.EdgeSerializer) SpecialIntSerializer(com.thinkaurelius.titan.graphdb.serializer.SpecialIntSerializer) Serializer(com.thinkaurelius.titan.graphdb.database.serialize.Serializer) KCVSLog(com.thinkaurelius.titan.diskstorage.log.kcvs.KCVSLog) Log(com.thinkaurelius.titan.diskstorage.log.Log) Instant(java.time.Instant) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) ReadMarker(com.thinkaurelius.titan.diskstorage.log.ReadMarker) Change(com.thinkaurelius.titan.core.log.Change) LogProcessorFramework(com.thinkaurelius.titan.core.log.LogProcessorFramework) TransactionId(com.thinkaurelius.titan.core.log.TransactionId) TimestampProvider(com.thinkaurelius.titan.diskstorage.util.time.TimestampProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardTransactionLogProcessor(com.thinkaurelius.titan.graphdb.log.StandardTransactionLogProcessor) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 2 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project titan by thinkaurelius.

the class TitanGraphTest method testGlobalGraphIndexingAndQueriesForInternalIndexes.

/* ==================================================================================
                            GLOBAL GRAPH QUERIES
     ==================================================================================*/
/**
     * Tests index defintions and their correct application for internal indexes only
     */
@Test
public void testGlobalGraphIndexingAndQueriesForInternalIndexes() {
    PropertyKey weight = makeKey("weight", Float.class);
    PropertyKey time = makeKey("time", Long.class);
    PropertyKey text = makeKey("text", String.class);
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.LIST).make();
    EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(weight).make();
    EdgeLabel related = mgmt.makeEdgeLabel("related").signature(time).make();
    VertexLabel person = mgmt.makeVertexLabel("person").make();
    VertexLabel organization = mgmt.makeVertexLabel("organization").make();
    TitanGraphIndex edge1 = mgmt.buildIndex("edge1", Edge.class).addKey(time).addKey(weight).buildCompositeIndex();
    TitanGraphIndex edge2 = mgmt.buildIndex("edge2", Edge.class).indexOnly(connect).addKey(text).buildCompositeIndex();
    TitanGraphIndex prop1 = mgmt.buildIndex("prop1", TitanVertexProperty.class).addKey(time).buildCompositeIndex();
    TitanGraphIndex prop2 = mgmt.buildIndex("prop2", TitanVertexProperty.class).addKey(weight).addKey(text).buildCompositeIndex();
    TitanGraphIndex vertex1 = mgmt.buildIndex("vertex1", Vertex.class).addKey(time).indexOnly(person).unique().buildCompositeIndex();
    TitanGraphIndex vertex12 = mgmt.buildIndex("vertex12", Vertex.class).addKey(text).indexOnly(person).buildCompositeIndex();
    TitanGraphIndex vertex2 = mgmt.buildIndex("vertex2", Vertex.class).addKey(time).addKey(name).indexOnly(organization).buildCompositeIndex();
    TitanGraphIndex vertex3 = mgmt.buildIndex("vertex3", Vertex.class).addKey(name).buildCompositeIndex();
    // ########### INSPECTION & FAILURE ##############
    assertTrue(mgmt.containsRelationType("name"));
    assertTrue(mgmt.containsGraphIndex("prop1"));
    assertFalse(mgmt.containsGraphIndex("prop3"));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(Edge.class)));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(TitanVertexProperty.class)));
    assertEquals(4, Iterables.size(mgmt.getGraphIndexes(Vertex.class)));
    assertNull(mgmt.getGraphIndex("balblub"));
    edge1 = mgmt.getGraphIndex("edge1");
    edge2 = mgmt.getGraphIndex("edge2");
    prop1 = mgmt.getGraphIndex("prop1");
    prop2 = mgmt.getGraphIndex("prop2");
    vertex1 = mgmt.getGraphIndex("vertex1");
    vertex12 = mgmt.getGraphIndex("vertex12");
    vertex2 = mgmt.getGraphIndex("vertex2");
    vertex3 = mgmt.getGraphIndex("vertex3");
    assertTrue(vertex1.isUnique());
    assertFalse(edge2.isUnique());
    assertEquals("prop1", prop1.name());
    assertTrue(Vertex.class.isAssignableFrom(vertex3.getIndexedElement()));
    assertTrue(TitanVertexProperty.class.isAssignableFrom(prop1.getIndexedElement()));
    assertTrue(Edge.class.isAssignableFrom(edge2.getIndexedElement()));
    assertEquals(2, vertex2.getFieldKeys().length);
    assertEquals(1, vertex1.getFieldKeys().length);
    try {
        //Parameters not supported
        mgmt.buildIndex("blablub", Vertex.class).addKey(text, Mapping.TEXT.asParameter()).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Name already in use
        mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //ImplicitKeys not allowed
        mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Unique is only allowed for vertex
        mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ########### END INSPECTION & FAILURE ##############
    finishSchema();
    clopen();
    text = mgmt.getPropertyKey("text");
    time = mgmt.getPropertyKey("time");
    weight = mgmt.getPropertyKey("weight");
    // ########### INSPECTION & FAILURE (copied from above) ##############
    assertTrue(mgmt.containsRelationType("name"));
    assertTrue(mgmt.containsGraphIndex("prop1"));
    assertFalse(mgmt.containsGraphIndex("prop3"));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(Edge.class)));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(TitanVertexProperty.class)));
    assertEquals(4, Iterables.size(mgmt.getGraphIndexes(Vertex.class)));
    assertNull(mgmt.getGraphIndex("balblub"));
    edge1 = mgmt.getGraphIndex("edge1");
    edge2 = mgmt.getGraphIndex("edge2");
    prop1 = mgmt.getGraphIndex("prop1");
    prop2 = mgmt.getGraphIndex("prop2");
    vertex1 = mgmt.getGraphIndex("vertex1");
    vertex12 = mgmt.getGraphIndex("vertex12");
    vertex2 = mgmt.getGraphIndex("vertex2");
    vertex3 = mgmt.getGraphIndex("vertex3");
    assertTrue(vertex1.isUnique());
    assertFalse(edge2.isUnique());
    assertEquals("prop1", prop1.name());
    assertTrue(Vertex.class.isAssignableFrom(vertex3.getIndexedElement()));
    assertTrue(TitanVertexProperty.class.isAssignableFrom(prop1.getIndexedElement()));
    assertTrue(Edge.class.isAssignableFrom(edge2.getIndexedElement()));
    assertEquals(2, vertex2.getFieldKeys().length);
    assertEquals(1, vertex1.getFieldKeys().length);
    try {
        //Parameters not supported
        mgmt.buildIndex("blablub", Vertex.class).addKey(text, Mapping.TEXT.asParameter()).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Name already in use
        mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //ImplicitKeys not allowed
        mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Unique is only allowed for vertex
        mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ########### END INSPECTION & FAILURE ##############
    final int numV = 100;
    final boolean sorted = true;
    TitanVertex[] ns = new TitanVertex[numV];
    String[] strs = { "aaa", "bbb", "ccc", "ddd" };
    for (int i = 0; i < numV; i++) {
        ns[i] = tx.addVertex(i % 2 == 0 ? "person" : "organization");
        VertexProperty p1 = ns[i].property("name", "v" + i);
        VertexProperty p2 = ns[i].property("name", "u" + (i % 5));
        double w = (i * 0.5) % 5;
        long t = i;
        String txt = strs[i % (strs.length)];
        ns[i].property(VertexProperty.Cardinality.single, "weight", w);
        ns[i].property(VertexProperty.Cardinality.single, "time", t);
        ns[i].property(VertexProperty.Cardinality.single, "text", txt);
        for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
            p.property("weight", w);
            p.property("time", t);
            p.property("text", txt);
        }
        //previous or self-loop
        TitanVertex u = ns[(i > 0 ? i - 1 : i)];
        for (String label : new String[] { "connect", "related" }) {
            Edge e = ns[i].addEdge(label, u, "weight", (w++) % 5, "time", t, "text", txt);
        }
    }
    //########## QUERIES ################
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(10, 20, 30)).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 3, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0).has("text", Cmp.EQUAL, strs[10 % strs.length]), ElementCategory.EDGE, 1, new boolean[] { false, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 1), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 3), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strs.length, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]), ElementCategory.EDGE, numV / strs.length * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]), ElementCategory.PROPERTY, 2 * numV / (4 * 5), new boolean[] { true, sorted }, prop2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]).has("time", Cmp.EQUAL, 0), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop2.name(), prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.PROPERTY, 2 * numV / 10, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Cmp.EQUAL, 2), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex12.name(), vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "v51").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(51, 61, 71, 31, 41)).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 5, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of()), ElementCategory.VERTEX, 0, new boolean[] { true, false });
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted }, vertex3.name());
    clopen();
    //########## QUERIES (copied from above) ################
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(10, 20, 30)).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 3, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0).has("text", Cmp.EQUAL, strs[10 % strs.length]), ElementCategory.EDGE, 1, new boolean[] { false, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 1), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 3), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strs.length, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]), ElementCategory.EDGE, numV / strs.length * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]), ElementCategory.PROPERTY, 2 * numV / (4 * 5), new boolean[] { true, sorted }, prop2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]).has("time", Cmp.EQUAL, 0), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop2.name(), prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.PROPERTY, 2 * numV / 10, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Cmp.EQUAL, 2), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex12.name(), vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "v51").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(51, 61, 71, 31, 41)).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 5, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of()), ElementCategory.VERTEX, 0, new boolean[] { true, false });
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    //Update in transaction
    for (int i = 0; i < numV / 2; i++) {
        TitanVertex v = getV(tx, ns[i]);
        v.remove();
    }
    ns = new TitanVertex[numV * 3 / 2];
    for (int i = numV; i < numV * 3 / 2; i++) {
        ns[i] = tx.addVertex(i % 2 == 0 ? "person" : "organization");
        VertexProperty p1 = ns[i].property("name", "v" + i);
        VertexProperty p2 = ns[i].property("name", "u" + (i % 5));
        double w = (i * 0.5) % 5;
        long t = i;
        String txt = strs[i % (strs.length)];
        ns[i].property(VertexProperty.Cardinality.single, "weight", w);
        ns[i].property(VertexProperty.Cardinality.single, "time", t);
        ns[i].property(VertexProperty.Cardinality.single, "text", txt);
        for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
            p.property("weight", w);
            p.property("time", t);
            p.property("text", txt);
        }
        //previous or self-loop
        TitanVertex u = ns[(i > numV ? i - 1 : i)];
        for (String label : new String[] { "connect", "related" }) {
            Edge e = ns[i].addEdge(label, u, "weight", (w++) % 5, "time", t, "text", txt);
        }
    }
    //######### UPDATED QUERIES ##########
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20), ElementCategory.PROPERTY, 0, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 20), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
    //######### END UPDATED QUERIES ##########
    newTx();
    //######### UPDATED QUERIES (copied from above) ##########
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20), ElementCategory.PROPERTY, 0, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 20), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
//*** INIVIDUAL USE CASE TESTS ******
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 3 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project titan by thinkaurelius.

the class TitanGraphTest method testRelationTypeIndexes.

@Test
public void testRelationTypeIndexes() {
    PropertyKey weight = makeKey("weight", Float.class);
    PropertyKey time = makeKey("time", Long.class);
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.LIST).make();
    EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(time).make();
    EdgeLabel child = mgmt.makeEdgeLabel("child").multiplicity(Multiplicity.ONE2MANY).make();
    EdgeLabel link = mgmt.makeEdgeLabel("link").unidirected().make();
    RelationTypeIndex name1 = mgmt.buildPropertyIndex(name, "weightDesc", weight);
    RelationTypeIndex connect1 = mgmt.buildEdgeIndex(connect, "weightAsc", Direction.BOTH, incr, weight);
    RelationTypeIndex connect2 = mgmt.buildEdgeIndex(connect, "weightDesc", Direction.OUT, decr, weight);
    RelationTypeIndex connect3 = mgmt.buildEdgeIndex(connect, "time+weight", Direction.OUT, decr, time, weight);
    RelationTypeIndex child1 = mgmt.buildEdgeIndex(child, "time", Direction.OUT, time);
    RelationTypeIndex link1 = mgmt.buildEdgeIndex(link, "time", Direction.OUT, time);
    final String name1n = name1.name(), connect1n = connect1.name(), connect2n = connect2.name(), connect3n = connect3.name(), child1n = child1.name(), link1n = link1.name();
    // ########### INSPECTION & FAILURE ##############
    assertTrue(mgmt.containsRelationIndex(name, "weightDesc"));
    assertTrue(mgmt.containsRelationIndex(connect, "weightDesc"));
    assertFalse(mgmt.containsRelationIndex(child, "weightDesc"));
    assertEquals("time+weight", mgmt.getRelationIndex(connect, "time+weight").name());
    assertNotNull(mgmt.getRelationIndex(link, "time"));
    assertNull(mgmt.getRelationIndex(name, "time"));
    assertEquals(1, size(mgmt.getRelationIndexes(child)));
    assertEquals(3, size(mgmt.getRelationIndexes(connect)));
    assertEquals(0, size(mgmt.getRelationIndexes(weight)));
    try {
        //Name already exists
        mgmt.buildEdgeIndex(connect, "weightAsc", Direction.OUT, time);
        fail();
    } catch (SchemaViolationException e) {
    }
    //        } catch (IllegalArgumentException e) {}
    try {
        //Not valid in this direction due to multiplicity constraint
        mgmt.buildEdgeIndex(child, "blablub", Direction.IN, time);
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Not valid in this direction due to unidirectionality
        mgmt.buildEdgeIndex(link, "blablub", Direction.BOTH, time);
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ########## END INSPECTION ###########
    finishSchema();
    weight = mgmt.getPropertyKey("weight");
    time = mgmt.getPropertyKey("time");
    name = mgmt.getPropertyKey("name");
    connect = mgmt.getEdgeLabel("connect");
    child = mgmt.getEdgeLabel("child");
    link = mgmt.getEdgeLabel("link");
    // ########### INSPECTION & FAILURE (copied from above) ##############
    assertTrue(mgmt.containsRelationIndex(name, "weightDesc"));
    assertTrue(mgmt.containsRelationIndex(connect, "weightDesc"));
    assertFalse(mgmt.containsRelationIndex(child, "weightDesc"));
    assertEquals("time+weight", mgmt.getRelationIndex(connect, "time+weight").name());
    assertNotNull(mgmt.getRelationIndex(link, "time"));
    assertNull(mgmt.getRelationIndex(name, "time"));
    assertEquals(1, Iterables.size(mgmt.getRelationIndexes(child)));
    assertEquals(3, Iterables.size(mgmt.getRelationIndexes(connect)));
    assertEquals(0, Iterables.size(mgmt.getRelationIndexes(weight)));
    try {
        //Name already exists
        mgmt.buildEdgeIndex(connect, "weightAsc", Direction.OUT, time);
        fail();
    } catch (SchemaViolationException e) {
    }
    //        } catch (IllegalArgumentException e) {}
    try {
        //Not valid in this direction due to multiplicity constraint
        mgmt.buildEdgeIndex(child, "blablub", Direction.IN, time);
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Not valid in this direction due to unidirectionality
        mgmt.buildEdgeIndex(link, "blablub", Direction.BOTH, time);
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ########## END INSPECTION ###########
    mgmt.rollback();
    /*
        ########## TEST WITHIN TRANSACTION ##################
        */
    weight = tx.getPropertyKey("weight");
    time = tx.getPropertyKey("time");
    final int numV = 100;
    TitanVertex v = tx.addVertex();
    TitanVertex[] ns = new TitanVertex[numV];
    for (int i = 0; i < numV; i++) {
        double w = (i * 0.5) % 5;
        long t = (i + 77) % numV;
        VertexProperty p = v.property("name", "v" + i, "weight", w, "time", t);
        ns[i] = tx.addVertex();
        for (String label : new String[] { "connect", "child", "link" }) {
            Edge e = v.addEdge(label, ns[i], "weight", w, "time", t);
        }
    }
    TitanVertex u = ns[0];
    VertexList vl;
    //######### QUERIES ##########
    v = getV(tx, v);
    u = getV(tx, u);
    evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().keys("name").has("weight", Cmp.LESS_THAN, 0.9).orderBy("weight", incr), PROPERTY, 2 * numV / 10, 1, new boolean[] { true, true }, weight, Order.ASC);
    evaluateQuery(v.query().keys("name").interval("weight", 1.1, 2.2).orderBy("weight", decr).limit(numV / 10), PROPERTY, numV / 10, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query().keys("name").has("time", Cmp.EQUAL, 5).orderBy("weight", decr), PROPERTY, 1, 1, new boolean[] { false, false }, weight, Order.DESC);
    evaluateQuery(v.query().keys("name"), PROPERTY, numV, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(BOTH).has("time", Cmp.EQUAL, 5), EDGE, 1, 2, new boolean[0]);
    evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", decr).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query().labels("child").direction(BOTH).interval("weight", 0.0, 1.0).orderBy("weight", decr), EDGE, 2 * numV / 10, 2, new boolean[] { false, false }, weight, Order.DESC);
    evaluateQuery(v.query().labels("child").direction(OUT).interval("weight", 0.0, 1.0), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("child").direction(BOTH), EDGE, numV, 1, new boolean[] { true, true });
    vl = v.query().labels("child").direction(BOTH).vertexIds();
    assertEquals(numV, vl.size());
    assertTrue(vl.isSorted());
    assertTrue(isSortedByID(vl));
    evaluateQuery(v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
    vl = v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT).vertexIds();
    assertEquals(2 * numV / 10, vl.size());
    assertTrue(vl.isSorted());
    assertTrue(isSortedByID(vl));
    evaluateQuery(v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", incr), EDGE, 10, 1, new boolean[] { true, true }, time, Order.ASC);
    vl = v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", incr).vertexIds();
    assertEquals(10, vl.size());
    assertFalse(vl.isSorted());
    assertFalse(isSortedByID(vl));
    vl.sort();
    assertTrue(vl.isSorted());
    assertTrue(isSortedByID(vl));
    evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(BOTH), EDGE, 1, 2, new boolean[0]);
    evaluateQuery(v.query().labels("connect").interval("time", 10, 20).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", incr).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.ASC);
    evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", decr).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.DESC);
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 1.4, 2.75).orderBy("weight", decr), EDGE, 3 * numV / 10, 1, new boolean[] { true, true }, weight, Order.DESC);
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", decr), EDGE, 1, 1, new boolean[] { true, true }, weight, Order.DESC);
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", incr), EDGE, 1, 1, new boolean[] { true, false }, weight, Order.ASC);
    evaluateQuery(v.query().labels("connect").direction(OUT).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).has("weight", Cmp.EQUAL, 0.0).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 0.0, 1.0).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 50, 100).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
    evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
    //--------------
    clopen();
    weight = tx.getPropertyKey("weight");
    time = tx.getPropertyKey("time");
    //######### QUERIES (copied from above) ##########
    v = getV(tx, v);
    u = getV(tx, u);
    evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().keys("name").has("weight", Cmp.LESS_THAN, 0.9).orderBy("weight", incr), PROPERTY, 2 * numV / 10, 1, new boolean[] { true, true }, weight, Order.ASC);
    evaluateQuery(v.query().keys("name").interval("weight", 1.1, 2.2).orderBy("weight", decr).limit(numV / 10), PROPERTY, numV / 10, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query().keys("name").has("time", Cmp.EQUAL, 5).orderBy("weight", decr), PROPERTY, 1, 1, new boolean[] { false, false }, weight, Order.DESC);
    evaluateQuery(v.query().keys("name"), PROPERTY, numV, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(BOTH).has("time", Cmp.EQUAL, 5), EDGE, 1, 2, new boolean[0]);
    evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", decr).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query().labels("child").direction(BOTH).interval("weight", 0.0, 1.0).orderBy("weight", decr), EDGE, 2 * numV / 10, 2, new boolean[] { false, false }, weight, Order.DESC);
    evaluateQuery(v.query().labels("child").direction(OUT).interval("weight", 0.0, 1.0), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("child").direction(BOTH), EDGE, numV, 1, new boolean[] { true, true });
    vl = v.query().labels("child").direction(BOTH).vertexIds();
    assertEquals(numV, vl.size());
    assertTrue(vl.isSorted());
    assertTrue(isSortedByID(vl));
    evaluateQuery(v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT), EDGE, 2 * numV / 10, 1, new boolean[] { false, true });
    vl = v.query().labels("child").interval("weight", 0.0, 1.0).direction(OUT).vertexIds();
    assertEquals(2 * numV / 10, vl.size());
    assertTrue(vl.isSorted());
    assertTrue(isSortedByID(vl));
    evaluateQuery(v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", incr), EDGE, 10, 1, new boolean[] { true, true }, time, Order.ASC);
    vl = v.query().labels("child").interval("time", 70, 80).direction(OUT).orderBy("time", incr).vertexIds();
    assertEquals(10, vl.size());
    assertFalse(vl.isSorted());
    assertFalse(isSortedByID(vl));
    vl.sort();
    assertTrue(vl.isSorted());
    assertTrue(isSortedByID(vl));
    evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").has("time", Cmp.EQUAL, 5).interval("weight", 0.0, 5.0).direction(BOTH), EDGE, 1, 2, new boolean[0]);
    evaluateQuery(v.query().labels("connect").interval("time", 10, 20).interval("weight", 0.0, 5.0).direction(OUT), EDGE, 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", incr).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.ASC);
    evaluateQuery(v.query().labels("connect").direction(OUT).orderBy("weight", decr).limit(10), EDGE, 10, 1, new boolean[] { true, true }, weight, Order.DESC);
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 1.4, 2.75).orderBy("weight", decr), EDGE, 3 * numV / 10, 1, new boolean[] { true, true }, weight, Order.DESC);
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", decr), EDGE, 1, 1, new boolean[] { true, true }, weight, Order.DESC);
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", Cmp.EQUAL, 22).orderBy("weight", incr), EDGE, 1, 1, new boolean[] { true, false }, weight, Order.ASC);
    evaluateQuery(v.query().labels("connect").direction(OUT).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).has("weight", Cmp.EQUAL, 0.0).adjacent(u), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("weight", 0.0, 1.0).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 50, 100).adjacent(u), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
    evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
    //Update in transaction
    for (TitanVertexProperty<String> p : v.query().labels("name").properties()) {
        if (p.<Long>value("time") < (numV / 2))
            p.remove();
    }
    for (TitanEdge e : v.query().direction(BOTH).edges()) {
        if (e.<Long>value("time") < (numV / 2))
            e.remove();
    }
    ns = new TitanVertex[numV * 3 / 2];
    for (int i = numV; i < numV * 3 / 2; i++) {
        double w = (i * 0.5) % 5;
        long t = i;
        v.property("name", "v" + i, "weight", w, "time", t);
        ns[i] = tx.addVertex();
        for (String label : new String[] { "connect", "child", "link" }) {
            TitanEdge e = v.addEdge(label, ns[i], "weight", w, "time", t);
        }
    }
    //######### UPDATED QUERIES ##########
    evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10), PROPERTY, 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10).orderBy("weight", decr), PROPERTY, 10, 1, new boolean[] { false, false }, weight, Order.DESC);
    evaluateQuery(v.query().keys("name").interval("time", numV, numV + 10).limit(5), PROPERTY, 5, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 0, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, numV + 5), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", decr).limit(5), EDGE, 0, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query().labels("child").direction(OUT).interval("time", numV + 10, numV + 20).orderBy("weight", decr).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
    evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
    //######### END UPDATED QUERIES ##########
    newTx();
    weight = tx.getPropertyKey("weight");
    time = tx.getPropertyKey("time");
    v = getV(tx, v);
    u = getV(tx, u);
    //######### UPDATED QUERIES (copied from above) ##########
    evaluateQuery(v.query().keys("name").has("weight", Cmp.GREATER_THAN, 3.6), PROPERTY, 2 * numV / 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10), PROPERTY, 10, 1, new boolean[] { false, true });
    evaluateQuery(v.query().keys("name").interval("time", numV / 2 - 10, numV / 2 + 10).orderBy("weight", decr), PROPERTY, 10, 1, new boolean[] { false, false }, weight, Order.DESC);
    evaluateQuery(v.query().keys("name").interval("time", numV, numV + 10).limit(5), PROPERTY, 5, 1, new boolean[] { false, true });
    evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, 5), EDGE, 0, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(OUT).has("time", Cmp.EQUAL, numV + 5), EDGE, 1, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("child").direction(OUT).interval("time", 10, 20).orderBy("weight", decr).limit(5), EDGE, 0, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query().labels("child").direction(OUT).interval("time", numV + 10, numV + 20).orderBy("weight", decr).limit(5), EDGE, 5, 1, new boolean[] { true, false }, weight, Order.DESC);
    evaluateQuery(v.query(), RELATION, numV * 4, 1, new boolean[] { true, true });
    evaluateQuery(v.query().direction(OUT), RELATION, numV * 4, 1, new boolean[] { false, true });
//######### END UPDATED QUERIES ##########
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) SchemaViolationException(com.thinkaurelius.titan.core.SchemaViolationException) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) VertexList(com.thinkaurelius.titan.core.VertexList) Test(org.junit.Test)

Example 4 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project titan by thinkaurelius.

the class TitanIndexTest method setupChainGraph.

private void setupChainGraph(int numV, String[] strs, boolean sameNameMapping) {
    clopen(option(INDEX_NAME_MAPPING, INDEX), sameNameMapping);
    TitanGraphIndex vindex = getExternalIndex(Vertex.class, INDEX);
    TitanGraphIndex eindex = getExternalIndex(Edge.class, INDEX);
    TitanGraphIndex pindex = getExternalIndex(TitanVertexProperty.class, INDEX);
    PropertyKey name = makeKey("name", String.class);
    mgmt.addIndexKey(vindex, name, getStringMapping());
    mgmt.addIndexKey(eindex, name, getStringMapping());
    mgmt.addIndexKey(pindex, name, getStringMapping(), Parameter.of("mapped-name", "xstr"));
    PropertyKey text = makeKey("text", String.class);
    mgmt.addIndexKey(vindex, text, getTextMapping(), Parameter.of("mapped-name", "xtext"));
    mgmt.addIndexKey(eindex, text, getTextMapping());
    mgmt.addIndexKey(pindex, text, getTextMapping());
    mgmt.makeEdgeLabel("knows").signature(name).make();
    mgmt.makePropertyKey("uid").dataType(String.class).signature(text).make();
    finishSchema();
    TitanVertex previous = null;
    for (int i = 0; i < numV; i++) {
        TitanVertex v = graph.addVertex("name", strs[i % strs.length], "text", strs[i % strs.length]);
        Edge e = v.addEdge("knows", previous == null ? v : previous, "name", strs[i % strs.length], "text", strs[i % strs.length]);
        VertexProperty p = v.property("uid", "v" + i, "name", strs[i % strs.length], "text", strs[i % strs.length]);
        previous = v;
    }
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 5 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project titan by thinkaurelius.

the class TitanH1RecordWriter method write.

@Override
public void write(NullWritable key, VertexWritable value) throws IOException, InterruptedException {
    // TODO tolerate possibility that concurrent OLTP activity has deleted the vertex?  maybe configurable...
    Object vertexID = value.get().id();
    Vertex vertex = tx.vertices(vertexID).next();
    Iterator<VertexProperty<Object>> vpIter = value.get().properties();
    while (vpIter.hasNext()) {
        VertexProperty<Object> vp = vpIter.next();
        if (!persistableKeys.isEmpty() && !persistableKeys.contains(vp.key())) {
            log.debug("[vid {}] skipping key {}", vertexID, vp.key());
            continue;
        }
        vertex.property(vp.key(), vp.value());
        log.debug("[vid {}] set {}={}", vertexID, vp.key(), vp.value());
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty)

Aggregations

VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)37 Edge (org.apache.tinkerpop.gremlin.structure.Edge)18 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)14 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)14 Test (org.junit.jupiter.api.Test)13 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)12 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Test (org.junit.Test)9 PropertyKey (org.janusgraph.core.PropertyKey)7 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)5 EdgeLabel (org.janusgraph.core.EdgeLabel)5 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)4 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)4 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)4 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)4 Instant (java.time.Instant)4 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)4 VertexLabel (org.janusgraph.core.VertexLabel)4 AbstractEdge (org.janusgraph.graphdb.relations.AbstractEdge)4