Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hugegraph.core;

import java.util.Objects;

/**
* Graph identity within the Server registry. Combined names remain in use at
* metadata, RPC and Gremlin boundaries.
*/
public record GraphKey(String graphSpace, String graphName) {

public GraphKey {
Objects.requireNonNull(graphSpace, "graphSpace");
Objects.requireNonNull(graphName, "graphName");
}

public static GraphKey fromString(String name) {
// Graph names cannot contain '-', so the last separator belongs to the
// graph name even when a graph-space name contains separators.
int separator = name.lastIndexOf(GraphManager.DELIMITER);
if (separator < 0) {
// An unqualified name must not resolve to a graph in DEFAULT.
return new GraphKey("", name);
}
return new GraphKey(name.substring(0, separator), name.substring(separator + 1));
}

public String spaceGraphName() {
return this.graphSpace + GraphManager.DELIMITER + this.graphName;
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.hugegraph.config.CoreOptions;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.ServerOptions;
import org.apache.hugegraph.core.GraphKey;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.dist.RegisterUtil;
import org.apache.hugegraph.event.EventHub;
Expand Down Expand Up @@ -120,7 +121,7 @@ public void testConfiguredWaitAllowsShortTaskToFinishBeforePdDrop() throws Excep
}).when(scheduler).close();
HugeGraph proxy = Mockito.mock(HugeGraph.class, org.mockito.AdditionalAnswers.delegatesTo(fixture.graph));
Mockito.doReturn(scheduler).when(proxy).taskScheduler();
fixture.graphs.put(fixture.graph.spaceGraphName(), proxy);
fixture.graphs.put(GraphKey.fromString(fixture.graph.spaceGraphName()), proxy);
ExecutorService caller = Executors.newSingleThreadExecutor();
try {
Future<?> deletion = caller.submit(fixture::drop);
Expand All @@ -134,7 +135,7 @@ public void testConfiguredWaitAllowsShortTaskToFinishBeforePdDrop() throws Excep
job.release.countDown();
caller.shutdown();
Assert.assertTrue(caller.awaitTermination(10L, TimeUnit.SECONDS));
fixture.graphs.replace(fixture.graph.spaceGraphName(), proxy, fixture.graph);
fixture.graphs.replace(GraphKey.fromString(fixture.graph.spaceGraphName()), proxy, fixture.graph);
}
}
}
Expand All @@ -147,15 +148,15 @@ public void testSchedulerCloseFailureDoesNotRemovePdMetadata() throws Exception
TaskScheduler failed = Mockito.spy(fixture.scheduler);
Mockito.doThrow(new IllegalStateException("scheduler close failed")).when(failed).close();
Mockito.doReturn(failed).when((HugeGraph) proxy).taskScheduler();
fixture.graphs.put(fixture.graph.spaceGraphName(), proxy);
fixture.graphs.put(GraphKey.fromString(fixture.graph.spaceGraphName()), proxy);
try {
RuntimeException error = Assert.assertThrows(RuntimeException.class, fixture::drop);
Assert.assertContains("scheduler close failed", error.getMessage());
Assert.assertTrue(fixture.metadataPresent.get());
Assert.assertFalse(fixture.graph.closed());
Assert.assertTrue(Files.exists(fixture.file));
} finally {
fixture.graphs.put(fixture.graph.spaceGraphName(), fixture.graph);
fixture.graphs.put(GraphKey.fromString(fixture.graph.spaceGraphName()), fixture.graph);
}
}
}
Expand Down Expand Up @@ -189,7 +190,7 @@ private static final class Fixture implements AutoCloseable {
private final Path directory;
private final Path file;
private final GraphManager manager;
private final Map<String, Graph> graphs;
private final Map<GraphKey, Graph> graphs;
private final HugeGraph graph;
private final HugeGraph neighbor;
private final HugeGraphParams params;
Expand Down Expand Up @@ -259,7 +260,7 @@ private HugeGraph open(String name) throws Exception {
result.addVertex(T.id, "kept", T.label, "person");
result.tx().commit();
((HugeGraphParams) Whitebox.getInternalState(result, "params")).closeTx();
this.graphs.put(result.spaceGraphName(), result);
this.graphs.put(GraphKey.fromString(result.spaceGraphName()), result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import org.apache.hugegraph.unit.core.GraphManagerAdminInitTest;
import org.apache.hugegraph.unit.core.GraphManagerClearGraphSpaceTest;
import org.apache.hugegraph.unit.core.GraphManagerConfigTest;
import org.apache.hugegraph.unit.core.GraphManagerGraphKeyTest;
import org.apache.hugegraph.unit.core.GraphManagerLocalGraphBindTest;
import org.apache.hugegraph.unit.core.GraphManagerServiceNamesTest;
import org.apache.hugegraph.unit.core.GroovyScriptEngineCompatibilityTest;
Expand Down Expand Up @@ -252,6 +253,7 @@
GraphManagerAdminInitTest.class,
GraphManagerClearGraphSpaceTest.class,
GraphManagerConfigTest.class,
GraphManagerGraphKeyTest.class,
GraphManagerLocalGraphBindTest.class,
GraphManagerServiceNamesTest.class,
HstoreSessionsTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.hugegraph.auth.ResourceType;
import org.apache.hugegraph.auth.HugeUser;
import org.apache.hugegraph.auth.RolePermission;
import org.apache.hugegraph.core.GraphKey;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.ServerOptions;
Expand Down Expand Up @@ -252,9 +253,9 @@ public void testPdGraphsIncludesLoadedLocalConfigGraphs() {
.thenReturn(Collections.emptyMap());
Whitebox.setInternalState(manager, "metaManager", metaManager);

Map<String, Graph> graphs = new ConcurrentHashMap<>();
graphs.put("DEFAULT-hugegraph", Mockito.mock(Graph.class));
graphs.put("DEFAULT-" + SYS_GRAPH, Mockito.mock(Graph.class));
Map<GraphKey, Graph> graphs = new ConcurrentHashMap<>();
graphs.put(GraphKey.fromString("DEFAULT-hugegraph"), Mockito.mock(Graph.class));
graphs.put(GraphKey.fromString("DEFAULT-" + SYS_GRAPH), Mockito.mock(Graph.class));
Whitebox.setInternalState(manager, "graphs", graphs);

Set<String> result = manager.graphs("DEFAULT");
Expand Down Expand Up @@ -499,8 +500,8 @@ private static GraphManager managerWithDefaultRoleContext(String operator,
GRAPHSPACE + "-" + GRAPH, Collections.emptyMap()));
Whitebox.setInternalState(manager, "metaManager", metaManager);

Map<String, Graph> graphs = new ConcurrentHashMap<>();
graphs.put(GRAPHSPACE + "-" + GRAPH, Mockito.mock(HugeGraph.class));
Map<GraphKey, Graph> graphs = new ConcurrentHashMap<>();
graphs.put(GraphKey.fromString(GRAPHSPACE + "-" + GRAPH), Mockito.mock(HugeGraph.class));
Whitebox.setInternalState(manager, "graphs", graphs);

Mockito.when(authManager.isDefaultRole(GRAPHSPACE, GRAPH, TARGET,
Expand Down Expand Up @@ -530,8 +531,8 @@ private static GraphManager managerWithLoadedDefaultGraph(String operator,
spaces.put("DEFAULT", new GraphSpace("DEFAULT"));
Whitebox.setInternalState(manager, "graphSpaces", spaces);

Map<String, Graph> graphs = new ConcurrentHashMap<>();
graphs.put("DEFAULT-hugegraph", Mockito.mock(HugeGraph.class));
Map<GraphKey, Graph> graphs = new ConcurrentHashMap<>();
graphs.put(GraphKey.fromString("DEFAULT-hugegraph"), Mockito.mock(HugeGraph.class));
Whitebox.setInternalState(manager, "graphs", graphs);
return manager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.hugegraph.HugeGraphParams;
import org.apache.hugegraph.api.graph.VertexAPI;
import org.apache.hugegraph.api.graph.EdgeAPI;
import org.apache.hugegraph.core.GraphKey;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.util.JsonUtil;
import org.apache.hugegraph.backend.cache.CachedGraphTransaction;
Expand Down Expand Up @@ -175,7 +176,8 @@ private static void updateThroughApi(HugeGraph graph, Class<?> api, String bodyC
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
GraphManager manager = (GraphManager) ((Unsafe) field.get(null)).allocateInstance(GraphManager.class);
Whitebox.setInternalState(manager, "graphs", Collections.singletonMap(graph.spaceGraphName(), graph));
Whitebox.setInternalState(manager, "graphs",
Collections.singletonMap(GraphKey.fromString(graph.spaceGraphName()), graph));
Class<?> bodyType = Class.forName(api.getName() + "$" + bodyClass);
Object body = JsonUtil.fromJson("{\"properties\":{\"added\":\"" + value + "\"}}", bodyType);
Method update = api.getMethod("update", GraphManager.class, String.class, String.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.hugegraph.api.space.GraphSpaceAPI;
import org.apache.hugegraph.config.CoreOptions;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.core.GraphKey;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.event.EventHub;
import org.apache.hugegraph.event.EventHub.NotifyResult;
Expand Down Expand Up @@ -129,11 +130,11 @@ public void testClearKeepsGraphsOfSpacesNamedLikeIt() {
for (String space : new String[]{"gs", "gs2", "gs_x"}) {
spaces.put(space, new GraphSpace(space));
}
Map<String, Graph> graphs =
Map<GraphKey, Graph> graphs =
Whitebox.getInternalState(manager, "graphs");
graphs.put("gs-g", cleared);
graphs.put("gs2-g", kept);
graphs.put("gs_x-g", keptUnderscore);
graphs.put(GraphKey.fromString("gs-g"), cleared);
graphs.put(GraphKey.fromString("gs2-g"), kept);
graphs.put(GraphKey.fromString("gs_x-g"), keptUnderscore);
Map<String, Service> services =
Whitebox.getInternalState(manager, "services");
services.put("gs-s", Mockito.mock(Service.class));
Expand All @@ -145,9 +146,9 @@ public void testClearKeepsGraphsOfSpacesNamedLikeIt() {
Mockito.verify(cleared).clearBackend();
Mockito.verify(kept, Mockito.never()).clearBackend();
Mockito.verify(keptUnderscore, Mockito.never()).clearBackend();
Assert.assertFalse(graphs.containsKey("gs-g"));
Assert.assertTrue(graphs.containsKey("gs2-g"));
Assert.assertTrue(graphs.containsKey("gs_x-g"));
Assert.assertFalse(graphs.containsKey(GraphKey.fromString("gs-g")));
Assert.assertTrue(graphs.containsKey(GraphKey.fromString("gs2-g")));
Assert.assertTrue(graphs.containsKey(GraphKey.fromString("gs_x-g")));

Mockito.verify(meta).service("gs", "s");
Mockito.verify(meta, Mockito.never()).service("gs2", "s");
Expand Down
Loading
Loading