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;
}
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;
}
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);
}
}
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);
}
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();
}
Aggregations