Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt for dynamically add/remove graph (#3) #138

Merged
merged 6 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions src/main/java/com/baidu/hugegraph/api/graphs/GraphsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.exception.InvalidResponseException;
Expand All @@ -33,6 +38,11 @@

public class GraphsAPI extends API {

private static final String CONFIRM_MESSAGE = "confirm_message";
private static final String DELIMITER = "/";
private static final String MODE = "mode";
private static final String GRAPH_READ_MODE = "graph_read_mode";

public GraphsAPI(RestClient client) {
super(client);
this.path(this.type());
Expand All @@ -43,6 +53,21 @@ protected String type() {
return HugeType.GRAPHS.string();
}

@SuppressWarnings("unchecked")
public Map<String, String> create(String name, String cloneGraphName,
String configText) {
this.client.checkApiVersion("0.67", "dynamic graph add");
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
Map<String, Object> params = null;
if (cloneGraphName != null && !cloneGraphName.isEmpty()) {
params = ImmutableMap.of("clone_graph_name", cloneGraphName);
}
RestResult result = this.client.post(joinPath(this.path(), name),
configText, headers, params);
return result.readObject(Map.class);
}

@SuppressWarnings("unchecked")
public Map<String, String> get(String name) {
RestResult result = this.client.get(this.path(), name);
Expand All @@ -54,19 +79,29 @@ public List<String> list() {
return result.readList(this.type(), String.class);
}

public void clear(String graph, String message) {
this.client.delete(joinPath(this.path(), graph, "clear"),
ImmutableMap.of(CONFIRM_MESSAGE, message));
}

public void delete(String graph, String message) {
this.client.checkApiVersion("0.67", "dynamic graph delete");
this.client.delete(joinPath(this.path(), graph),
ImmutableMap.of(CONFIRM_MESSAGE, message));
}

public void mode(String graph, GraphMode mode) {
String path = String.join("/", this.path(), graph);
// NOTE: Must provide id for PUT. If use "graph/mode", "/" will
// be encoded to "%2F". So use "mode" here although inaccurate.
this.client.put(path, "mode", mode);
this.client.put(joinPath(this.path(), graph), MODE, mode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can add joinPath(path, graph, action) method: joinPath(this.path(), graph, MODE),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PUT method must have id, MODE is id, should not treated as path

}

public GraphMode mode(String graph) {
String path = String.join("/", this.path(), graph);
RestResult result = this.client.get(path, "mode");
RestResult result = this.client.get(joinPath(this.path(), graph),
MODE);
@SuppressWarnings("unchecked")
Map<String, String> mode = result.readObject(Map.class);
String value = mode.get("mode");
String value = mode.get(MODE);
if (value == null) {
throw new InvalidResponseException(
"Invalid response, expect 'mode' in response");
Expand All @@ -81,20 +116,20 @@ public GraphMode mode(String graph) {

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

public GraphReadMode readMode(String graph) {
this.client.checkApiVersion("0.59", "graph read mode");
String path = String.join("/", this.path(), graph);
RestResult result = this.client.get(path, "graph_read_mode");
RestResult result = this.client.get(joinPath(this.path(), graph),
GRAPH_READ_MODE);
@SuppressWarnings("unchecked")
Map<String, String> readMode = result.readObject(Map.class);
String value = readMode.get("graph_read_mode");
String value = readMode.get(GRAPH_READ_MODE);
if (value == null) {
throw new InvalidResponseException(
"Invalid response, expect 'graph_read_mode' in response");
Expand All @@ -107,9 +142,11 @@ public GraphReadMode readMode(String graph) {
}
}

public void clear(String graph, String message) {
String path = String.join("/", this.path(), graph, "clear");
this.client.delete(path,
ImmutableMap.of("confirm_message", message));
private static String joinPath(String path, String id) {
return String.join(DELIMITER, path, id);
}

private static String joinPath(String path, String id, String action) {
return String.join(DELIMITER, path, id, action);
}
}
49 changes: 45 additions & 4 deletions src/main/java/com/baidu/hugegraph/driver/GraphsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@

package com.baidu.hugegraph.driver;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.baidu.hugegraph.api.graphs.GraphsAPI;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.ClientException;
import com.baidu.hugegraph.structure.constant.GraphMode;
import com.baidu.hugegraph.structure.constant.GraphReadMode;

Expand All @@ -35,6 +42,36 @@ public GraphsManager(RestClient client) {
this.graphsAPI = new GraphsAPI(client);
}

public Map<String, String> createGraph(String name, String config) {
return this.createGraph(name, null, config);
}

public Map<String, String> createGraph(String name, String cloneGraphName,
String config) {
return this.graphsAPI.create(name, cloneGraphName, config);
}

public Map<String, String> createGraph(String name, File file) {
String config;
try {
config = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ClientException("Failed to read config file: %s", file);
}
return this.createGraph(name, config);
}

public Map<String, String> createGraph(String name, String cloneGraphName,
File file) {
String config;
try {
config = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ClientException("Failed to read config file: %s", file);
}
return this.createGraph(name, cloneGraphName, config);
}

public Map<String, String> getGraph(String graph) {
return this.graphsAPI.get(graph);
}
Expand All @@ -43,6 +80,14 @@ public List<String> listGraph() {
return this.graphsAPI.list();
}

public void clear(String graph, String message) {
this.graphsAPI.clear(graph, message);
}

public void remove(String graph, String message) {
this.graphsAPI.delete(graph, message);
}

public void mode(String graph, GraphMode mode) {
this.graphsAPI.mode(graph, mode);
}
Expand All @@ -58,8 +103,4 @@ public void readMode(String graph, GraphReadMode readMode) {
public GraphReadMode readMode(String graph) {
return this.graphsAPI.readMode(graph);
}

public void clear(String graph, String message) {
this.graphsAPI.clear(graph, message);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/baidu/hugegraph/driver/HugeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void initManagers(RestClient client, String graph) {
private void checkServerApiVersion() {
VersionUtil.Version apiVersion = VersionUtil.Version.of(
this.version.getApiVersion());
VersionUtil.check(apiVersion, "0.38", "0.67",
VersionUtil.check(apiVersion, "0.38", "0.68",
"hugegraph-api in server");
this.client.apiVersion(apiVersion);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/baidu/hugegraph/api/ApiTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
TaskApiTest.class,
JobApiTest.class,
RestoreApiTest.class,
GraphsApiTest.class,

CommonTraverserApiTest.class,
KoutApiTest.class,
Expand Down
Loading