2019-03-07 11:32:45 +01:00
|
|
|
package org.warp.jcwdb.database;
|
2019-01-31 20:05:23 +01:00
|
|
|
|
2019-03-07 11:32:45 +01:00
|
|
|
import org.warp.jcwdb.EnhancedObject;
|
|
|
|
import org.warp.jcwdb.functionalinterfaces.FunctionWithIO;
|
2019-01-31 20:05:23 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-03-06 23:56:04 +01:00
|
|
|
import java.nio.ByteBuffer;
|
2019-01-31 20:05:23 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2019-03-06 23:56:04 +01:00
|
|
|
import java.nio.file.StandardCopyOption;
|
2019-01-31 20:05:23 +01:00
|
|
|
|
2019-03-02 17:47:24 +01:00
|
|
|
public class Database implements IDatabase, IDatabaseTools {
|
2019-01-31 20:05:23 +01:00
|
|
|
|
2019-03-02 17:47:24 +01:00
|
|
|
private final IDatabaseTools databaseTools;
|
2019-01-31 20:05:23 +01:00
|
|
|
private final DatabaseFileIO fileIO;
|
|
|
|
private final DatabaseBlocksIO blocksIO;
|
|
|
|
private final DatabaseBlocksMetadata blocksMetadata;
|
|
|
|
private final DatabaseReferencesIO referencesIO;
|
|
|
|
private final DatabaseReferencesMetadata referencesMetadata;
|
|
|
|
private final DatabaseObjectsIO objectsIO;
|
2019-03-06 23:56:04 +01:00
|
|
|
private final Path dataFile;
|
|
|
|
private final Path blocksMetaFile;
|
|
|
|
private final Path referencesMetaFile;
|
2019-01-31 20:05:23 +01:00
|
|
|
private EnhancedObject loadedRootObject;
|
2019-03-06 23:56:04 +01:00
|
|
|
private volatile boolean closed;
|
2019-01-31 20:05:23 +01:00
|
|
|
|
|
|
|
public Database(Path dataFile, Path blocksMetaFile, Path referencesMetaFile) throws IOException {
|
|
|
|
if (Files.notExists(dataFile)) {
|
|
|
|
Files.createFile(dataFile);
|
|
|
|
}
|
|
|
|
if (Files.notExists(blocksMetaFile)) {
|
|
|
|
Files.createFile(blocksMetaFile);
|
|
|
|
}
|
|
|
|
if (Files.notExists(referencesMetaFile)) {
|
|
|
|
Files.createFile(referencesMetaFile);
|
|
|
|
}
|
2019-03-06 23:56:04 +01:00
|
|
|
this.dataFile = dataFile;
|
|
|
|
this.blocksMetaFile = blocksMetaFile;
|
|
|
|
this.referencesMetaFile = referencesMetaFile;
|
2019-03-02 17:47:24 +01:00
|
|
|
this.databaseTools = this;
|
2019-01-31 20:05:23 +01:00
|
|
|
this.fileIO = new DatabaseFileIO(dataFile);
|
|
|
|
this.blocksMetadata = new DatabaseBlocksMetadata(blocksMetaFile);
|
|
|
|
this.blocksIO = new DatabaseBlocksIO(fileIO, blocksMetadata);
|
|
|
|
this.referencesMetadata = new DatabaseReferencesMetadata(referencesMetaFile);
|
|
|
|
this.referencesIO = new DatabaseReferencesIO(blocksIO, referencesMetadata);
|
2019-03-02 17:47:24 +01:00
|
|
|
this.objectsIO = new DatabaseObjectsIO(databaseTools, referencesIO);
|
2019-01-31 20:05:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-06 23:56:04 +01:00
|
|
|
public synchronized void close() throws IOException {
|
|
|
|
if (this.closed) {
|
|
|
|
throw new IOException("The database has been already closed!");
|
|
|
|
}
|
2019-01-31 20:05:23 +01:00
|
|
|
this.objectsIO.setEnhancedObject(0, loadedRootObject);
|
|
|
|
this.referencesMetadata.close();
|
|
|
|
this.blocksMetadata.close();
|
|
|
|
this.fileIO.close();
|
2019-03-06 23:56:04 +01:00
|
|
|
this.closed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isClosed() {
|
|
|
|
return closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeAndClean() throws IOException {
|
|
|
|
if (!this.closed) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
Path newDataFile = dataFile.resolveSibling("compressed-data-file.tmp");
|
|
|
|
Path newBlocksFile = blocksMetaFile.resolveSibling("compressed-blocks-file.tmp");
|
|
|
|
Path newReferencesFile = referencesMetaFile.resolveSibling("compressed-references-file.tmp");
|
|
|
|
Path backupDataFile = dataFile.resolveSibling("backup-data.db.bak");
|
|
|
|
Path backupBlocksFile = blocksMetaFile.resolveSibling("backup-blocks.dat.bak");
|
|
|
|
Path backupReferencesFile = referencesMetaFile.resolveSibling("backup-references.dat.bak");
|
|
|
|
Files.copy(dataFile, backupDataFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
|
|
|
|
Files.copy(blocksMetaFile, backupBlocksFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
|
|
|
|
Files.copy(referencesMetaFile, backupReferencesFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
|
|
|
|
Files.move(dataFile, newDataFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
Files.move(blocksMetaFile, newBlocksFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
Files.move(referencesMetaFile, newReferencesFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
Database databaseToClean = new Database(newDataFile, newBlocksFile, newReferencesFile);
|
|
|
|
Database newDatabase = new Database(dataFile, blocksMetaFile, referencesMetaFile);
|
|
|
|
|
|
|
|
long referencesCount = databaseToClean.referencesMetadata.getFirstFreeReference();
|
|
|
|
long blocksCount = databaseToClean.blocksMetadata.getTotalBlocksCount();
|
|
|
|
long writtenReferences = 0;
|
|
|
|
|
|
|
|
for (int referenceID = 0; referenceID < referencesCount; referenceID++) {
|
|
|
|
try {
|
|
|
|
ByteBuffer buffer = databaseToClean.referencesIO.readFromReference(referenceID);
|
|
|
|
newDatabase.referencesIO.writeToReference(referenceID, buffer.limit(), buffer);
|
|
|
|
writtenReferences++;
|
|
|
|
} catch (IOException ex) {
|
|
|
|
System.out.println("Error while reading reference " + referenceID + ". References written: " + writtenReferences);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("References written: " + writtenReferences + ". Removed " + (blocksCount - writtenReferences) + " blocks. Removed " + (referencesCount - writtenReferences) + " references.");
|
|
|
|
databaseToClean.close();
|
|
|
|
newDatabase.close();
|
|
|
|
Files.deleteIfExists(newDataFile);
|
|
|
|
Files.deleteIfExists(newBlocksFile);
|
|
|
|
Files.deleteIfExists(newReferencesFile);
|
2019-01-31 20:05:23 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 17:47:24 +01:00
|
|
|
public <T extends EnhancedObject> T loadRoot(Class<T> type, FunctionWithIO<IDatabaseTools, T> ifAbsent) throws IOException {
|
2019-01-31 20:05:23 +01:00
|
|
|
if (loadedRootObject != null) {
|
|
|
|
throw new RuntimeException("Root already set!");
|
|
|
|
}
|
|
|
|
T root;
|
2019-03-02 17:47:24 +01:00
|
|
|
if (referencesMetadata.getFirstFreeReference() > 0) {
|
2019-01-31 20:05:23 +01:00
|
|
|
root = objectsIO.loadEnhancedObject(0, type);
|
|
|
|
} else {
|
|
|
|
if (objectsIO.newNullObject() != 0) {
|
|
|
|
throw new IOException("Can't allocate root!");
|
|
|
|
} else {
|
2019-03-02 17:47:24 +01:00
|
|
|
root = ifAbsent.apply(Database.this);
|
2019-01-31 20:05:23 +01:00
|
|
|
objectsIO.setEnhancedObject(0, root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadedRootObject = root;
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2019-02-01 00:04:51 +01:00
|
|
|
protected void registerClass(Class<?> type, int id) {
|
|
|
|
this.objectsIO.registerClass(type, id);
|
|
|
|
}
|
|
|
|
|
2019-03-02 17:47:24 +01:00
|
|
|
@Override
|
|
|
|
public void initializeEnhancedObject(EnhancedObject enhancedObject) throws IOException {
|
|
|
|
this.objectsIO.getDataInitializer().initializeDBObject(enhancedObject);
|
2019-01-31 20:05:23 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 17:47:24 +01:00
|
|
|
@Override
|
|
|
|
public IObjectsIO getObjectsIO() {
|
|
|
|
return objectsIO;
|
2019-01-31 20:05:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|