Skip to content

Commit 43da581

Browse files
Thespicaimbajin
andauthored
feat(client): support graphspace (#633)
* add todo about be compatitable to server do not support gs --------- Co-authored-by: imbajin <[email protected]>
1 parent c672e35 commit 43da581

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2227
-252
lines changed

.github/workflows/client-ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ jobs:
5555

5656
- name: Prepare env and service
5757
run: |
58+
# TODO(@Thespica): test both servers of supporting gs and not supporting gs
59+
# when the server supports gs
5860
$TRAVIS_DIR/install-hugegraph-from-source.sh $COMMIT_ID
5961
6062
- name: Install Java ${{ matrix.JAVA_VERSION }} for client

hugegraph-client/src/main/java/org/apache/hugegraph/api/API.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ public API(RestClient client) {
3737
this.path = null;
3838
}
3939

40+
protected static void checkOffset(long value) {
41+
E.checkArgument(value >= 0, "Offset must be >= 0, but got: %s", value);
42+
}
43+
44+
protected static void checkLimit(long value, String name) {
45+
E.checkArgument(value > 0 || value == NO_LIMIT,
46+
"%s must be > 0 or == %s, but got: %s",
47+
name, NO_LIMIT, value);
48+
}
49+
50+
protected static String joinPath(String... paths) {
51+
return String.join(PATH_SPLITOR, paths);
52+
}
53+
4054
public String path() {
4155
E.checkState(this.path != null, "Path can't be null");
4256
return this.path;
@@ -51,14 +65,4 @@ protected void path(String pathTemplate, Object... args) {
5165
}
5266

5367
protected abstract String type();
54-
55-
protected static void checkOffset(long value) {
56-
E.checkArgument(value >= 0, "Offset must be >= 0, but got: %s", value);
57-
}
58-
59-
protected static void checkLimit(long value, String name) {
60-
E.checkArgument(value > 0 || value == NO_LIMIT,
61-
"%s must be > 0 or == %s, but got: %s",
62-
name, NO_LIMIT, value);
63-
}
6468
}

hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/EdgeAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
public class EdgeAPI extends GraphAPI {
3737

38-
public EdgeAPI(RestClient client, String graph) {
39-
super(client, graph);
38+
public EdgeAPI(RestClient client, String graphSpace, String graph) {
39+
super(client, graphSpace, graph);
4040
}
4141

4242
@Override

hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/GraphAPI.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,16 @@
2727

2828
public abstract class GraphAPI extends API {
2929

30-
private static final String PATH = "graphs/%s/graph/%s";
30+
private static final String PATH = "graphspaces/%s/graphs/%s/graph/%s";
3131

3232
private final String batchPath;
3333

34-
public GraphAPI(RestClient client, String graph) {
34+
public GraphAPI(RestClient client, String graphSpace, String graph) {
3535
super(client);
36-
this.path(PATH, graph, this.type());
36+
this.path(PATH, graphSpace, graph, this.type());
3737
this.batchPath = String.join("/", this.path(), "batch");
3838
}
3939

40-
public String batchPath() {
41-
return this.batchPath;
42-
}
43-
4440
public static String formatVertexId(Object id) {
4541
return formatVertexId(id, false);
4642
}
@@ -68,4 +64,8 @@ public static String formatProperties(Map<String, Object> properties) {
6864
}
6965
return JsonUtil.toJson(properties);
7066
}
67+
68+
public String batchPath() {
69+
return this.batchPath;
70+
}
7171
}

hugegraph-client/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737
public class VertexAPI extends GraphAPI {
3838

39-
public VertexAPI(RestClient client, String graph) {
40-
super(client, graph);
39+
public VertexAPI(RestClient client, String graphSpace, String graph) {
40+
super(client, graphSpace, graph);
4141
}
4242

4343
@Override
@@ -83,21 +83,21 @@ public int update(BatchOlapPropertyRequest request) {
8383
}
8484

8585
public Vertex append(Vertex vertex) {
86-
String id = formatVertexId(vertex.id());
86+
String id = GraphAPI.formatVertexId(vertex.id());
8787
Map<String, Object> params = ImmutableMap.of("action", "append");
8888
RestResult result = this.client.put(this.path(), id, vertex, params);
8989
return result.readObject(Vertex.class);
9090
}
9191

9292
public Vertex eliminate(Vertex vertex) {
93-
String id = formatVertexId(vertex.id());
93+
String id = GraphAPI.formatVertexId(vertex.id());
9494
Map<String, Object> params = ImmutableMap.of("action", "eliminate");
9595
RestResult result = this.client.put(this.path(), id, vertex, params);
9696
return result.readObject(Vertex.class);
9797
}
9898

9999
public Vertex get(Object id) {
100-
String vertexId = formatVertexId(id);
100+
String vertexId = GraphAPI.formatVertexId(id);
101101
RestResult result = this.client.get(this.path(), vertexId);
102102
return result.readObject(Vertex.class);
103103
}
@@ -115,7 +115,7 @@ public Vertices list(String label, Map<String, Object> properties,
115115
boolean keepP, int offset, String page, int limit) {
116116
checkOffset(offset);
117117
checkLimit(limit, "Limit");
118-
String props = formatProperties(properties);
118+
String props = GraphAPI.formatProperties(properties);
119119
Map<String, Object> params = new LinkedHashMap<>();
120120
params.put("label", label);
121121
params.put("properties", props);
@@ -128,7 +128,7 @@ public Vertices list(String label, Map<String, Object> properties,
128128
}
129129

130130
public void delete(Object id) {
131-
String vertexId = formatVertexId(id);
131+
String vertexId = GraphAPI.formatVertexId(id);
132132
this.client.delete(this.path(), vertexId);
133133
}
134134
}

hugegraph-client/src/main/java/org/apache/hugegraph/api/graphs/GraphsAPI.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ public class GraphsAPI extends API {
3939
private static final String GRAPH_READ_MODE = "graph_read_mode";
4040
private static final String CLEAR = "clear";
4141
private static final String CONFIRM_MESSAGE = "confirm_message";
42+
private static final String PATH = "graphspaces/%s/graphs";
4243

43-
public GraphsAPI(RestClient client) {
44+
public GraphsAPI(RestClient client, String graphSpace) {
4445
super(client);
45-
this.path(this.type());
46+
this.path(String.format(PATH, graphSpace));
47+
}
48+
49+
private static String joinPath(String path, String graph) {
50+
return String.join(DELIMITER, path, graph);
51+
}
52+
53+
private static String joinPath(String path, String graph, String action) {
54+
return String.join(DELIMITER, path, graph, action);
4655
}
4756

4857
@Override
@@ -130,12 +139,4 @@ public GraphReadMode readMode(String graph) {
130139
throw new InvalidResponseException("Invalid GraphReadMode value '%s'", value);
131140
}
132141
}
133-
134-
private static String joinPath(String path, String graph) {
135-
return String.join(DELIMITER, path, graph);
136-
}
137-
138-
private static String joinPath(String path, String graph, String action) {
139-
return String.join(DELIMITER, path, graph, action);
140-
}
141142
}

hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/CypherAPI.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@
2020
import org.apache.hugegraph.api.API;
2121
import org.apache.hugegraph.client.RestClient;
2222
import org.apache.hugegraph.rest.RestResult;
23-
import org.apache.hugegraph.structure.constant.HugeType;
2423
import org.apache.hugegraph.structure.gremlin.Response;
2524

2625
public class CypherAPI extends API {
2726

28-
private static final String PATH = "graphs/%s/cypher";
27+
private static final String PATH = "graphspaces/%s/graphs/%s/cypher";
2928

30-
public CypherAPI(RestClient client, String graph) {
29+
public CypherAPI(RestClient client) {
3130
super(client);
32-
this.path(PATH, graph);
3331
}
3432

3533
@Override
3634
protected String type() {
37-
return HugeType.CYPHER.string();
35+
return PATH;
3836
}
3937

40-
public Response post(String cypher) {
38+
public Response post(String graphSpace, String graph, String cypher) {
39+
this.path(type(), graphSpace, graph);
4140
RestResult result = this.client.post(this.path(), cypher);
4241
return result.readObject(Response.class);
4342
}

hugegraph-client/src/main/java/org/apache/hugegraph/api/gremlin/GremlinAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ public Response post(GremlinRequest request) {
3939
RestResult result = this.client.post(this.path(), request);
4040
return result.readObject(Response.class);
4141
}
42+
43+
public boolean isSupportGs() {
44+
return client.isSupportGs();
45+
}
4246
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to You under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
package org.apache.hugegraph.api.job;
19+
20+
import java.util.Map;
21+
22+
import org.apache.hugegraph.api.task.TaskAPI;
23+
import org.apache.hugegraph.client.RestClient;
24+
import org.apache.hugegraph.rest.RestResult;
25+
26+
public class CypherJobAPI extends JobAPI {
27+
28+
private static final String JOB_TYPE = "cypher";
29+
30+
public CypherJobAPI(RestClient client, String graphSpace, String graph) {
31+
super(client, graphSpace, graph);
32+
}
33+
34+
@Override
35+
protected String jobType() {
36+
return JOB_TYPE;
37+
}
38+
39+
public long execute(String cypher) {
40+
RestResult result = this.client.post(this.path(), cypher);
41+
@SuppressWarnings("unchecked")
42+
Map<String, Object> task = result.readObject(Map.class);
43+
return TaskAPI.parseTaskId(task);
44+
}
45+
}

hugegraph-client/src/main/java/org/apache/hugegraph/api/job/GremlinJobAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class GremlinJobAPI extends JobAPI {
2828

2929
private static final String JOB_TYPE = "gremlin";
3030

31-
public GremlinJobAPI(RestClient client, String graph) {
32-
super(client, graph);
31+
public GremlinJobAPI(RestClient client, String graphSpace, String graph) {
32+
super(client, graphSpace, graph);
3333
}
3434

3535
@Override

hugegraph-client/src/main/java/org/apache/hugegraph/api/job/JobAPI.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
public abstract class JobAPI extends API {
2525

2626
// For example: graphs/hugegraph/jobs/gremlin
27-
private static final String PATH = "graphs/%s/%s/%s";
27+
private static final String PATH = "graphspaces/%s/graphs/%s/%s/%s";
2828

29-
public JobAPI(RestClient client, String graph) {
29+
public JobAPI(RestClient client, String graphSpace, String graph) {
3030
super(client);
31-
this.path(String.format(PATH, graph, this.type(), this.jobType()));
31+
this.path(String.format(PATH, graphSpace, graph, this.type(), this.jobType()));
3232
}
3333

3434
@Override

hugegraph-client/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class RebuildAPI extends JobAPI {
3232

3333
private static final String JOB_TYPE = "rebuild";
3434

35-
public RebuildAPI(RestClient client, String graph) {
36-
super(client, graph);
35+
public RebuildAPI(RestClient client, String graphSpace, String graph) {
36+
super(client, graphSpace, graph);
3737
}
3838

3939
@Override

hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
public class EdgeLabelAPI extends SchemaElementAPI {
3434

35-
public EdgeLabelAPI(RestClient client, String graph) {
36-
super(client, graph);
35+
public EdgeLabelAPI(RestClient client, String graphSpace, String graph) {
36+
super(client, graphSpace, graph);
3737
}
3838

3939
@Override

hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
public class IndexLabelAPI extends SchemaElementAPI {
3636

37-
public IndexLabelAPI(RestClient client, String graph) {
38-
super(client, graph);
37+
public IndexLabelAPI(RestClient client, String graphSpace, String graph) {
38+
super(client, graphSpace, graph);
3939
}
4040

4141
@Override

hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
public class PropertyKeyAPI extends SchemaElementAPI {
3636

37-
public PropertyKeyAPI(RestClient client, String graph) {
38-
super(client, graph);
37+
public PropertyKeyAPI(RestClient client, String graphSpace, String graph) {
38+
super(client, graphSpace, graph);
3939
}
4040

4141
@Override

hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828

2929
public class SchemaAPI extends API {
3030

31-
private static final String PATH = "graphs/%s/%s";
31+
private static final String PATH = "graphspaces/%s/graphs/%s/%s";
3232

33-
public SchemaAPI(RestClient client, String graph) {
33+
public SchemaAPI(RestClient client, String graphSpace, String graph) {
3434
super(client);
35-
this.path(PATH, graph, this.type());
35+
this.path(PATH, graphSpace, graph, this.type());
3636
}
3737

3838
@SuppressWarnings("unchecked")

hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/SchemaElementAPI.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
public abstract class SchemaElementAPI extends API {
2525

26-
private static final String PATH = "graphs/%s/schema/%s";
26+
private static final String PATH = "graphspaces/%s/graphs/%s/schema/%s";
2727

28-
public SchemaElementAPI(RestClient client, String graph) {
28+
public SchemaElementAPI(RestClient client, String graphSpace, String graph) {
2929
super(client);
30-
this.path(PATH, graph, this.type());
30+
this.path(PATH, graphSpace, graph, this.type());
3131
}
3232

3333
protected abstract Object checkCreateOrUpdate(SchemaElement schemaElement);

hugegraph-client/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
public class VertexLabelAPI extends SchemaElementAPI {
3434

35-
public VertexLabelAPI(RestClient client, String graph) {
36-
super(client, graph);
35+
public VertexLabelAPI(RestClient client, String graphSpace, String graph) {
36+
super(client, graphSpace, graph);
3737
}
3838

3939
@Override

0 commit comments

Comments
 (0)