Search in sources :

Example 56 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class GraphTransaction method checkVertexLabel.

private VertexLabel checkVertexLabel(Object label, boolean verifyLabel) {
    HugeVertexFeatures features = graph().features().vertex();
    // Check Vertex label
    if (label == null && features.supportsDefaultLabel()) {
        label = features.defaultLabel();
    }
    if (label == null) {
        throw Element.Exceptions.labelCanNotBeNull();
    }
    E.checkArgument(label instanceof String || label instanceof VertexLabel, "Expect a string or a VertexLabel object " + "as the vertex label argument, but got: '%s'", label);
    // The label must be an instance of String or VertexLabel
    if (label instanceof String) {
        if (verifyLabel) {
            ElementHelper.validateLabel((String) label);
        }
        label = graph().vertexLabel((String) label);
    }
    assert (label instanceof VertexLabel);
    return (VertexLabel) label;
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) HugeVertexFeatures(com.baidu.hugegraph.structure.HugeFeatures.HugeVertexFeatures)

Example 57 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class GraphTransaction method optimizeQuery.

private Query optimizeQuery(ConditionQuery query) {
    if (query.idsSize() > 0) {
        throw new HugeException("Not supported querying by id and conditions: %s", query);
    }
    Id label = (Id) query.condition(HugeKeys.LABEL);
    // Optimize vertex query
    if (label != null && query.resultType().isVertex()) {
        VertexLabel vertexLabel = this.graph().vertexLabel(label);
        if (vertexLabel.idStrategy() == IdStrategy.PRIMARY_KEY) {
            List<Id> keys = vertexLabel.primaryKeys();
            E.checkState(!keys.isEmpty(), "The primary keys can't be empty when using " + "'%s' id strategy for vertex label '%s'", IdStrategy.PRIMARY_KEY, vertexLabel.name());
            if (query.matchUserpropKeys(keys)) {
                // Query vertex by label + primary-values
                query.optimized(OptimizedType.PRIMARY_KEY);
                String primaryValues = query.userpropValuesString(keys);
                LOG.debug("Query vertices by primaryKeys: {}", query);
                // Convert {vertex-label + primary-key} to vertex-id
                Id id = SplicingIdGenerator.splicing(label.asString(), primaryValues);
                /*
                     * Just query by primary-key(id), ignore other userprop(if
                     * exists) that it will be filtered by queryVertices(Query)
                     */
                return new IdQuery(query, id);
            }
        }
    }
    // Optimize edge query
    if (query.resultType().isEdge() && label != null && query.condition(HugeKeys.OWNER_VERTEX) != null && query.condition(HugeKeys.DIRECTION) != null && matchEdgeSortKeys(query, false, this.graph())) {
        // Query edge by sourceVertex + direction + label + sort-values
        query.optimized(OptimizedType.SORT_KEYS);
        query = query.copy();
        // Serialize sort-values
        List<Id> keys = this.graph().edgeLabel(label).sortKeys();
        List<Condition> conditions = GraphIndexTransaction.constructShardConditions(query, keys, HugeKeys.SORT_VALUES);
        query.query(conditions);
        /*
             * Reset all userprop since transferred to sort-keys, ignore other
             * userprop(if exists) that it will be filtered by queryEdges(Query)
             */
        query.resetUserpropConditions();
        LOG.debug("Query edges by sortKeys: {}", query);
        return query;
    }
    /*
         * Query only by sysprops, like: by vertex label, by edge label.
         * NOTE: we assume sysprops would be indexed by backend store
         * but we don't support query edges only by direction/target-vertex.
         */
    if (query.allSysprop()) {
        if (query.resultType().isVertex()) {
            verifyVerticesConditionQuery(query);
        } else if (query.resultType().isEdge()) {
            verifyEdgesConditionQuery(query);
        }
        /*
             * Just support:
             *  1.not query by label
             *  2.or query by label and store supports this feature
             */
        boolean byLabel = (label != null && query.conditionsSize() == 1);
        if (!byLabel || this.store().features().supportsQueryByLabel()) {
            return query;
        }
    }
    return null;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeException(com.baidu.hugegraph.HugeException)

Example 58 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class GraphTransaction method constructVertex.

@Watched(prefix = "graph")
public HugeVertex constructVertex(boolean verifyVL, Object... keyValues) {
    HugeElement.ElementKeys elemKeys = HugeElement.classifyKeys(keyValues);
    if (possibleOlapVertex(elemKeys)) {
        Id id = HugeVertex.getIdValue(elemKeys.id());
        HugeVertex vertex = HugeVertex.create(this, id, VertexLabel.OLAP_VL);
        ElementHelper.attachProperties(vertex, keyValues);
        Iterator<HugeProperty<?>> iterator = vertex.getProperties().iterator();
        assert iterator.hasNext();
        if (iterator.next().propertyKey().olap()) {
            return vertex;
        }
    }
    VertexLabel vertexLabel = this.checkVertexLabel(elemKeys.label(), verifyVL);
    Id id = HugeVertex.getIdValue(elemKeys.id());
    List<Id> keys = this.graph().mapPkName2Id(elemKeys.keys());
    // Check whether id match with id strategy
    this.checkId(id, keys, vertexLabel);
    // Create HugeVertex
    HugeVertex vertex = HugeVertex.create(this, null, vertexLabel);
    // Set properties
    ElementHelper.attachProperties(vertex, keyValues);
    // Assign vertex id
    if (this.params().mode().maintaining() && vertexLabel.idStrategy() == IdStrategy.AUTOMATIC) {
        // Resume id for AUTOMATIC id strategy in restoring mode
        vertex.assignId(id, true);
    } else {
        vertex.assignId(id);
    }
    return vertex;
}
Also used : HugeProperty(com.baidu.hugegraph.structure.HugeProperty) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) HugeElement(com.baidu.hugegraph.structure.HugeElement) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 59 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class GraphTransaction method checkNonnullProperty.

private void checkNonnullProperty(HugeVertex vertex) {
    Set<Id> keys = vertex.getPropertyKeys();
    VertexLabel vertexLabel = vertex.schemaLabel();
    // Check whether passed all non-null property
    @SuppressWarnings("unchecked") Collection<Id> nonNullKeys = CollectionUtils.subtract(vertexLabel.properties(), vertexLabel.nullableKeys());
    if (!keys.containsAll(nonNullKeys)) {
        @SuppressWarnings("unchecked") Collection<Id> missed = CollectionUtils.subtract(nonNullKeys, keys);
        HugeGraph graph = this.graph();
        E.checkArgument(false, "All non-null property keys %s of " + "vertex label '%s' must be set, missed keys %s", graph.mapPkId2Name(nonNullKeys), vertexLabel.name(), graph.mapPkId2Name(missed));
    }
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId)

Example 60 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class EdgeLabelBuilder method checkTtl.

private void checkTtl() {
    E.checkArgument(this.ttl >= 0, "The ttl must be >= 0, but got: %s", this.ttl);
    if (this.ttl == 0L) {
        E.checkArgument(this.ttlStartTime == null, "Can't set ttl start time if ttl is not set");
        return;
    }
    VertexLabel source = this.graph().vertexLabel(this.sourceLabel);
    VertexLabel target = this.graph().vertexLabel(this.targetLabel);
    E.checkArgument((source.ttl() == 0L || this.ttl <= source.ttl()) && (target.ttl() == 0L || this.ttl <= target.ttl()), "The ttl(%s) of edge label '%s' should less than " + "ttl(%s) of source label '%s' and ttl(%s) of target " + "label '%s'", this.ttl, this.name, source.ttl(), this.sourceLabel, target.ttl(), this.targetLabel);
    if (this.ttlStartTime == null) {
        return;
    }
    // Check whether the properties contains the specified keys
    E.checkArgument(!this.properties.isEmpty(), "The properties can't be empty when exist " + "ttl start time for edge label '%s'", this.name);
    E.checkArgument(this.properties.contains(this.ttlStartTime), "The ttl start time '%s' must be contained in " + "properties '%s' for edge label '%s'", this.ttlStartTime, this.name, this.properties);
    PropertyKey pkey = this.graph().propertyKey(this.ttlStartTime);
    E.checkArgument(pkey.dataType().isDate(), "The ttl start time property must be date type," + "but got '%s(%s)'", this.ttlStartTime, pkey.dataType());
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Aggregations

VertexLabel (com.baidu.hugegraph.schema.VertexLabel)86 Test (org.junit.Test)35 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)27 Id (com.baidu.hugegraph.backend.id.Id)23 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)20 HugeGraph (com.baidu.hugegraph.HugeGraph)19 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)19 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)12 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)12 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)11 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)7 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)6 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)5 Timed (com.codahale.metrics.annotation.Timed)5 RolesAllowed (jakarta.annotation.security.RolesAllowed)5 Produces (jakarta.ws.rs.Produces)5 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)4 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)4 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4 Date (java.util.Date)4