Search in sources :

Example 51 with PropertyKey

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

the class GraphIndexTransaction method validQueryConditionValues.

private static boolean validQueryConditionValues(HugeGraph graph, ConditionQuery query) {
    Set<Id> keys = query.userpropKeys();
    for (Id key : keys) {
        PropertyKey pk = graph.propertyKey(key);
        Set<Object> values = query.userpropValues(key);
        E.checkState(!values.isEmpty(), "Expect user property values for key '%s', " + "but got none", pk);
        boolean hasContains = query.containsContainsCondition(key);
        if (pk.cardinality().multiple()) {
            // If contains collection index, relation should be contains
            E.checkState(hasContains, "The relation of property '%s' must be " + "CONTAINS or TEXT_CONTAINS, but got %s", pk.name(), query.relation(key).relation());
        }
        for (Object value : values) {
            if (hasContains) {
                value = toCollectionIfNeeded(pk, value);
            }
            if (!pk.checkValueType(value)) {
                return false;
            }
        }
    }
    return true;
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 52 with PropertyKey

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

the class GraphIndexTransaction method queryByUserprop.

@Watched(prefix = "index")
private IdHolderList queryByUserprop(ConditionQuery query) {
    // related index labels
    if (!this.graph().readMode().showOlap()) {
        for (Id pkId : query.userpropKeys()) {
            PropertyKey propertyKey = this.graph().propertyKey(pkId);
            if (propertyKey.olap()) {
                throw new NotAllowException("Not allowed to query by olap property key '%s'" + " when graph-read-mode is '%s'", propertyKey, this.graph().readMode());
            }
        }
    }
    Set<MatchedIndex> indexes = this.collectMatchedIndexes(query);
    if (indexes.isEmpty()) {
        Id label = query.condition(HugeKeys.LABEL);
        throw noIndexException(this.graph(), query, label);
    }
    // Value type of Condition not matched
    boolean paging = query.paging();
    if (!validQueryConditionValues(this.graph(), query)) {
        return IdHolderList.empty(paging);
    }
    // Do index query
    IdHolderList holders = new IdHolderList(paging);
    for (MatchedIndex index : indexes) {
        for (IndexLabel il : index.indexLabels()) {
            validateIndexLabel(il);
        }
        if (paging && index.indexLabels().size() > 1) {
            throw new NotSupportException("joint index query in paging");
        }
        if (index.containsSearchIndex()) {
            // Do search-index query
            holders.addAll(this.doSearchIndex(query, index));
        } else {
            // Do secondary-index, range-index or shard-index query
            IndexQueries queries = index.constructIndexQueries(query);
            assert !paging || queries.size() <= 1;
            IdHolder holder = this.doSingleOrJointIndex(queries);
            holders.add(holder);
        }
    /*
             * NOTE: need to skip the offset if offset > 0, but can't handle
             * it here because the query may a sub-query after flatten,
             * so the offset will be handle in QueryList.IndexQuery
             *
             * TODO: finish early here if records exceeds required limit with
             *       FixedIdHolder.
             */
    }
    return holders;
}
Also used : SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) Id(com.baidu.hugegraph.backend.id.Id) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) NotAllowException(com.baidu.hugegraph.exception.NotAllowException) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 53 with PropertyKey

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

the class SchemaTransaction method removePropertyKey.

@Watched(prefix = "schema")
public Id removePropertyKey(Id id) {
    LOG.debug("SchemaTransaction remove property key '{}'", id);
    PropertyKey propertyKey = this.getPropertyKey(id);
    // If the property key does not exist, return directly
    if (propertyKey == null) {
        return null;
    }
    List<VertexLabel> vertexLabels = this.getVertexLabels();
    for (VertexLabel vertexLabel : vertexLabels) {
        if (vertexLabel.properties().contains(id)) {
            throw new NotAllowException("Not allowed to remove property key: '%s' " + "because the vertex label '%s' is still using it.", propertyKey, vertexLabel.name());
        }
    }
    List<EdgeLabel> edgeLabels = this.getEdgeLabels();
    for (EdgeLabel edgeLabel : edgeLabels) {
        if (edgeLabel.properties().contains(id)) {
            throw new NotAllowException("Not allowed to remove property key: '%s' " + "because the edge label '%s' is still using it.", propertyKey, edgeLabel.name());
        }
    }
    if (propertyKey.oltp()) {
        this.removeSchema(propertyKey);
        return IdGenerator.ZERO;
    } else {
        return this.removeOlapPk(propertyKey);
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) NotAllowException(com.baidu.hugegraph.exception.NotAllowException) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 54 with PropertyKey

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

the class EdgeAPI method update.

@PUT
@Timed(name = "single-update")
@Path("{id}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=edge_write" })
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String id, @QueryParam("action") String action, JsonEdge jsonEdge) {
    LOG.debug("Graph [{}] update edge: {}", graph, jsonEdge);
    checkUpdatingBody(jsonEdge);
    if (jsonEdge.id != null) {
        E.checkArgument(id.equals(jsonEdge.id), "The ids are different between url and " + "request body ('%s' != '%s')", id, jsonEdge.id);
    }
    // Parse action param
    boolean append = checkAndParseAction(action);
    HugeGraph g = graph(manager, graph);
    HugeEdge edge = (HugeEdge) g.edge(id);
    EdgeLabel edgeLabel = edge.schemaLabel();
    for (String key : jsonEdge.properties.keySet()) {
        PropertyKey pkey = g.propertyKey(key);
        E.checkArgument(edgeLabel.properties().contains(pkey.id()), "Can't update property for edge '%s' because " + "there is no property key '%s' in its edge label", id, key);
    }
    commit(g, () -> updateProperties(edge, jsonEdge, append));
    return manager.serializer(g).writeEdge(edge);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Path(jakarta.ws.rs.Path) RolesAllowed(jakarta.annotation.security.RolesAllowed) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) PUT(jakarta.ws.rs.PUT)

Example 55 with PropertyKey

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

the class JsonSerializer method writeTaskWithSchema.

@Override
public String writeTaskWithSchema(SchemaElement.TaskWithSchema taskWithSchema) {
    StringBuilder builder = new StringBuilder();
    long id = taskWithSchema.task() == null ? 0L : taskWithSchema.task().asLong();
    SchemaElement schemaElement = taskWithSchema.schemaElement();
    String type;
    String schema;
    if (schemaElement instanceof PropertyKey) {
        type = "property_key";
        schema = this.writePropertyKey((PropertyKey) schemaElement);
    } else if (schemaElement instanceof IndexLabel) {
        type = "index_label";
        schema = this.writeIndexlabel((IndexLabel) schemaElement);
    } else {
        throw new HugeException("Invalid schema element '%s' in " + "TaskWithSchema, only support " + "[PropertyKey, IndexLabel]", schemaElement);
    }
    return builder.append("{\"").append(type).append("\": ").append(schema).append(", \"task_id\": ").append(id).append("}").toString();
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) SchemaElement(com.baidu.hugegraph.schema.SchemaElement) HugeException(com.baidu.hugegraph.HugeException) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Aggregations

PropertyKey (com.baidu.hugegraph.schema.PropertyKey)94 Id (com.baidu.hugegraph.backend.id.Id)31 Test (org.junit.Test)24 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)20 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)15 HugeGraph (com.baidu.hugegraph.HugeGraph)13 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)11 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)9 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)9 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)9 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)7 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)7 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)6 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)6 DataType (com.baidu.hugegraph.type.define.DataType)6 Map (java.util.Map)6 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)5 Timed (com.codahale.metrics.annotation.Timed)5 RolesAllowed (jakarta.annotation.security.RolesAllowed)5 Collection (java.util.Collection)5