Skip to content

Commit

Permalink
get supportGs by core version
Browse files Browse the repository at this point in the history
  • Loading branch information
Thespica committed Nov 11, 2024
1 parent ae8e62e commit 1e749b6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

import com.fasterxml.jackson.databind.module.SimpleModule;

import lombok.Getter;
import lombok.Setter;

public class RestClient extends AbstractRestClient {

private static final int SECOND = 1000;
Expand All @@ -44,7 +47,9 @@ public class RestClient extends AbstractRestClient {
}

private Version apiVersion = null;
private String coreVersion = null;
@Setter
@Getter
private boolean supportGs = false;

public RestClient(String url, String username, String password, int timeout) {
super(url, username, password, timeout * SECOND);
Expand Down Expand Up @@ -73,11 +78,6 @@ public void apiVersion(Version version) {
this.apiVersion = version;
}

public void coreVersion(String version) {
E.checkNotNull(version, "core version");
this.coreVersion = version;
}

public Version apiVersion() {
return this.apiVersion;
}
Expand All @@ -90,70 +90,84 @@ public void checkApiVersion(String minVersion, String message) {
}
}

public boolean isSupportGs() {
return VersionUtil.gte(this.coreVersion, "2.0");
}

public boolean apiVersionLt(String minVersion) {
String apiVersion = this.apiVersion == null ? null : this.apiVersion.get();
return apiVersion != null && !VersionUtil.gte(apiVersion, minVersion);
}

@Override
public RestResult post(String path, Object object) {
return super.post(this.isSupportGs() ? path : removeDefaultGsPrefix(path), object);
return super.post(supportGs ? path : removeDefaultGsPrefix(path), object);
}

@Override
public RestResult get(String path, String id) {
return super.get(this.isSupportGs() ? path : removeDefaultGsPrefix(path), 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<String, Object> params) {
return super.delete(this.isSupportGs() ? path : removeDefaultGsPrefix(path), params);
return super.delete(supportGs ? path : removeDefaultGsPrefix(path), params);
}

@Override
public RestResult delete(String path, String id) {
return super.delete(this.isSupportGs() ? path : removeDefaultGsPrefix(path), id);
return super.delete(supportGs ? path : removeDefaultGsPrefix(path), id);
}

@Override
public RestResult post(String path, Object object, RestHeaders headers) {
return super.post(this.isSupportGs() ? path : removeDefaultGsPrefix(path), object, headers);
return super.post(supportGs ? path : removeDefaultGsPrefix(path), object, headers);
}

@Override
public RestResult post(String path, Object object, Map<String, Object> params) {
return super.post(this.isSupportGs() ? path : removeDefaultGsPrefix(path), object, params);
return super.post(supportGs ? path : removeDefaultGsPrefix(path), object, params);
}

@Override
public RestResult post(String path, Object object, RestHeaders headers,
Map<String, Object> params) {
return super.post(supportGs ? path : removeDefaultGsPrefix(path), object, headers, params);
}

@Override
public RestResult put(String path, String id, Object object) {
return super.put(this.isSupportGs() ? path : removeDefaultGsPrefix(path), id, 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(this.isSupportGs() ? path : removeDefaultGsPrefix(path), id, object,
return super.put(supportGs ? path : removeDefaultGsPrefix(path), id, object,
headers);
}

@Override
public RestResult put(String path, String id, Object object, Map<String, Object> params) {
return super.put(this.isSupportGs() ? path : removeDefaultGsPrefix(path), id, object,
return super.put(supportGs ? path : removeDefaultGsPrefix(path), id, object,
params);
}

@Override
public RestResult put(String path, String id, Object object,
RestHeaders headers,
Map<String, Object> params) {
return super.put(supportGs ? path : removeDefaultGsPrefix(path), id, object, headers,
params);
}

@Override
public RestResult get(String path) {
return super.get(this.isSupportGs() ? path : removeDefaultGsPrefix(path));
return super.get(supportGs ? path : removeDefaultGsPrefix(path));
}

@Override
public RestResult get(String path, Map<String, Object> params) {
return super.get(this.isSupportGs() ? path : removeDefaultGsPrefix(path), params);
return super.get(supportGs ? path : removeDefaultGsPrefix(path), params);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ public GremlinManager(RestClient client, String graphSpace, String graph,
}

public ResultSet execute(GremlinRequest request) {
// Bind "graph" to all graphs
request.aliases.put("graph", this.graphSpace + "-" + this.graph);
// Bind "g" to all graphs by custom rule which define in gremlin server.
request.aliases.put("g", "__g_" + this.graphSpace + "-" + 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ 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);
this.client.coreVersion(this.version.getCoreVersion());
boolean supportGs = VersionUtil.gte(this.version.getCoreVersion(), "2.0");
this.client.setSupportGs(supportGs);
}

public String getGraphSpaceName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public static void prepareSchema() {
BaseApiTest.initEdgeLabel();
}

@SuppressWarnings("unused")
private static void assertContains(List<Vertex> vertices, Vertex vertex) {
Assert.assertTrue(Utils.contains(vertices, vertex));
}

@Override
@After
public void teardown() {
Expand Down Expand Up @@ -665,9 +670,4 @@ public void testDeleteNotExist() {
vertexAPI.delete("not-exist-v");
});
}

@SuppressWarnings("unused")
private static void assertContains(List<Vertex> vertices, Vertex vertex) {
Assert.assertTrue(Utils.contains(vertices, vertex));
}
}

0 comments on commit 1e749b6

Please sign in to comment.