Search in sources :

Example 6 with VertexProperty

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

the class TitanEventualGraphTest method testTimestampSetting.

/**
     * Tests the correct interpretation of the commit time and that timestamps can be read
     */
@Test
public void testTimestampSetting() {
    clopen(option(GraphDatabaseConfiguration.STORE_META_TIMESTAMPS, "edgestore"), true, option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true);
    // Transaction 1: Init graph with two vertices, having set "name" and "age" properties
    TitanTransaction tx1 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start();
    String name = "name";
    String age = "age";
    String address = "address";
    TitanVertex v1 = tx1.addVertex(name, "a");
    TitanVertex v2 = tx1.addVertex(age, "14", name, "b", age, "42");
    tx1.commit();
    // Fetch vertex ids
    long id1 = getId(v1);
    long id2 = getId(v2);
    // Transaction 2: Remove "name" property from v1, set "address" property; create
    // an edge v2 -> v1
    TitanTransaction tx2 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(1000)).start();
    v1 = getV(tx2, id1);
    v2 = getV(tx2, id2);
    for (Iterator<VertexProperty<Object>> propiter = v1.properties(name); propiter.hasNext(); ) {
        VertexProperty prop = propiter.next();
        if (features.hasTimestamps()) {
            Instant t = prop.value("~timestamp");
            assertEquals(100, t.getEpochSecond());
            assertEquals(Instant.ofEpochSecond(0, 1000).getNano(), t.getNano());
        }
        if (features.hasCellTTL()) {
            Duration d = prop.value("~ttl");
            assertEquals(0l, d.getSeconds());
            assertTrue(d.isZero());
        }
    }
    assertEquals(1, v1.query().propertyCount());
    assertEquals(1, v1.query().has("~timestamp", Cmp.GREATER_THAN, Instant.ofEpochSecond(10)).propertyCount());
    assertEquals(1, v1.query().has("~timestamp", Instant.ofEpochSecond(100, 1000)).propertyCount());
    v1.property(name).remove();
    v1.property(VertexProperty.Cardinality.single, address, "xyz");
    Edge edge = v2.addEdge("parent", v1);
    tx2.commit();
    Object edgeId = edge.id();
    TitanVertex afterTx2 = getV(graph, id1);
    // Verify that "name" property is gone
    assertFalse(afterTx2.keys().contains(name));
    // Verify that "address" property is set
    assertEquals("xyz", afterTx2.value(address));
    // Verify that the edge is properly registered with the endpoint vertex
    assertCount(1, afterTx2.query().direction(IN).labels("parent").edges());
    // Verify that edge is registered under the id
    assertNotNull(getE(graph, edgeId));
    graph.tx().commit();
    // Transaction 3: Remove "address" property from v1 with earlier timestamp than
    // when the value was set
    TitanTransaction tx3 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(200)).start();
    v1 = getV(tx3, id1);
    v1.property(address).remove();
    tx3.commit();
    TitanVertex afterTx3 = getV(graph, id1);
    graph.tx().commit();
    // Verify that "address" is still set
    assertEquals("xyz", afterTx3.value(address));
    // Transaction 4: Modify "age" property on v2, remove edge between v2 and v1
    TitanTransaction tx4 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(2000)).start();
    v2 = getV(tx4, id2);
    v2.property(VertexProperty.Cardinality.single, age, "15");
    getE(tx4, edgeId).remove();
    tx4.commit();
    TitanVertex afterTx4 = getV(graph, id2);
    // Verify that "age" property is modified
    assertEquals("15", afterTx4.value(age));
    // Verify that edge is no longer registered with the endpoint vertex
    assertCount(0, afterTx4.query().direction(OUT).labels("parent").edges());
    // Verify that edge entry disappeared from id registry
    assertNull(getE(graph, edgeId));
    // Transaction 5: Modify "age" property on v2 with earlier timestamp
    TitanTransaction tx5 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(1500)).start();
    v2 = getV(tx5, id2);
    v2.property(VertexProperty.Cardinality.single, age, "16");
    tx5.commit();
    TitanVertex afterTx5 = getV(graph, id2);
    // Verify that the property value is unchanged
    assertEquals("15", afterTx5.value(age));
}
Also used : Instant(java.time.Instant) Duration(java.time.Duration) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 7 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 8 with VertexProperty

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

the class TitanGraphTest method testSchemaTypes.

/* ==================================================================================
                            SCHEMA TESTS
     ==================================================================================*/
/**
     * Test the definition and inspection of various schema types and ensure their correct interpretation
     * within the graph
     */
@Test
public void testSchemaTypes() {
    // ---------- PROPERTY KEYS ----------------
    //Normal single-valued property key
    PropertyKey weight = makeKey("weight", Float.class);
    //Indexed unique property key
    PropertyKey uid = makeVertexIndexedUniqueKey("uid", String.class);
    //Indexed but not unique
    PropertyKey someid = makeVertexIndexedKey("someid", Object.class);
    //Set-valued property key
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SET).make();
    //List-valued property key with signature
    PropertyKey value = mgmt.makePropertyKey("value").dataType(Double.class).signature(weight).cardinality(Cardinality.LIST).make();
    // ---------- EDGE LABELS ----------------
    //Standard edge label
    EdgeLabel friend = mgmt.makeEdgeLabel("friend").make();
    //Unidirected
    EdgeLabel link = mgmt.makeEdgeLabel("link").unidirected().multiplicity(Multiplicity.MANY2ONE).make();
    //Signature label
    EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(uid).multiplicity(Multiplicity.SIMPLE).make();
    //Edge labels with different cardinalities
    EdgeLabel parent = mgmt.makeEdgeLabel("parent").multiplicity(Multiplicity.MANY2ONE).make();
    EdgeLabel child = mgmt.makeEdgeLabel("child").multiplicity(Multiplicity.ONE2MANY).make();
    EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
    // ---------- VERTEX LABELS ----------------
    VertexLabel person = mgmt.makeVertexLabel("person").make();
    VertexLabel tag = mgmt.makeVertexLabel("tag").make();
    VertexLabel tweet = mgmt.makeVertexLabel("tweet").setStatic().make();
    long[] sig;
    // ######### INSPECTION & FAILURE ############
    assertTrue(mgmt.isOpen());
    assertEquals("weight", weight.toString());
    assertTrue(mgmt.containsRelationType("weight"));
    assertTrue(mgmt.containsPropertyKey("weight"));
    assertFalse(mgmt.containsEdgeLabel("weight"));
    assertTrue(mgmt.containsEdgeLabel("connect"));
    assertFalse(mgmt.containsPropertyKey("connect"));
    assertFalse(mgmt.containsRelationType("bla"));
    assertNull(mgmt.getPropertyKey("bla"));
    assertNull(mgmt.getEdgeLabel("bla"));
    assertNotNull(mgmt.getPropertyKey("weight"));
    assertNotNull(mgmt.getEdgeLabel("connect"));
    assertTrue(weight.isPropertyKey());
    assertFalse(weight.isEdgeLabel());
    assertEquals(Cardinality.SINGLE, weight.cardinality());
    assertEquals(Cardinality.SINGLE, someid.cardinality());
    assertEquals(Cardinality.SET, name.cardinality());
    assertEquals(Cardinality.LIST, value.cardinality());
    assertEquals(Object.class, someid.dataType());
    assertEquals(Float.class, weight.dataType());
    sig = ((InternalRelationType) value).getSignature();
    assertEquals(1, sig.length);
    assertEquals(weight.longId(), sig[0]);
    assertTrue(mgmt.getGraphIndex(uid.name()).isUnique());
    assertFalse(mgmt.getGraphIndex(someid.name()).isUnique());
    assertEquals("friend", friend.name());
    assertTrue(friend.isEdgeLabel());
    assertFalse(friend.isPropertyKey());
    assertEquals(Multiplicity.ONE2ONE, spouse.multiplicity());
    assertEquals(Multiplicity.ONE2MANY, child.multiplicity());
    assertEquals(Multiplicity.MANY2ONE, parent.multiplicity());
    assertEquals(Multiplicity.MULTI, friend.multiplicity());
    assertEquals(Multiplicity.SIMPLE, connect.multiplicity());
    assertTrue(link.isUnidirected());
    assertFalse(link.isDirected());
    assertFalse(child.isUnidirected());
    assertTrue(spouse.isDirected());
    assertFalse(((InternalRelationType) friend).isInvisibleType());
    assertTrue(((InternalRelationType) friend).isInvisible());
    assertEquals(0, ((InternalRelationType) friend).getSignature().length);
    sig = ((InternalRelationType) connect).getSignature();
    assertEquals(1, sig.length);
    assertEquals(uid.longId(), sig[0]);
    assertEquals(0, ((InternalRelationType) friend).getSortKey().length);
    assertEquals(Order.DEFAULT, ((InternalRelationType) friend).getSortOrder());
    assertEquals(SchemaStatus.ENABLED, ((InternalRelationType) friend).getStatus());
    assertEquals(5, Iterables.size(mgmt.getRelationTypes(PropertyKey.class)));
    assertEquals(6, Iterables.size(mgmt.getRelationTypes(EdgeLabel.class)));
    assertEquals(11, Iterables.size(mgmt.getRelationTypes(RelationType.class)));
    assertEquals(3, Iterables.size(mgmt.getVertexLabels()));
    assertEquals("tweet", tweet.name());
    assertTrue(mgmt.containsVertexLabel("person"));
    assertFalse(mgmt.containsVertexLabel("bla"));
    assertFalse(person.isPartitioned());
    assertFalse(person.isStatic());
    assertFalse(tag.isPartitioned());
    assertTrue(tweet.isStatic());
    //Failures
    try {
        //No datatype
        mgmt.makePropertyKey("fid").make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Already exists
        mgmt.makeEdgeLabel("link").unidirected().make();
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //signature and sort-key collide
        ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).sortKey(someid, weight).signature(someid).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    //        } catch (IllegalArgumentException e) {}
    try {
        //sort key requires the label to be non-constrained
        ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.SIMPLE).sortKey(weight).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //sort key requires the label to be non-constrained
        ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.MANY2ONE).sortKey(weight).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Already exists
        mgmt.makeVertexLabel("tweet").make();
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //signature key must have non-generic data type
        mgmt.makeEdgeLabel("test").signature(someid).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ######### END INSPECTION ############
    finishSchema();
    clopen();
    //Load schema types into current transaction
    weight = mgmt.getPropertyKey("weight");
    uid = mgmt.getPropertyKey("uid");
    someid = mgmt.getPropertyKey("someid");
    name = mgmt.getPropertyKey("name");
    value = mgmt.getPropertyKey("value");
    friend = mgmt.getEdgeLabel("friend");
    link = mgmt.getEdgeLabel("link");
    connect = mgmt.getEdgeLabel("connect");
    parent = mgmt.getEdgeLabel("parent");
    child = mgmt.getEdgeLabel("child");
    spouse = mgmt.getEdgeLabel("spouse");
    person = mgmt.getVertexLabel("person");
    tag = mgmt.getVertexLabel("tag");
    tweet = mgmt.getVertexLabel("tweet");
    // ######### INSPECTION & FAILURE (COPIED FROM ABOVE) ############
    assertTrue(mgmt.isOpen());
    assertEquals("weight", weight.toString());
    assertTrue(mgmt.containsRelationType("weight"));
    assertTrue(mgmt.containsPropertyKey("weight"));
    assertFalse(mgmt.containsEdgeLabel("weight"));
    assertTrue(mgmt.containsEdgeLabel("connect"));
    assertFalse(mgmt.containsPropertyKey("connect"));
    assertFalse(mgmt.containsRelationType("bla"));
    assertNull(mgmt.getPropertyKey("bla"));
    assertNull(mgmt.getEdgeLabel("bla"));
    assertNotNull(mgmt.getPropertyKey("weight"));
    assertNotNull(mgmt.getEdgeLabel("connect"));
    assertTrue(weight.isPropertyKey());
    assertFalse(weight.isEdgeLabel());
    assertEquals(Cardinality.SINGLE, weight.cardinality());
    assertEquals(Cardinality.SINGLE, someid.cardinality());
    assertEquals(Cardinality.SET, name.cardinality());
    assertEquals(Cardinality.LIST, value.cardinality());
    assertEquals(Object.class, someid.dataType());
    assertEquals(Float.class, weight.dataType());
    sig = ((InternalRelationType) value).getSignature();
    assertEquals(1, sig.length);
    assertEquals(weight.longId(), sig[0]);
    assertTrue(mgmt.getGraphIndex(uid.name()).isUnique());
    assertFalse(mgmt.getGraphIndex(someid.name()).isUnique());
    assertEquals("friend", friend.name());
    assertTrue(friend.isEdgeLabel());
    assertFalse(friend.isPropertyKey());
    assertEquals(Multiplicity.ONE2ONE, spouse.multiplicity());
    assertEquals(Multiplicity.ONE2MANY, child.multiplicity());
    assertEquals(Multiplicity.MANY2ONE, parent.multiplicity());
    assertEquals(Multiplicity.MULTI, friend.multiplicity());
    assertEquals(Multiplicity.SIMPLE, connect.multiplicity());
    assertTrue(link.isUnidirected());
    assertFalse(link.isDirected());
    assertFalse(child.isUnidirected());
    assertTrue(spouse.isDirected());
    assertFalse(((InternalRelationType) friend).isInvisibleType());
    assertTrue(((InternalRelationType) friend).isInvisible());
    assertEquals(0, ((InternalRelationType) friend).getSignature().length);
    sig = ((InternalRelationType) connect).getSignature();
    assertEquals(1, sig.length);
    assertEquals(uid.longId(), sig[0]);
    assertEquals(0, ((InternalRelationType) friend).getSortKey().length);
    assertEquals(Order.DEFAULT, ((InternalRelationType) friend).getSortOrder());
    assertEquals(SchemaStatus.ENABLED, ((InternalRelationType) friend).getStatus());
    assertEquals(5, Iterables.size(mgmt.getRelationTypes(PropertyKey.class)));
    assertEquals(6, Iterables.size(mgmt.getRelationTypes(EdgeLabel.class)));
    assertEquals(11, Iterables.size(mgmt.getRelationTypes(RelationType.class)));
    assertEquals(3, Iterables.size(mgmt.getVertexLabels()));
    assertEquals("tweet", tweet.name());
    assertTrue(mgmt.containsVertexLabel("person"));
    assertFalse(mgmt.containsVertexLabel("bla"));
    assertFalse(person.isPartitioned());
    assertFalse(person.isStatic());
    assertFalse(tag.isPartitioned());
    assertTrue(tweet.isStatic());
    //Failures
    try {
        //No datatype
        mgmt.makePropertyKey("fid").make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Already exists
        mgmt.makeEdgeLabel("link").unidirected().make();
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //signature and sort-key collide
        ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).sortKey(someid, weight).signature(someid).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    //        } catch (IllegalArgumentException e) {}
    try {
        //sort key requires the label to be non-constrained
        ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.SIMPLE).sortKey(weight).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //sort key requires the label to be non-constrained
        ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.MANY2ONE).sortKey(weight).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Already exists
        mgmt.makeVertexLabel("tweet").make();
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //signature key must have non-generic data type
        mgmt.makeEdgeLabel("test").signature(someid).make();
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ######### END INSPECTION ############
    /*
          ####### Make sure schema semantics are honored in transactions ######
        */
    clopen();
    TitanTransaction tx2;
    //shouldn't exist
    assertEmpty(tx.query().has("uid", "v1").vertices());
    TitanVertex v = tx.addVertex();
    //test property keys
    v.property("uid", "v1");
    v.property("weight", 1.5);
    v.property("someid", "Hello");
    v.property("name", "Bob");
    v.property("name", "John");
    VertexProperty p = v.property("value", 11);
    p.property("weight", 22);
    v.property("value", 33.3, "weight", 66.6);
    //same values are supported for list-properties
    v.property("value", 11, "weight", 22);
    //test edges
    TitanVertex v12 = tx.addVertex("person"), v13 = tx.addVertex("person");
    v12.property("uid", "v12");
    v13.property("uid", "v13");
    v12.addEdge("parent", v, "weight", 4.5);
    v13.addEdge("parent", v, "weight", 4.5);
    v.addEdge("child", v12);
    v.addEdge("child", v13);
    v.addEdge("spouse", v12);
    v.addEdge("friend", v12);
    //supports multi edges
    v.addEdge("friend", v12);
    v.addEdge("connect", v12, "uid", "e1");
    v.addEdge("link", v13);
    TitanVertex v2 = tx.addVertex("tweet");
    v2.addEdge("link", v13);
    v12.addEdge("connect", v2);
    TitanEdge edge;
    // ######### INSPECTION & FAILURE ############
    assertEquals(v, (Vertex) getOnlyElement(tx.query().has("uid", Cmp.EQUAL, "v1").vertices()));
    v = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1"));
    v12 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v12"));
    v13 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v13"));
    try {
        //Invalid data type
        v.property("weight", "x");
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //Only one "John" should be allowed
        v.property(VertexProperty.Cardinality.list, "name", "John");
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //Cannot set a property as edge
        v.property("link", v);
        fail();
    } catch (IllegalArgumentException e) {
    }
    //Only one property for weight allowed
    v.property(single, "weight", 1.0);
    assertCount(1, v.properties("weight"));
    v.property(VertexProperty.Cardinality.single, "weight", 0.5);
    assertEquals(0.5, v.<Float>value("weight").doubleValue(), 0.00001);
    assertEquals("v1", v.value("uid"));
    assertCount(2, v.properties("name"));
    for (TitanVertexProperty<String> prop : v.query().labels("name").properties()) {
        String nstr = prop.value();
        assertTrue(nstr.equals("Bob") || nstr.equals("John"));
    }
    assertTrue(size(v.properties("value")) >= 3);
    for (TitanVertexProperty<Double> prop : v.query().labels("value").properties()) {
        double prec = prop.value().doubleValue();
        assertEquals(prec * 2, prop.<Number>value("weight").doubleValue(), 0.00001);
    }
    //Ensure we can add additional values
    p = v.property("value", 44.4, "weight", 88.8);
    assertEquals(v, (Vertex) getOnlyElement(tx.query().has("someid", Cmp.EQUAL, "Hello").vertices()));
    //------- EDGES -------
    try {
        //multiplicity violation
        v12.addEdge("parent", v13);
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //multiplicity violation
        v13.addEdge("child", v12);
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //multiplicity violation
        v13.addEdge("spouse", v12);
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //multiplicity violation
        v.addEdge("spouse", v13);
        fail();
    } catch (SchemaViolationException e) {
    }
    assertCount(2, v.query().direction(Direction.IN).labels("parent").edges());
    assertCount(1, v12.query().direction(Direction.OUT).labels("parent").has("weight").edges());
    assertCount(1, v13.query().direction(Direction.OUT).labels("parent").has("weight").edges());
    assertEquals(v12, getOnlyElement(v.query().direction(Direction.OUT).labels("spouse").vertices()));
    edge = getOnlyElement(v.query().direction(Direction.BOTH).labels("connect").edges());
    assertEquals(1, edge.keys().size());
    assertEquals("e1", edge.value("uid"));
    try {
        //connect is simple
        v.addEdge("connect", v12);
        fail();
    } catch (SchemaViolationException e) {
    }
    //Make sure "link" is unidirected
    assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
    assertCount(0, v13.query().direction(Direction.BOTH).labels("link").edges());
    //Assert we can add more friendships
    v.addEdge("friend", v12);
    v2 = getOnlyElement(v12.query().direction(Direction.OUT).labels("connect").vertices());
    assertEquals(v13, getOnlyElement(v2.query().direction(Direction.OUT).labels("link").vertices()));
    assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.label());
    assertEquals("person", v12.label());
    assertEquals("person", v13.label());
    assertCount(4, tx.query().vertices());
    // ######### END INSPECTION & FAILURE ############
    clopen();
    // ######### INSPECTION & FAILURE (copied from above) ############
    assertEquals(v, getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1")));
    v = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1"));
    v12 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v12"));
    v13 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v13"));
    try {
        //Invalid data type
        v.property("weight", "x");
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //Only one "John" should be allowed
        v.property(VertexProperty.Cardinality.list, "name", "John");
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //Cannot set a property as edge
        v.property("link", v);
        fail();
    } catch (IllegalArgumentException e) {
    }
    //Only one property for weight allowed
    v.property(VertexProperty.Cardinality.single, "weight", 1.0);
    assertCount(1, v.properties("weight"));
    v.property(VertexProperty.Cardinality.single, "weight", 0.5);
    assertEquals(0.5, v.<Float>value("weight").doubleValue(), 0.00001);
    assertEquals("v1", v.value("uid"));
    assertCount(2, v.properties("name"));
    for (TitanVertexProperty<String> prop : v.query().labels("name").properties()) {
        String nstr = prop.value();
        assertTrue(nstr.equals("Bob") || nstr.equals("John"));
    }
    assertTrue(Iterables.size(v.query().labels("value").properties()) >= 3);
    for (TitanVertexProperty<Double> prop : v.query().labels("value").properties()) {
        double prec = prop.value().doubleValue();
        assertEquals(prec * 2, prop.<Number>value("weight").doubleValue(), 0.00001);
    }
    //Ensure we can add additional values
    p = v.property("value", 44.4, "weight", 88.8);
    assertEquals(v, (Vertex) getOnlyElement(tx.query().has("someid", Cmp.EQUAL, "Hello").vertices()));
    //------- EDGES -------
    try {
        //multiplicity violation
        v12.addEdge("parent", v13);
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //multiplicity violation
        v13.addEdge("child", v12);
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //multiplicity violation
        v13.addEdge("spouse", v12);
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        //multiplicity violation
        v.addEdge("spouse", v13);
        fail();
    } catch (SchemaViolationException e) {
    }
    assertCount(2, v.query().direction(Direction.IN).labels("parent").edges());
    assertCount(1, v12.query().direction(Direction.OUT).labels("parent").has("weight").edges());
    assertCount(1, v13.query().direction(Direction.OUT).labels("parent").has("weight").edges());
    assertEquals(v12, getOnlyElement(v.query().direction(Direction.OUT).labels("spouse").vertices()));
    edge = getOnlyElement(v.query().direction(Direction.BOTH).labels("connect").edges());
    assertEquals(1, edge.keys().size());
    assertEquals("e1", edge.value("uid"));
    try {
        //connect is simple
        v.addEdge("connect", v12);
        fail();
    } catch (SchemaViolationException e) {
    }
    //Make sure "link" is unidirected
    assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
    assertCount(0, v13.query().direction(Direction.BOTH).labels("link").edges());
    //Assert we can add more friendships
    v.addEdge("friend", v12);
    v2 = getOnlyElement(v12.query().direction(Direction.OUT).labels("connect").vertices());
    assertEquals(v13, getOnlyElement(v2.query().direction(Direction.OUT).labels("link").vertices()));
    assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.label());
    assertEquals("person", v12.label());
    assertEquals("person", v13.label());
    assertCount(4, tx.query().vertices());
    // ######### END INSPECTION & FAILURE ############
    //Ensure index uniqueness enforcement
    tx2 = graph.newTransaction();
    try {
        TitanVertex vx = tx2.addVertex();
        try {
            //property is unique
            vx.property(VertexProperty.Cardinality.single, "uid", "v1");
            fail();
        } catch (SchemaViolationException e) {
        }
        vx.property(VertexProperty.Cardinality.single, "uid", "unique");
        TitanVertex vx2 = tx2.addVertex();
        try {
            //property unique
            vx2.property(VertexProperty.Cardinality.single, "uid", "unique");
            fail();
        } catch (SchemaViolationException e) {
        }
    } finally {
        tx2.rollback();
    }
    //Ensure that v2 is really static
    v2 = getV(tx, v2);
    assertEquals("tweet", v2.label());
    try {
        v2.property(VertexProperty.Cardinality.single, "weight", 11);
        fail();
    } catch (SchemaViolationException e) {
    }
    try {
        v2.addEdge("friend", v12);
        fail();
    } catch (SchemaViolationException e) {
    }
    //Ensure that unidirected edges keep pointing to deleted vertices
    getV(tx, v13).remove();
    assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
    //Finally, test the schema container
    SchemaContainer schemaContainer = new SchemaContainer(graph);
    assertTrue(schemaContainer.containsRelationType("weight"));
    assertTrue(schemaContainer.containsRelationType("friend"));
    assertTrue(schemaContainer.containsVertexLabel("person"));
    VertexLabelDefinition vld = schemaContainer.getVertexLabel("tag");
    assertFalse(vld.isPartitioned());
    assertFalse(vld.isStatic());
    PropertyKeyDefinition pkd = schemaContainer.getPropertyKey("name");
    assertEquals(Cardinality.SET, pkd.getCardinality());
    assertEquals(String.class, pkd.getDataType());
    EdgeLabelDefinition eld = schemaContainer.getEdgeLabel("child");
    assertEquals("child", eld.getName());
    assertEquals(child.longId(), eld.getLongId());
    assertEquals(Multiplicity.ONE2MANY, eld.getMultiplicity());
    assertFalse(eld.isUnidirected());
}
Also used : SchemaContainer(com.thinkaurelius.titan.graphdb.schema.SchemaContainer) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) PropertyKeyDefinition(com.thinkaurelius.titan.graphdb.schema.PropertyKeyDefinition) EdgeLabelDefinition(com.thinkaurelius.titan.graphdb.schema.EdgeLabelDefinition) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) StandardEdgeLabelMaker(com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) SchemaViolationException(com.thinkaurelius.titan.core.SchemaViolationException) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) VertexLabelDefinition(com.thinkaurelius.titan.graphdb.schema.VertexLabelDefinition) Test(org.junit.Test)

Example 9 with VertexProperty

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

the class TitanPartitionGraphTest method testVertexPartitioning.

@Test
public void testVertexPartitioning() throws Exception {
    Object[] options = { option(GraphDatabaseConfiguration.IDS_FLUSH), false };
    clopen(options);
    PropertyKey gid = makeVertexIndexedUniqueKey("gid", Integer.class);
    PropertyKey sig = makeKey("sig", Integer.class);
    PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
    EdgeLabel knows = makeLabel("knows");
    EdgeLabel base = makeLabel("base");
    EdgeLabel one = mgmt.makeEdgeLabel("one").multiplicity(Multiplicity.ONE2ONE).make();
    VertexLabel person = mgmt.makeVertexLabel("person").make();
    VertexLabel group = mgmt.makeVertexLabel("group").partition().make();
    finishSchema();
    final Set<String> names = ImmutableSet.of("Marko", "Dan", "Stephen", "Daniel", "Josh", "Thad", "Pavel", "Matthias");
    final int numG = 10;
    final long[] gids = new long[numG];
    for (int i = 0; i < numG; i++) {
        TitanVertex g = tx.addVertex("group");
        g.property(VertexProperty.Cardinality.single, "gid", i);
        g.property(VertexProperty.Cardinality.single, "sig", 0);
        for (String n : names) {
            g.property("name", n);
        }
        assertEquals(i, g.<Integer>value("gid").intValue());
        assertEquals(0, g.<Integer>value("sig").intValue());
        assertEquals("group", g.label());
        assertCount(names.size(), g.properties("name"));
        assertTrue(getId(g) > 0);
        gids[i] = getId(g);
        if (i > 0) {
            g.addEdge("base", getV(tx, gids[0]));
        }
        if (i % 2 == 1) {
            g.addEdge("one", getV(tx, gids[i - 1]));
        }
    }
    for (int i = 0; i < numG; i++) {
        TitanVertex g = getV(tx, gids[i]);
        assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
        assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
        assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
        if (i > 0) {
            assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
        } else {
            assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
        }
    }
    newTx();
    for (int i = 0; i < numG; i++) {
        long gId = gids[i];
        assertTrue(idManager.isPartitionedVertex(gId));
        assertEquals(idManager.getCanonicalVertexId(gId), gId);
        TitanVertex g = getV(tx, gId);
        final int canonicalPartition = getPartitionID(g);
        assertEquals(g, (Vertex) getOnlyElement(tx.query().has("gid", i).vertices()));
        assertEquals(i, g.<Integer>value("gid").intValue());
        assertCount(names.size(), g.properties("name"));
        //Verify that properties are distributed correctly
        TitanVertexProperty p = (TitanVertexProperty) getOnlyElement(g.properties("gid"));
        assertEquals(canonicalPartition, getPartitionID(p));
        for (Iterator<VertexProperty<Object>> niter = g.properties("name"); niter.hasNext(); ) {
            assertEquals(canonicalPartition, getPartitionID((TitanVertex) niter.next().element()));
        }
        //Copied from above
        assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
        assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
        assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
        if (i > 0) {
            assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
        } else {
            assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
        }
    }
    clopen(options);
    final int numTx = 100;
    final int vPerTx = 10;
    Multiset<Integer> partitions = HashMultiset.create();
    for (int t = 1; t <= numTx; t++) {
        TitanVertex g1 = getV(tx, gids[0]), g2 = getV(tx, gids[1]);
        assertNotNull(g1);
        TitanVertex[] vs = new TitanVertex[vPerTx];
        for (int vi = 0; vi < vPerTx; vi++) {
            vs[vi] = tx.addVertex("person");
            vs[vi].property(VertexProperty.Cardinality.single, "sig", t);
            Edge e = vs[vi].addEdge("knows", g1);
            e.property("sig", t);
            e = g1.addEdge("knows", vs[vi]);
            e.property("sig", t);
            if (vi % 2 == 0) {
                e = vs[vi].addEdge("knows", g2);
                e.property("sig", t);
            }
        }
        newTx();
        //Verify that all elements are in the same partition
        TitanTransaction txx = graph.buildTransaction().readOnly().start();
        g1 = getV(tx, gids[0]);
        g2 = getV(tx, gids[1]);
        int partition = -1;
        for (int vi = 0; vi < vPerTx; vi++) {
            assertTrue(vs[vi].hasId());
            int pid = getPartitionID(vs[vi]);
            if (partition < 0)
                partition = pid;
            else
                assertEquals(partition, pid);
            int numRels = 0;
            TitanVertex v = getV(txx, vs[vi].longId());
            for (TitanRelation r : v.query().relations()) {
                numRels++;
                assertEquals(partition, getPartitionID(r));
                if (r instanceof TitanEdge) {
                    TitanVertex o = ((TitanEdge) r).otherVertex(v);
                    assertTrue(o.equals(g1) || o.equals(g2));
                }
            }
            assertEquals(3 + (vi % 2 == 0 ? 1 : 0), numRels);
        }
        partitions.add(partition);
        txx.commit();
    }
    //Verify spread across partitions; this number is a pessimistic lower bound but might fail since it is probabilistic
    //
    assertTrue(partitions.elementSet().size() >= 3);
    newTx();
    //Verify edge querying across partitions
    TitanVertex g1 = getV(tx, gids[0]);
    assertEquals(0, g1.<Integer>value("gid").intValue());
    assertEquals("group", g1.label());
    assertCount(names.size(), g1.properties("name"));
    assertCount(numTx * vPerTx, g1.query().direction(Direction.OUT).labels("knows").edges());
    assertCount(numTx * vPerTx, g1.query().direction(Direction.IN).labels("knows").edges());
    assertCount(numTx * vPerTx * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
    assertCount(numTx * vPerTx + numG, tx.query().vertices());
    newTx();
    //Restrict to partitions
    for (int t = 0; t < 10; t++) {
        int numP = random.nextInt(3) + 1;
        Set<Integer> parts = Sets.newHashSet();
        int numV = 0;
        while (parts.size() < numP) {
            int part = Iterables.get(partitions.elementSet(), random.nextInt(partitions.elementSet().size()));
            if (parts.add(part))
                numV += partitions.count(part);
        }
        numV *= vPerTx;
        int[] partarr = new int[numP];
        int i = 0;
        for (Integer part : parts) partarr[i++] = part;
        TitanTransaction tx2 = graph.buildTransaction().restrictedPartitions(partarr).readOnly().start();
        //Copied from above
        g1 = getV(tx2, gids[0]);
        assertEquals(0, g1.<Integer>value("gid").intValue());
        assertEquals("group", g1.label());
        assertTrue(names.size() >= size(g1.properties("name")));
        assertCount(numV, g1.query().direction(Direction.OUT).labels("knows").edges());
        assertCount(numV, g1.query().direction(Direction.IN).labels("knows").edges());
        assertCount(numV * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
        //Test local intersection
        TitanVertex g2 = getV(tx2, gids[1]);
        VertexList v1 = g1.query().direction(Direction.IN).labels("knows").vertexIds();
        VertexList v2 = g2.query().direction(Direction.IN).labels("knows").vertexIds();
        assertEquals(numV, v1.size());
        assertEquals(numV / 2, v2.size());
        v1.sort();
        v2.sort();
        LongArrayList al1 = v1.getIDs();
        LongArrayList al2 = v2.getIDs();
        assertTrue(AbstractLongListUtil.isSorted(al1));
        assertTrue(AbstractLongListUtil.isSorted(al2));
        LongArrayList alr = AbstractLongListUtil.mergeJoin(al1, al2, false);
        assertEquals(numV / 2, alr.size());
        tx2.commit();
    }
}
Also used : VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) LongArrayList(com.carrotsearch.hppc.LongArrayList) Edge(org.apache.tinkerpop.gremlin.structure.Edge) OLAPTest(com.thinkaurelius.titan.olap.OLAPTest) Test(org.junit.Test)

Example 10 with VertexProperty

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

the class TitanEventualGraphTest method testConsistencyModifier.

/**
     * Tests that consistency modes are correctly interpreted in the absence of locks (or tx isolation)
     */
@Test
public void testConsistencyModifier() throws InterruptedException {
    PropertyKey sig = makeKey("sig", Integer.class);
    PropertyKey weight = makeKey("weight", Double.class);
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SET).make();
    PropertyKey value = mgmt.makePropertyKey("value").dataType(Integer.class).cardinality(Cardinality.LIST).make();
    PropertyKey valuef = mgmt.makePropertyKey("valuef").dataType(Integer.class).cardinality(Cardinality.LIST).make();
    mgmt.setConsistency(valuef, ConsistencyModifier.FORK);
    EdgeLabel em = mgmt.makeEdgeLabel("em").multiplicity(Multiplicity.MULTI).make();
    EdgeLabel emf = mgmt.makeEdgeLabel("emf").multiplicity(Multiplicity.MULTI).make();
    mgmt.setConsistency(emf, ConsistencyModifier.FORK);
    EdgeLabel es = mgmt.makeEdgeLabel("es").multiplicity(Multiplicity.SIMPLE).make();
    EdgeLabel o2o = mgmt.makeEdgeLabel("o2o").multiplicity(Multiplicity.ONE2ONE).make();
    EdgeLabel o2m = mgmt.makeEdgeLabel("o2m").multiplicity(Multiplicity.ONE2MANY).make();
    finishSchema();
    TitanVertex u = tx.addVertex(), v = tx.addVertex();
    TitanRelation[] rs = new TitanRelation[9];
    final int txid = 1;
    rs[0] = sign(v.property("weight", 5.0), txid);
    rs[1] = sign(v.property("name", "John"), txid);
    rs[2] = sign(v.property("value", 2), txid);
    rs[3] = sign(v.property("valuef", 2), txid);
    rs[6] = sign(v.addEdge("es", u), txid);
    rs[7] = sign(v.addEdge("o2o", u), txid);
    rs[8] = sign(v.addEdge("o2m", u), txid);
    rs[4] = sign(v.addEdge("em", u), txid);
    rs[5] = sign(v.addEdge("emf", u), txid);
    newTx();
    long vid = getId(v), uid = getId(u);
    TitanTransaction tx1 = graph.newTransaction();
    TitanTransaction tx2 = graph.newTransaction();
    final int wintx = 20;
    processTx(tx1, wintx - 10, vid, uid);
    processTx(tx2, wintx, vid, uid);
    tx1.commit();
    Thread.sleep(5);
    //tx2 should win using time-based eventual consistency
    tx2.commit();
    newTx();
    v = getV(tx, vid);
    assertEquals(6.0, v.<Double>value("weight").doubleValue(), 0.00001);
    VertexProperty p = getOnlyElement(v.properties("weight"));
    assertEquals(wintx, p.<Integer>value("sig").intValue());
    p = getOnlyElement(v.properties("name"));
    assertEquals("Bob", p.value());
    assertEquals(wintx, p.<Integer>value("sig").intValue());
    p = getOnlyElement(v.properties("value"));
    assertEquals(rs[2].longId(), getId(p));
    assertEquals(wintx, p.<Integer>value("sig").intValue());
    assertCount(2, v.properties("valuef"));
    for (Iterator<VertexProperty<Object>> ppiter = v.properties("valuef"); ppiter.hasNext(); ) {
        VertexProperty pp = ppiter.next();
        assertNotEquals(rs[3].longId(), getId(pp));
        assertEquals(2, pp.value());
    }
    Edge e = getOnlyElement(v.query().direction(OUT).labels("es").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertNotEquals(rs[6].longId(), getId(e));
    e = getOnlyElement(v.query().direction(OUT).labels("o2o").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertEquals(rs[7].longId(), getId(e));
    e = getOnlyElement(v.query().direction(OUT).labels("o2m").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertNotEquals(rs[8].longId(), getId(e));
    e = getOnlyElement(v.query().direction(OUT).labels("em").edges());
    assertEquals(wintx, e.<Integer>value("sig").intValue());
    assertEquals(rs[4].longId(), getId(e));
    for (Edge ee : v.query().direction(OUT).labels("emf").edges()) {
        assertNotEquals(rs[5].longId(), getId(ee));
        assertEquals(uid, ee.inVertex().id());
    }
}
Also used : VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Aggregations

VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)12 Edge (org.apache.tinkerpop.gremlin.structure.Edge)10 Test (org.junit.Test)8 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)7 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)7 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)7 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)6 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 SchemaViolationException (com.thinkaurelius.titan.core.SchemaViolationException)2 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)2 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)2 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)2 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)2 Instant (java.time.Instant)2 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1 VertexList (com.thinkaurelius.titan.core.VertexList)1 Change (com.thinkaurelius.titan.core.log.Change)1 ChangeProcessor (com.thinkaurelius.titan.core.log.ChangeProcessor)1