Skip to content

Commit 852d76a

Browse files
refactor(client): adjust APIs to compatible with 1.7.0 server/graphspace (#685)
Enhanced Javadoc comments for GraphMode and GraphReadMode enums to clarify their operational contexts, permissions, and use cases. Refactored GraphsAPI.clear() for cleaner path selection logic. --------- Co-authored-by: imbajin <jin@apache.org>
1 parent d57cb22 commit 852d76a

File tree

6 files changed

+162
-55
lines changed

6 files changed

+162
-55
lines changed

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

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,14 @@ public void clear(String graph, boolean clearSchema) {
136136
}
137137

138138
public void clear(String graph, String message) {
139-
this.client.delete(joinPath(this.path(), graph, CLEAR),
140-
ImmutableMap.of(CONFIRM_MESSAGE, message));
139+
clear(null, graph, message);
140+
}
141+
142+
public void clear(String graphSpace, String graph, String message) {
143+
String path = (graphSpace == null)
144+
? joinPath(this.path(), graph, CLEAR)
145+
: joinPath(this.path(), graphSpace, graph, CLEAR);
146+
this.client.delete(path, ImmutableMap.of(CONFIRM_MESSAGE, message));
141147
}
142148

143149
public Map<String, String> update(String name, String nickname) {
@@ -198,51 +204,85 @@ public Map<String, String> reload() {
198204
}
199205

200206
public void mode(String graph, GraphMode mode) {
201-
// NOTE: Must provide id for PUT. If you use "graph/mode", "/" will
202-
// be encoded to "%2F". So use "mode" here, although inaccurate.
203-
this.client.put(joinPath(this.path(), graph, MODE), null, mode);
207+
mode(null, graph, mode);
204208
}
205209

206-
public GraphMode mode(String graph) {
207-
RestResult result = this.client.get(joinPath(this.path(), graph), MODE);
208-
@SuppressWarnings("unchecked")
209-
Map<String, String> mode = result.readObject(Map.class);
210-
String value = mode.get(MODE);
211-
if (value == null) {
212-
throw new InvalidResponseException("Invalid response, expect 'mode' in response");
213-
}
214-
try {
215-
return GraphMode.valueOf(value);
216-
} catch (IllegalArgumentException e) {
217-
throw new InvalidResponseException("Invalid GraphMode value '%s'", value);
210+
public void mode(String graphSpace, String graph, GraphMode mode) {
211+
// NOTE: Must provide id for PUT. If you use "graph/mode", "/" will
212+
// be encoded to "%2F". So use "mode" here, although inaccurate.
213+
if (graphSpace == null) {
214+
this.client.put(joinPath(this.path(), graph, MODE), null, mode);
215+
return;
218216
}
217+
this.client.put(joinPath(this.path(), graphSpace, graph, MODE), null, mode);
219218
}
220219

221220
public void readMode(String graph, GraphReadMode readMode) {
221+
readMode(null, graph, readMode);
222+
}
223+
224+
225+
public void readMode(String graphSpace, String graph, GraphReadMode readMode) {
222226
this.client.checkApiVersion("0.59", "graph read mode");
223227
// NOTE: Must provide id for PUT. If you use "graph/graph_read_mode", "/"
224228
// will be encoded to "%2F". So use "graph_read_mode" here, although
225229
// inaccurate.
226-
this.client.put(joinPath(this.path(), graph, GRAPH_READ_MODE), null, readMode);
230+
if (graphSpace == null) {
231+
this.client.put(joinPath(this.path(), graph, GRAPH_READ_MODE), null, readMode);
232+
return;
233+
}
234+
this.client.put(joinPath(this.path(), graphSpace, graph, GRAPH_READ_MODE), null, readMode);
227235
}
228236

229-
public GraphReadMode readMode(String graph) {
230-
this.client.checkApiVersion("0.59", "graph read mode");
231-
RestResult result = this.client.get(joinPath(this.path(), graph), GRAPH_READ_MODE);
237+
/**
238+
* Get graph mode value from server response
239+
*
240+
* @param graphSpace the graph space name, null for non-graphspace mode
241+
* @param graph the graph name
242+
* @param modeKey the mode key in response (MODE or GRAPH_READ_MODE)
243+
* @param enumClass the enum class type
244+
* @return the mode enum value
245+
*/
246+
private <T extends Enum<T>> T getModeValue(String graphSpace, String graph,
247+
String modeKey, Class<T> enumClass) {
248+
String path = (graphSpace != null)
249+
? joinPath(this.path(), graphSpace, graph)
250+
: joinPath(this.path(), graph);
251+
252+
RestResult result = this.client.get(path, modeKey);
232253
@SuppressWarnings("unchecked")
233-
Map<String, String> readMode = result.readObject(Map.class);
234-
String value = readMode.get(GRAPH_READ_MODE);
254+
Map<String, String> map = result.readObject(Map.class);
255+
String value = map.get(modeKey);
256+
235257
if (value == null) {
236-
throw new InvalidResponseException("Invalid response, expect 'graph_read_mode' " +
237-
"in response");
258+
throw new InvalidResponseException(
259+
"Invalid response, expect '%s' in response", modeKey);
238260
}
239261
try {
240-
return GraphReadMode.valueOf(value);
262+
return Enum.valueOf(enumClass, value);
241263
} catch (IllegalArgumentException e) {
242-
throw new InvalidResponseException("Invalid GraphReadMode value '%s'", value);
264+
throw new InvalidResponseException(
265+
"Invalid %s value '%s'", enumClass.getSimpleName(), value);
243266
}
244267
}
245268

269+
public GraphMode mode(String graphSpace, String graph) {
270+
return getModeValue(graphSpace, graph, MODE, GraphMode.class);
271+
}
272+
273+
public GraphMode mode(String graph) {
274+
return mode(null, graph);
275+
}
276+
277+
public GraphReadMode readMode(String graphSpace, String graph) {
278+
this.client.checkApiVersion("0.59", "graph read mode");
279+
return getModeValue(graphSpace, graph, GRAPH_READ_MODE, GraphReadMode.class);
280+
}
281+
282+
public GraphReadMode readMode(String graph) {
283+
return readMode(null, graph);
284+
}
285+
246286
public String clone(String graph, Map<String, Object> body) {
247287
RestResult result = this.client.post(joinPath(this.path(), graph,
248288
"clone"), body);

hugegraph-client/src/main/java/org/apache/hugegraph/client/RestClient.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Map;
2121

22+
import org.apache.hugegraph.driver.VersionManager;
2223
import org.apache.hugegraph.exception.ServerException;
2324
import org.apache.hugegraph.rest.AbstractRestClient;
2425
import org.apache.hugegraph.rest.ClientException;
@@ -39,18 +40,18 @@
3940
public class RestClient extends AbstractRestClient {
4041

4142
private static final int SECOND = 1000;
43+
private String version;
44+
@Getter
45+
@Setter
46+
private boolean supportGs;
47+
private Version apiVersion = null;
4248

4349
static {
4450
SimpleModule module = new SimpleModule();
4551
module.addDeserializer(Path.class, new PathDeserializer());
4652
RestResult.registerModule(module);
4753
}
4854

49-
private Version apiVersion = null;
50-
@Setter
51-
@Getter
52-
private boolean supportGs = false;
53-
5455
public RestClient(String url, String username, String password, int timeout) {
5556
super(url, username, password, timeout * SECOND);
5657
}

hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphsManager.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public void clearGraph(String graph, String message) {
9494
this.graphsAPI.clear(graph, message);
9595
}
9696

97+
public void clearGraph(String graphSpace, String graph, String message) {
98+
this.graphsAPI.clear(graphSpace, graph, message);
99+
}
100+
97101
public void update(String graph, String nickname) {
98102
this.graphsAPI.update(graph, nickname);
99103
}
@@ -115,14 +119,30 @@ public void mode(String graph, GraphMode mode) {
115119
this.graphsAPI.mode(graph, mode);
116120
}
117121

122+
public void mode(String graphSpace, String graph, GraphMode mode) {
123+
this.graphsAPI.mode(graphSpace, graph, mode);
124+
}
125+
118126
public GraphMode mode(String graph) {
119127
return this.graphsAPI.mode(graph);
120128
}
121129

130+
public GraphMode mode(String graphSpace, String graph) {
131+
return this.graphsAPI.mode(graphSpace, graph);
132+
}
133+
134+
public void readMode(String graphSpace, String graph, GraphReadMode readMode) {
135+
this.graphsAPI.readMode(graphSpace, graph, readMode);
136+
}
137+
122138
public void readMode(String graph, GraphReadMode readMode) {
123139
this.graphsAPI.readMode(graph, readMode);
124140
}
125141

142+
public GraphReadMode readMode(String graphSpace, String graph) {
143+
return this.graphsAPI.readMode(graphSpace, graph);
144+
}
145+
126146
public GraphReadMode readMode(String graph) {
127147
return this.graphsAPI.readMode(graph);
128148
}

hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.io.Closeable;
2121

22+
import lombok.Getter;
23+
2224
import org.apache.hugegraph.client.RestClient;
2325
import org.apache.hugegraph.rest.ClientException;
2426
import org.apache.hugegraph.rest.RestClientConfig;
@@ -42,8 +44,11 @@ public class HugeClient implements Closeable {
4244
ClientVersion.check();
4345
}
4446

47+
@Getter
4548
protected String graphSpaceName;
49+
@Getter
4650
protected String graphName;
51+
4752
private final boolean borrowedClient;
4853
private final RestClient client;
4954
private VersionManager version;
@@ -173,18 +178,10 @@ private void checkServerApiVersion() {
173178
// 0.81 equals to the {latest_api_version} +10
174179
VersionUtil.check(apiVersion, "0.38", "0.81", "hugegraph-api in server");
175180
this.client.apiVersion(apiVersion);
176-
boolean supportGs = VersionUtil.gte(this.version.getCoreVersion(), "2.0");
181+
boolean supportGs = VersionUtil.gte(this.version.getCoreVersion(), "1.7.0");
177182
this.client.setSupportGs(supportGs);
178183
}
179184

180-
public String getGraphSpaceName() {
181-
return graphSpaceName;
182-
}
183-
184-
public String getGraphName() {
185-
return graphName;
186-
}
187-
188185
public GraphsManager graphs() {
189186
return this.graphs;
190187
}

hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/GraphMode.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,45 @@
1717

1818
package org.apache.hugegraph.structure.constant;
1919

20+
/**
21+
* GraphMode defines the operational modes of a HugeGraph instance.
22+
* Different modes have different permissions for schema and vertex ID creation.
23+
*/
2024
public enum GraphMode {
2125

22-
/*
23-
* None mode is regular mode
24-
* 1. Not allowed to create schema with specified id
25-
* 2. Not support create vertex with id for AUTOMATIC id strategy
26+
/**
27+
* NONE mode is the default regular mode for normal graph operations.
28+
* Restrictions:
29+
* 1. Not allowed to create schema with specified ID
30+
* 2. Not allowed to create vertex with custom ID for AUTOMATIC ID strategy
31+
* Use case: Daily graph database operations
2632
*/
2733
NONE(1, "none"),
2834

29-
/*
30-
* Restoring mode is used to restore schema and graph data to an new graph.
31-
* 1. Support create schema with specified id
32-
* 2. Support create vertex with id for AUTOMATIC id strategy
35+
/**
36+
* RESTORING mode is used to restore schema and graph data to a new graph.
37+
* This mode allows full control over IDs during restoration.
38+
* Permissions:
39+
* 1. Allowed to create schema with specified ID
40+
* 2. Allowed to create vertex with custom ID for AUTOMATIC ID strategy
41+
* Use case: Database backup recovery, graph migration
3342
*/
3443
RESTORING(2, "restoring"),
3544

36-
/*
37-
* MERGING mode is used to merge schema and graph data to an existing graph.
38-
* 1. Not allowed to create schema with specified id
39-
* 2. Support create vertex with id for AUTOMATIC id strategy
45+
/**
46+
* MERGING mode is used to merge schema and graph data into an existing graph.
47+
* This mode allows vertex ID control but not schema ID control to avoid conflicts.
48+
* Permissions:
49+
* 1. Not allowed to create schema with specified ID (to prevent conflicts)
50+
* 2. Allowed to create vertex with custom ID for AUTOMATIC ID strategy
51+
* Use case: Data merging, incremental data import
4052
*/
4153
MERGING(3, "merging"),
4254

43-
/*
44-
* LOADING mode used to load data via hugegraph-loader.
55+
/**
56+
* LOADING mode is used for bulk data loading via hugegraph-loader.
57+
* This mode is optimized for high-throughput data ingestion.
58+
* Use case: Bulk data import operations
4559
*/
4660
LOADING(4, "loading");
4761

@@ -62,10 +76,22 @@ public String string() {
6276
return this.name;
6377
}
6478

79+
/**
80+
* Check if the graph is in maintenance mode (RESTORING or MERGING).
81+
* In maintenance mode, the graph allows creating vertices with custom IDs.
82+
*
83+
* @return true if mode is RESTORING or MERGING
84+
*/
6585
public boolean maintaining() {
6686
return this == RESTORING || this == MERGING;
6787
}
6888

89+
/**
90+
* Check if the graph is in loading mode.
91+
* Loading mode is optimized for bulk data import operations.
92+
*
93+
* @return true if mode is LOADING
94+
*/
6995
public boolean loading() {
7096
return this == LOADING;
7197
}

hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/GraphReadMode.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,30 @@
1717

1818
package org.apache.hugegraph.structure.constant;
1919

20+
/**
21+
* GraphReadMode defines the read modes for querying graph data.
22+
* This determines which type of data (OLTP/OLAP) should be included in query results.
23+
*/
2024
public enum GraphReadMode {
2125

26+
/**
27+
* ALL mode returns both OLTP and OLAP data.
28+
* Use case: When you need complete data from both transactional and analytical storage
29+
*/
2230
ALL(1, "all"),
2331

32+
/**
33+
* OLTP_ONLY mode returns only Online Transaction Processing data.
34+
* OLTP data is optimized for real-time queries and low-latency transactions.
35+
* Use case: Real-time queries, transactional operations
36+
*/
2437
OLTP_ONLY(2, "oltp_only"),
2538

39+
/**
40+
* OLAP_ONLY mode returns only Online Analytical Processing data.
41+
* OLAP data is optimized for complex analytical queries and large-scale computations.
42+
* Use case: Big data analytics, graph algorithms, complex queries
43+
*/
2644
OLAP_ONLY(3, "olap_only");
2745

2846
private final byte code;
@@ -42,6 +60,11 @@ public String string() {
4260
return this.name;
4361
}
4462

63+
/**
64+
* Check if this mode includes OLAP data in query results.
65+
*
66+
* @return true if mode is ALL or OLAP_ONLY
67+
*/
4568
public boolean showOlap() {
4669
return this == ALL || this == OLAP_ONLY;
4770
}

0 commit comments

Comments
 (0)