Search in sources :

Example 11 with VertexLabel

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

the class TitanGraphTest method testIndexUniqueness.

@Test
public void testIndexUniqueness() {
    PropertyKey time = makeKey("time", Long.class);
    PropertyKey text = makeKey("text", String.class);
    VertexLabel person = mgmt.makeVertexLabel("person").make();
    VertexLabel org = mgmt.makeVertexLabel("organization").make();
    TitanGraphIndex vindex1 = mgmt.buildIndex("vindex1", Vertex.class).addKey(time).indexOnly(person).unique().buildCompositeIndex();
    TitanGraphIndex vindex2 = mgmt.buildIndex("vindex2", Vertex.class).addKey(time).addKey(text).unique().buildCompositeIndex();
    finishSchema();
    //================== VERTEX UNIQUENESS ====================
    //I) Label uniqueness
    //Ia) Uniqueness violation in same transaction
    failTransactionOnCommit(new TransactionJob() {

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex v0 = tx.addVertex("person");
            v0.property(VertexProperty.Cardinality.single, "time", 1);
            TitanVertex v1 = tx.addVertex("person");
            v1.property(VertexProperty.Cardinality.single, "time", 1);
        }
    });
    //Ib) Uniqueness violation across transactions
    TitanVertex v0 = tx.addVertex("person");
    v0.property(VertexProperty.Cardinality.single, "time", 1);
    newTx();
    failTransactionOnCommit(new TransactionJob() {

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex v1 = tx.addVertex("person");
            v1.property(VertexProperty.Cardinality.single, "time", 1);
        }
    });
    //Ic) However, this should work since the label is different
    TitanVertex v1 = tx.addVertex("organization");
    v1.property(VertexProperty.Cardinality.single, "time", 1);
    newTx();
    //II) Composite uniqueness
    //IIa) Uniqueness violation in same transaction
    failTransactionOnCommit(new TransactionJob() {

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex v0 = tx.addVertex("time", 2, "text", "hello");
            TitanVertex v1 = tx.addVertex("time", 2, "text", "hello");
        }
    });
    //IIb) Uniqueness violation across transactions
    v0 = tx.addVertex("time", 2, "text", "hello");
    newTx();
    failTransactionOnCommit(new TransactionJob() {

        @Override
        public void run(TitanTransaction tx) {
            TitanVertex v1 = tx.addVertex("time", 2, "text", "hello");
        }
    });
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 12 with VertexLabel

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

the class TitanGraphTest method testPropertyTTLTiming.

@Category({ BrittleTests.class })
@Test
public void testPropertyTTLTiming() throws Exception {
    if (!features.hasCellTTL()) {
        return;
    }
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    PropertyKey place = mgmt.makePropertyKey("place").dataType(String.class).make();
    mgmt.setTTL(name, Duration.ofSeconds(42));
    mgmt.setTTL(place, Duration.ofSeconds(1));
    TitanGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name).buildCompositeIndex();
    TitanGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).addKey(name).addKey(place).buildCompositeIndex();
    VertexLabel label1 = mgmt.makeVertexLabel("event").setStatic().make();
    mgmt.setTTL(label1, Duration.ofSeconds(2));
    assertEquals(Duration.ofSeconds(42), mgmt.getTTL(name));
    assertEquals(Duration.ofSeconds(1), mgmt.getTTL(place));
    assertEquals(Duration.ofSeconds(2), mgmt.getTTL(label1));
    mgmt.commit();
    TitanVertex v1 = tx.addVertex(T.label, "event", "name", "some event", "place", "somewhere");
    tx.commit();
    Object id = v1.id();
    v1 = getV(graph, id);
    assertNotNull(v1);
    assertNotEmpty(graph.query().has("name", "some event").has("place", "somewhere").vertices());
    assertNotEmpty(graph.query().has("name", "some event").vertices());
    Thread.sleep(1001);
    graph.tx().rollback();
    // short-lived property expires first
    v1 = getV(graph, id);
    assertNotNull(v1);
    assertEmpty(graph.query().has("name", "some event").has("place", "somewhere").vertices());
    assertNotEmpty(graph.query().has("name", "some event").vertices());
    Thread.sleep(1001);
    graph.tx().rollback();
    // vertex expires before defined TTL of the long-lived property
    assertEmpty(graph.query().has("name", "some event").has("place", "somewhere").vertices());
    assertEmpty(graph.query().has("name", "some event").vertices());
    v1 = getV(graph, id);
    assertNull(v1);
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Category(org.junit.experimental.categories.Category) RelationCategory(com.thinkaurelius.titan.graphdb.internal.RelationCategory) ElementCategory(com.thinkaurelius.titan.graphdb.internal.ElementCategory) Test(org.junit.Test)

Example 13 with VertexLabel

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

the class TitanGraphTest method testSchemaNameChange.

@Test
public void testSchemaNameChange() {
    PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
    EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.MULTI).make();
    mgmt.buildEdgeIndex(knows, "byTime", Direction.BOTH, time);
    mgmt.buildIndex("timeIndex", Vertex.class).addKey(time).buildCompositeIndex();
    mgmt.makeVertexLabel("people").make();
    finishSchema();
    //CREATE SMALL GRAPH
    TitanVertex v = tx.addVertex("people");
    v.property(VertexProperty.Cardinality.single, "time", 5);
    v.addEdge("knows", v, "time", 11);
    newTx();
    v = getOnlyElement(tx.query().has("time", 5).vertices());
    assertNotNull(v);
    assertEquals("people", v.label());
    assertEquals(5, v.<Integer>value("time").intValue());
    assertCount(1, v.query().direction(Direction.IN).labels("knows").edges());
    assertCount(1, v.query().direction(Direction.OUT).labels("knows").has("time", 11).edges());
    newTx();
    //UPDATE SCHEMA NAMES
    assertTrue(mgmt.containsRelationType("knows"));
    knows = mgmt.getEdgeLabel("knows");
    mgmt.changeName(knows, "know");
    assertEquals("know", knows.name());
    assertTrue(mgmt.containsRelationIndex(knows, "byTime"));
    RelationTypeIndex rindex = mgmt.getRelationIndex(knows, "byTime");
    assertEquals("byTime", rindex.name());
    mgmt.changeName(rindex, "overTime");
    assertEquals("overTime", rindex.name());
    assertTrue(mgmt.containsVertexLabel("people"));
    VertexLabel vl = mgmt.getVertexLabel("people");
    mgmt.changeName(vl, "person");
    assertEquals("person", vl.name());
    assertTrue(mgmt.containsGraphIndex("timeIndex"));
    TitanGraphIndex gindex = mgmt.getGraphIndex("timeIndex");
    mgmt.changeName(gindex, "byTime");
    assertEquals("byTime", gindex.name());
    finishSchema();
    //VERIFY UPDATES IN MGMT SYSTEM
    assertTrue(mgmt.containsRelationType("know"));
    assertFalse(mgmt.containsRelationType("knows"));
    knows = mgmt.getEdgeLabel("know");
    assertTrue(mgmt.containsRelationIndex(knows, "overTime"));
    assertFalse(mgmt.containsRelationIndex(knows, "byTime"));
    assertTrue(mgmt.containsVertexLabel("person"));
    assertFalse(mgmt.containsVertexLabel("people"));
    assertTrue(mgmt.containsGraphIndex("byTime"));
    assertFalse(mgmt.containsGraphIndex("timeIndex"));
    //VERIFY UPDATES IN TRANSACTION
    newTx();
    v = getOnlyElement(tx.query().has("time", 5).vertices());
    assertNotNull(v);
    assertEquals("person", v.label());
    assertEquals(5, v.<Integer>value("time").intValue());
    assertCount(1, v.query().direction(Direction.IN).labels("know").edges());
    assertCount(0, v.query().direction(Direction.IN).labels("knows").edges());
    assertCount(1, v.query().direction(Direction.OUT).labels("know").has("time", 11).edges());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 14 with VertexLabel

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

the class TitanIndexTest method testVertexTTLWithMixedIndices.

/* ==================================================================================
                                     TIME-TO-LIVE
     ==================================================================================*/
@Test
public void testVertexTTLWithMixedIndices() throws Exception {
    if (!features.hasCellTTL() || !indexFeatures.supportsDocumentTTL()) {
        return;
    }
    PropertyKey name = makeKey("name", String.class);
    PropertyKey time = makeKey("time", Long.class);
    PropertyKey text = makeKey("text", String.class);
    VertexLabel event = mgmt.makeVertexLabel("event").setStatic().make();
    final int eventTTLSeconds = (int) TestGraphConfigs.getTTL(TimeUnit.SECONDS);
    mgmt.setTTL(event, Duration.ofSeconds(eventTTLSeconds));
    mgmt.buildIndex("index1", Vertex.class).addKey(name, getStringMapping()).addKey(time).buildMixedIndex(INDEX);
    mgmt.buildIndex("index2", Vertex.class).indexOnly(event).addKey(text, getTextMapping()).buildMixedIndex(INDEX);
    assertEquals(Duration.ZERO, mgmt.getTTL(name));
    assertEquals(Duration.ZERO, mgmt.getTTL(time));
    assertEquals(Duration.ofSeconds(eventTTLSeconds), mgmt.getTTL(event));
    finishSchema();
    TitanVertex v1 = tx.addVertex("event");
    v1.property(VertexProperty.Cardinality.single, "name", "first event");
    v1.property(VertexProperty.Cardinality.single, "text", "this text will help to identify the first event");
    long time1 = System.currentTimeMillis();
    v1.property(VertexProperty.Cardinality.single, "time", time1);
    TitanVertex v2 = tx.addVertex("event");
    v2.property(VertexProperty.Cardinality.single, "name", "second event");
    v2.property(VertexProperty.Cardinality.single, "text", "this text won't match");
    long time2 = time1 + 1;
    v2.property(VertexProperty.Cardinality.single, "time", time2);
    evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr), ElementCategory.VERTEX, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "index2");
    clopen();
    Object v1Id = v1.id();
    Object v2Id = v2.id();
    evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr), ElementCategory.VERTEX, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "index2");
    v1 = getV(tx, v1Id);
    v2 = getV(tx, v1Id);
    assertNotNull(v1);
    assertNotNull(v2);
    Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(eventTTLSeconds * 1.25), TimeUnit.SECONDS));
    clopen();
    Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(eventTTLSeconds * 1.25), TimeUnit.SECONDS));
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"), ElementCategory.VERTEX, 0, new boolean[] { true, true }, "index2");
    evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr), ElementCategory.VERTEX, 0, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
    v1 = getV(tx, v1Id);
    v2 = getV(tx, v2Id);
    assertNull(v1);
    assertNull(v2);
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 15 with VertexLabel

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

the class AbstractTitanGraphProvider method createIndices.

private void createIndices(final TitanGraph g, final LoadGraphWith.GraphData graphData) {
    TitanManagement mgmt = g.openManagement();
    if (graphData.equals(LoadGraphWith.GraphData.GRATEFUL)) {
        VertexLabel artist = mgmt.makeVertexLabel("artist").make();
        VertexLabel song = mgmt.makeVertexLabel("song").make();
        PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
        PropertyKey songType = mgmt.makePropertyKey("songType").cardinality(Cardinality.LIST).dataType(String.class).make();
        PropertyKey performances = mgmt.makePropertyKey("performances").cardinality(Cardinality.LIST).dataType(Integer.class).make();
        mgmt.buildIndex("artistByName", Vertex.class).addKey(name).indexOnly(artist).buildCompositeIndex();
        mgmt.buildIndex("songByName", Vertex.class).addKey(name).indexOnly(song).buildCompositeIndex();
        mgmt.buildIndex("songByType", Vertex.class).addKey(songType).indexOnly(song).buildCompositeIndex();
        mgmt.buildIndex("songByPerformances", Vertex.class).addKey(performances).indexOnly(song).buildCompositeIndex();
    } else if (graphData.equals(LoadGraphWith.GraphData.MODERN)) {
        VertexLabel person = mgmt.makeVertexLabel("person").make();
        VertexLabel software = mgmt.makeVertexLabel("software").make();
        PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
        PropertyKey lang = mgmt.makePropertyKey("lang").cardinality(Cardinality.LIST).dataType(String.class).make();
        PropertyKey age = mgmt.makePropertyKey("age").cardinality(Cardinality.LIST).dataType(Integer.class).make();
        mgmt.buildIndex("personByName", Vertex.class).addKey(name).indexOnly(person).buildCompositeIndex();
        mgmt.buildIndex("softwareByName", Vertex.class).addKey(name).indexOnly(software).buildCompositeIndex();
        mgmt.buildIndex("personByAge", Vertex.class).addKey(age).indexOnly(person).buildCompositeIndex();
        mgmt.buildIndex("softwareByLang", Vertex.class).addKey(lang).indexOnly(software).buildCompositeIndex();
    } else if (graphData.equals(LoadGraphWith.GraphData.CLASSIC)) {
        PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
        PropertyKey lang = mgmt.makePropertyKey("lang").cardinality(Cardinality.LIST).dataType(String.class).make();
        PropertyKey age = mgmt.makePropertyKey("age").cardinality(Cardinality.LIST).dataType(Integer.class).make();
        mgmt.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex();
        mgmt.buildIndex("byAge", Vertex.class).addKey(age).buildCompositeIndex();
        mgmt.buildIndex("byLang", Vertex.class).addKey(lang).buildCompositeIndex();
    } else {
    // TODO: add CREW work here.
    // TODO: add meta_property indices when meta_property graph is provided
    //throw new RuntimeException("Could not load graph with " + graphData);
    }
    mgmt.commit();
}
Also used : CacheVertex(com.thinkaurelius.titan.graphdb.vertices.CacheVertex) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) PropertyKeyVertex(com.thinkaurelius.titan.graphdb.types.vertices.PropertyKeyVertex) PreloadedVertex(com.thinkaurelius.titan.graphdb.vertices.PreloadedVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) EdgeLabelVertex(com.thinkaurelius.titan.graphdb.types.vertices.EdgeLabelVertex) EmptyVertex(com.thinkaurelius.titan.graphdb.types.system.EmptyVertex) StandardVertex(com.thinkaurelius.titan.graphdb.vertices.StandardVertex) VertexLabelVertex(com.thinkaurelius.titan.graphdb.types.VertexLabelVertex) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Aggregations

VertexLabel (com.thinkaurelius.titan.core.VertexLabel)15 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)12 Test (org.junit.Test)12 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)11 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)10 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)8 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)4 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)3 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)2 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)2 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)2 ElementCategory (com.thinkaurelius.titan.graphdb.internal.ElementCategory)2 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)2 RelationCategory (com.thinkaurelius.titan.graphdb.internal.RelationCategory)2 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)2 Category (org.junit.experimental.categories.Category)2 RelationType (com.thinkaurelius.titan.core.RelationType)1