Skip to content
Merged
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
25 changes: 18 additions & 7 deletions engine/src/main/java/com/arcadedb/database/DatabaseFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@

import java.io.*;
import java.nio.charset.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;

public class DatabaseFactory implements AutoCloseable {
private SecurityManager security;
private boolean autoTransaction = false;
private final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private static final Map<String, Database> ACTIVE_INSTANCES = new ConcurrentHashMap<>();
private static final Map<Path, Database> ACTIVE_INSTANCES = new ConcurrentHashMap<>();
private final ContextConfiguration contextConfiguration = new ContextConfiguration();
private final String databasePath;
private final Map<DatabaseInternal.CALLBACK_EVENT, List<Callable<Void>>> callbacks = new HashMap<>();
Expand Down Expand Up @@ -122,27 +124,36 @@ public void registerCallback(final DatabaseInternal.CALLBACK_EVENT event, final
callbacks.add(callback);
}

private static Path getNormalizedPath(final String path) {
return Paths.get(path).toAbsolutePath().normalize();
}


public static Database getActiveDatabaseInstance(final String databasePath) {
return ACTIVE_INSTANCES.get(databasePath);
var normalizedPath = getNormalizedPath(databasePath);
return ACTIVE_INSTANCES.get(normalizedPath);
}

protected static void removeActiveDatabaseInstance(final String databasePath) {
ACTIVE_INSTANCES.remove(databasePath);
var normalizedPath = getNormalizedPath(databasePath);
ACTIVE_INSTANCES.remove(normalizedPath);
}

public static Collection<Database> getActiveDatabaseInstances() {
return Collections.unmodifiableCollection(ACTIVE_INSTANCES.values());
}

private static void checkForActiveInstance(final String databasePath) {
if (ACTIVE_INSTANCES.get(databasePath) != null)
throw new DatabaseOperationException("Found active instance of database '" + databasePath + "' already in use");
var normalizedPath = getNormalizedPath(databasePath);
if (ACTIVE_INSTANCES.get(normalizedPath) != null)
throw new DatabaseOperationException("Found active instance of database '" + normalizedPath + "' already in use");
}

private static void registerActiveInstance(final LocalDatabase database) {
if (ACTIVE_INSTANCES.putIfAbsent(database.databasePath, database) != null) {
var normalizedPath = getNormalizedPath(database.databasePath);
if (ACTIVE_INSTANCES.putIfAbsent(normalizedPath, database) != null) {
database.close();
throw new DatabaseOperationException("Found active instance of database '" + database.databasePath + "' already in use");
throw new DatabaseOperationException("Found active instance of database '" + normalizedPath + "' already in use");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.arcadedb.database;

import com.arcadedb.TestHelper;
import com.arcadedb.exception.DatabaseOperationException;
import com.arcadedb.security.SecurityManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -72,4 +73,30 @@ void testDatabaseRegistration() {
db.drop();
f.close();
}

@Test
void testDatabaseRegistrationWithDifferentPathTypes() {
final DatabaseFactory f = new DatabaseFactory("path/to/database");
final Database db = f.create();

Assertions.assertEquals(db, DatabaseFactory.getActiveDatabaseInstance("path/to/database"));
Assertions.assertEquals(db, DatabaseFactory.getActiveDatabaseInstance("path\\to\\database"));
Assertions.assertEquals(db, DatabaseFactory.getActiveDatabaseInstance("./path/../path/to/database"));

db.drop();
f.close();
}

@Test
void testDuplicatedDatabaseCreationWithDifferentPathTypes() {
final DatabaseFactory f1 = new DatabaseFactory("path/to/database");
final Database db = f1.create();

final DatabaseFactory f2 = new DatabaseFactory(".\\path\\to\\database");
Assertions.assertThrows(DatabaseOperationException.class, () -> f2.create());

db.drop();
f1.close();
f2.close();
}
}