Search in sources :

Example 1 with StandardPropertyKeyMaker

use of com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker in project titan by thinkaurelius.

the class TitanGraphTest method testMultivaluedVertexProperty.

/**
     * Tests multi-valued properties with special focus on indexing and incident unidirectional edges
     * which is not tested in {@link #testSchemaTypes()}
     * <p/>
     * -->TODO: split and move this into other test cases: ordering to query, indexing to index
     */
@Test
public <V> void testMultivaluedVertexProperty() {
    /*
         * Constant test data
         *
         * The values list below must have at least two elements. The string
         * literals were chosen arbitrarily and have no special significance.
         */
    final String foo = "foo", bar = "bar", weight = "weight";
    final List<String> values = ImmutableList.of("four", "score", "and", "seven");
    assertTrue("Values list must have multiple elements for this test to make sense", 2 <= values.size());
    // Create property with name pname and a vertex
    PropertyKey w = makeKey(weight, Integer.class);
    PropertyKey f = ((StandardPropertyKeyMaker) mgmt.makePropertyKey(foo)).dataType(String.class).cardinality(Cardinality.LIST).sortKey(w).sortOrder(Order.DESC).make();
    mgmt.buildIndex(foo, Vertex.class).addKey(f).buildCompositeIndex();
    PropertyKey b = mgmt.makePropertyKey(bar).dataType(String.class).cardinality(Cardinality.LIST).make();
    mgmt.buildIndex(bar, Vertex.class).addKey(b).buildCompositeIndex();
    finishSchema();
    TitanVertex v = tx.addVertex();
    // Insert prop values
    int i = 0;
    for (String s : values) {
        v.property(foo, s, weight, ++i);
        v.property(bar, s, weight, i);
    }
    //Verify correct number of properties
    assertCount(values.size(), v.properties(foo));
    assertCount(values.size(), v.properties(bar));
    //Verify order
    for (String prop : new String[] { foo, bar }) {
        int sum = 0;
        int index = values.size();
        for (TitanVertexProperty<String> p : v.query().labels(foo).properties()) {
            assertTrue(values.contains(p.value()));
            int wint = p.value(weight);
            sum += wint;
            if (prop == foo)
                assertEquals(index, wint);
            index--;
        }
        assertEquals(values.size() * (values.size() + 1) / 2, sum);
    }
    assertCount(1, tx.query().has(foo, values.get(1)).vertices());
    assertCount(1, tx.query().has(foo, values.get(3)).vertices());
    assertCount(1, tx.query().has(bar, values.get(1)).vertices());
    assertCount(1, tx.query().has(bar, values.get(3)).vertices());
    // Check that removing properties works
    asStream(v.properties(foo)).forEach(p -> p.remove());
    // Check that the properties were actually deleted from v
    assertEmpty(v.properties(foo));
    // Reopen database
    clopen();
    assertCount(0, tx.query().has(foo, values.get(1)).vertices());
    assertCount(0, tx.query().has(foo, values.get(3)).vertices());
    assertCount(1, tx.query().has(bar, values.get(1)).vertices());
    assertCount(1, tx.query().has(bar, values.get(3)).vertices());
    // Retrieve and check our test vertex
    v = getV(tx, v);
    assertEmpty(v.properties(foo));
    assertCount(values.size(), v.properties(bar));
    // Reinsert prop values
    for (String s : values) {
        v.property(foo, s);
    }
    assertCount(values.size(), v.properties(foo));
    // Check that removing properties works
    asStream(v.properties(foo)).forEach(p -> p.remove());
    // Check that the properties were actually deleted from v
    assertEmpty(v.properties(foo));
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) StandardPropertyKeyMaker(com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 2 with StandardPropertyKeyMaker

use of com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker in project titan by thinkaurelius.

the class ManagementSystem method buildRelationTypeIndex.

private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
    Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT, "Can only index uni-directed labels in the out-direction: %s", type);
    Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isConstrained(direction), "The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction);
    String composedName = composeRelationTypeIndexName(type, name);
    StandardRelationTypeMaker maker;
    if (type.isEdgeLabel()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction == Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey) type).dataType());
        maker = lm;
    }
    maker.status(type.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    maker.invisible();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);
    //Compose signature
    long[] typeSig = ((InternalRelationType) type).getSignature();
    Set<PropertyKey> signature = Sets.newHashSet();
    for (long typeId : typeSig) signature.add(transaction.getExistingPropertyKey(typeId));
    for (RelationType sortType : sortKeys) signature.remove(sortType);
    if (!signature.isEmpty()) {
        PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
    if (!type.isNew())
        updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
Also used : StandardRelationTypeMaker(com.thinkaurelius.titan.graphdb.types.StandardRelationTypeMaker) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) StandardEdgeLabelMaker(com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) StandardPropertyKeyMaker(com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Aggregations

PropertyKey (com.thinkaurelius.titan.core.PropertyKey)2 StandardPropertyKeyMaker (com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker)2 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)1 RelationType (com.thinkaurelius.titan.core.RelationType)1 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)1 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)1 StandardEdgeLabelMaker (com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker)1 StandardRelationTypeMaker (com.thinkaurelius.titan.graphdb.types.StandardRelationTypeMaker)1 Test (org.junit.Test)1