use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class CleanupOperation method writeMergeFile.
/**
* This method writes the file with merged databases for a single client and adds it to a Map containing all merged
* database files. This is done by querying the local database for all {@link DatabaseVersion}s by this client and
* serializing them.
*
* @param clientName for which we want to write the merged dataabse file.
* @param allMergedDatabaseFiles Map where we add the merged file once it is written.
*/
private void writeMergeFile(String clientName, Map<File, DatabaseRemoteFile> allMergedDatabaseFiles) throws StorageException, IOException {
// Increment the version by 1, to signal cleanup has occurred
long lastClientVersion = getNewestDatabaseFileVersion(clientName, localDatabase.getKnownDatabases());
DatabaseRemoteFile newRemoteMergeDatabaseFile = new DatabaseRemoteFile(clientName, lastClientVersion + 1);
File newLocalMergeDatabaseFile = config.getCache().getDatabaseFile(newRemoteMergeDatabaseFile.getName());
logger.log(Level.INFO, " + Writing new merge file (all files up to {0}) to {1} ...", new Object[] { lastClientVersion, newLocalMergeDatabaseFile });
Iterator<DatabaseVersion> lastNDatabaseVersions = localDatabase.getDatabaseVersionsTo(clientName, lastClientVersion);
DatabaseXmlSerializer databaseDAO = new DatabaseXmlSerializer(config.getTransformer());
databaseDAO.save(lastNDatabaseVersions, newLocalMergeDatabaseFile);
allMergedDatabaseFiles.put(newLocalMergeDatabaseFile, newRemoteMergeDatabaseFile);
}
use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class UpOperation method saveDeltaDatabase.
/**
* Serializes a {@link MemoryDatabase} to a file, using the configured transformer.
*/
protected void saveDeltaDatabase(MemoryDatabase db, File localDatabaseFile) throws IOException {
logger.log(Level.INFO, "- Saving database to " + localDatabaseFile + " ...");
DatabaseXmlSerializer dao = new DatabaseXmlSerializer(config.getTransformer());
dao.save(db.getDatabaseVersions(), localDatabaseFile);
}
use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class TestDatabaseUtil method readDatabaseFileFromDisk.
public static MemoryDatabase readDatabaseFileFromDisk(File databaseFile, Transformer transformer) throws IOException {
MemoryDatabase db = new MemoryDatabase();
DatabaseXmlSerializer dao = new DatabaseXmlSerializer(transformer);
dao.load(db, databaseFile, null, null, DatabaseReadType.FULL);
return db;
}
use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class UpOperation method attemptResumeDatabaseVersions.
private Collection<DatabaseVersion> attemptResumeDatabaseVersions(Collection<Long> versions) throws Exception {
try {
Collection<DatabaseVersion> databaseVersions = new ArrayList<>();
for (Long version : versions) {
File databaseFile = config.getTransactionDatabaseFile(version);
// If a single database file is missing, we should restart
if (!databaseFile.exists()) {
return null;
}
DatabaseXmlSerializer databaseSerializer = new DatabaseXmlSerializer();
MemoryDatabase memoryDatabase = new MemoryDatabase();
databaseSerializer.load(memoryDatabase, databaseFile, null, null, DatabaseReadType.FULL);
if (memoryDatabase.getDatabaseVersions().size() == 0) {
return null;
}
databaseVersions.add(memoryDatabase.getLastDatabaseVersion());
}
return databaseVersions;
} catch (Exception e) {
logger.log(Level.WARNING, "Cannot load database versions from 'state'. Cannot resume.");
return null;
}
}
use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class UpOperation method serializeRemoteTransactionsAndMetadata.
/**
* Serializes both the remote transaction and the current database version
* that would be added if Up was successful.
* @param newDatabaseVersion the current metadata
*/
private void serializeRemoteTransactionsAndMetadata(List<RemoteTransaction> remoteTransactions, List<DatabaseVersion> newDatabaseVersions) {
try {
logger.log(Level.INFO, "Persisting status of UpOperation to " + config.getStateDir() + " ...");
// Collect a list of all database version numbers that will be saved
List<Long> databaseVersionClocks = new ArrayList<>();
for (int i = 0; i < remoteTransactions.size(); i++) {
DatabaseVersion databaseVersion = newDatabaseVersions.get(i);
long databaseVersionClock = databaseVersion.getVectorClock().getClock(config.getMachineName());
databaseVersionClocks.add(databaseVersionClock);
}
// Write the list of version number to a file, before serializing any transactions!
// This ensures that no transaction files can exist without a "reference" to them.
File transactionListFile = config.getTransactionListFile();
PrintWriter transactionListWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(transactionListFile), "UTF-8"));
for (Long databaseVersion : databaseVersionClocks) {
transactionListWriter.println(databaseVersion);
}
transactionListWriter.close();
// For each database version write the transaction and database files
for (int i = 0; i < remoteTransactions.size(); i++) {
DatabaseVersion databaseVersion = newDatabaseVersions.get(i);
long databaseVersionClock = databaseVersionClocks.get(i);
// Writing transaction file to state dir
remoteTransactions.get(i).writeToFile(null, config.getTransactionFile(databaseVersionClock));
// Writing database representation of new database version to state dir
MemoryDatabase memoryDatabase = new MemoryDatabase();
memoryDatabase.addDatabaseVersion(databaseVersion);
DatabaseXmlSerializer dao = new DatabaseXmlSerializer();
dao.save(memoryDatabase.getDatabaseVersions(), config.getTransactionDatabaseFile(databaseVersionClock));
}
// The first transaction may be resumable, so write it to the default transaction file
remoteTransactions.get(0).writeToFile(null, config.getTransactionFile());
} catch (Exception e) {
logger.log(Level.WARNING, "Failure when persisting status of Up: ", e);
}
}
Aggregations