Search in sources :

Example 1 with StandardPropertyKeyMaker

use of org.janusgraph.graphdb.types.StandardPropertyKeyMaker in project janusgraph by JanusGraph.

the class JanusGraphTest 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();
    JanusGraphVertex 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 (Object o : v.query().labels(foo).properties()) {
            JanusGraphVertexProperty<String> p = (JanusGraphVertexProperty<String>) o;
            assertTrue(values.contains(p.value()));
            int weightAsInteger = p.value(weight);
            sum += weightAsInteger;
            if (prop == foo)
                assertEquals(index, weightAsInteger);
            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(Property::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(Property::remove);
    // Check that the properties were actually deleted from v
    assertEmpty(v.properties(foo));
}
Also used : StandardPropertyKeyMaker(org.janusgraph.graphdb.types.StandardPropertyKeyMaker) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) Property(org.apache.tinkerpop.gremlin.structure.Property) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 2 with StandardPropertyKeyMaker

use of org.janusgraph.graphdb.types.StandardPropertyKeyMaker in project janusgraph by JanusGraph.

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().isUnique(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(org.janusgraph.graphdb.types.StandardRelationTypeMaker) EdgeLabel(org.janusgraph.core.EdgeLabel) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) StandardPropertyKeyMaker(org.janusgraph.graphdb.types.StandardPropertyKeyMaker) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) PropertyKey(org.janusgraph.core.PropertyKey)

Aggregations

PropertyKey (org.janusgraph.core.PropertyKey)2 StandardPropertyKeyMaker (org.janusgraph.graphdb.types.StandardPropertyKeyMaker)2 Property (org.apache.tinkerpop.gremlin.structure.Property)1 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)1 EdgeLabel (org.janusgraph.core.EdgeLabel)1 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)1 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)1 RelationType (org.janusgraph.core.RelationType)1 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)1 StandardEdgeLabelMaker (org.janusgraph.graphdb.types.StandardEdgeLabelMaker)1 StandardRelationTypeMaker (org.janusgraph.graphdb.types.StandardRelationTypeMaker)1 Test (org.junit.Test)1