Search in sources :

Example 51 with TitanVertex

use of com.thinkaurelius.titan.core.TitanVertex 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 52 with TitanVertex

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

the class TitanGraphTest method testForceIndexUsage.

@Test
public void testForceIndexUsage() {
    PropertyKey age = makeKey("age", Integer.class);
    PropertyKey time = makeKey("time", Long.class);
    mgmt.buildIndex("time", Vertex.class).addKey(time).buildCompositeIndex();
    finishSchema();
    for (int i = 1; i <= 10; i++) {
        TitanVertex v = tx.addVertex("time", i, "age", i);
    }
    //Graph query with and with-out index support
    assertCount(1, tx.query().has("time", 5).vertices());
    assertCount(1, tx.query().has("age", 6).vertices());
    clopen(option(FORCE_INDEX_USAGE), true);
    //Query with-out index support should now throw exception
    assertCount(1, tx.query().has("time", 5).vertices());
    try {
        assertCount(1, tx.query().has("age", 6).vertices());
        fail();
    } catch (Exception e) {
    }
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) ExecutionException(java.util.concurrent.ExecutionException) SchemaViolationException(com.thinkaurelius.titan.core.SchemaViolationException) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TitanConfigurationException(com.thinkaurelius.titan.core.TitanConfigurationException) TitanException(com.thinkaurelius.titan.core.TitanException) Test(org.junit.Test)

Example 53 with TitanVertex

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

the class TitanIndexTest method addVertex.

private void addVertex(int time, String text, double height, String[] phones) {
    newTx();
    TitanVertex v = tx.addVertex("text", text, "time", time, "height", height);
    for (String phone : phones) {
        v.property("phone", phone);
    }
    newTx();
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex)

Example 54 with TitanVertex

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

the class TitanIndexTest method testUUIDIndexing.

/**
     * Tests indexing boolean
     */
@Test
public void testUUIDIndexing() {
    PropertyKey name = makeKey("uid", UUID.class);
    mgmt.buildIndex("uuidIndex", Vertex.class).addKey(name).buildMixedIndex(INDEX);
    finishSchema();
    clopen();
    UUID uid1 = UUID.randomUUID();
    UUID uid2 = UUID.randomUUID();
    TitanVertex v1 = graph.addVertex();
    v1.property("uid", uid1);
    TitanVertex v2 = graph.addVertex();
    v2.property("uid", uid2);
    assertCount(2, graph.query().vertices());
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", uid1)));
    assertEquals(v2, getOnlyVertex(graph.query().has("uid", uid2)));
    assertEquals(v2, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid1)));
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid2)));
    //Flush the index
    clopen();
    assertCount(2, graph.query().vertices());
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", uid1)));
    assertEquals(v2, getOnlyVertex(graph.query().has("uid", uid2)));
    assertEquals(v2, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid1)));
    assertEquals(v1, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid2)));
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) UUID(java.util.UUID) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 55 with TitanVertex

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

the class TitanIndexTest method testConditionalIndexing.

/**
     * Tests conditional indexing and the different management features
     */
@Test
public void testConditionalIndexing() {
    PropertyKey name = makeKey("name", String.class);
    PropertyKey weight = makeKey("weight", Double.class);
    PropertyKey text = makeKey("text", String.class);
    VertexLabel person = mgmt.makeVertexLabel("person").make();
    VertexLabel org = mgmt.makeVertexLabel("org").make();
    TitanGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name, getStringMapping()).buildMixedIndex(INDEX);
    TitanGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).indexOnly(person).addKey(text, getTextMapping()).addKey(weight).buildMixedIndex(INDEX);
    TitanGraphIndex index3 = mgmt.buildIndex("index3", Vertex.class).indexOnly(org).addKey(text, getTextMapping()).addKey(weight).buildMixedIndex(INDEX);
    // ########### INSPECTION & FAILURE ##############
    assertTrue(mgmt.containsGraphIndex("index1"));
    assertFalse(mgmt.containsGraphIndex("index"));
    assertCount(3, mgmt.getGraphIndexes(Vertex.class));
    assertNull(mgmt.getGraphIndex("indexx"));
    name = mgmt.getPropertyKey("name");
    weight = mgmt.getPropertyKey("weight");
    text = mgmt.getPropertyKey("text");
    person = mgmt.getVertexLabel("person");
    org = mgmt.getVertexLabel("org");
    index1 = mgmt.getGraphIndex("index1");
    index2 = mgmt.getGraphIndex("index2");
    index3 = mgmt.getGraphIndex("index3");
    assertTrue(Vertex.class.isAssignableFrom(index1.getIndexedElement()));
    assertEquals("index2", index2.name());
    assertEquals(INDEX, index3.getBackingIndex());
    assertFalse(index2.isUnique());
    assertEquals(2, index3.getFieldKeys().length);
    assertEquals(1, index1.getFieldKeys().length);
    assertEquals(3, index3.getParametersFor(text).length);
    assertEquals(2, index3.getParametersFor(weight).length);
    try {
        //Already exists
        mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildMixedIndex(INDEX);
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Already exists
        mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Key is already added
        mgmt.addIndexKey(index2, weight);
        fail();
    } catch (IllegalArgumentException e) {
    }
    finishSchema();
    clopen();
    // ########### INSPECTION & FAILURE (copied from above) ##############
    assertTrue(mgmt.containsGraphIndex("index1"));
    assertFalse(mgmt.containsGraphIndex("index"));
    assertCount(3, mgmt.getGraphIndexes(Vertex.class));
    assertNull(mgmt.getGraphIndex("indexx"));
    name = mgmt.getPropertyKey("name");
    weight = mgmt.getPropertyKey("weight");
    text = mgmt.getPropertyKey("text");
    person = mgmt.getVertexLabel("person");
    org = mgmt.getVertexLabel("org");
    index1 = mgmt.getGraphIndex("index1");
    index2 = mgmt.getGraphIndex("index2");
    index3 = mgmt.getGraphIndex("index3");
    assertTrue(Vertex.class.isAssignableFrom(index1.getIndexedElement()));
    assertEquals("index2", index2.name());
    assertEquals(INDEX, index3.getBackingIndex());
    assertFalse(index2.isUnique());
    assertEquals(2, index3.getFieldKeys().length);
    assertEquals(1, index1.getFieldKeys().length);
    assertEquals(3, index3.getParametersFor(text).length);
    assertEquals(2, index3.getParametersFor(weight).length);
    try {
        //Already exists
        mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildMixedIndex(INDEX);
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Already exists
        mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Key is already added
        mgmt.addIndexKey(index2, weight);
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ########### TRANSACTIONAL ##############
    weight = tx.getPropertyKey("weight");
    final int numV = 200;
    String[] strs = { "houseboat", "humanoid", "differential", "extraordinary" };
    String[] strs2 = new String[strs.length];
    for (int i = 0; i < strs.length; i++) strs2[i] = strs[i] + " " + strs[i];
    final int modulo = 5;
    assert numV % (modulo * strs.length * 2) == 0;
    for (int i = 0; i < numV; i++) {
        TitanVertex v = tx.addVertex(i % 2 == 0 ? "person" : "org");
        v.property("name", strs[i % strs.length]);
        v.property("text", strs[i % strs.length]);
        v.property("weight", (i % modulo) + 0.5);
    }
    //########## QUERIES ################
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index2.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", decr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, weight, Order.DESC, index2.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[3]).has(LABEL_NAME, Cmp.EQUAL, "org"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index3.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[1]).has(LABEL_NAME, Cmp.EQUAL, "org").orderBy("weight", decr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, weight, Order.DESC, index3.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has("weight", Cmp.EQUAL, 2.5).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / (modulo * strs.length), new boolean[] { true, true }, index2.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { false, true }, index1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[3]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, 0, new boolean[] { false, true }, index1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[2]).has("text", Text.CONTAINS, strs[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index1.name(), index2.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]).has("text", Text.CONTAINS, strs[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", incr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, weight, Order.ASC, index1.name(), index2.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]), ElementCategory.VERTEX, numV / strs.length, new boolean[] { false, true });
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).orderBy("weight", incr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { false, false }, weight, Order.ASC);
    clopen();
    weight = tx.getPropertyKey("weight");
    //########## QUERIES (copied from above) ################
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index2.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", decr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, weight, Order.DESC, index2.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[3]).has(LABEL_NAME, Cmp.EQUAL, "org"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index3.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[1]).has(LABEL_NAME, Cmp.EQUAL, "org").orderBy("weight", decr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, weight, Order.DESC, index3.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).has("weight", Cmp.EQUAL, 2.5).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / (modulo * strs.length), new boolean[] { true, true }, index2.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { false, true }, index1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[3]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, 0, new boolean[] { false, true }, index1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[2]).has("text", Text.CONTAINS, strs[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, index1.name(), index2.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, strs[0]).has("text", Text.CONTAINS, strs[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", incr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, true }, weight, Order.ASC, index1.name(), index2.name());
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]), ElementCategory.VERTEX, numV / strs.length, new boolean[] { false, true });
    evaluateQuery(tx.query().has("text", Text.CONTAINS, strs[0]).orderBy("weight", incr), ElementCategory.VERTEX, numV / strs.length, new boolean[] { false, false }, weight, Order.ASC);
}
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) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Aggregations

TitanVertex (com.thinkaurelius.titan.core.TitanVertex)77 Test (org.junit.Test)63 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)46 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)21 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)17 Edge (org.apache.tinkerpop.gremlin.structure.Edge)17 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)14 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)12 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)12 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)10 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)8 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)7 ElementCategory (com.thinkaurelius.titan.graphdb.internal.ElementCategory)4 Category (org.junit.experimental.categories.Category)4 SchemaViolationException (com.thinkaurelius.titan.core.SchemaViolationException)3 VertexList (com.thinkaurelius.titan.core.VertexList)3 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)3 TitanGraphBaseTest (com.thinkaurelius.titan.graphdb.TitanGraphBaseTest)3