Skip to content

Commit

Permalink
fix: avoid to scan all the database files for identify a database, is…
Browse files Browse the repository at this point in the history
…sue #10024
  • Loading branch information
tglman committed Sep 14, 2023
1 parent cc7bff0 commit 448f0f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException;
import com.orientechnologies.common.exception.OException;
import com.orientechnologies.common.io.OIOUtils;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.thread.OThreadPoolExecutors;
import com.orientechnologies.orient.core.Orient;
Expand Down Expand Up @@ -54,7 +53,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -1097,38 +1095,23 @@ public void removePool(ODatabasePoolInternal pool) {
}

private static void scanDatabaseDirectory(final File directory, DatabaseFound found) {
try {
if (directory.exists() && directory.isDirectory()) {
final File[] files = directory.listFiles();
if (files != null)
for (File db : files) {
if (db.isDirectory()) {
final Path dbPath = Paths.get(db.getAbsolutePath());
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dbPath)) {
stream.forEach(
(p) -> {
if (!Files.isDirectory(p)) {
final String fileName = p.getFileName().toString();

if (fileName.equals("database.ocf")
|| (fileName.startsWith(
OClusterBasedStorageConfiguration.COMPONENT_NAME)
&& fileName.endsWith(
OClusterBasedStorageConfiguration.DATA_FILE_EXTENSION))) {
final int count = p.getNameCount();
found.found(
OIOUtils.getDatabaseNameFromPath(
p.subpath(count - 2, count - 1).toString()));
}
}
});
if (directory.exists() && directory.isDirectory()) {
final File[] files = directory.listFiles();
if (files != null)
for (File db : files) {
if (db.isDirectory()) {
for (File cf : db.listFiles()) {
String fileName = cf.getName();
if (fileName.equals("database.ocf")
|| (fileName.startsWith(OClusterBasedStorageConfiguration.COMPONENT_NAME)
&& fileName.endsWith(
OClusterBasedStorageConfiguration.DATA_FILE_EXTENSION))) {
found.found(db.getName());
break;
}
}
}
}
} catch (IOException e) {
throw OException.wrapException(
new ODatabaseException("Exception during scanning of database directory"), e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public void testMultiThread() {
@Test
public void testListDatabases() {
OrientDB orientDb = new OrientDB("embedded:", OrientDBConfig.defaultConfig());
// OrientDBInternal orientDb = OrientDBInternal.fromUrl("local:.", null);
assertEquals(orientDb.list().size(), 0);
orientDb.create("test", ODatabaseType.MEMORY);
List<String> databases = orientDb.list();
Expand All @@ -120,6 +119,18 @@ public void testListDatabases() {
orientDb.close();
}

@Test
public void testListDatabasesPersistent() {
OrientDB orientDb = new OrientDB("embedded:./target/listTest", OrientDBConfig.defaultConfig());
assertEquals(orientDb.list().size(), 0);
orientDb.create("testListDatabase", ODatabaseType.PLOCAL);
List<String> databases = orientDb.list();
assertEquals(databases.size(), 1);
assertTrue(databases.contains("testListDatabase"));
orientDb.drop("testListDatabase");
orientDb.close();
}

@Test
public void testRegisterDatabase() {
final OrientDB orient = new OrientDB("embedded:", OrientDBConfig.defaultConfig());
Expand Down

0 comments on commit 448f0f8

Please sign in to comment.