diff --git a/src/main/java/it/cavallium/strangedb/java/database/DatabaseJava.java b/src/main/java/it/cavallium/strangedb/java/database/DatabaseJava.java index a5f3b23..d45e68d 100644 --- a/src/main/java/it/cavallium/strangedb/java/database/DatabaseJava.java +++ b/src/main/java/it/cavallium/strangedb/java/database/DatabaseJava.java @@ -15,7 +15,7 @@ import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.UUID; -import static it.cavallium.strangedb.database.references.DatabaseReferencesMetadata.*; +import static it.cavallium.strangedb.database.references.ConcurrentDatabaseReferencesMetadata.*; import static it.cavallium.strangedb.java.database.DatabaseObjectsIO.ENHANCED_OBJECT_METADATA_CLEANER; import static it.cavallium.strangedb.java.database.DatabaseObjectsIO.REFERENCES_LIST_CLEANER; @@ -48,7 +48,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools { throw new RuntimeException("Root already set!"); } T root; - if (referencesMetadata.getFirstFreeReference() > 0) { + if (referencesIO.getFirstFreeReference() > 0) { root = objectsIO.loadEnhancedObject(0); } else { if (objectsIO.newNullObject() != 0) { @@ -68,7 +68,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools { throw new RuntimeException("Root already set!"); } T root; - if (referencesMetadata.getFirstFreeReference() > 0) { + if (referencesIO.getFirstFreeReference() > 0) { root = objectsIO.loadEnhancedObject(0, forcedClassType); } else { if (objectsIO.newNullObject() != 0) { @@ -88,6 +88,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools { if (!this.closed) { this.close(); } + /** UUID uid = UUID.randomUUID(); Path newDataFile = dataFile.resolveSibling("compressed-data-file-" + uid + ".tmp"); Path newReferencesFile = referencesMetaFile.resolveSibling("compressed-references-file-" + uid + ".tmp"); @@ -100,7 +101,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools { DatabaseJava databaseToClean = instantiateNewDatabase(newDataFile, newReferencesFile); DatabaseJava newDatabase = instantiateNewDatabase(dataFile, referencesMetaFile); - long firstFreeReference = databaseToClean.referencesMetadata.getFirstFreeReference(); + long firstFreeReference = databaseToClean.referencesIO.getFirstFreeReference(); long referencesCount = 0; long writtenReferences = 0; @@ -109,7 +110,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools { for (int referenceID = 0; referenceID < firstFreeReference; referenceID++) { try { - ReferenceInfo ref = databaseToClean.referencesMetadata.getReferenceInfo(referenceID); + ReferenceInfo ref = databaseToClean.referencesIO.getReferenceInfo(referenceID); if (!NONEXISTENT_REFERENCE_INFO.equals(ref)) { referencesCount++; if (idsToKeep.contains(referenceID)) { @@ -127,11 +128,12 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools { newDatabase.close(); Files.deleteIfExists(newDataFile); Files.deleteIfExists(newReferencesFile); + **/ } private void cleanRef(DatabaseJava db, LongLinkedOpenHashSet idsToKeep, long ref) throws IOException { idsToKeep.add(ref); - ReferenceInfo refInfo = db.referencesMetadata.getReferenceInfo(ref); + ReferenceInfo refInfo = db.referencesIO.getReferenceInfo(ref); if (!NONEXISTENT_REFERENCE_INFO.equals(refInfo)) { switch (refInfo.getCleanerId()) { case ENHANCED_OBJECT_METADATA_CLEANER: { diff --git a/src/main/java/it/cavallium/strangedb/java/database/DatabaseObjectsIO.java b/src/main/java/it/cavallium/strangedb/java/database/DatabaseObjectsIO.java index 22f86d0..a1c5a34 100644 --- a/src/main/java/it/cavallium/strangedb/java/database/DatabaseObjectsIO.java +++ b/src/main/java/it/cavallium/strangedb/java/database/DatabaseObjectsIO.java @@ -1,6 +1,6 @@ package it.cavallium.strangedb.java.database; -import it.cavallium.strangedb.database.references.DatabaseReferencesIO; +import it.cavallium.strangedb.database.references.ConcurrentDatabaseReferencesIO; import it.cavallium.strangedb.java.annotations.*; import it.cavallium.strangedb.java.objects.EnhancedObject; import it.cavallium.strangedb.java.objects.EnhancedObjectFullInfo; @@ -24,7 +24,7 @@ import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.util.*; -import static it.cavallium.strangedb.database.references.DatabaseReferencesMetadata.BLANK_DATA_CLEANER; +import static it.cavallium.strangedb.database.references.ConcurrentDatabaseReferencesMetadata.BLANK_DATA_CLEANER; public class DatabaseObjectsIO implements IObjectsIO { @@ -32,12 +32,12 @@ public class DatabaseObjectsIO implements IObjectsIO { public static final byte REFERENCES_LIST_CLEANER = 2; private final IDatabaseTools databaseTools; - private final DatabaseReferencesIO referencesIO; + private final ConcurrentDatabaseReferencesIO referencesIO; private final DatabaseDataInitializer dataInitializer; private final KryoSerializer serializer; - public DatabaseObjectsIO(IDatabaseTools databaseTools, DatabaseReferencesIO referencesIO) { + public DatabaseObjectsIO(IDatabaseTools databaseTools, ConcurrentDatabaseReferencesIO referencesIO) { this.databaseTools = databaseTools; this.referencesIO = referencesIO; this.serializer = new KryoSerializer(); diff --git a/src/test/java/it/cavallium/strangedb/tests/EnhancedClassUpdate.java b/src/test/java/it/cavallium/strangedb/tests/EnhancedClassUpdate.java index 32d94e9..cc43900 100644 --- a/src/test/java/it/cavallium/strangedb/tests/EnhancedClassUpdate.java +++ b/src/test/java/it/cavallium/strangedb/tests/EnhancedClassUpdate.java @@ -7,22 +7,22 @@ import org.junit.Before; import org.junit.Test; import java.io.IOException; +import java.nio.file.DirectoryStream; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.Path; import static org.junit.Assert.assertEquals; public class EnhancedClassUpdate { - private Path path1; - private Path path3; + private Path dir; private DatabaseJava db; @Before public void setUp() throws Exception { - path1 = Files.createTempFile("db-tests-data-", ".db"); - path3 = Files.createTempFile("db-tests-references-", ".db"); - db = new DatabaseJava(path1, path3); + dir = Files.createTempDirectory("db-test"); + db = new DatabaseJava(dir.resolve("a.db"), dir.resolve("b.db")); db.registerClass(V2Class.class, 0); db.registerClass(OldClass.class, 1); OldClass root = db.loadRoot(OldClass::new); @@ -34,7 +34,7 @@ public class EnhancedClassUpdate { @Test public void shouldUpdateClass() throws IOException { - db = new DatabaseJava(path1, path3); + db = new DatabaseJava(dir.resolve("a.db"), dir.resolve("b.db")); db.registerClass(V2Class.class, 0); db.registerClass(OldClass.class, 1); V2Class root = db.loadRoot(V2Class::new, V2Class.class); @@ -46,7 +46,17 @@ public class EnhancedClassUpdate { @After public void tearDown() throws Exception { - Files.deleteIfExists(path1); - Files.deleteIfExists(path3); + deleteDirectoryRecursion(dir); + } + + void deleteDirectoryRecursion(Path path) throws IOException { + if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) { + try (DirectoryStream entries = Files.newDirectoryStream(path)) { + for (Path entry : entries) { + deleteDirectoryRecursion(entry); + } + } + } + Files.delete(path); } } diff --git a/src/test/java/it/cavallium/strangedb/tests/Performance.java b/src/test/java/it/cavallium/strangedb/tests/Performance.java index ae6456e..acffb0e 100644 --- a/src/test/java/it/cavallium/strangedb/tests/Performance.java +++ b/src/test/java/it/cavallium/strangedb/tests/Performance.java @@ -1,5 +1,6 @@ package it.cavallium.strangedb.tests; +import it.cavallium.strangedb.database.io.ExtensionUtils; import it.cavallium.strangedb.functionalinterfaces.RunnableWithIO; import it.cavallium.strangedb.java.annotations.*; import it.cavallium.strangedb.java.database.DatabaseJava; @@ -316,8 +317,6 @@ public class Performance { dbDataFile = rootDirectory.resolve("db_data.dat"); dbReferencesFile = rootDirectory.resolve("db_references.dat"); deleteDbFolders(); - Files.createFile(dbDataFile); - Files.createFile(dbReferencesFile); db = new DatabaseJava(dbDataFile, dbReferencesFile); int i = 0; db.registerClass(SimpleEnhancedObject.class, i++); @@ -336,8 +335,12 @@ public class Performance { } public static void deleteDbFolders() throws IOException { - Files.deleteIfExists(dbDataFile); - Files.deleteIfExists(dbReferencesFile); + for (int i = 0; i < 100; i++) { + Files.deleteIfExists(dbDataFile.resolveSibling(ExtensionUtils.insertExtension(dbDataFile.toFile().getName(), i))); + } + for (int i = 0; i < 100; i++) { + Files.deleteIfExists(dbReferencesFile.resolveSibling(ExtensionUtils.insertExtension(dbReferencesFile.toFile().getName(), i))); + } } public static void regenDb() throws IOException { diff --git a/src/test/java/it/cavallium/strangedb/tests/QueryTests.java b/src/test/java/it/cavallium/strangedb/tests/QueryTests.java index 7b4fbbe..6299e1f 100644 --- a/src/test/java/it/cavallium/strangedb/tests/QueryTests.java +++ b/src/test/java/it/cavallium/strangedb/tests/QueryTests.java @@ -10,8 +10,7 @@ import org.junit.Before; import org.junit.Test; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; +import java.nio.file.*; import java.util.ArrayList; import static org.junit.Assert.assertNotEquals; @@ -21,15 +20,13 @@ public class QueryTests { public static final String constantFirstName = "Jesus christ this is a very strange first name"; public static final String constantUsername = "is this an username?"; public static final String constantBio = "and is this a bio??? Are you mad?"; - private Path path1; - private Path path3; private DatabaseJava db; + private Path dir; @Before public void setUp() throws Exception { - path1 = Files.createTempFile("db-tests-data-", ".db"); - path3 = Files.createTempFile("db-tests-references-", ".db"); - db = new DatabaseJava(path1, path3); + dir = Files.createTempDirectory("db-test"); + db = new DatabaseJava(dir.resolve("data.db"), dir.resolve("refs.db")); int i = 0; db.registerClass(UserFullInfo.class, i++); db.registerClass(User.class, i++); @@ -55,7 +52,17 @@ public class QueryTests { @After public void tearDown() throws Exception { - Files.deleteIfExists(path1); - Files.deleteIfExists(path3); + deleteDirectoryRecursion(dir); + } + + void deleteDirectoryRecursion(Path path) throws IOException { + if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) { + try (DirectoryStream entries = Files.newDirectoryStream(path)) { + for (Path entry : entries) { + deleteDirectoryRecursion(entry); + } + } + } + Files.delete(path); } } diff --git a/strangedb-core b/strangedb-core index 7fd7497..7a4dd57 160000 --- a/strangedb-core +++ b/strangedb-core @@ -1 +1 @@ -Subproject commit 7fd7497464b5d5e92745974ed12ff4a9f51919d0 +Subproject commit 7a4dd5757ce68b85820e759a62054a3d8264ac1f