Multidatabase

This commit is contained in:
Andrea Cavalli 2019-04-24 21:32:36 +02:00
parent 8617a1848b
commit 6497d2cff9
6 changed files with 54 additions and 32 deletions

View File

@ -15,7 +15,7 @@ import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.UUID; 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.ENHANCED_OBJECT_METADATA_CLEANER;
import static it.cavallium.strangedb.java.database.DatabaseObjectsIO.REFERENCES_LIST_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!"); throw new RuntimeException("Root already set!");
} }
T root; T root;
if (referencesMetadata.getFirstFreeReference() > 0) { if (referencesIO.getFirstFreeReference() > 0) {
root = objectsIO.loadEnhancedObject(0); root = objectsIO.loadEnhancedObject(0);
} else { } else {
if (objectsIO.newNullObject() != 0) { if (objectsIO.newNullObject() != 0) {
@ -68,7 +68,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools {
throw new RuntimeException("Root already set!"); throw new RuntimeException("Root already set!");
} }
T root; T root;
if (referencesMetadata.getFirstFreeReference() > 0) { if (referencesIO.getFirstFreeReference() > 0) {
root = objectsIO.loadEnhancedObject(0, forcedClassType); root = objectsIO.loadEnhancedObject(0, forcedClassType);
} else { } else {
if (objectsIO.newNullObject() != 0) { if (objectsIO.newNullObject() != 0) {
@ -88,6 +88,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools {
if (!this.closed) { if (!this.closed) {
this.close(); this.close();
} }
/**
UUID uid = UUID.randomUUID(); UUID uid = UUID.randomUUID();
Path newDataFile = dataFile.resolveSibling("compressed-data-file-" + uid + ".tmp"); Path newDataFile = dataFile.resolveSibling("compressed-data-file-" + uid + ".tmp");
Path newReferencesFile = referencesMetaFile.resolveSibling("compressed-references-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 databaseToClean = instantiateNewDatabase(newDataFile, newReferencesFile);
DatabaseJava newDatabase = instantiateNewDatabase(dataFile, referencesMetaFile); DatabaseJava newDatabase = instantiateNewDatabase(dataFile, referencesMetaFile);
long firstFreeReference = databaseToClean.referencesMetadata.getFirstFreeReference(); long firstFreeReference = databaseToClean.referencesIO.getFirstFreeReference();
long referencesCount = 0; long referencesCount = 0;
long writtenReferences = 0; long writtenReferences = 0;
@ -109,7 +110,7 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools {
for (int referenceID = 0; referenceID < firstFreeReference; referenceID++) { for (int referenceID = 0; referenceID < firstFreeReference; referenceID++) {
try { try {
ReferenceInfo ref = databaseToClean.referencesMetadata.getReferenceInfo(referenceID); ReferenceInfo ref = databaseToClean.referencesIO.getReferenceInfo(referenceID);
if (!NONEXISTENT_REFERENCE_INFO.equals(ref)) { if (!NONEXISTENT_REFERENCE_INFO.equals(ref)) {
referencesCount++; referencesCount++;
if (idsToKeep.contains(referenceID)) { if (idsToKeep.contains(referenceID)) {
@ -127,11 +128,12 @@ public class DatabaseJava extends DatabaseCore implements IDatabaseTools {
newDatabase.close(); newDatabase.close();
Files.deleteIfExists(newDataFile); Files.deleteIfExists(newDataFile);
Files.deleteIfExists(newReferencesFile); Files.deleteIfExists(newReferencesFile);
**/
} }
private void cleanRef(DatabaseJava db, LongLinkedOpenHashSet idsToKeep, long ref) throws IOException { private void cleanRef(DatabaseJava db, LongLinkedOpenHashSet idsToKeep, long ref) throws IOException {
idsToKeep.add(ref); idsToKeep.add(ref);
ReferenceInfo refInfo = db.referencesMetadata.getReferenceInfo(ref); ReferenceInfo refInfo = db.referencesIO.getReferenceInfo(ref);
if (!NONEXISTENT_REFERENCE_INFO.equals(refInfo)) { if (!NONEXISTENT_REFERENCE_INFO.equals(refInfo)) {
switch (refInfo.getCleanerId()) { switch (refInfo.getCleanerId()) {
case ENHANCED_OBJECT_METADATA_CLEANER: { case ENHANCED_OBJECT_METADATA_CLEANER: {

View File

@ -1,6 +1,6 @@
package it.cavallium.strangedb.java.database; 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.annotations.*;
import it.cavallium.strangedb.java.objects.EnhancedObject; import it.cavallium.strangedb.java.objects.EnhancedObject;
import it.cavallium.strangedb.java.objects.EnhancedObjectFullInfo; import it.cavallium.strangedb.java.objects.EnhancedObjectFullInfo;
@ -24,7 +24,7 @@ import java.lang.reflect.Method;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.*; 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 { public class DatabaseObjectsIO implements IObjectsIO {
@ -32,12 +32,12 @@ public class DatabaseObjectsIO implements IObjectsIO {
public static final byte REFERENCES_LIST_CLEANER = 2; public static final byte REFERENCES_LIST_CLEANER = 2;
private final IDatabaseTools databaseTools; private final IDatabaseTools databaseTools;
private final DatabaseReferencesIO referencesIO; private final ConcurrentDatabaseReferencesIO referencesIO;
private final DatabaseDataInitializer dataInitializer; private final DatabaseDataInitializer dataInitializer;
private final KryoSerializer serializer; private final KryoSerializer serializer;
public DatabaseObjectsIO(IDatabaseTools databaseTools, DatabaseReferencesIO referencesIO) { public DatabaseObjectsIO(IDatabaseTools databaseTools, ConcurrentDatabaseReferencesIO referencesIO) {
this.databaseTools = databaseTools; this.databaseTools = databaseTools;
this.referencesIO = referencesIO; this.referencesIO = referencesIO;
this.serializer = new KryoSerializer(); this.serializer = new KryoSerializer();

View File

@ -7,22 +7,22 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path; import java.nio.file.Path;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class EnhancedClassUpdate { public class EnhancedClassUpdate {
private Path path1; private Path dir;
private Path path3;
private DatabaseJava db; private DatabaseJava db;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
path1 = Files.createTempFile("db-tests-data-", ".db"); dir = Files.createTempDirectory("db-test");
path3 = Files.createTempFile("db-tests-references-", ".db"); db = new DatabaseJava(dir.resolve("a.db"), dir.resolve("b.db"));
db = new DatabaseJava(path1, path3);
db.registerClass(V2Class.class, 0); db.registerClass(V2Class.class, 0);
db.registerClass(OldClass.class, 1); db.registerClass(OldClass.class, 1);
OldClass root = db.loadRoot(OldClass::new); OldClass root = db.loadRoot(OldClass::new);
@ -34,7 +34,7 @@ public class EnhancedClassUpdate {
@Test @Test
public void shouldUpdateClass() throws IOException { 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(V2Class.class, 0);
db.registerClass(OldClass.class, 1); db.registerClass(OldClass.class, 1);
V2Class root = db.loadRoot(V2Class::new, V2Class.class); V2Class root = db.loadRoot(V2Class::new, V2Class.class);
@ -46,7 +46,17 @@ public class EnhancedClassUpdate {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
Files.deleteIfExists(path1); deleteDirectoryRecursion(dir);
Files.deleteIfExists(path3); }
void deleteDirectoryRecursion(Path path) throws IOException {
if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
try (DirectoryStream<Path> entries = Files.newDirectoryStream(path)) {
for (Path entry : entries) {
deleteDirectoryRecursion(entry);
}
}
}
Files.delete(path);
} }
} }

View File

@ -1,5 +1,6 @@
package it.cavallium.strangedb.tests; package it.cavallium.strangedb.tests;
import it.cavallium.strangedb.database.io.ExtensionUtils;
import it.cavallium.strangedb.functionalinterfaces.RunnableWithIO; import it.cavallium.strangedb.functionalinterfaces.RunnableWithIO;
import it.cavallium.strangedb.java.annotations.*; import it.cavallium.strangedb.java.annotations.*;
import it.cavallium.strangedb.java.database.DatabaseJava; import it.cavallium.strangedb.java.database.DatabaseJava;
@ -316,8 +317,6 @@ public class Performance {
dbDataFile = rootDirectory.resolve("db_data.dat"); dbDataFile = rootDirectory.resolve("db_data.dat");
dbReferencesFile = rootDirectory.resolve("db_references.dat"); dbReferencesFile = rootDirectory.resolve("db_references.dat");
deleteDbFolders(); deleteDbFolders();
Files.createFile(dbDataFile);
Files.createFile(dbReferencesFile);
db = new DatabaseJava(dbDataFile, dbReferencesFile); db = new DatabaseJava(dbDataFile, dbReferencesFile);
int i = 0; int i = 0;
db.registerClass(SimpleEnhancedObject.class, i++); db.registerClass(SimpleEnhancedObject.class, i++);
@ -336,8 +335,12 @@ public class Performance {
} }
public static void deleteDbFolders() throws IOException { public static void deleteDbFolders() throws IOException {
Files.deleteIfExists(dbDataFile); for (int i = 0; i < 100; i++) {
Files.deleteIfExists(dbReferencesFile); 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 { public static void regenDb() throws IOException {

View File

@ -10,8 +10,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import static org.junit.Assert.assertNotEquals; 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 constantFirstName = "Jesus christ this is a very strange first name";
public static final String constantUsername = "is this an username?"; public static final String constantUsername = "is this an username?";
public static final String constantBio = "and is this a bio??? Are you mad?"; public static final String constantBio = "and is this a bio??? Are you mad?";
private Path path1;
private Path path3;
private DatabaseJava db; private DatabaseJava db;
private Path dir;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
path1 = Files.createTempFile("db-tests-data-", ".db"); dir = Files.createTempDirectory("db-test");
path3 = Files.createTempFile("db-tests-references-", ".db"); db = new DatabaseJava(dir.resolve("data.db"), dir.resolve("refs.db"));
db = new DatabaseJava(path1, path3);
int i = 0; int i = 0;
db.registerClass(UserFullInfo.class, i++); db.registerClass(UserFullInfo.class, i++);
db.registerClass(User.class, i++); db.registerClass(User.class, i++);
@ -55,7 +52,17 @@ public class QueryTests {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
Files.deleteIfExists(path1); deleteDirectoryRecursion(dir);
Files.deleteIfExists(path3); }
void deleteDirectoryRecursion(Path path) throws IOException {
if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
try (DirectoryStream<Path> entries = Files.newDirectoryStream(path)) {
for (Path entry : entries) {
deleteDirectoryRecursion(entry);
}
}
}
Files.delete(path);
} }
} }

@ -1 +1 @@
Subproject commit 7fd7497464b5d5e92745974ed12ff4a9f51919d0 Subproject commit 7a4dd5757ce68b85820e759a62054a3d8264ac1f