Skip to content

Commit ca5fc0c

Browse files
authored
fix(server): support GraphAPI for rocksdb & add tests (#2900)
1 parent 2e0cffe commit ca5fc0c

File tree

6 files changed

+392
-27
lines changed

6 files changed

+392
-27
lines changed

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.hugegraph.api.API;
3030
import org.apache.hugegraph.api.filter.StatusFilter;
3131
import org.apache.hugegraph.auth.HugeAuthenticator.RequiredPerm;
32+
import org.apache.hugegraph.auth.HugeGraphAuthProxy;
3233
import org.apache.hugegraph.auth.HugePermission;
3334
import org.apache.hugegraph.config.HugeConfig;
3435
import org.apache.hugegraph.core.GraphManager;
@@ -128,7 +129,7 @@ public Object get(@Context GraphManager manager,
128129
LOG.debug("Get graph by name '{}'", name);
129130

130131
HugeGraph g = graph(manager, graphSpace, name);
131-
return ImmutableMap.of("name", g.spaceGraphName(), "backend", g.backend());
132+
return ImmutableMap.of("name", g.name(), "backend", g.backend());
132133
}
133134

134135
@DELETE
@@ -198,8 +199,7 @@ public Object create(@Context GraphManager manager,
198199
}
199200
}
200201

201-
// todo: auth get actual user info
202-
String creator = "admin";
202+
String creator = HugeGraphAuthProxy.getContext().user().username();
203203

204204
if (StringUtils.isNotEmpty(clone)) {
205205
// Clone from existing graph
@@ -214,7 +214,7 @@ public Object create(@Context GraphManager manager,
214214
if (description == null) {
215215
description = Strings.EMPTY;
216216
}
217-
Object result = ImmutableMap.of("name", graph.spaceGraphName(),
217+
Object result = ImmutableMap.of("name", graph.name(),
218218
"nickname", graph.nickname(),
219219
"backend", graph.backend(),
220220
"description", description);

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.commons.lang.StringUtils;
2727
import org.apache.hugegraph.api.API;
2828
import org.apache.hugegraph.api.filter.StatusFilter.Status;
29+
import org.apache.hugegraph.auth.HugeGraphAuthProxy;
2930
import org.apache.hugegraph.core.GraphManager;
3031
import org.apache.hugegraph.define.Checkable;
3132
import org.apache.hugegraph.exception.NotFoundException;
@@ -103,7 +104,7 @@ public String create(@Context GraphManager manager,
103104

104105
jsonGraphSpace.checkCreate(false);
105106

106-
String creator = "admin";
107+
String creator = HugeGraphAuthProxy.getContext().user().username();
107108
GraphSpace exist = manager.graphSpace(jsonGraphSpace.name);
108109
E.checkArgument(exist == null, "The graph space '%s' has existed",
109110
jsonGraphSpace.name);

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.apache.hugegraph.space.GraphSpace.DEFAULT_GRAPH_SPACE_SERVICE_NAME;
2323

2424
import java.io.IOException;
25+
import java.io.StringWriter;
2526
import java.text.ParseException;
2627
import java.util.Arrays;
2728
import java.util.Collections;
@@ -1227,10 +1228,42 @@ private void dropGraphLocal(HugeGraph graph) {
12271228
public HugeGraph createGraph(String graphSpace, String name, String creator,
12281229
Map<String, Object> configs, boolean init) {
12291230
if (!usePD()) {
1230-
return createGraphLocal(configs.toString(), name);
1231+
// Extract nickname from configs
1232+
String nickname;
1233+
if (configs.get("nickname") != null) {
1234+
nickname = configs.get("nickname").toString();
1235+
checkNickname(nickname);
1236+
} else {
1237+
nickname = name;
1238+
}
1239+
1240+
Date timeStamp = new Date();
1241+
1242+
// Convert Map to Properties format string
1243+
PropertiesConfiguration propConfig = new PropertiesConfiguration();
1244+
for (Map.Entry<String, Object> entry : configs.entrySet()) {
1245+
propConfig.setProperty(entry.getKey(), entry.getValue());
1246+
}
1247+
StringWriter writer = new StringWriter();
1248+
try {
1249+
propConfig.write(writer);
1250+
} catch (Exception e) {
1251+
throw new HugeException("Failed to convert config map to properties", e);
1252+
}
1253+
1254+
HugeGraph graph = createGraphLocal(name, writer.toString());
1255+
1256+
// Set metadata fields for non-PD mode
1257+
graph.nickname(nickname);
1258+
graph.creator(creator);
1259+
graph.createTime(timeStamp);
1260+
graph.updateTime(timeStamp);
1261+
1262+
return graph;
12311263
}
12321264

1233-
// When the registered graph space is not DEFAULT, only the graphs within that registered graph space are loaded.
1265+
// When the registered graph space is not DEFAULT, only the graphs within that registered
1266+
// graph space are loaded.
12341267
if (!"DEFAULT".equals(this.serviceGraphSpace) &&
12351268
!this.serviceGraphSpace.equals(graphSpace)) {
12361269
throw new HugeException(String.format(
@@ -1291,7 +1324,7 @@ public HugeGraph createGraph(String graphSpace, String name, String creator,
12911324

12921325
Date timeStamp = new Date();
12931326

1294-
configs.putIfAbsent("nickname", nickname);
1327+
// Note: nickname was already extracted and removed from configs earlier
12951328
configs.putIfAbsent("creator", creator);
12961329
configs.putIfAbsent("create_time", timeStamp);
12971330
configs.putIfAbsent("update_time", timeStamp);
@@ -1514,7 +1547,7 @@ private void closeTx(final Set<String> graphSourceNamesToCloseTxOn,
15141547
}
15151548

15161549
private String defaultSpaceGraphName(String graphName) {
1517-
return "DEFAULT-" + graphName;
1550+
return spaceGraphName("DEFAULT", graphName);
15181551
}
15191552

15201553
private void loadGraph(String name, String graphConfPath) {
@@ -1931,7 +1964,7 @@ public HugeGraph graph(String graphSpace, String name) {
19311964
}
19321965

19331966
public void dropGraphLocal(String name) {
1934-
HugeGraph graph = this.graph(name);
1967+
HugeGraph graph = this.graph(DEFAULT_GRAPH_SPACE_SERVICE_NAME + "-" + name);
19351968
E.checkArgument(this.conf.get(ServerOptions.ENABLE_DYNAMIC_CREATE_DROP),
19361969
"Not allowed to drop graph '%s' dynamically, " +
19371970
"please set `enable_dynamic_create_drop` to true.",
@@ -2052,6 +2085,17 @@ private void checkOptionsUnique(String graphSpace,
20522085

20532086
public Set<String> graphs(String graphSpace) {
20542087
Set<String> graphs = new HashSet<>();
2088+
2089+
if (!usePD()) {
2090+
for (String key : this.graphs.keySet()) {
2091+
String[] parts = key.split(DELIMITER);
2092+
if (parts[0].equals(graphSpace)) {
2093+
graphs.add(parts[1]);
2094+
}
2095+
}
2096+
return graphs;
2097+
}
2098+
20552099
for (String key : this.metaManager.graphConfigs(graphSpace).keySet()) {
20562100
graphs.add(key.split(DELIMITER)[1]);
20572101
}
@@ -2107,6 +2151,13 @@ private MapConfiguration buildConfig(Map<String, Object> configs) {
21072151

21082152
public void graphReadMode(String graphSpace, String graphName,
21092153
GraphReadMode readMode) {
2154+
2155+
if (!usePD()) {
2156+
HugeGraph g = this.graph(spaceGraphName(graphSpace, graphName));
2157+
g.readMode(readMode);
2158+
return;
2159+
}
2160+
21102161
try {
21112162
Map<String, Object> configs =
21122163
this.metaManager.getGraphConfig(graphSpace, graphName);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ public void create(String configPath, GlobalMasterInfo nodeInfo) {
11201120
this.serverStarted(nodeInfo);
11211121

11221122
// Write config to the disk file
1123-
String confPath = ConfigUtil.writeToFile(configPath, this.spaceGraphName(),
1123+
String confPath = ConfigUtil.writeToFile(configPath, this.name,
11241124
this.configuration());
11251125
this.configuration.file(confPath);
11261126
}

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ public class BaseApiTest {
7474
private static final String GRAPH_EDGE = "/graph/edges";
7575
private static final String BATCH = "/batch";
7676

77+
private static final String ROCKSDB_CONFIG_TEMPLATE =
78+
"{ \"gremlin.graph\": \"org.apache.hugegraph.HugeFactory\"," +
79+
"\"backend\": \"rocksdb\", \"serializer\": \"binary\"," +
80+
"\"store\": \"%s\", \"nickname\": \"%s\"," +
81+
"\"rocksdb.data_path\": \"rocksdbtest-data-%s\"," +
82+
"\"rocksdb.wal_path\": \"rocksdbtest-data-%s\"," +
83+
"\"search.text_analyzer\": \"jieba\"," +
84+
"\"search.text_analyzer_mode\": \"INDEX\" }";
85+
7786
protected static RestClient client;
7887

7988
private static final ObjectMapper MAPPER = new ObjectMapper();
@@ -661,6 +670,17 @@ public static Response createGraph(String graphSpace, String name) {
661670
return createGraph(graphSpace, name, name);
662671
}
663672

673+
public static Response createGraphInRocksDB(String graphSpace, String name) {
674+
return createGraphInRocksDB(graphSpace, name, name);
675+
}
676+
677+
public static Response createGraphInRocksDB(String graphSpace, String name,
678+
String nickname) {
679+
String path = String.format("graphspaces/%s/graphs/%s", graphSpace, name);
680+
String config = String.format(ROCKSDB_CONFIG_TEMPLATE, name, nickname, name, name);
681+
return client.post(path, Entity.json(config));
682+
}
683+
664684
public static Response createGraph(String graphSpace, String name,
665685
String nickname) {
666686
String config = "{\n" +

0 commit comments

Comments
 (0)