Skip to content
Permalink
Browse files
Olap props writeback (#103)
* support olap property key with ReadFrequency
* support graph read mode 
* support olap property writeback

Change-Id: I9498ec80d11e1de6059dc2cca620cb208f33b5a7
  • Loading branch information
zhoney committed Jan 16, 2021
1 parent c0fc36d commit efb036890fc7a532f18af8008a34ace5ad3e50c9
Show file tree
Hide file tree
Showing 12 changed files with 383 additions and 5 deletions.
@@ -26,9 +26,11 @@
import javax.ws.rs.core.MultivaluedHashMap;

import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.exception.InvalidResponseException;
import com.baidu.hugegraph.exception.NotAllCreatedException;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.constant.HugeType;
import com.baidu.hugegraph.structure.graph.BatchOlapPropertyRequest;
import com.baidu.hugegraph.structure.graph.BatchVertexRequest;
import com.baidu.hugegraph.structure.graph.Vertex;
import com.baidu.hugegraph.structure.graph.Vertices;
@@ -74,6 +76,21 @@ public List<Vertex> update(BatchVertexRequest request) {
return result.readList(this.type(), Vertex.class);
}

public int update(BatchOlapPropertyRequest request) {
this.client.checkApiVersion("0.59", "olap property batch update");
MultivaluedHashMap<String, Object> headers = new MultivaluedHashMap<>();
headers.putSingle("Content-Encoding", BATCH_ENCODING);
String path = String.join("/", this.path(), "olap/batch");
RestResult result = this.client.put(path, null, request, headers);
Object size = result.readObject(Map.class).get("size");
if (!(size instanceof Integer)) {
throw new InvalidResponseException(
"The 'size' in response must be int, but got: %s(%s)",
size, size.getClass());
}
return (int) size;
}

public Vertex append(Vertex vertex) {
String id = GraphAPI.formatVertexId(vertex.id());
Map<String, Object> params = ImmutableMap.of("action", "append");
@@ -27,6 +27,7 @@
import com.baidu.hugegraph.exception.InvalidResponseException;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.constant.GraphMode;
import com.baidu.hugegraph.structure.constant.GraphReadMode;
import com.baidu.hugegraph.structure.constant.HugeType;
import com.google.common.collect.ImmutableMap;

@@ -67,7 +68,8 @@ public GraphMode mode(String graph) {
Map<String, String> mode = result.readObject(Map.class);
String value = mode.get("mode");
if (value == null) {
throw new InvalidResponseException("mode", "mode response");
throw new InvalidResponseException(
"Invalid response, expect 'mode' in response");
}
try {
return GraphMode.valueOf(value);
@@ -77,6 +79,34 @@ public GraphMode mode(String graph) {
}
}

public void readMode(String graph, GraphReadMode readMode) {
this.client.checkApiVersion("0.59", "graph read mode");
String path = String.join("/", this.path(), graph);
// NOTE: Must provide id for PUT. If use "graph/graph_read_mode", "/"
// will be encoded to "%2F". So use "graph_read_mode" here although
// inaccurate.
this.client.put(path, "graph_read_mode", readMode);
}

public GraphReadMode readMode(String graph) {
this.client.checkApiVersion("0.59", "graph read mode");
String path = String.join("/", this.path(), graph);
RestResult result = this.client.get(path, "graph_read_mode");
@SuppressWarnings("unchecked")
Map<String, String> readMode = result.readObject(Map.class);
String value = readMode.get("graph_read_mode");
if (value == null) {
throw new InvalidResponseException(
"Invalid response, expect 'graph_read_mode' in response");
}
try {
return GraphReadMode.valueOf(value);
} catch (IllegalArgumentException e) {
throw new InvalidResponseException(
"Invalid GraphReadMode value '%s'", value);
}
}

public void clear(String graph, String message) {
String path = String.join("/", this.path(), graph, "clear");
this.client.delete(path,
@@ -26,6 +26,7 @@
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.SchemaElement;
import com.baidu.hugegraph.structure.constant.HugeType;
import com.baidu.hugegraph.structure.constant.ReadFrequency;
import com.baidu.hugegraph.structure.schema.PropertyKey;
import com.baidu.hugegraph.util.E;
import com.google.common.collect.ImmutableMap;
@@ -95,6 +96,11 @@ protected Object checkCreateOrUpdate(SchemaElement schemaElement) {
"Not support aggregate property until " +
"api version 0.47");
pkey = propertyKey.switchV46();
} else if (this.client.apiVersionLt("0.59")) {
E.checkArgument(propertyKey.readFrequency() == ReadFrequency.OLTP,
"Not support olap property key until " +
"api version 0.59");
pkey = propertyKey.switchV58();
}
return pkey;
}
@@ -32,6 +32,7 @@
import com.baidu.hugegraph.structure.constant.Direction;
import com.baidu.hugegraph.structure.constant.T;
import com.baidu.hugegraph.structure.graph.BatchEdgeRequest;
import com.baidu.hugegraph.structure.graph.BatchOlapPropertyRequest;
import com.baidu.hugegraph.structure.graph.BatchVertexRequest;
import com.baidu.hugegraph.structure.graph.Edge;
import com.baidu.hugegraph.structure.graph.GraphIterator;
@@ -192,6 +193,10 @@ public List<Vertex> updateVertices(BatchVertexRequest request) {
return newVertices;
}

public int updateVertices(BatchOlapPropertyRequest request) {
return this.vertexAPI.update(request);
}

public Vertex appendVertexProperty(Vertex vertex) {
vertex = this.vertexAPI.append(vertex);
this.attachManager(vertex);
@@ -25,6 +25,7 @@
import com.baidu.hugegraph.api.graphs.GraphsAPI;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.structure.constant.GraphMode;
import com.baidu.hugegraph.structure.constant.GraphReadMode;

public class GraphsManager {

@@ -50,6 +51,14 @@ public GraphMode mode(String graph) {
return this.graphsAPI.mode(graph);
}

public void readMode(String graph, GraphReadMode readMode) {
this.graphsAPI.readMode(graph, readMode);
}

public GraphReadMode readMode(String graph) {
return this.graphsAPI.readMode(graph);
}

public void clear(String graph, String message) {
this.graphsAPI.clear(graph, message);
}
@@ -97,7 +97,7 @@ private void initManagers(RestClient client, String graph) {
private void checkServerApiVersion() {
VersionUtil.Version apiVersion = VersionUtil.Version.of(
this.version.getApiVersion());
VersionUtil.check(apiVersion, "0.38", "0.59",
VersionUtil.check(apiVersion, "0.38", "0.60",
"hugegraph-api in server");
this.client.apiVersion(apiVersion);
}
@@ -0,0 +1,50 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.structure.constant;

public enum GraphReadMode {

ALL(1, "all"),

OLTP_ONLY(2, "oltp_only"),

OLAP_ONLY(3, "olap_only");

private final byte code;
private final String name;

private GraphReadMode(int code, String name) {
assert code < 256;
this.code = (byte) code;
this.name = name;
}

public byte code() {
return this.code;
}

public String string() {
return this.name;
}

public boolean showOlap() {
return this == ALL || this == OLAP_ONLY;
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.structure.constant;

public enum ReadFrequency {

OLTP(1, "oltp"),

OLAP(2, "olap");

private byte code = 0;
private String name = null;

ReadFrequency(int code, String name) {
assert code < 256;
this.code = (byte) code;
this.name = name;
}

public byte code() {
return this.code;
}

public String string() {
return this.name;
}
}
@@ -0,0 +1,137 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.structure.graph;

import java.util.List;

import com.baidu.hugegraph.util.E;
import com.fasterxml.jackson.annotation.JsonProperty;

public class BatchOlapPropertyRequest {

@JsonProperty("vertices")
private List<OlapVertex> vertices;
@JsonProperty("property_key")
private String propertyKey;

public BatchOlapPropertyRequest() {
this.vertices = null;
this.propertyKey = null;
}

public static Builder builder() {
return new Builder();
}

@Override
public String toString() {
return String.format("BatchOlapPropertyRequest{vertices=%s," +
"propertyKey=%s}",
this.vertices, this.propertyKey);
}

public static class Builder {

private BatchOlapPropertyRequest req;

private Builder() {
this.req = new BatchOlapPropertyRequest();
}

public Builder vertices(List<OlapVertex> vertices) {
E.checkArgument(vertices != null && !vertices.isEmpty(),
"Parameter 'vertices' can't be null or empty");
this.req.vertices = vertices;
return this;
}

public Builder propertyKey(String propertyKey) {
E.checkArgumentNotNull(propertyKey,
"The property key can't be null");
this.req.propertyKey = propertyKey;
return this;
}

public BatchOlapPropertyRequest build() {
E.checkArgumentNotNull(req,
"BatchOlapPropertyRequest can't be null");
E.checkArgumentNotNull(req.vertices,
"Parameter 'vertices' can't be null");
E.checkArgument(req.propertyKey != null,
"Parameter 'property_key' can't be empty");
return this.req;
}
}

public static class OlapVertex {

@JsonProperty("id")
public Object id;
@JsonProperty("label")
public String label;
@JsonProperty("value")
public Object value;

@Override
public String toString() {
return String.format("OlapVertex{id=%s,label=%s,value=%s}",
this.id, this.label, this.value);
}

public static class Builder {

private OlapVertex vertex;

public Builder() {
this.vertex = new OlapVertex();
}

public Builder id(Object id) {
E.checkArgumentNotNull(id, "The id of vertex can't be null");
this.vertex.id = id;
return this;
}

public Builder label(String label) {
E.checkArgumentNotNull(label,
"The label of vertex can't be null");
this.vertex.label = label;
return this;
}

public Builder value(Object value) {
E.checkArgumentNotNull(value,
"The value of vertex can't be null");
this.vertex.value = value;
return this;
}

public OlapVertex build() {
E.checkArgumentNotNull(this.vertex.id,
"The id of vertex can't be null");
E.checkArgumentNotNull(this.vertex.label,
"The label of vertex can't be null");
E.checkArgumentNotNull(this.vertex.value,
"The value of vertex can't be null");
return this.vertex;
}
}
}
}

0 comments on commit efb0368

Please sign in to comment.