Search in sources :

Example 16 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project janusgraph by JanusGraph.

the class JanusGraphTest method testUpdatePropertyPropThenRemoveProperty.

/**
 * update property of a vertex property and remove the vertex property
 */
@Test
public void testUpdatePropertyPropThenRemoveProperty() {
    for (boolean reload : Arrays.asList(true, false)) {
        graph.traversal().V().drop().iterate();
        initializeGraphWithVerticesAndEdges();
        assertTrue(graph.traversal().V().has("_v", 1).values("_v").hasNext());
        VertexProperty p = (VertexProperty) graph.traversal().V().has("_v", 1).properties("_v").next();
        assertEquals(false, p.property("flag").value());
        p.property("flag", true);
        assertEquals(true, p.property("flag").value());
        if (!reload) {
            p.remove();
        } else {
            graph.traversal().V().has("_v", 1).properties("_v").next().remove();
        }
        graph.tx().commit();
        assertFalse(graph.traversal().V().has("_v", 1).values("_v").hasNext());
    }
}
Also used : VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 17 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project janusgraph by JanusGraph.

the class JanusGraphTest method testUpdatePropertyPropThenRemoveVertex.

@Test
public void testUpdatePropertyPropThenRemoveVertex() {
    initializeGraphWithVerticesAndEdges();
    Vertex v = graph.traversal().V().has("_v", 1).next();
    VertexProperty p = v.properties("_v").next();
    assertEquals(false, p.property("flag").value());
    p.property("flag", true);
    assertEquals(true, p.property("flag").value());
    p.property("flag").remove();
    v.remove();
    graph.tx().commit();
    assertFalse(graph.traversal().V().has("_v", 1).hasNext());
}
Also used : CacheVertex(org.janusgraph.graphdb.vertices.CacheVertex) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

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