diff --git a/.github/workflows/client-ci.yml b/.github/workflows/client-ci.yml index 7275a039e..e41bbcd1b 100644 --- a/.github/workflows/client-ci.yml +++ b/.github/workflows/client-ci.yml @@ -55,6 +55,8 @@ jobs: - name: Prepare env and service run: | + # TODO(@Thespica): test both servers of supporting gs and not supporting gs + # when the server supports gs $TRAVIS_DIR/install-hugegraph-from-source.sh $COMMIT_ID - name: Install Java ${{ matrix.JAVA_VERSION }} for client diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/API.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/API.java index 1f9d5025d..24d48a3ca 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/API.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/API.java @@ -37,6 +37,20 @@ public API(RestClient client) { this.path = null; } + protected static void checkOffset(long value) { + E.checkArgument(value >= 0, "Offset must be >= 0, but got: %s", value); + } + + protected static void checkLimit(long value, String name) { + E.checkArgument(value > 0 || value == NO_LIMIT, + "%s must be > 0 or == %s, but got: %s", + name, NO_LIMIT, value); + } + + protected static String joinPath(String... paths) { + return String.join(PATH_SPLITOR, paths); + } + public String path() { E.checkState(this.path != null, "Path can't be null"); return this.path; @@ -51,14 +65,4 @@ protected void path(String pathTemplate, Object... args) { } protected abstract String type(); - - protected static void checkOffset(long value) { - E.checkArgument(value >= 0, "Offset must be >= 0, but got: %s", value); - } - - protected static void checkLimit(long value, String name) { - E.checkArgument(value > 0 || value == NO_LIMIT, - "%s must be > 0 or == %s, but got: %s", - name, NO_LIMIT, value); - } } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/EdgeAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/EdgeAPI.java index 36c3eb19b..64f025939 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/EdgeAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/EdgeAPI.java @@ -35,8 +35,8 @@ public class EdgeAPI extends GraphAPI { - public EdgeAPI(RestClient client, String graph) { - super(client, graph); + public EdgeAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/GraphAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/GraphAPI.java index c2eb76819..b47a9ad5f 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/GraphAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/GraphAPI.java @@ -27,20 +27,16 @@ public abstract class GraphAPI extends API { - private static final String PATH = "graphs/%s/graph/%s"; + private static final String PATH = "graphspaces/%s/graphs/%s/graph/%s"; private final String batchPath; - public GraphAPI(RestClient client, String graph) { + public GraphAPI(RestClient client, String graphSpace, String graph) { super(client); - this.path(PATH, graph, this.type()); + this.path(PATH, graphSpace, graph, this.type()); this.batchPath = String.join("/", this.path(), "batch"); } - public String batchPath() { - return this.batchPath; - } - public static String formatVertexId(Object id) { return formatVertexId(id, false); } @@ -68,4 +64,8 @@ public static String formatProperties(Map properties) { } return JsonUtil.toJson(properties); } + + public String batchPath() { + return this.batchPath; + } } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java index 70f4de78a..734757bb1 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java @@ -36,8 +36,8 @@ public class VertexAPI extends GraphAPI { - public VertexAPI(RestClient client, String graph) { - super(client, graph); + public VertexAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override @@ -83,21 +83,21 @@ public int update(BatchOlapPropertyRequest request) { } public Vertex append(Vertex vertex) { - String id = formatVertexId(vertex.id()); + String id = GraphAPI.formatVertexId(vertex.id()); Map params = ImmutableMap.of("action", "append"); RestResult result = this.client.put(this.path(), id, vertex, params); return result.readObject(Vertex.class); } public Vertex eliminate(Vertex vertex) { - String id = formatVertexId(vertex.id()); + String id = GraphAPI.formatVertexId(vertex.id()); Map params = ImmutableMap.of("action", "eliminate"); RestResult result = this.client.put(this.path(), id, vertex, params); return result.readObject(Vertex.class); } public Vertex get(Object id) { - String vertexId = formatVertexId(id); + String vertexId = GraphAPI.formatVertexId(id); RestResult result = this.client.get(this.path(), vertexId); return result.readObject(Vertex.class); } @@ -115,7 +115,7 @@ public Vertices list(String label, Map properties, boolean keepP, int offset, String page, int limit) { checkOffset(offset); checkLimit(limit, "Limit"); - String props = formatProperties(properties); + String props = GraphAPI.formatProperties(properties); Map params = new LinkedHashMap<>(); params.put("label", label); params.put("properties", props); @@ -128,7 +128,7 @@ public Vertices list(String label, Map properties, } public void delete(Object id) { - String vertexId = formatVertexId(id); + String vertexId = GraphAPI.formatVertexId(id); this.client.delete(this.path(), vertexId); } } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graphs/GraphsAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graphs/GraphsAPI.java index 8310e44f1..1abf54b74 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/graphs/GraphsAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/graphs/GraphsAPI.java @@ -39,10 +39,19 @@ public class GraphsAPI extends API { private static final String GRAPH_READ_MODE = "graph_read_mode"; private static final String CLEAR = "clear"; private static final String CONFIRM_MESSAGE = "confirm_message"; + private static final String PATH = "graphspaces/%s/graphs"; - public GraphsAPI(RestClient client) { + public GraphsAPI(RestClient client, String graphSpace) { super(client); - this.path(this.type()); + this.path(String.format(PATH, graphSpace)); + } + + private static String joinPath(String path, String graph) { + return String.join(DELIMITER, path, graph); + } + + private static String joinPath(String path, String graph, String action) { + return String.join(DELIMITER, path, graph, action); } @Override @@ -130,12 +139,4 @@ public GraphReadMode readMode(String graph) { throw new InvalidResponseException("Invalid GraphReadMode value '%s'", value); } } - - private static String joinPath(String path, String graph) { - return String.join(DELIMITER, path, graph); - } - - private static String joinPath(String path, String graph, String action) { - return String.join(DELIMITER, path, graph, action); - } } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/CypherAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/CypherAPI.java index 63cf438b9..0d9ea5f96 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/CypherAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/CypherAPI.java @@ -20,24 +20,23 @@ import org.apache.hugegraph.api.API; import org.apache.hugegraph.client.RestClient; import org.apache.hugegraph.rest.RestResult; -import org.apache.hugegraph.structure.constant.HugeType; import org.apache.hugegraph.structure.gremlin.Response; public class CypherAPI extends API { - private static final String PATH = "graphs/%s/cypher"; + private static final String PATH = "graphspaces/%s/graphs/%s/cypher"; - public CypherAPI(RestClient client, String graph) { + public CypherAPI(RestClient client) { super(client); - this.path(PATH, graph); } @Override protected String type() { - return HugeType.CYPHER.string(); + return PATH; } - public Response post(String cypher) { + public Response post(String graphSpace, String graph, String cypher) { + this.path(type(), graphSpace, graph); RestResult result = this.client.post(this.path(), cypher); return result.readObject(Response.class); } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/GremlinAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/GremlinAPI.java index 31795d193..9cb652e44 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/GremlinAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/GremlinAPI.java @@ -39,4 +39,8 @@ public Response post(GremlinRequest request) { RestResult result = this.client.post(this.path(), request); return result.readObject(Response.class); } + + public boolean isSupportGs() { + return client.isSupportGs(); + } } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/CypherJobAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/CypherJobAPI.java new file mode 100644 index 000000000..e4163ff5f --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/CypherJobAPI.java @@ -0,0 +1,45 @@ +/* + * 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 org.apache.hugegraph.api.job; + +import java.util.Map; + +import org.apache.hugegraph.api.task.TaskAPI; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; + +public class CypherJobAPI extends JobAPI { + + private static final String JOB_TYPE = "cypher"; + + public CypherJobAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); + } + + @Override + protected String jobType() { + return JOB_TYPE; + } + + public long execute(String cypher) { + RestResult result = this.client.post(this.path(), cypher); + @SuppressWarnings("unchecked") + Map task = result.readObject(Map.class); + return TaskAPI.parseTaskId(task); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/GremlinJobAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/GremlinJobAPI.java index 36bb720be..5119e411e 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/GremlinJobAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/GremlinJobAPI.java @@ -28,8 +28,8 @@ public class GremlinJobAPI extends JobAPI { private static final String JOB_TYPE = "gremlin"; - public GremlinJobAPI(RestClient client, String graph) { - super(client, graph); + public GremlinJobAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/JobAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/JobAPI.java index da2300acb..d4d7e9d84 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/JobAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/JobAPI.java @@ -24,11 +24,11 @@ public abstract class JobAPI extends API { // For example: graphs/hugegraph/jobs/gremlin - private static final String PATH = "graphs/%s/%s/%s"; + private static final String PATH = "graphspaces/%s/graphs/%s/%s/%s"; - public JobAPI(RestClient client, String graph) { + public JobAPI(RestClient client, String graphSpace, String graph) { super(client); - this.path(String.format(PATH, graph, this.type(), this.jobType())); + this.path(String.format(PATH, graphSpace, graph, this.type(), this.jobType())); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java index d3ea915b0..49d8f8703 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java @@ -32,8 +32,8 @@ public class RebuildAPI extends JobAPI { private static final String JOB_TYPE = "rebuild"; - public RebuildAPI(RestClient client, String graph) { - super(client, graph); + public RebuildAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java index 08efe67a4..cc6f2ddc5 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java @@ -32,8 +32,8 @@ public class EdgeLabelAPI extends SchemaElementAPI { - public EdgeLabelAPI(RestClient client, String graph) { - super(client, graph); + public EdgeLabelAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java index aa8d08b79..b8b248841 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java @@ -34,8 +34,8 @@ public class IndexLabelAPI extends SchemaElementAPI { - public IndexLabelAPI(RestClient client, String graph) { - super(client, graph); + public IndexLabelAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java index 11b3546bd..414e8979b 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java @@ -34,8 +34,8 @@ public class PropertyKeyAPI extends SchemaElementAPI { - public PropertyKeyAPI(RestClient client, String graph) { - super(client, graph); + public PropertyKeyAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java index 5b114aa3b..078b7bce8 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java @@ -28,11 +28,11 @@ public class SchemaAPI extends API { - private static final String PATH = "graphs/%s/%s"; + private static final String PATH = "graphspaces/%s/graphs/%s/%s"; - public SchemaAPI(RestClient client, String graph) { + public SchemaAPI(RestClient client, String graphSpace, String graph) { super(client); - this.path(PATH, graph, this.type()); + this.path(PATH, graphSpace, graph, this.type()); } @SuppressWarnings("unchecked") diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaElementAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaElementAPI.java index 417294476..9e8936eeb 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaElementAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaElementAPI.java @@ -23,11 +23,11 @@ public abstract class SchemaElementAPI extends API { - private static final String PATH = "graphs/%s/schema/%s"; + private static final String PATH = "graphspaces/%s/graphs/%s/schema/%s"; - public SchemaElementAPI(RestClient client, String graph) { + public SchemaElementAPI(RestClient client, String graphSpace, String graph) { super(client); - this.path(PATH, graph, this.type()); + this.path(PATH, graphSpace, graph, this.type()); } protected abstract Object checkCreateOrUpdate(SchemaElement schemaElement); diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java index 79d2477ca..22eb4cc09 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java @@ -32,8 +32,8 @@ public class VertexLabelAPI extends SchemaElementAPI { - public VertexLabelAPI(RestClient client, String graph) { - super(client, graph); + public VertexLabelAPI(RestClient client, String graphSpace, String graph) { + super(client, graphSpace, graph); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ConfigAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ConfigAPI.java new file mode 100644 index 000000000..efb4ed5ff --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ConfigAPI.java @@ -0,0 +1,74 @@ +/* + * 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 org.apache.hugegraph.api.space; + +import com.google.common.collect.ImmutableMap; + +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; +import org.apache.hugegraph.structure.constant.HugeType; + +import java.util.List; +import java.util.Map; + +public class ConfigAPI extends API { + + private static final String PATH = "graphspaces/%s/configs/rest"; + + public ConfigAPI(RestClient client, String graphSpace) { + super(client); + this.path(String.format(PATH, graphSpace)); + } + + @Override + protected String type() { + return HugeType.CONFIGS.string(); + } + + public List listConfigOptions() { + RestResult result = client.get(this.path(), "config-fields"); + return result.readList("fields", String.class); + } + + public Map get(String serviceName) { + RestResult result = client.get(this.path(), serviceName); + + return result.readObject(Map.class); + } + + public Map add(String serviceName, + Map configs) { + ImmutableMap data + = ImmutableMap.of("name", serviceName, "config", configs); + RestResult result = client.post(this.path(), data); + + return result.readObject(Map.class); + } + + public Map update(String serviceName, + Map config) { + RestResult result = client.put(this.path(), serviceName, config); + + return result.readObject(Map.class); + } + + public void delete(String serviceName) { + client.delete(this.path(), serviceName); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java new file mode 100644 index 000000000..b04408fda --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java @@ -0,0 +1,153 @@ +/* + * 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 org.apache.hugegraph.api.space; + +import com.google.common.collect.ImmutableMap; + +import org.apache.commons.lang.StringUtils; +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; +import org.apache.hugegraph.structure.constant.HugeType; +import org.apache.hugegraph.structure.space.GraphSpace; +import org.apache.hugegraph.util.JsonUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class GraphSpaceAPI extends API { + + private static final String PATH = "graphspaces"; + private static final String DELIMITER = "/"; + + public GraphSpaceAPI(RestClient client) { + super(client); + this.path(PATH); + } + + private static String joinPath(String path, String id) { + return String.join(DELIMITER, path, id); + } + + @Override + protected String type() { + return HugeType.GRAPHSPACES.string(); + } + + public GraphSpace create(GraphSpace graphSpace) { + this.client.checkApiVersion("0.67", "dynamic graph space add"); + Object obj = graphSpace.convertReq(); + RestResult result = this.client.post(this.path(), obj); + return result.readObject(GraphSpace.class); + } + + public GraphSpace get(String name) { + RestResult result = this.client.get(this.path(), name); + return result.readObject(GraphSpace.class); + } + + public List list() { + RestResult result = this.client.get(this.path()); + + return result.readList(this.type(), String.class); + } + + public List> listProfile(String prefix) { + String profilePath = joinPath(this.path(), "profile"); + Map params = new LinkedHashMap<>(); + params.put("prefix", prefix); + RestResult result = this.client.get(profilePath, params); + List results = result.readList(Map.class); + List> profiles = new ArrayList<>(); + for (Object entry : results) { + profiles.add(JsonUtil.fromJson(JsonUtil.toJson(entry), Map.class)); + } + return profiles; + } + + public Map setDefault(String name) { + String defaultPath = joinPath(this.path(), name, "default"); + RestResult result = this.client.get(defaultPath); + return result.readObject(Map.class); + } + + public Map getDefault() { + String defaultPath = joinPath(this.path(), "default"); + RestResult result = this.client.get(defaultPath); + return result.readObject(Map.class); + } + + public Map setDefaultRole(String name, String user, + String role, String graph) { + String path = joinPath(this.path(), name, "role"); + Map params = new HashMap<>(); + params.put("user", user); + params.put("role", role); + if (StringUtils.isNotEmpty(graph)) { + params.put("graph", graph); + } + RestResult result = this.client.post(path, params); + return result.readObject(Map.class); + } + + public boolean checkDefaultRole(String name, String user, + String role, String graph) { + String path = joinPath(this.path(), name, "role"); + Map params = new HashMap<>(); + params.put("user", user); + params.put("role", role); + if (StringUtils.isNotEmpty(graph)) { + params.put("graph", graph); + } + RestResult result = this.client.get(path, params); + return (boolean) result.readObject(Map.class).getOrDefault("check", + false); + } + + public Map deleteDefaultRole(String name, String user, + String role, String graph) { + String path = joinPath(this.path(), name, "role"); + Map params = new HashMap<>(); + params.put("user", user); + params.put("role", role); + if (StringUtils.isNotEmpty(graph)) { + params.put("graph", graph); + } + RestResult result = this.client.delete(path, params); + return result.readObject(Map.class); + } + + public void delete(String name) { + this.client.delete(joinPath(this.path(), name), + ImmutableMap.of()); + } + + public GraphSpace update(GraphSpace graphSpace) { + Object obj = graphSpace.convertReq(); + RestResult result = this.client.put(this.path(), + graphSpace.getName(), + ImmutableMap.of("action", "update", + "update", + obj)); + + return result.readObject(GraphSpace.class); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/HStoreAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/HStoreAPI.java new file mode 100644 index 000000000..b43da323d --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/HStoreAPI.java @@ -0,0 +1,71 @@ +/* + * 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 org.apache.hugegraph.api.space; + +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; +import org.apache.hugegraph.structure.constant.HugeType; +import org.apache.hugegraph.structure.space.HStoreNodeInfo; + +import java.util.List; +import java.util.Map; + +public class HStoreAPI extends API { + + private static final String PATH = "hstore"; + + public HStoreAPI(RestClient client) { + super(client); + this.path(this.type()); + } + + @Override + protected String type() { + return HugeType.HSTORE.string(); + } + + public List list() { + RestResult result = this.client.get(this.path()); + return result.readList("nodes", String.class); + } + + public HStoreNodeInfo get(String id) { + RestResult result = this.client.get(this.path(), id); + return result.readObject(HStoreNodeInfo.class); + } + + public String status() { + RestResult result = this.client.get(this.path(), "status"); + return (String) result.readObject(Map.class).get("status"); + } + + public void startSplit() { + this.client.get(this.path(), "split"); + } + + public void nodeStartup(String id) { + String startupPath = joinPath(this.path(), id, "startup"); + this.client.get(startupPath); + } + + public void nodeShutdown(String id) { + String startupPath = joinPath(this.path(), id, "shutdown"); + this.client.get(startupPath); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/PDAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/PDAPI.java new file mode 100644 index 000000000..66be9440f --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/PDAPI.java @@ -0,0 +1,46 @@ +/* + * 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 org.apache.hugegraph.api.space; + +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; +import org.apache.hugegraph.structure.constant.HugeType; +import org.apache.hugegraph.structure.space.PDNodeInfo; + +import java.util.List; + +public class PDAPI extends API { + + private static final String PATH = "pd"; + + public PDAPI(RestClient client) { + super(client); + this.path(this.type()); + } + + @Override + protected String type() { + return HugeType.PD.string(); + } + + public List list() { + RestResult result = this.client.get(this.path()); + return result.readList("members", PDNodeInfo.class); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/SchemaTemplateAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/SchemaTemplateAPI.java new file mode 100644 index 000000000..700b8ea68 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/SchemaTemplateAPI.java @@ -0,0 +1,68 @@ +/* + * 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 org.apache.hugegraph.api.space; + +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; +import org.apache.hugegraph.structure.constant.HugeType; +import org.apache.hugegraph.structure.space.SchemaTemplate; + +import java.util.List; +import java.util.Map; + +public class SchemaTemplateAPI extends API { + + private static final String PATH = "graphspaces/%s/schematemplates"; + + public SchemaTemplateAPI(RestClient client, String graphSpace) { + super(client); + this.path(String.format(PATH, graphSpace)); + } + + @Override + protected String type() { + return HugeType.SCHEMATEMPLATES.string(); + } + + public Map create(SchemaTemplate template) { + RestResult result = this.client.post(this.path(), template); + return result.readObject(Map.class); + } + + public List list() { + RestResult result = this.client.get(this.path()); + return result.readList(this.type(), String.class); + } + + public Map get(String name) { + RestResult result = this.client.get(this.path(), name); + return result.readObject(Map.class); + } + + public void delete(String name) { + this.client.delete(this.path(), name); + } + + public Map update(SchemaTemplate template) { + RestResult result = this.client.put(this.path(), template.name(), + template); + + return result.readObject(Map.class); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ServiceAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ServiceAPI.java new file mode 100644 index 000000000..9355a2e27 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ServiceAPI.java @@ -0,0 +1,84 @@ +/* + * 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 org.apache.hugegraph.api.space; + +import com.google.common.collect.ImmutableMap; + +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; +import org.apache.hugegraph.structure.constant.HugeType; +import org.apache.hugegraph.structure.space.OLTPService; + +import java.util.List; +import java.util.Map; + +public class ServiceAPI extends API { + + private static final String PATH = "graphspaces/%s/services"; + private static final String CONFIRM_MESSAGE = "confirm_message"; + private static final String DELIMITER = "/"; + + public ServiceAPI(RestClient client, String graphSpace) { + super(client); + this.path(String.format(PATH, graphSpace)); + } + + private static String joinPath(String path, String id) { + return String.join(DELIMITER, path, id); + } + + @Override + protected String type() { + return HugeType.SERVICES.string(); + } + + public List list() { + RestResult result = this.client.get(this.path()); + + return result.readList(this.type(), String.class); + } + + public Object add(OLTPService.OLTPServiceReq req) { + RestResult result + = this.client.post(this.path(), req); + return result.readObject(Map.class); + } + + public void delete(String service, String message) { + this.client.delete(joinPath(this.path(), service), + ImmutableMap.of(CONFIRM_MESSAGE, message)); + } + + public OLTPService get(String serviceName) { + RestResult result = this.client.get(this.path(), serviceName); + + return result.readObject(OLTPService.class); + } + + public void startService(String serviceName) { + this.client.put(joinPath(this.path(), "start"), serviceName, + ImmutableMap.of()); + } + + public void stopService(String serviceName) { + this.client.put(joinPath(this.path(), "stop"), serviceName, + ImmutableMap.of()); + } +} + diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ServiceOptionAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ServiceOptionAPI.java new file mode 100644 index 000000000..f47de8551 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/space/ServiceOptionAPI.java @@ -0,0 +1,45 @@ +/* + * 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 org.apache.hugegraph.api.space; + +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.rest.RestResult; +import org.apache.hugegraph.structure.constant.HugeType; + +import java.util.List; + +public class ServiceOptionAPI extends API { + + private static final String PATH = ""; + + public ServiceOptionAPI(RestClient client) { + super(client); + } + + @Override + protected String type() { + return HugeType.SERVICEOPTIONS.string(); + } + + public Object get() { + RestResult result = this.client.get(this.path()); + + return result.readObject(List.class); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/task/TaskAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/task/TaskAPI.java index b348ef017..c0f048e6b 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/task/TaskAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/task/TaskAPI.java @@ -21,7 +21,6 @@ import java.util.List; import java.util.Map; -import org.apache.hugegraph.util.TaskCache; import org.apache.hugegraph.api.API; import org.apache.hugegraph.client.RestClient; import org.apache.hugegraph.rest.ClientException; @@ -29,24 +28,37 @@ import org.apache.hugegraph.structure.Task; import org.apache.hugegraph.structure.constant.HugeType; import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.TaskCache; import com.google.common.collect.ImmutableMap; public class TaskAPI extends API { - private static final String PATH = "graphs/%s/tasks"; - private String graph; public static final String TASKS = "tasks"; public static final String TASK_ID = "task_id"; public static final long TASK_TIMEOUT = 60L; + private static final String PATH = "graphspaces/%s/graphs/%s/tasks"; private static final long QUERY_INTERVAL = 500L; + private final String graphSpace; + private final String graph; - public TaskAPI(RestClient client, String graph) { + public TaskAPI(RestClient client, String graphSpace, String graph) { super(client); - this.path(String.format(PATH, graph)); + this.path(String.format(PATH, graphSpace, graph)); + this.graphSpace = graphSpace; this.graph = graph; } + public static long parseTaskId(Map task) { + E.checkState(task.size() == 1 && task.containsKey(TASK_ID), + "Task must be formatted to {\"%s\" : id}, but got %s", + TASK_ID, task); + Object taskId = task.get(TASK_ID); + E.checkState(taskId instanceof Number, + "Task id must be number, but got '%s'", taskId); + return ((Number) taskId).longValue(); + } + @Override protected String type() { return HugeType.TASK.string(); @@ -142,14 +154,4 @@ private Task getFromCache(long taskId) { private void removeFromCache(long taskId) { TaskCache.instance().remove(this, taskId); } - - public static long parseTaskId(Map task) { - E.checkState(task.size() == 1 && task.containsKey(TASK_ID), - "Task must be formatted to {\"%s\" : id}, but got %s", - TASK_ID, task); - Object taskId = task.get(TASK_ID); - E.checkState(taskId instanceof Number, - "Task id must be number, but got '%s'", taskId); - return ((Number) taskId).longValue(); - } } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/variables/VariablesAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/variables/VariablesAPI.java index 016be729b..a7825596f 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/variables/VariablesAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/variables/VariablesAPI.java @@ -28,11 +28,13 @@ public class VariablesAPI extends API { - private static final String PATH = "graphs/%s/%s"; + private static final String PATH = "graphspaces/%s/graphs/%s/%s"; + private static String batchPath = ""; - public VariablesAPI(RestClient client, String graph) { + public VariablesAPI(RestClient client, String graphSpace, String graph) { super(client); - this.path(PATH, graph, this.type()); + this.path(PATH, graphSpace, graph, this.type()); + batchPath = String.join("/", this.path(), "batch"); } @Override diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/api/version/VersionAPI.java b/hugegraph-client/src/main/java/org/apache/hugegraph/api/version/VersionAPI.java index 2071a3411..d192fae04 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/api/version/VersionAPI.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/api/version/VersionAPI.java @@ -36,7 +36,7 @@ protected String type() { } public Versions get() { - RestResult result = this.client.get(this.path()); + RestResult result = this.client.getVersions(this.path()); return result.readObject(Versions.class); } } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/client/RestClient.java b/hugegraph-client/src/main/java/org/apache/hugegraph/client/RestClient.java index 25462c704..5d29c845c 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/client/RestClient.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/client/RestClient.java @@ -17,10 +17,13 @@ package org.apache.hugegraph.client; +import java.util.Map; + import org.apache.hugegraph.exception.ServerException; import org.apache.hugegraph.rest.AbstractRestClient; import org.apache.hugegraph.rest.ClientException; import org.apache.hugegraph.rest.RestClientConfig; +import org.apache.hugegraph.rest.RestHeaders; import org.apache.hugegraph.rest.RestResult; import org.apache.hugegraph.serializer.PathDeserializer; import org.apache.hugegraph.structure.graph.Path; @@ -30,18 +33,24 @@ import com.fasterxml.jackson.databind.module.SimpleModule; +import lombok.Getter; +import lombok.Setter; + public class RestClient extends AbstractRestClient { private static final int SECOND = 1000; - private Version apiVersion = null; - static { SimpleModule module = new SimpleModule(); module.addDeserializer(Path.class, new PathDeserializer()); RestResult.registerModule(module); } + private Version apiVersion = null; + @Setter + @Getter + private boolean supportGs = false; + public RestClient(String url, String username, String password, int timeout) { super(url, username, password, timeout * SECOND); } @@ -58,6 +67,12 @@ public RestClient(String url, RestClientConfig config) { super(url, config); } + private static String removeDefaultGsPrefix(String path) { + final String DEFAULT_GS_PATH_PREFIX = "graphspaces/DEFAULT/"; + final String EMPTY = ""; + return path.replaceFirst(DEFAULT_GS_PATH_PREFIX, EMPTY); + } + public void apiVersion(Version version) { E.checkNotNull(version, "api version"); this.apiVersion = version; @@ -80,6 +95,81 @@ public boolean apiVersionLt(String minVersion) { return apiVersion != null && !VersionUtil.gte(apiVersion, minVersion); } + @Override + public RestResult post(String path, Object object) { + return super.post(supportGs ? path : removeDefaultGsPrefix(path), object); + } + + @Override + public RestResult get(String path, String id) { + return super.get(supportGs ? path : removeDefaultGsPrefix(path), id); + } + + public RestResult getVersions(String path) { + return super.get(path); + } + + @Override + public RestResult delete(String path, Map params) { + return super.delete(supportGs ? path : removeDefaultGsPrefix(path), params); + } + + @Override + public RestResult delete(String path, String id) { + return super.delete(supportGs ? path : removeDefaultGsPrefix(path), id); + } + + @Override + public RestResult post(String path, Object object, RestHeaders headers) { + return super.post(supportGs ? path : removeDefaultGsPrefix(path), object, headers); + } + + @Override + public RestResult post(String path, Object object, Map params) { + return super.post(supportGs ? path : removeDefaultGsPrefix(path), object, params); + } + + @Override + public RestResult post(String path, Object object, RestHeaders headers, + Map params) { + return super.post(supportGs ? path : removeDefaultGsPrefix(path), object, headers, params); + } + + @Override + public RestResult put(String path, String id, Object object) { + return super.put(supportGs ? path : removeDefaultGsPrefix(path), id, object); + } + + @Override + public RestResult put(String path, String id, Object object, RestHeaders headers) { + return super.put(supportGs ? path : removeDefaultGsPrefix(path), id, object, + headers); + } + + @Override + public RestResult put(String path, String id, Object object, Map params) { + return super.put(supportGs ? path : removeDefaultGsPrefix(path), id, object, + params); + } + + @Override + public RestResult put(String path, String id, Object object, + RestHeaders headers, + Map params) { + return super.put(supportGs ? path : removeDefaultGsPrefix(path), id, object, headers, + params); + } + + @Override + public RestResult get(String path) { + return super.get(supportGs ? path : removeDefaultGsPrefix(path)); + } + + @Override + public RestResult get(String path, Map params) { + return super.get(supportGs ? path : removeDefaultGsPrefix(path), params); + } + @Override protected void checkStatus(okhttp3.Response response, int... statuses) { boolean match = false; diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/CypherManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/CypherManager.java index c53c48a94..498e568a0 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/CypherManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/CypherManager.java @@ -18,25 +18,42 @@ package org.apache.hugegraph.driver; import org.apache.hugegraph.api.gremlin.CypherAPI; +import org.apache.hugegraph.api.job.CypherJobAPI; +import org.apache.hugegraph.client.RestClient; import org.apache.hugegraph.structure.gremlin.Response; import org.apache.hugegraph.structure.gremlin.ResultSet; -import org.apache.hugegraph.client.RestClient; public class CypherManager { private final GraphManager graphManager; + private final CypherJobAPI cypherJobAPI; + private final String graphSpace; + private final String graph; + private final CypherAPI cypherAPI; - public CypherManager(RestClient client, String graph, + public CypherManager(RestClient client, String graphSpace, String graph, GraphManager graphManager) { this.graphManager = graphManager; - this.cypherAPI = new CypherAPI(client, graph); + this.cypherAPI = new CypherAPI(client); + this.cypherJobAPI = new CypherJobAPI(client, graphSpace, graph); + this.graphSpace = graphSpace; + this.graph = graph; + } + + public ResultSet cypher(String cypher) { + return execute(cypher); } public ResultSet execute(String cypher) { - Response response = this.cypherAPI.post(cypher); + Response response = this.cypherAPI.post(this.graphSpace, this.graph, cypher); response.graphManager(this.graphManager); // TODO: Can add some checks later return response.result(); } + + public long executeAsTask(String cypher) { + return this.cypherJobAPI.execute(cypher); + } + } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphManager.java index 702d3c2f2..98b82cd42 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphManager.java @@ -39,14 +39,20 @@ public class GraphManager { + private final String graphSpace; private final String graph; private final VertexAPI vertexAPI; private final EdgeAPI edgeAPI; - public GraphManager(RestClient client, String graph) { + public GraphManager(RestClient client, String graphSpace, String graph) { + this.graphSpace = graphSpace; this.graph = graph; - this.vertexAPI = new VertexAPI(client, graph); - this.edgeAPI = new EdgeAPI(client, graph); + this.vertexAPI = new VertexAPI(client, graphSpace, graph); + this.edgeAPI = new EdgeAPI(client, graphSpace, graph); + } + + public String graphSpace() { + return this.graphSpace; } public String graph() { diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphSpaceManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphSpaceManager.java new file mode 100644 index 000000000..fd0ee9ebd --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphSpaceManager.java @@ -0,0 +1,100 @@ +/* + * 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 org.apache.hugegraph.driver; + +import java.util.List; +import java.util.Map; + +import org.apache.hugegraph.api.space.GraphSpaceAPI; +import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.structure.space.GraphSpace; + +public class GraphSpaceManager { + + private final GraphSpaceAPI graphSpaceAPI; + + public GraphSpaceManager(RestClient client) { + this.graphSpaceAPI = new GraphSpaceAPI(client); + } + + public List listGraphSpace() { + return this.graphSpaceAPI.list(); + } + + public List> listProfile() { + return this.graphSpaceAPI.listProfile(null); + } + + public List> listProfile(String prefix) { + return this.graphSpaceAPI.listProfile(prefix); + } + + public Map setDefault(String name) { + return this.graphSpaceAPI.setDefault(name); + } + + public Map getDefault() { + return this.graphSpaceAPI.getDefault(); + } + + public GraphSpace getGraphSpace(String name) { + return this.graphSpaceAPI.get(name); + } + + public GraphSpace createGraphSpace(GraphSpace graphSpace) { + return this.graphSpaceAPI.create(graphSpace); + } + + public void deleteGraphSpace(String name) { + this.graphSpaceAPI.delete(name); + } + + public GraphSpace updateGraphSpace(GraphSpace graphSpace) { + return this.graphSpaceAPI.update(graphSpace); + } + + public Map setDefaultRole(String name, String user, + String role) { + return this.graphSpaceAPI.setDefaultRole(name, user, role, ""); + } + + public Map setDefaultRole(String name, String user, + String role, String graph) { + return this.graphSpaceAPI.setDefaultRole(name, user, role, graph); + } + + public boolean checkDefaultRole(String name, String user, + String role) { + return this.graphSpaceAPI.checkDefaultRole(name, user, role, ""); + } + + public boolean checkDefaultRole(String name, String user, + String role, String graph) { + return this.graphSpaceAPI.checkDefaultRole(name, user, role, graph); + } + + public Map deleteDefaultRole(String name, String user, + String role) { + return this.graphSpaceAPI.deleteDefaultRole(name, user, role, ""); + } + + public Map deleteDefaultRole(String name, String user, + String role, String graph) { + return this.graphSpaceAPI.deleteDefaultRole(name, user, role, graph); + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphsManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphsManager.java index a9674d876..e4ebf40cf 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphsManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphsManager.java @@ -29,8 +29,8 @@ public class GraphsManager { private GraphsAPI graphsAPI; - public GraphsManager(RestClient client) { - this.graphsAPI = new GraphsAPI(client); + public GraphsManager(RestClient client, String graphSpace) { + this.graphsAPI = new GraphsAPI(client, graphSpace); } public Map createGraph(String name, String configText) { diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GremlinManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GremlinManager.java index a558e3e15..b77a9d3a7 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GremlinManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/GremlinManager.java @@ -20,31 +20,40 @@ import org.apache.hugegraph.api.gremlin.GremlinAPI; import org.apache.hugegraph.api.gremlin.GremlinRequest; import org.apache.hugegraph.api.job.GremlinJobAPI; +import org.apache.hugegraph.client.RestClient; import org.apache.hugegraph.structure.gremlin.Response; import org.apache.hugegraph.structure.gremlin.ResultSet; -import org.apache.hugegraph.client.RestClient; public class GremlinManager { private final GraphManager graphManager; - private GremlinAPI gremlinAPI; - private GremlinJobAPI gremlinJobAPI; - private String graph; + private final GremlinAPI gremlinAPI; + private final GremlinJobAPI gremlinJobAPI; + private final String graphSpace; + private final String graph; - public GremlinManager(RestClient client, String graph, + public GremlinManager(RestClient client, String graphSpace, String graph, GraphManager graphManager) { this.graphManager = graphManager; this.gremlinAPI = new GremlinAPI(client); - this.gremlinJobAPI = new GremlinJobAPI(client, graph); + this.gremlinJobAPI = new GremlinJobAPI(client, graphSpace, graph); + this.graphSpace = graphSpace; this.graph = graph; } public ResultSet execute(GremlinRequest request) { - // Bind "graph" to all graphs - request.aliases.put("graph", this.graph); - // Bind "g" to all graphs by custom rule which define in gremlin server. - request.aliases.put("g", "__g_" + this.graph); + if (this.gremlinAPI.isSupportGs()) { + // Bind "graph" and graph space to all graphs + request.aliases.put("graph", this.graphSpace + "-" + this.graph); + // Bind "g" and graph space to all graphs by custom rule which define in gremlin server. + request.aliases.put("g", "__g_" + this.graphSpace + "-" + this.graph); + } else { + // Bind "graph" to all graphs + request.aliases.put("graph", this.graph); + // Bind "g" to all graphs by custom rule which define in gremlin server. + request.aliases.put("g", "__g_" + this.graph); + } Response response = this.gremlinAPI.post(request); response.graphManager(this.graphManager); diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java index 798405d7d..8a9ef20cb 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java @@ -27,6 +27,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Strings; + /** * The HugeClient class is the main entry point for interacting with a HugeGraph server. * It provides methods for managing graphs, schemas, jobs, tasks, and other resources. @@ -42,7 +44,9 @@ public class HugeClient implements Closeable { private final RestClient client; private final boolean borrowedClient; - + protected String graphSpaceName; + protected String graphName; + protected GraphSpaceManager graphSpace; private VersionManager version; private GraphsManager graphs; private SchemaManager schema; @@ -63,9 +67,12 @@ public class HugeClient implements Closeable { */ public HugeClient(HugeClientBuilder builder) { this.borrowedClient = false; + this.graphSpaceName = builder.graphSpace(); + this.graphName = builder.graph(); RestClientConfig config; try { config = RestClientConfig.builder() + .token(builder.token()) .user(builder.username()) .password(builder.password()) .timeout(builder.timeout()) @@ -84,7 +91,7 @@ public HugeClient(HugeClientBuilder builder) { } try { - this.initManagers(this.client, builder.graph()); + this.initManagers(this.client, builder.graphSpace(), builder.graph()); } catch (Throwable e) { // TODO: catch some exception(like IO/Network related) rather than throw Throwable this.client.close(); @@ -92,14 +99,26 @@ public HugeClient(HugeClientBuilder builder) { } } - public HugeClient(HugeClient client, String graph) { + // QUESTION: add gs to method param? + public HugeClient(HugeClient client, String graphSpace, String graph) { this.borrowedClient = true; this.client = client.client; - this.initManagers(this.client, graph); + this.initManagers(this.client, graphSpace, graph); + } + + public static HugeClientBuilder builder(String url, String graphSpace, String graph) { + return new HugeClientBuilder(url, graphSpace, graph); } public static HugeClientBuilder builder(String url, String graph) { - return new HugeClientBuilder(url, graph); + return new HugeClientBuilder(url, HugeClientBuilder.DEFAULT_GRAPHSPACE, graph); + } + + public HugeClient assignGraph(String graphSpace, String graph) { + this.graphSpaceName = graphSpace; + this.graphName = graph; + this.initManagers(this.client, this.graphSpaceName, this.graphName); + return this; } @Override @@ -109,23 +128,29 @@ public void close() { } } - private void initManagers(RestClient client, String graph) { + public void initManagers(RestClient client, String graphSpace, + String graph) { assert client != null; // Check hugegraph-server api version this.version = new VersionManager(client); this.checkServerApiVersion(); - this.graphs = new GraphsManager(client); - this.schema = new SchemaManager(client, graph); - this.graph = new GraphManager(client, graph); - this.gremlin = new GremlinManager(client, graph, this.graph); - this.cypher = new CypherManager(client, graph, this.graph); - this.traverser = new TraverserManager(client, this.graph); - this.variable = new VariablesManager(client, graph); - this.job = new JobManager(client, graph); - this.task = new TaskManager(client, graph); + this.graphs = new GraphsManager(client, graphSpace); this.auth = new AuthManager(client, graph); this.metrics = new MetricsManager(client); + this.graphSpace = new GraphSpaceManager(client); + if (!Strings.isNullOrEmpty(graph)) { + this.schema = new SchemaManager(client, graphSpace, graph); + this.graph = new GraphManager(client, graphSpace, graph); + this.gremlin = new GremlinManager(client, graphSpace, + graph, this.graph); + this.cypher = new CypherManager(client, graphSpace, + graph, this.graph); + this.traverser = new TraverserManager(client, this.graph); + this.variable = new VariablesManager(client, graphSpace, graph); + this.job = new JobManager(client, graphSpace, graph); + this.task = new TaskManager(client, graphSpace, graph); + } } private void checkServerApiVersion() { @@ -134,6 +159,16 @@ private void checkServerApiVersion() { // 0.81 equals to the {latest_api_version} +10 VersionUtil.check(apiVersion, "0.38", "0.81", "hugegraph-api in server"); this.client.apiVersion(apiVersion); + boolean supportGs = VersionUtil.gte(this.version.getCoreVersion(), "2.0"); + this.client.setSupportGs(supportGs); + } + + public String getGraphSpaceName() { + return graphSpaceName; + } + + public String getGraphName() { + return graphName; } public GraphsManager graphs() { @@ -180,14 +215,22 @@ public MetricsManager metrics() { return this.metrics; } - public void setAuthContext(String auth) { - this.client.setAuthContext(auth); + public GraphSpaceManager graphSpace() { + return this.graphSpace; + } + + public VersionManager versionManager() { + return version; } public String getAuthContext() { return this.client.getAuthContext(); } + public void setAuthContext(String auth) { + this.client.setAuthContext(auth); + } + public void resetAuthContext() { this.client.resetAuthContext(); } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java index b3f684121..9b9c03b9e 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java @@ -25,17 +25,21 @@ public class HugeClientBuilder { + static final String DEFAULT_GRAPHSPACE = "DEFAULT"; + private static final int CPUS = Runtime.getRuntime().availableProcessors(); - private static final int DEFAULT_TIMEOUT = 20; private static final int DEFAULT_MAX_CONNS = 4 * CPUS; private static final int DEFAULT_MAX_CONNS_PER_ROUTE = 2 * CPUS; private static final int DEFAULT_IDLE_TIME = 30; private static final int SECOND = 1000; + private static final int DEFAULT_TIMEOUT = 20 * SECOND; private String url; + private String graphSpace; private String graph; private String username; private String password; + private String token; private int timeout; private int maxConns; private int maxConnsPerRoute; @@ -47,17 +51,19 @@ public class HugeClientBuilder { private Integer connectTimeout; private Integer readTimeout; - public HugeClientBuilder(String url, String graph) { + public HugeClientBuilder(String url, String graphSpace, String graph) { E.checkArgument(url != null && !url.isEmpty(), "Expect a string value as the url parameter argument, but got: %s", url); E.checkArgument(graph != null && !graph.isEmpty(), "Expect a string value as the graph name parameter argument, but got: %s", graph); this.url = url; + this.graphSpace = graphSpace; this.graph = graph; this.username = ""; this.password = ""; - this.timeout = DEFAULT_TIMEOUT * SECOND; + this.token = ""; + this.timeout = DEFAULT_TIMEOUT; this.maxConns = DEFAULT_MAX_CONNS; this.maxConnsPerRoute = DEFAULT_MAX_CONNS_PER_ROUTE; @@ -75,6 +81,11 @@ public HugeClient build() { return new HugeClient(this); } + public HugeClientBuilder configGraphSpace(String graphSpace) { + this.graphSpace = graphSpace; + return this; + } + public HugeClientBuilder configGraph(String graph) { this.graph = graph; return this; @@ -149,10 +160,22 @@ public HugeClientBuilder configHttpBuilder(Consumer builde return this; } + public HugeClientBuilder configToken(String token) { + if (token != null) { + this.token = token; + } + + return this; + } + public String url() { return this.url; } + public String graphSpace() { + return this.graphSpace; + } + public String graph() { return this.graph; } @@ -165,6 +188,10 @@ public String password() { return this.password; } + public String token() { + return this.token; + } + public int timeout() { return this.timeout; } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/JobManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/JobManager.java index 501a2e57f..ed4c92bd6 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/JobManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/JobManager.java @@ -29,9 +29,9 @@ public class JobManager { private RebuildAPI rebuildAPI; private TaskAPI taskAPI; - public JobManager(RestClient client, String graph) { - this.rebuildAPI = new RebuildAPI(client, graph); - this.taskAPI = new TaskAPI(client, graph); + public JobManager(RestClient client, String graphSpace, String graph) { + this.rebuildAPI = new RebuildAPI(client, graphSpace, graph); + this.taskAPI = new TaskAPI(client, graphSpace, graph); } public void rebuild(VertexLabel vertexLabel) { diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/SchemaManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/SchemaManager.java index 8b088db3e..9a13205f9 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/SchemaManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/SchemaManager.java @@ -43,13 +43,13 @@ public class SchemaManager { private SchemaAPI schemaAPI; private TaskAPI taskAPI; - public SchemaManager(RestClient client, String graph) { - this.propertyKeyAPI = new PropertyKeyAPI(client, graph); - this.vertexLabelAPI = new VertexLabelAPI(client, graph); - this.edgeLabelAPI = new EdgeLabelAPI(client, graph); - this.indexLabelAPI = new IndexLabelAPI(client, graph); - this.schemaAPI = new SchemaAPI(client, graph); - this.taskAPI = new TaskAPI(client, graph); + public SchemaManager(RestClient client, String graphSpace, String graph) { + this.propertyKeyAPI = new PropertyKeyAPI(client, graphSpace, graph); + this.vertexLabelAPI = new VertexLabelAPI(client, graphSpace, graph); + this.edgeLabelAPI = new EdgeLabelAPI(client, graphSpace, graph); + this.indexLabelAPI = new IndexLabelAPI(client, graphSpace, graph); + this.schemaAPI = new SchemaAPI(client, graphSpace, graph); + this.taskAPI = new TaskAPI(client, graphSpace, graph); } public PropertyKey.Builder propertyKey(String name) { diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/TaskManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/TaskManager.java index 6d92e416b..e04a6311d 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/TaskManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/TaskManager.java @@ -21,15 +21,15 @@ import org.apache.hugegraph.api.task.TaskAPI; import org.apache.hugegraph.api.task.TasksWithPage; -import org.apache.hugegraph.structure.Task; import org.apache.hugegraph.client.RestClient; +import org.apache.hugegraph.structure.Task; public class TaskManager { private TaskAPI taskAPI; - public TaskManager(RestClient client, String graph) { - this.taskAPI = new TaskAPI(client, graph); + public TaskManager(RestClient client, String graphSpace, String graph) { + this.taskAPI = new TaskAPI(client, graphSpace, graph); } public List list() { diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/VariablesManager.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/VariablesManager.java index a62bb708f..535ad2992 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/VariablesManager.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/VariablesManager.java @@ -17,17 +17,17 @@ package org.apache.hugegraph.driver; +import java.util.Map; + import org.apache.hugegraph.api.variables.VariablesAPI; import org.apache.hugegraph.client.RestClient; -import java.util.Map; - public class VariablesManager { - private VariablesAPI variablesAPI; + private final VariablesAPI variablesAPI; - public VariablesManager(RestClient client, String graph) { - this.variablesAPI = new VariablesAPI(client, graph); + public VariablesManager(RestClient client, String graphSpace, String graph) { + this.variablesAPI = new VariablesAPI(client, graphSpace, graph); } public Map get(String key) { diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/factory/DefaultHugeClientFactory.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/factory/DefaultHugeClientFactory.java new file mode 100644 index 000000000..33a86c9e9 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/factory/DefaultHugeClientFactory.java @@ -0,0 +1,63 @@ +/* + * 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 org.apache.hugegraph.driver.factory; + +import org.apache.hugegraph.driver.HugeClient; +import org.apache.hugegraph.util.E; + +public class DefaultHugeClientFactory { + + private final String defaultHugeGraph = "hugegraph"; + private final String[] urls; + + public DefaultHugeClientFactory(String[] urls) { + this.urls = urls; + } + + public HugeClient createClient(String graphSpace, String graph) { + return this.createClient(graphSpace, graph, 60); + } + + public HugeClient createClient(String graphSpace, String graph, + int timeout) { + return this.createClient(graphSpace, graph, null, null, null, timeout); + } + + public HugeClient createClient(String graphSpace, String graph, + String token, String username, + String password) { + return createClient(graphSpace, graph, token, username, password, 60); + } + + public HugeClient createClient(String graphSpace, String graph, + String token, String username, + String password, int timeout) { + E.checkArgument(timeout > 0, "Client timeout must > 0"); + + int r = (int) Math.floor(Math.random() * urls.length); + String url = this.urls[r]; + + HugeClient client = HugeClient.builder(url, graphSpace, graph) + .configToken(token) + .configUser(username, password) + .configTimeout(timeout) + .build(); + + return client; + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/example/BatchExample.java b/hugegraph-client/src/main/java/org/apache/hugegraph/example/BatchExample.java index 7b86a099c..c7bec016d 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/example/BatchExample.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/example/BatchExample.java @@ -20,17 +20,18 @@ import java.util.ArrayList; import java.util.List; -import org.apache.hugegraph.structure.graph.Edge; -import org.apache.hugegraph.structure.graph.Vertex; import org.apache.hugegraph.driver.GraphManager; import org.apache.hugegraph.driver.HugeClient; import org.apache.hugegraph.driver.SchemaManager; +import org.apache.hugegraph.structure.graph.Edge; +import org.apache.hugegraph.structure.graph.Vertex; public class BatchExample { public static void main(String[] args) { // If connect failed will throw an exception. HugeClient hugeClient = HugeClient.builder("http://localhost:8080", + "DEFAULT", "hugegraph").build(); SchemaManager schema = hugeClient.schema(); diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/example/MovieExample.java b/hugegraph-client/src/main/java/org/apache/hugegraph/example/MovieExample.java index 15fb4e843..aec7e49a4 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/example/MovieExample.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/example/MovieExample.java @@ -17,17 +17,18 @@ package org.apache.hugegraph.example; -import org.apache.hugegraph.structure.constant.T; -import org.apache.hugegraph.structure.graph.Vertex; import org.apache.hugegraph.driver.GraphManager; import org.apache.hugegraph.driver.HugeClient; import org.apache.hugegraph.driver.SchemaManager; +import org.apache.hugegraph.structure.constant.T; +import org.apache.hugegraph.structure.graph.Vertex; public class MovieExample { public static void main(String[] args) { // If connect failed will throw a exception. HugeClient hugeClient = HugeClient.builder("http://localhost:8080", + "DEFAULT", "hugegraph").build(); SchemaManager schema = hugeClient.schema(); diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/example/SingleExample.java b/hugegraph-client/src/main/java/org/apache/hugegraph/example/SingleExample.java index 6c710ed03..66eb98dfb 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/example/SingleExample.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/example/SingleExample.java @@ -21,23 +21,26 @@ import java.util.Iterator; import java.util.List; +import org.apache.hugegraph.driver.GraphManager; +import org.apache.hugegraph.driver.GremlinManager; +import org.apache.hugegraph.driver.HugeClient; +import org.apache.hugegraph.driver.SchemaManager; import org.apache.hugegraph.structure.constant.T; import org.apache.hugegraph.structure.graph.Edge; import org.apache.hugegraph.structure.graph.Path; import org.apache.hugegraph.structure.graph.Vertex; import org.apache.hugegraph.structure.gremlin.Result; import org.apache.hugegraph.structure.gremlin.ResultSet; -import org.apache.hugegraph.driver.GraphManager; -import org.apache.hugegraph.driver.GremlinManager; -import org.apache.hugegraph.driver.HugeClient; -import org.apache.hugegraph.driver.SchemaManager; public class SingleExample { public static void main(String[] args) throws IOException { // If connect failed will throw a exception. - HugeClient hugeClient = HugeClient.builder("http://localhost:8080", - "hugegraph").build(); + HugeClient hugeClient = HugeClient.builder("http://127.0.0.1:8080", + "DEFAULT", + "hugegraph") + .configUser("admin", "admin") + .build(); SchemaManager schema = hugeClient.schema(); diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/serializer/direct/reuse/BytesDemo.java b/hugegraph-client/src/main/java/org/apache/hugegraph/serializer/direct/reuse/BytesDemo.java index ea7bbbd9a..4ab709f10 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/serializer/direct/reuse/BytesDemo.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/serializer/direct/reuse/BytesDemo.java @@ -50,7 +50,7 @@ void initGraph() { int edgeLogicPartitions = 16; int vertexLogicPartitions = 8; // If connect failed will throw an exception. - client = HugeClient.builder("http://localhost:8081", "hugegraph").build(); + client = HugeClient.builder("http://localhost:8081", "DEFAULT", "hugegraph").build(); SchemaManager schema = client.schema(); diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/HugeType.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/HugeType.java index b28716fc8..7dc1efc2a 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/HugeType.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/HugeType.java @@ -17,6 +17,9 @@ package org.apache.hugegraph.structure.constant; +import java.util.HashMap; +import java.util.Map; + public enum HugeType { // Schema @@ -35,6 +38,11 @@ public enum HugeType { LOGIN(56, "login"), LOGOUT(57, "logout"), TOKEN_VERIFY(58, "verify"), + MANAGER(59, "managers"), + KG_LOGIN(60, "kglogin"), + ROLE(61, "roles"), + WHITE_IP_LIST(62, "whiteiplist"), + VERMEER(63, "vermeer"), // Data VERTEX(101, "vertices"), @@ -49,6 +57,15 @@ public enum HugeType { // Job JOB(150, "jobs"), + // Services + SERVICES(190, "services"), + + // Service configs + SERVICECONFIGS(192, "service_configs"), + + // Service options + SERVICEOPTIONS(194, "service_options"), + // Gremlin GREMLIN(201, "gremlin"), @@ -61,17 +78,50 @@ public enum HugeType { VERSION(230, "versions"), // Metrics - METRICS(240, "metrics"); + METRICS(240, "metrics"), + + // Configs + CONFIGS(249, "configs"), + + // GraphSpaces + GRAPHSPACES(250, "graphSpaces"), + + // SchemeTemplate + SCHEMATEMPLATES(252, "schema_templates"), + + // PD + PD(253, "pd"), + + // HStore + HSTORE(254, "hstore"), + + // System Graph + SYSTEM_GRAPH(255, "system_graph"), + + // graph auth + GRAPH_AUTH(256, "graph_auth"); + + static Map fromNames = new HashMap<>(); + + static { + for (HugeType type : HugeType.values()) { + fromNames.put(type.name, type); + } + } private final int code; private final String name; HugeType(int code, String name) { - assert code < 256; + assert code < 300; this.code = code; this.name = name; } + public static HugeType fromName(String name) { + return fromNames.get(name); + } + public int code() { return this.code; } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/GraphSpace.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/GraphSpace.java new file mode 100644 index 000000000..5120e7e46 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/GraphSpace.java @@ -0,0 +1,360 @@ +/* + * 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 org.apache.hugegraph.structure.space; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.HashMap; +import java.util.Map; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class GraphSpace { + + @JsonProperty("store_group") + public int storeGroup; + @JsonProperty("storage_limit") + public int storageLimit; // GB + @JsonProperty("oltp_namespace") + public String oltpNamespace; + @JsonProperty("name") + private String name; + @JsonProperty("nickname") + private String nickname; + @JsonProperty("description") + private String description; + @JsonProperty("cpu_limit") + private int cpuLimit; + @JsonProperty("memory_limit") + private int memoryLimit; // GB + @JsonProperty("compute_cpu_limit") + private int computeCpuLimit; + @JsonProperty("compute_memory_limit") + private int computeMemoryLimit; // GB + @JsonProperty("max_graph_number") + private int maxGraphNumber; + @JsonProperty("max_role_number") + private int maxRoleNumber; + @JsonProperty("operator_image_path") + private String operatorImagePath; // + @JsonProperty("internal_algorithm_image_url") + private String internalAlgorithmImageUrl; + @JsonProperty("olap_namespace") + private String olapNamespace; + @Deprecated + @JsonProperty("storage_namespace") + private String storageNamespace; + + @JsonProperty("cpu_used") + private int cpuUsed; + @JsonProperty("memory_used") + private int memoryUsed; // GB + @JsonProperty("storage_used") + private int storageUsed; // GB + @JsonProperty("storage_percent") + private double storagePercent; + @JsonProperty("graph_number_used") + private int graphNumberUsed; + @JsonProperty("role_number_used") + private int roleNumberUsed; + @JsonProperty("auth") + private boolean auth = false; + @JsonProperty("dp_username") + private String dpUserName; + @JsonProperty("dp_password") + private String dpPassWord; + + @JsonIgnore + private String createTime; + @JsonIgnore + private String updateTime; + + @JsonProperty("configs") + private Map configs = new HashMap<>(); + + public GraphSpace() { + } + + public GraphSpace(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public GraphSpace setName(String name) { + this.name = name; + return this; + } + + public String getNickname() { + return nickname; + } + + public GraphSpace setNickname(String nickname) { + this.nickname = nickname; + return this; + } + + public String getDpUserName() { + return dpUserName; + } + + public void setDpUserName(String dpUserName) { + this.dpUserName = dpUserName; + } + + public String getDpPassWord() { + return dpPassWord; + } + + public void setDpPassWord(String dpPassWord) { + this.dpPassWord = dpPassWord; + } + + public String getDescription() { + return description; + } + + public GraphSpace setDescription(String description) { + this.description = description; + return this; + } + + public int getCpuLimit() { + return cpuLimit; + } + + public GraphSpace setCpuLimit(int cpuLimit) { + this.cpuLimit = cpuLimit; + return this; + } + + public int getMemoryLimit() { + return memoryLimit; + } + + public GraphSpace setMemoryLimit(int memoryLimit) { + this.memoryLimit = memoryLimit; + return this; + } + + public int getStorageLimit() { + return storageLimit; + } + + public GraphSpace setStorageLimit(int storageLimit) { + this.storageLimit = storageLimit; + return this; + } + + public int getMaxGraphNumber() { + return maxGraphNumber; + } + + public GraphSpace setMaxGraphNumber(int maxGraphNumber) { + this.maxGraphNumber = maxGraphNumber; + return this; + } + + public int getMaxRoleNumber() { + return maxRoleNumber; + } + + public GraphSpace setMaxRoleNumber(int maxRoleNumber) { + this.maxRoleNumber = maxRoleNumber; + return this; + } + + public String getOltpNamespace() { + return oltpNamespace; + } + + public GraphSpace setOltpNamespace(String oltpNamespace) { + this.oltpNamespace = oltpNamespace; + return this; + } + + public String getOlapNamespace() { + return olapNamespace; + } + + public GraphSpace setOlapNamespace(String olapNamespace) { + this.olapNamespace = olapNamespace; + return this; + } + + public String getOperatorImagePath() { + return operatorImagePath; + } + + public void setOperatorImagePath(String operatorImagePath) { + this.operatorImagePath = operatorImagePath; + } + + public String getInternalAlgorithmImageUrl() { + return internalAlgorithmImageUrl; + } + + public void setInternalAlgorithmImageUrl(String internalAlgorithmImageUrl) { + this.internalAlgorithmImageUrl = internalAlgorithmImageUrl; + } + + public String getStorageNamespace() { + return storageNamespace; + } + + public GraphSpace setStorageNamespace(String storageNamespace) { + this.storageNamespace = storageNamespace; + return this; + } + + public int getCpuUsed() { + return cpuUsed; + } + + public GraphSpace setCpuUsed(int cpuUsed) { + this.cpuUsed = cpuUsed; + return this; + } + + public int getMemoryUsed() { + return memoryUsed; + } + + public GraphSpace setMemoryUsed(int memoryUsed) { + this.memoryUsed = memoryUsed; + return this; + } + + public int getComputeCpuLimit() { + return computeCpuLimit; + } + + public void setComputeCpuLimit(int computeCpuLimit) { + this.computeCpuLimit = computeCpuLimit; + } + + public int getComputeMemoryLimit() { + return computeMemoryLimit; + } + + public void setComputeMemoryLimit(int computeMemoryLimit) { + this.computeMemoryLimit = computeMemoryLimit; + } + + public int getStorageUsed() { + return storageUsed; + } + + public GraphSpace setStorageUsed(int storageUsed) { + this.storageUsed = storageUsed; + return this; + } + + public double getStoragePercent() { + return storagePercent; + } + + public GraphSpace setStoragePercent(double storagePercent) { + this.storagePercent = storagePercent; + return this; + } + + public int getGraphNumberUsed() { + return graphNumberUsed; + } + + public GraphSpace setGraphNumberUsed(int graphNumberUsed) { + this.graphNumberUsed = graphNumberUsed; + return this; + } + + public int getRoleNumberUsed() { + return roleNumberUsed; + } + + public GraphSpace setRoleNumberUsed(int roleNumberUsed) { + this.roleNumberUsed = roleNumberUsed; + return this; + } + + public boolean isAuth() { + return auth; + } + + public void setAuth(boolean auth) { + this.auth = auth; + } + + public String getCreateTime() { + return createTime; + } + + public void setCreateTime(String createTime) { + this.createTime = createTime; + } + + public String getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(String updateTime) { + this.updateTime = updateTime; + } + + public Map getConfigs() { + return configs; + } + + public GraphSpace setConfigs( + Map configs) { + this.configs = configs; + return this; + } + + public Object convertReq() { + return new GraphSpaceReq(this); + } + + @JsonIgnoreProperties({"cpu_used", "memory_used", "storage_used", + "storage_percent", "graph_number_used", "role_number_used", + "create_time", "update_time"}) + public static class GraphSpaceReq extends GraphSpace { + + public GraphSpaceReq(GraphSpace graphSpace) { + this.setName(graphSpace.getName()); + this.setNickname(graphSpace.getNickname()); + this.setAuth(graphSpace.isAuth()); + this.setDescription(graphSpace.getDescription()); + this.setCpuLimit(graphSpace.getCpuLimit()); + this.setMemoryLimit(graphSpace.getMemoryLimit()); + this.setComputeCpuLimit(graphSpace.getComputeCpuLimit()); + this.setComputeMemoryLimit(graphSpace.getComputeMemoryLimit()); + this.setOperatorImagePath(graphSpace.getOperatorImagePath()); + this.setInternalAlgorithmImageUrl(graphSpace.getInternalAlgorithmImageUrl()); + this.setStorageLimit(graphSpace.getStorageLimit()); + this.setMaxGraphNumber(graphSpace.getMaxGraphNumber()); + this.setMaxRoleNumber(graphSpace.getMaxRoleNumber()); + this.setOltpNamespace(graphSpace.getOltpNamespace()); + this.setOlapNamespace(graphSpace.getOlapNamespace()); + } + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/HStoreNodeInfo.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/HStoreNodeInfo.java new file mode 100644 index 000000000..94568d74f --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/HStoreNodeInfo.java @@ -0,0 +1,117 @@ +/* + * 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 org.apache.hugegraph.structure.space; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public class HStoreNodeInfo { + + // partitions + @JsonProperty("partitions") + List hStorePartitionInfoList; + // Node id + @JsonProperty("id") + private String id; + // 总空间大小 + @JsonProperty("capacity") + private long capacity; + // 已使用空间 + @JsonProperty("used") + private long used; + // 节点状态 + @JsonProperty("state") + private String state; + // grpc ip:port + @JsonProperty("address") + private String address; + + public String id() { + return id; + } + + public void id(String id) { + this.id = id; + } + + public long capacity() { + return capacity; + } + + public void capacity(long capacity) { + this.capacity = capacity; + } + + public long used() { + return used; + } + + public void used(long used) { + this.used = used; + } + + public String address() { + return address; + } + + public void address(String address) { + this.address = address; + } + + public List hStorePartitionInfoList() { + return hStorePartitionInfoList; + } + + public void hStorePartitionInfoList( + List hStorePartitionInfoList) { + this.hStorePartitionInfoList = hStorePartitionInfoList; + } + + public static class HStorePartitionInfo { + + @JsonProperty("id") + private int id; + @JsonProperty("graph_name") + private String graphName; + + public HStorePartitionInfo() { + } + + public HStorePartitionInfo(int id, String graphName) { + this.id = id; + this.graphName = graphName; + } + + public int id() { + return id; + } + + public void id(int id) { + this.id = id; + } + + public String graphName() { + return graphName; + } + + public void graphName(String graphName) { + this.graphName = graphName; + } + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/OLTPService.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/OLTPService.java new file mode 100644 index 000000000..d3daf38e0 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/OLTPService.java @@ -0,0 +1,241 @@ +/* + * 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 org.apache.hugegraph.structure.space; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class OLTPService { + + @JsonProperty("name") + private String name; + @JsonProperty("description") + private String description; + @JsonProperty("deployment_type") + private DepleymentType depleymentType; + + @JsonProperty("type") + private String type = "OLTP"; + + @JsonProperty("count") + private int count = 1; // maximum number of runnable nodes + @JsonProperty("running") + private int running; + + @JsonProperty("cpu_limit") + private int cpuLimit = 1; + @JsonProperty("memory_limit") + private int memoryLimit = 4; // GB + @JsonProperty("storage_limit") + private int storageLimit = 100; + + @JsonProperty("route_type") + private String routeType = null; + @JsonProperty("port") + private int port = 0; + + @JsonProperty("create_time") + private String createTime; + + @JsonProperty("update_time") + private String updateTime; + + @JsonProperty("urls") + private List urls = new ArrayList<>(); + + @JsonProperty("configs") + private Map configs = new HashMap(); + + @JsonProperty + private ServiceStatus status = ServiceStatus.UNKNOWN; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public DepleymentType getDepleymentType() { + return depleymentType; + } + + public void setDepleymentType( + DepleymentType depleymentType) { + this.depleymentType = depleymentType; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getRunning() { + return running; + } + + public void setRunning(int running) { + this.running = running; + } + + public int getCpuLimit() { + return cpuLimit; + } + + public void setCpuLimit(int cpuLimit) { + this.cpuLimit = cpuLimit; + } + + public int getMemoryLimit() { + return memoryLimit; + } + + public void setMemoryLimit(int memoryLimit) { + this.memoryLimit = memoryLimit; + } + + public int getStorageLimit() { + return storageLimit; + } + + public void setStorageLimit(int storageLimit) { + this.storageLimit = storageLimit; + } + + public String getRouteType() { + return routeType; + } + + public void setRouteType(String routeType) { + this.routeType = routeType; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public List getUrls() { + return urls; + } + + public void setUrls(List urls) { + this.urls = urls; + } + + public Map getConfigs() { + return configs; + } + + public void setConfigs(Map configs) { + this.configs = configs; + } + + public ServiceStatus getStatus() { + return status; + } + + public void setStatus( + ServiceStatus status) { + this.status = status; + } + + public String getCreateTime() { + return createTime; + } + + public void setCreateTime(String createTime) { + this.createTime = createTime; + } + + public String getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(String updateTime) { + this.updateTime = updateTime; + } + + public boolean checkIsK8s() { + return DepleymentType.K8S.equals(this.depleymentType); + } + + public enum DepleymentType { + K8S, + MANUAL + } + + public enum ServiceStatus { + UNKNOWN, + STARTING, + RUNNING, + STOPPED + } + + @JsonIgnoreProperties(value = {"configs", "create_time", "update_time", + "running", "status"}, ignoreUnknown = true) + public static class OLTPServiceReq extends OLTPService { + + public static OLTPServiceReq fromBase(OLTPService service) { + OLTPServiceReq req = new OLTPServiceReq(); + + req.setName(service.name); + req.setDescription(service.description); + req.setDepleymentType(service.depleymentType); + req.setType(service.type); + req.setCount(service.count); + req.setCpuLimit(service.cpuLimit); + req.setMemoryLimit(service.memoryLimit); + req.setStorageLimit(service.storageLimit); + req.setRouteType(service.routeType); + req.setPort(service.port); + req.setUrls(service.urls); + + return req; + } + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/OLTPServiceConfig.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/OLTPServiceConfig.java new file mode 100644 index 000000000..701d7e379 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/OLTPServiceConfig.java @@ -0,0 +1,48 @@ +/* + * 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 org.apache.hugegraph.structure.space; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.HashMap; +import java.util.Map; + +public class OLTPServiceConfig { + + @JsonProperty("name") + private String name; + + @JsonProperty("configs") + private Map configs = new HashMap(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getConfigs() { + return configs; + } + + public void setConfigs(Map configs) { + this.configs = configs; + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/PDNodeInfo.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/PDNodeInfo.java new file mode 100644 index 000000000..ce1815452 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/PDNodeInfo.java @@ -0,0 +1,59 @@ +/* + * 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 org.apache.hugegraph.structure.space; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class PDNodeInfo { + + @JsonProperty("ip") + private String ip; + + @JsonProperty("state") + private String state; + + @JsonProperty("is_leader") + private boolean isLeader; + + public PDNodeInfo() { + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public boolean isLeader() { + return isLeader; + } + + public void setLeader(boolean leader) { + isLeader = leader; + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/SchemaTemplate.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/SchemaTemplate.java new file mode 100644 index 000000000..8b6538d14 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/SchemaTemplate.java @@ -0,0 +1,110 @@ +/* + * 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 org.apache.hugegraph.structure.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.ImmutableMap; + +import java.util.Map; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class SchemaTemplate { + + @JsonProperty("name") + private String name; + @JsonProperty("schema") + private String schema; + + @JsonProperty("create_time") + private String createTime; + + @JsonProperty("update_time") + private String updateTime; + + @JsonProperty("creator") + private String creator; + + public SchemaTemplate() { + } + + public SchemaTemplate(String name, String schema) { + this.name = name; + this.schema = schema; + } + + public static SchemaTemplate fromMap(Map map) { + return new SchemaTemplate(map.get("name"), map.get("schema")); + } + + public void name(String name) { + this.name = name; + } + + public String name() { + return this.name; + } + + public String schema() { + return this.schema; + } + + public void schema(String schema) { + this.schema = schema; + } + + public String createTime() { + return createTime; + } + + public void createTime(String createTime) { + this.createTime = createTime; + } + + public String updateTime() { + return updateTime; + } + + public void updateTime(String updateTime) { + this.updateTime = updateTime; + } + + public String creator() { + return creator; + } + + public void creator(String creator) { + this.creator = creator; + } + + public Map asMap() { + return ImmutableMap.of("name", this.name, "schema", this.schema); + } + + @JsonIgnoreProperties({"create_time", "update_time", "creator"}) + public static class SchemaTemplateReq extends SchemaTemplate { + + public static SchemaTemplateReq fromBase(SchemaTemplate schemaTemplate) { + SchemaTemplateReq req = new SchemaTemplateReq(); + req.name(schemaTemplate.name); + req.schema(schemaTemplate.schema); + + return req; + } + } +} diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/StorageService.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/StorageService.java new file mode 100644 index 000000000..6a1ebe565 --- /dev/null +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/space/StorageService.java @@ -0,0 +1,25 @@ +/* + * 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 org.apache.hugegraph.structure.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties +public class StorageService { + +} diff --git a/hugegraph-client/src/test/java/org/apache/hugegraph/BaseClientTest.java b/hugegraph-client/src/test/java/org/apache/hugegraph/BaseClientTest.java index 471f135de..2fda81d66 100644 --- a/hugegraph-client/src/test/java/org/apache/hugegraph/BaseClientTest.java +++ b/hugegraph-client/src/test/java/org/apache/hugegraph/BaseClientTest.java @@ -50,6 +50,8 @@ public class BaseClientTest { protected static final String BASE_URL = "http://127.0.0.1:8080"; + protected static final String DEFAULT_GRAPHSPACE = "DEFAULT"; + protected static final String GRAPHSPACE = DEFAULT_GRAPHSPACE; protected static final String GRAPH = "hugegraph"; protected static final String USERNAME = "admin"; protected static final String PASSWORD = "pa"; @@ -57,8 +59,11 @@ public class BaseClientTest { private static HugeClient client; - protected static void open() { - client = HugeClient.builder(BASE_URL, GRAPH).configUser(USERNAME, PASSWORD).build(); + protected static HugeClient open() { + client = HugeClient.builder(BASE_URL, DEFAULT_GRAPHSPACE, GRAPH) + .configUser(USERNAME, PASSWORD) + .build(); + return client; } @BeforeClass diff --git a/hugegraph-client/src/test/java/org/apache/hugegraph/api/BaseApiTest.java b/hugegraph-client/src/test/java/org/apache/hugegraph/api/BaseApiTest.java index c6570149b..e9759faa8 100644 --- a/hugegraph-client/src/test/java/org/apache/hugegraph/api/BaseApiTest.java +++ b/hugegraph-client/src/test/java/org/apache/hugegraph/api/BaseApiTest.java @@ -74,20 +74,19 @@ public static void init() { versionAPI = new VersionAPI(client); client.apiVersion(VersionUtil.Version.of(versionAPI.get().get("api"))); - graphsAPI = new GraphsAPI(client); - - propertyKeyAPI = new PropertyKeyAPI(client, GRAPH); - vertexLabelAPI = new VertexLabelAPI(client, GRAPH); - edgeLabelAPI = new EdgeLabelAPI(client, GRAPH); - indexLabelAPI = new IndexLabelAPI(client, GRAPH); - schemaAPI = new SchemaAPI(client, GRAPH); - - vertexAPI = new VertexAPI(client, GRAPH); - edgeAPI = new EdgeAPI(client, GRAPH); - - variablesAPI = new VariablesAPI(client, GRAPH); - taskAPI = new TaskAPI(client, GRAPH); - rebuildAPI = new RebuildAPI(client, GRAPH); + graphsAPI = new GraphsAPI(client, GRAPHSPACE); + propertyKeyAPI = new PropertyKeyAPI(client, GRAPHSPACE, GRAPH); + vertexLabelAPI = new VertexLabelAPI(client, GRAPHSPACE, GRAPH); + edgeLabelAPI = new EdgeLabelAPI(client, GRAPHSPACE, GRAPH); + indexLabelAPI = new IndexLabelAPI(client, GRAPHSPACE, GRAPH); + schemaAPI = new SchemaAPI(client, GRAPHSPACE, GRAPH); + + vertexAPI = new VertexAPI(client, GRAPHSPACE, GRAPH); + edgeAPI = new EdgeAPI(client, GRAPHSPACE, GRAPH); + + variablesAPI = new VariablesAPI(client, GRAPHSPACE, GRAPH); + taskAPI = new TaskAPI(client, GRAPHSPACE, GRAPH); + rebuildAPI = new RebuildAPI(client, GRAPHSPACE, GRAPH); } @AfterClass @@ -101,10 +100,6 @@ public static void clear() throws Exception { BaseClientTest.clear(); } - protected RestClient client() { - return client; - } - protected static void clearData() { // Clear edge edgeAPI.list(-1).results().forEach(edge -> { @@ -159,4 +154,8 @@ protected static void waitUntilTaskCompleted(long taskId, long timeout) { } taskAPI.waitUntilTaskSuccess(taskId, timeout); } + + protected RestClient client() { + return client; + } } diff --git a/hugegraph-client/src/test/java/org/apache/hugegraph/api/GraphsApiTest.java b/hugegraph-client/src/test/java/org/apache/hugegraph/api/GraphsApiTest.java index 5cd7b37d3..8904ead6c 100644 --- a/hugegraph-client/src/test/java/org/apache/hugegraph/api/GraphsApiTest.java +++ b/hugegraph-client/src/test/java/org/apache/hugegraph/api/GraphsApiTest.java @@ -46,6 +46,64 @@ public class GraphsApiTest extends BaseApiTest { private static final String GRAPH3 = "hugegraph3"; private static final String CONFIG3_PATH = "src/test/resources/hugegraph-clone.properties"; + protected static void initPropertyKey(HugeClient client) { + SchemaManager schema = client.schema(); + schema.propertyKey("name").asText().ifNotExist().create(); + schema.propertyKey("age").asInt().ifNotExist().create(); + schema.propertyKey("city").asText().ifNotExist().create(); + schema.propertyKey("lang").asText().ifNotExist().create(); + schema.propertyKey("date").asDate().ifNotExist().create(); + schema.propertyKey("price").asInt().ifNotExist().create(); + schema.propertyKey("weight").asDouble().ifNotExist().create(); + } + + protected static void initVertexLabel(HugeClient client) { + SchemaManager schema = client.schema(); + + schema.vertexLabel("person") + .properties("name", "age", "city") + .primaryKeys("name") + .nullableKeys("city") + .ifNotExist() + .create(); + + schema.vertexLabel("software") + .properties("name", "lang", "price") + .primaryKeys("name") + .nullableKeys("price") + .ifNotExist() + .create(); + + schema.vertexLabel("book") + .useCustomizeStringId() + .properties("name", "price") + .nullableKeys("price") + .ifNotExist() + .create(); + } + + protected static void initEdgeLabel(HugeClient client) { + SchemaManager schema = client.schema(); + + schema.edgeLabel("knows") + .sourceLabel("person") + .targetLabel("person") + .multiTimes() + .properties("date", "city") + .sortKeys("date") + .nullableKeys("city") + .ifNotExist() + .create(); + + schema.edgeLabel("created") + .sourceLabel("person") + .targetLabel("software") + .properties("date", "city") + .nullableKeys("city") + .ifNotExist() + .create(); + } + @Override @After public void teardown() { @@ -79,7 +137,7 @@ public void testCreateAndDropGraph() { Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size()); - HugeClient client = new HugeClient(baseClient(), GRAPH2); + HugeClient client = new HugeClient(baseClient(), GRAPHSPACE, GRAPH2); // Insert graph schema and data initPropertyKey(client); initVertexLabel(client); @@ -151,7 +209,7 @@ public void testCloneAndDropGraph() { Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size()); - HugeClient client = new HugeClient(baseClient(), GRAPH3); + HugeClient client = new HugeClient(baseClient(), GRAPHSPACE, GRAPH3); // Insert graph schema and data initPropertyKey(client); initVertexLabel(client); @@ -216,7 +274,7 @@ public void testCloneAndDropGraphWithoutConfig() { Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size()); - HugeClient client = new HugeClient(baseClient(), GRAPH3); + HugeClient client = new HugeClient(baseClient(), GRAPHSPACE, GRAPH3); // Insert graph schema and data initPropertyKey(client); initVertexLabel(client); @@ -266,62 +324,4 @@ public void testCloneAndDropGraphWithoutConfig() { Assert.assertEquals(initialGraphNumber, graphsAPI.list().size()); } - - protected static void initPropertyKey(HugeClient client) { - SchemaManager schema = client.schema(); - schema.propertyKey("name").asText().ifNotExist().create(); - schema.propertyKey("age").asInt().ifNotExist().create(); - schema.propertyKey("city").asText().ifNotExist().create(); - schema.propertyKey("lang").asText().ifNotExist().create(); - schema.propertyKey("date").asDate().ifNotExist().create(); - schema.propertyKey("price").asInt().ifNotExist().create(); - schema.propertyKey("weight").asDouble().ifNotExist().create(); - } - - protected static void initVertexLabel(HugeClient client) { - SchemaManager schema = client.schema(); - - schema.vertexLabel("person") - .properties("name", "age", "city") - .primaryKeys("name") - .nullableKeys("city") - .ifNotExist() - .create(); - - schema.vertexLabel("software") - .properties("name", "lang", "price") - .primaryKeys("name") - .nullableKeys("price") - .ifNotExist() - .create(); - - schema.vertexLabel("book") - .useCustomizeStringId() - .properties("name", "price") - .nullableKeys("price") - .ifNotExist() - .create(); - } - - protected static void initEdgeLabel(HugeClient client) { - SchemaManager schema = client.schema(); - - schema.edgeLabel("knows") - .sourceLabel("person") - .targetLabel("person") - .multiTimes() - .properties("date", "city") - .sortKeys("date") - .nullableKeys("city") - .ifNotExist() - .create(); - - schema.edgeLabel("created") - .sourceLabel("person") - .targetLabel("software") - .properties("date", "city") - .nullableKeys("city") - .ifNotExist() - .create(); - } } diff --git a/hugegraph-client/src/test/java/org/apache/hugegraph/api/VertexApiTest.java b/hugegraph-client/src/test/java/org/apache/hugegraph/api/VertexApiTest.java index e22300511..49530a5e9 100644 --- a/hugegraph-client/src/test/java/org/apache/hugegraph/api/VertexApiTest.java +++ b/hugegraph-client/src/test/java/org/apache/hugegraph/api/VertexApiTest.java @@ -50,6 +50,11 @@ public static void prepareSchema() { BaseApiTest.initEdgeLabel(); } + @SuppressWarnings("unused") + private static void assertContains(List vertices, Vertex vertex) { + Assert.assertTrue(Utils.contains(vertices, vertex)); + } + @Override @After public void teardown() { @@ -665,9 +670,4 @@ public void testDeleteNotExist() { vertexAPI.delete("not-exist-v"); }); } - - @SuppressWarnings("unused") - private static void assertContains(List vertices, Vertex vertex) { - Assert.assertTrue(Utils.contains(vertices, vertex)); - } } diff --git a/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientHttpsTest.java b/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientHttpsTest.java index ab84ad27d..b53575121 100644 --- a/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientHttpsTest.java +++ b/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientHttpsTest.java @@ -32,7 +32,7 @@ import com.google.common.collect.ImmutableMap; -public class HugeClientHttpsTest { +public class HugeClientHttpsTest extends BaseFuncTest { private static final String BASE_URL = "https://127.0.0.1:8443"; private static final String GRAPH = "hugegraph"; @@ -48,7 +48,7 @@ public class HugeClientHttpsTest { private static HugeClient client; @Before - public void init() { + public void initBaseFuncTest() { CommonUtil.downloadFileByUrl(CommonUtil.PREFIX + "hugegraph.truststore", TRUST_STORE_PATH); } @@ -60,7 +60,7 @@ public void teardown() throws Exception { @Test public void testHttpsClientBuilderWithConnection() { - client = HugeClient.builder(BASE_URL, GRAPH) + client = HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configUser(USERNAME, PASSWORD) .configSSL(TRUST_STORE_PATH, TRUST_STORE_PASSWORD) .build(); @@ -70,7 +70,7 @@ public void testHttpsClientBuilderWithConnection() { @Test public void testHttpsClientWithConnectionPoolNoUserParam() { - client = HugeClient.builder(BASE_URL, GRAPH) + client = HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configConnectTimeout(3) .configReadTimeout(10) .configPool(MAX_CONNS, MAX_CONNS_PER_ROUTE) @@ -82,7 +82,7 @@ public void testHttpsClientWithConnectionPoolNoUserParam() { @Test public void testHttpsClientWithConnectionPoolNoTimeOutParam() { - client = HugeClient.builder(BASE_URL, GRAPH) + client = HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configUser(USERNAME, PASSWORD) .configPool(MAX_CONNS, MAX_CONNS_PER_ROUTE) .configSSL(TRUST_STORE_PATH, TRUST_STORE_PASSWORD) @@ -93,7 +93,7 @@ public void testHttpsClientWithConnectionPoolNoTimeOutParam() { @Test public void testHttpsClientNewBuilderWithConnectionNoPoolParam() { - client = HugeClient.builder(BASE_URL, GRAPH) + client = HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configUser(USERNAME, PASSWORD) .configTimeout(TIMEOUT) .configSSL(TRUST_STORE_PATH, TRUST_STORE_PASSWORD) @@ -104,7 +104,7 @@ public void testHttpsClientNewBuilderWithConnectionNoPoolParam() { @Test public void testHttpsClientNewBuilderWithConnectionPool() { - client = HugeClient.builder(BASE_URL, GRAPH) + client = HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configUser(USERNAME, PASSWORD) .configTimeout(TIMEOUT) .configPool(MAX_CONNS, MAX_CONNS_PER_ROUTE) @@ -117,7 +117,7 @@ public void testHttpsClientNewBuilderWithConnectionPool() { @Test public void testHttpsClientNewBuilderZeroPoolParam() { - client = HugeClient.builder(BASE_URL, GRAPH) + client = HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configUser(USERNAME, PASSWORD) .configTimeout(TIMEOUT) .configPool(0, 0) @@ -130,7 +130,7 @@ public void testHttpsClientNewBuilderZeroPoolParam() { @Test public void testHttpsClientBuilderWithConnectionPoolNoParam() { Assert.assertThrows(IllegalArgumentException.class, () -> { - HugeClient.builder(BASE_URL, GRAPH) + HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configUrl(null) .configGraph(null) .configSSL("", "") @@ -144,7 +144,7 @@ public void testHttpsClientBuilderWithConnectionPoolNoParam() { @Test public void testHttpsClientBuilderWithConnectionPoolNoGraphParam() { Assert.assertThrows(IllegalArgumentException.class, () -> { - HugeClient.builder(BASE_URL, GRAPH) + HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configGraph(null) .configSSL("", "") .build(); @@ -157,7 +157,7 @@ public void testHttpsClientBuilderWithConnectionPoolNoGraphParam() { @Test public void testHttpsClientBuilderWithConnectionPoolZeroIdleTimeParam() { Assert.assertThrows(IllegalArgumentException.class, () -> { - HugeClient.builder(BASE_URL, GRAPH) + HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configIdleTime(0) .build(); }, e -> { diff --git a/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientTest.java b/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientTest.java index 84c1249f1..871bb7626 100644 --- a/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientTest.java +++ b/hugegraph-client/src/test/java/org/apache/hugegraph/functional/HugeClientTest.java @@ -24,13 +24,14 @@ public class HugeClientTest { protected static final String BASE_URL = "http://127.0.0.1:8080"; + protected static final String GRAPHSPACE = "DEFAULT"; protected static final String GRAPH = "hugegraph"; protected static final String USERNAME = "admin"; protected static final String PASSWORD = "pa"; @Test public void testContext() { - HugeClient client = HugeClient.builder(BASE_URL, GRAPH) + HugeClient client = HugeClient.builder(BASE_URL, GRAPHSPACE, GRAPH) .configUser(USERNAME, PASSWORD) .configHttpBuilder(builder -> builder.followRedirects(false)) .build();