|
| 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 | +} |
0 commit comments