Search in sources :

Example 21 with PropertyKey

use of com.thinkaurelius.titan.core.PropertyKey in project titan by thinkaurelius.

the class TitanIndexTest method testIndexing.

@Test
public void testIndexing() {
    PropertyKey text = makeKey("text", String.class);
    createExternalVertexIndex(text, INDEX);
    createExternalEdgeIndex(text, INDEX);
    PropertyKey location = makeKey("location", Geoshape.class);
    createExternalVertexIndex(location, INDEX);
    createExternalEdgeIndex(location, INDEX);
    PropertyKey time = makeKey("time", Long.class);
    createExternalVertexIndex(time, INDEX);
    createExternalEdgeIndex(time, INDEX);
    PropertyKey category = makeKey("category", Integer.class);
    mgmt.buildIndex("vcategory", Vertex.class).addKey(category).buildCompositeIndex();
    mgmt.buildIndex("ecategory", Edge.class).addKey(category).buildCompositeIndex();
    PropertyKey group = makeKey("group", Byte.class);
    createExternalVertexIndex(group, INDEX);
    createExternalEdgeIndex(group, INDEX);
    PropertyKey id = makeVertexIndexedKey("uid", Integer.class);
    EdgeLabel knows = ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(time).signature(location).make();
    finishSchema();
    clopen();
    String[] words = { "world", "aurelius", "titan", "graph" };
    int numCategories = 5;
    int numGroups = 10;
    double distance, offset;
    int numV = 100;
    final int originalNumV = numV;
    for (int i = 0; i < numV; i++) {
        TitanVertex v = tx.addVertex();
        v.property(VertexProperty.Cardinality.single, "uid", i);
        v.property(VertexProperty.Cardinality.single, "category", i % numCategories);
        v.property(VertexProperty.Cardinality.single, "group", i % numGroups);
        v.property(VertexProperty.Cardinality.single, "text", "Vertex " + words[i % words.length]);
        v.property(VertexProperty.Cardinality.single, "time", i);
        offset = (i % 2 == 0 ? 1 : -1) * (i * 50.0 / numV);
        v.property(VertexProperty.Cardinality.single, "location", Geoshape.point(0.0 + offset, 0.0 + offset));
        Edge e = v.addEdge("knows", getVertex("uid", Math.max(0, i - 1)));
        e.property("text", "Vertex " + words[i % words.length]);
        e.property("time", i);
        e.property("category", i % numCategories);
        e.property("group", i % numGroups);
        e.property("location", Geoshape.point(0.0 + offset, 0.0 + offset));
    }
    for (int i = 0; i < words.length; i++) {
        int expectedSize = numV / words.length;
        assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
        assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).edges());
        //Test ordering
        for (String orderKey : new String[] { "time", "category" }) {
            for (Order order : Order.values()) {
                for (TitanGraphQuery traversal : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()))) {
                    verifyElementOrder(traversal.vertices(), orderKey, order, expectedSize);
                }
            }
        }
    }
    assertCount(3, tx.query().has("group", 3).orderBy("time", incr).limit(3).vertices());
    assertCount(3, tx.query().has("group", 3).orderBy("time", decr).limit(3).edges());
    for (int i = 0; i < numV / 2; i += numV / 10) {
        assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
        assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
    }
    for (int i = 0; i < numV; i += 10) {
        offset = (i * 50.0 / originalNumV);
        distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
        assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
        assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
    }
    //Queries combining mixed and composite indexes
    assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).vertices());
    assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).edges());
    assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
    offset = (19 * 50.0 / originalNumV);
    distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
    assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
    assertCount(numV, tx.query().vertices());
    assertCount(numV, tx.query().edges());
    //--------------
    clopen();
    for (int i = 0; i < words.length; i++) {
        int expectedSize = numV / words.length;
        assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
        assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).edges());
        //Test ordering
        for (String orderKey : new String[] { "time", "category" }) {
            for (Order order : Order.values()) {
                for (TitanGraphQuery traversal : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()))) {
                    verifyElementOrder(traversal.vertices(), orderKey, order, expectedSize);
                }
            }
        }
    }
    assertCount(3, tx.query().has("group", 3).orderBy("time", incr).limit(3).vertices());
    assertCount(3, tx.query().has("group", 3).orderBy("time", decr).limit(3).edges());
    for (int i = 0; i < numV / 2; i += numV / 10) {
        assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
        assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
    }
    for (int i = 0; i < numV; i += 10) {
        offset = (i * 50.0 / originalNumV);
        distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
        assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
        assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
    }
    //Queries combining mixed and composite indexes
    assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).vertices());
    assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).edges());
    assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
    offset = (19 * 50.0 / originalNumV);
    distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
    assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
    assertCount(numV, tx.query().vertices());
    assertCount(numV, tx.query().edges());
    newTx();
    int numDelete = 12;
    for (int i = numV - numDelete; i < numV; i++) {
        getVertex("uid", i).remove();
    }
    numV = numV - numDelete;
    //Copied from above
    for (int i = 0; i < words.length; i++) {
        assertCount(numV / words.length, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
        assertCount(numV / words.length, tx.query().has("text", Text.CONTAINS, words[i]).edges());
    }
    for (int i = 0; i < numV / 2; i += numV / 10) {
        assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
        assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
    }
    for (int i = 0; i < numV; i += 10) {
        offset = (i * 50.0 / originalNumV);
        distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
        assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
        assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
    }
    assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
    offset = (19 * 50.0 / originalNumV);
    distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
    assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
    assertCount(numV, tx.query().vertices());
    assertCount(numV, tx.query().edges());
}
Also used : Order(com.thinkaurelius.titan.graphdb.internal.Order) StandardEdgeLabelMaker(com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) TitanGraphQuery(com.thinkaurelius.titan.core.TitanGraphQuery) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 22 with PropertyKey

use of com.thinkaurelius.titan.core.PropertyKey in project titan by thinkaurelius.

the class TitanIndexTest method testEdgeTTLWithMixedIndices.

@Test
public void testEdgeTTLWithMixedIndices() throws Exception {
    if (!features.hasCellTTL() || !indexFeatures.supportsDocumentTTL()) {
        return;
    }
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    PropertyKey text = mgmt.makePropertyKey("text").dataType(String.class).make();
    PropertyKey time = makeKey("time", Long.class);
    EdgeLabel label = mgmt.makeEdgeLabel("likes").make();
    final int likesTTLSeconds = (int) TestGraphConfigs.getTTL(TimeUnit.SECONDS);
    mgmt.setTTL(label, Duration.ofSeconds(likesTTLSeconds));
    mgmt.buildIndex("index1", Edge.class).addKey(name, getStringMapping()).addKey(time).buildMixedIndex(INDEX);
    mgmt.buildIndex("index2", Edge.class).indexOnly(label).addKey(text, getTextMapping()).buildMixedIndex(INDEX);
    assertEquals(Duration.ZERO, mgmt.getTTL(name));
    assertEquals(Duration.ofSeconds(likesTTLSeconds), mgmt.getTTL(label));
    finishSchema();
    TitanVertex v1 = tx.addVertex(), v2 = tx.addVertex(), v3 = tx.addVertex();
    Edge e1 = v1.addEdge("likes", v2, "name", "v1 likes v2", "text", "this will help to identify the edge");
    long time1 = System.currentTimeMillis();
    e1.property("time", time1);
    Edge e2 = v2.addEdge("likes", v3, "name", "v2 likes v3", "text", "this won't match anything");
    long time2 = time1 + 1;
    e2.property("time", time2);
    tx.commit();
    clopen();
    Object e1Id = e1.id();
    Object e2Id = e2.id();
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 1, new boolean[] { true, true }, "index2");
    evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
    v1 = getV(tx, v1.id());
    v2 = getV(tx, v2.id());
    v3 = getV(tx, v3.id());
    e1 = getE(tx, e1Id);
    e2 = getE(tx, e1Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v3);
    assertNotNull(e1);
    assertNotNull(e2);
    assertNotEmpty(v1.query().direction(Direction.OUT).edges());
    assertNotEmpty(v2.query().direction(Direction.OUT).edges());
    Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(likesTTLSeconds * 1.25), TimeUnit.SECONDS));
    clopen();
    // ...indexes have expired
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 0, new boolean[] { true, true }, "index2");
    evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 0, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
    v1 = getV(tx, v1.id());
    v2 = getV(tx, v2.id());
    v3 = getV(tx, v3.id());
    e1 = getE(tx, e1Id);
    e2 = getE(tx, e1Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v3);
    // edges have expired from the graph...
    assertNull(e1);
    assertNull(e2);
    assertEmpty(v1.query().direction(Direction.OUT).edges());
    assertEmpty(v2.query().direction(Direction.OUT).edges());
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 23 with PropertyKey

use of com.thinkaurelius.titan.core.PropertyKey in project titan by thinkaurelius.

the class TitanIndexTest method testIndexUpdatesWithoutReindex.

@Test
public void testIndexUpdatesWithoutReindex() throws InterruptedException, ExecutionException {
    Object[] settings = new Object[] { option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ofMillis(0), option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250) };
    clopen(settings);
    final String defText = "Mountain rocks are great friends";
    final int defTime = 5;
    final double defHeight = 101.1;
    final String[] defPhones = new String[] { "1234", "5678" };
    //Creates types and index only two keys key
    mgmt.makePropertyKey("time").dataType(Integer.class).make();
    PropertyKey text = mgmt.makePropertyKey("text").dataType(String.class).make();
    mgmt.makePropertyKey("height").dataType(Double.class).make();
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        mgmt.makePropertyKey("phone").dataType(String.class).cardinality(Cardinality.LIST).make();
    }
    mgmt.buildIndex("theIndex", Vertex.class).addKey(text, getTextMapping(), getFieldMap(text)).buildMixedIndex(INDEX);
    finishSchema();
    //Add initial data
    addVertex(defTime, defText, defHeight, defPhones);
    //Indexes should not yet be in use
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 1, new boolean[] { false, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    }
    newTx();
    //Add another key to index ------------------------------------------------------
    finishSchema();
    PropertyKey time = mgmt.getPropertyKey("time");
    mgmt.addIndexKey(mgmt.getGraphIndex("theIndex"), time, getFieldMap(time));
    finishSchema();
    newTx();
    //Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    tx.commit();
    //Should not yet be able to enable since not yet registered
    assertNull(mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX));
    //This call is redundant and just here to make sure it doesn't mess anything up
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.REGISTER_INDEX).get();
    mgmt.commit();
    ManagementSystem.awaitGraphIndexStatus(graph, "theIndex").timeout(10L, ChronoUnit.SECONDS).call();
    finishSchema();
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX).get();
    finishSchema();
    //Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    //One more key should be indexed but only sees partial data
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 3, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 3, new boolean[] { false, true });
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 2, new boolean[] { false, true }, "theIndex");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 2, new boolean[] { false, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 3, new boolean[] { false, true });
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 3, new boolean[] { false, true });
    }
    newTx();
    //Add another key to index ------------------------------------------------------
    finishSchema();
    PropertyKey height = mgmt.getPropertyKey("height");
    mgmt.addIndexKey(mgmt.getGraphIndex("theIndex"), height);
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        PropertyKey phone = mgmt.getPropertyKey("phone");
        mgmt.addIndexKey(mgmt.getGraphIndex("theIndex"), phone, new Parameter("mapping", Mapping.STRING));
    }
    finishSchema();
    //Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    tx.commit();
    mgmt.commit();
    ManagementUtil.awaitGraphIndexUpdate(graph, "theIndex", 10, ChronoUnit.SECONDS);
    finishSchema();
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX);
    finishSchema();
    TitanGraphIndex index = mgmt.getGraphIndex("theIndex");
    for (PropertyKey key : index.getFieldKeys()) {
        assertEquals(SchemaStatus.ENABLED, index.getIndexStatus(key));
    }
    //Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    //One more key should be indexed but only sees partial data
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 4, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    }
    newTx();
    finishSchema();
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.REINDEX).get();
    mgmt.commit();
    finishSchema();
    //All the data should now be in the index
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    }
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.DISABLE_INDEX).get();
    tx.commit();
    mgmt.commit();
    ManagementUtil.awaitGraphIndexUpdate(graph, "theIndex", 10, ChronoUnit.SECONDS);
    finishSchema();
    index = mgmt.getGraphIndex("theIndex");
    for (PropertyKey key : index.getFieldKeys()) {
        assertEquals(SchemaStatus.DISABLED, index.getIndexStatus(key));
    }
    newTx();
    //This now requires a full graph scan
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 5, new boolean[] { false, true });
}
Also used : Parameter(com.thinkaurelius.titan.core.schema.Parameter) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 24 with PropertyKey

use of com.thinkaurelius.titan.core.PropertyKey in project titan by thinkaurelius.

the class TitanGraphTest method testConsistencyEnforcement.

/* ==================================================================================
                            CONSISTENCY
     ==================================================================================*/
/**
     * Tests the correct application of ConsistencyModifiers across transactional boundaries
     */
@Test
public void testConsistencyEnforcement() {
    PropertyKey uid = makeVertexIndexedUniqueKey("uid", Integer.class);
    PropertyKey name = makeKey("name", String.class);
    mgmt.setConsistency(uid, ConsistencyModifier.LOCK);
    mgmt.setConsistency(name, ConsistencyModifier.LOCK);
    mgmt.setConsistency(mgmt.getGraphIndex("uid"), ConsistencyModifier.LOCK);
    EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.SIMPLE).make();
    EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
    EdgeLabel connect = mgmt.makeEdgeLabel("connect").multiplicity(Multiplicity.MULTI).make();
    EdgeLabel related = mgmt.makeEdgeLabel("related").multiplicity(Multiplicity.MULTI).make();
    mgmt.setConsistency(knows, ConsistencyModifier.LOCK);
    mgmt.setConsistency(spouse, ConsistencyModifier.LOCK);
    mgmt.setConsistency(related, ConsistencyModifier.FORK);
    finishSchema();
    name = tx.getPropertyKey("name");
    connect = tx.getEdgeLabel("connect");
    related = tx.getEdgeLabel("related");
    TitanVertex v1 = tx.addVertex("uid", 1);
    TitanVertex v2 = tx.addVertex("uid", 2);
    TitanVertex v3 = tx.addVertex("uid", 3);
    Edge e1 = v1.addEdge(connect.name(), v2, name.name(), "e1");
    Edge e2 = v1.addEdge(related.name(), v2, name.name(), "e2");
    newTx();
    v1 = getV(tx, v1);
    /*
         ==== check fork, no fork behavior
         */
    long e1id = getId(e1);
    long e2id = getId(e2);
    e1 = getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
    assertEquals("e1", e1.value("name"));
    assertEquals(e1id, getId(e1));
    e2 = getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
    assertEquals("e2", e2.value("name"));
    assertEquals(e2id, getId(e2));
    //Update edges - one should simply update, the other fork
    e1.property("name", "e1.2");
    e2.property("name", "e2.2");
    newTx();
    v1 = getV(tx, v1);
    e1 = getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
    assertEquals("e1.2", e1.value("name"));
    //should have same id
    assertEquals(e1id, getId(e1));
    e2 = getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
    assertEquals("e2.2", e2.value("name"));
    //should have different id since forked
    assertNotEquals(e2id, getId(e2));
    clopen();
    /*
         === check cross transaction
         */
    final Random random = new Random();
    final long[] vids = { getId(v1), getId(v2), getId(v3) };
    //1) Index uniqueness
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        private int pos = 0;

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex u = getV(tx, vids[pos++]);
            u.property(VertexProperty.Cardinality.single, "uid", 5);
        }
    });
    //2) Property out-uniqueness
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex u = getV(tx, vids[0]);
            u.property(VertexProperty.Cardinality.single, "name", "v" + random.nextInt(10));
        }
    });
    //3) knows simpleness
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex u1 = getV(tx, vids[0]), u2 = getV(tx, vids[1]);
            u1.addEdge("knows", u2);
        }
    });
    //4) knows one2one (in 2 separate configurations)
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        private int pos = 1;

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex u1 = getV(tx, vids[0]), u2 = getV(tx, vids[pos++]);
            u1.addEdge("spouse", u2);
        }
    });
    executeLockConflictingTransactionJobs(graph, new TransactionJob() {

        private int pos = 1;

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex u1 = getV(tx, vids[pos++]), u2 = getV(tx, vids[0]);
            u1.addEdge("spouse", u2);
        }
    });
    //######### TRY INVALID CONSISTENCY
    try {
        //Fork does not apply to constrained types
        mgmt.setConsistency(mgmt.getPropertyKey("name"), ConsistencyModifier.FORK);
        fail();
    } catch (IllegalArgumentException e) {
    }
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Random(java.util.Random) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 25 with PropertyKey

use of com.thinkaurelius.titan.core.PropertyKey in project titan by thinkaurelius.

the class TitanGraphTest method testWithoutIndex.

@Test
public void testWithoutIndex() {
    PropertyKey kid = mgmt.makePropertyKey("kid").dataType(Long.class).make();
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makeEdgeLabel("knows").signature(kid).make();
    finishSchema();
    Random random = new Random();
    int numV = 1000;
    TitanVertex previous = null;
    for (int i = 0; i < numV; i++) {
        TitanVertex v = graph.addVertex("kid", random.nextInt(numV), "name", "v" + i);
        if (previous != null) {
            Edge e = v.addEdge("knows", previous, "kid", random.nextInt(numV / 2));
        }
        previous = v;
    }
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
    clopen();
    //Copied from above
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Random(java.util.Random) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Aggregations

PropertyKey (com.thinkaurelius.titan.core.PropertyKey)79 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)49 Test (org.junit.Test)49 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)21 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)20 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)18 Edge (org.apache.tinkerpop.gremlin.structure.Edge)15 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)14 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 AtlasPropertyKey (org.apache.atlas.repository.graphdb.AtlasPropertyKey)12 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)11 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)11 TitanManagement (com.thinkaurelius.titan.core.schema.TitanManagement)11 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)8 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)8 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)7 TitanException (com.thinkaurelius.titan.core.TitanException)6 HashSet (java.util.HashSet)6 Instant (java.time.Instant)5