Skip to content

Commit 69f4e02

Browse files
Implement drop cypher db procedure
1 parent 3773da2 commit 69f4e02

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.cypher;
21+
22+
import org.apache.commons.lang3.mutable.MutableLong;
23+
import org.neo4j.dbms.api.DatabaseManagementService;
24+
import org.neo4j.dbms.api.DatabaseNotFoundException;
25+
import org.neo4j.gds.BaseProc;
26+
import org.neo4j.gds.compat.GraphDatabaseApiProxy;
27+
import org.neo4j.gds.core.utils.ProgressTimer;
28+
import org.neo4j.gds.executor.ProcPreconditions;
29+
import org.neo4j.gds.storageengine.InMemoryDatabaseCreationCatalog;
30+
import org.neo4j.procedure.Description;
31+
import org.neo4j.procedure.Name;
32+
import org.neo4j.procedure.Procedure;
33+
34+
import java.util.stream.Stream;
35+
36+
import static org.neo4j.gds.utils.StringFormatting.formatWithLocale;
37+
import static org.neo4j.procedure.Mode.WRITE;
38+
39+
public class DropCypherDbProc extends BaseProc {
40+
41+
private static final String DESCRIPTION = "Drop a database backed by an in-memory graph";
42+
43+
@Procedure(name = "gds.alpha.drop.cypherdb", mode = WRITE)
44+
@Description(DESCRIPTION)
45+
public Stream<DropCypherDbResult> dropDb(
46+
@Name(value = "dbName") String dbName
47+
) {
48+
ProcPreconditions.check();
49+
50+
DropCypherDbResult result = runWithExceptionLogging(
51+
"Drop in-memory Cypher database failed",
52+
() -> {
53+
GraphCreateCypherDbProc.validateNeo4jEnterpriseEdition(databaseService);
54+
var dbms = GraphDatabaseApiProxy.resolveDependency(databaseService, DatabaseManagementService.class);
55+
validateDatabaseName(dbName, dbms);
56+
var dropMillis = new MutableLong(0);
57+
try (var ignored = ProgressTimer.start(dropMillis::setValue)) {
58+
dbms.dropDatabase(dbName);
59+
}
60+
return new DropCypherDbResult(dbName, dropMillis.getValue());
61+
}
62+
);
63+
64+
return Stream.of(result);
65+
}
66+
67+
private static void validateDatabaseName(String dbName, DatabaseManagementService dbms) {
68+
if (!dbms.listDatabases().contains(dbName)) {
69+
throw new DatabaseNotFoundException(formatWithLocale("A database with name `%s` does not exist", dbName));
70+
}
71+
72+
var graphName = InMemoryDatabaseCreationCatalog.getRegisteredDbCreationGraphName(dbName);
73+
if (graphName == null) {
74+
throw new IllegalArgumentException(formatWithLocale(
75+
"Database with name `%s` is not an in-memory database",
76+
dbName
77+
));
78+
}
79+
}
80+
81+
public static class DropCypherDbResult {
82+
public final String dbName;
83+
public final long dropMillis;
84+
85+
public DropCypherDbResult(String dbName, long dropMillis) {
86+
this.dbName = dbName;
87+
this.dropMillis = dropMillis;
88+
}
89+
}
90+
}

alpha/alpha-proc/src/main/java/org/neo4j/gds/cypher/GraphCreateCypherDbProc.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.neo4j.gds.compat.StorageEngineProxy;
2828
import org.neo4j.gds.core.utils.ProgressTimer;
2929
import org.neo4j.gds.storageengine.InMemoryDatabaseCreator;
30+
import org.neo4j.graphdb.GraphDatabaseService;
3031
import org.neo4j.procedure.Description;
3132
import org.neo4j.procedure.Name;
3233
import org.neo4j.procedure.Procedure;
@@ -52,7 +53,7 @@ public Stream<CreateCypherDbResult> createDb(
5253
CreateCypherDbResult result = runWithExceptionLogging(
5354
"In-memory Cypher database creation failed",
5455
() -> {
55-
validateNeo4jEnterpriseEdition();
56+
validateNeo4jEnterpriseEdition(databaseService);
5657
MutableLong createMillis = new MutableLong(0);
5758
try (ProgressTimer ignored = ProgressTimer.start(createMillis::setValue)) {
5859
InMemoryDatabaseCreator.createDatabase(databaseService, username(), graphName, dbName);
@@ -78,7 +79,7 @@ public CreateCypherDbResult(String dbName, String graphName, long createMillis)
7879
}
7980
}
8081

81-
private void validateNeo4jEnterpriseEdition() {
82+
static void validateNeo4jEnterpriseEdition(GraphDatabaseService databaseService) {
8283
var edition = StorageEngineProxy.dbmsEdition(databaseService);
8384
if (!(edition == Edition.ENTERPRISE)) {
8485
throw new DatabaseManagementException(formatWithLocale(

open-packaging/src/test/java/org/neo4j/gds/OpenGdsProcedureSmokeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class OpenGdsProcedureSmokeTest extends BaseProcTest {
5252
"gds.alpha.graph.sample.rwr",
5353

5454
"gds.alpha.create.cypherdb",
55+
"gds.alpha.drop.cypherdb",
5556

5657
"gds.alpha.allShortestPaths.stream",
5758

@@ -532,7 +533,7 @@ void countShouldMatch() {
532533
);
533534

534535
// If you find yourself updating this count, please also update the count in SmokeTest.kt
535-
int expectedCount = 377;
536+
int expectedCount = 378;
536537
assertEquals(
537538
expectedCount,
538539
registeredProcedures.size(),

0 commit comments

Comments
 (0)