Search in sources :

Example 1 with DatabaseXmlSerializer

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);
}
Also used : DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) CleanupRemoteFile(org.syncany.plugins.transfer.files.CleanupRemoteFile) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) DatabaseVersion(org.syncany.database.DatabaseVersion)

Example 2 with DatabaseXmlSerializer

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);
}
Also used : DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer)

Example 3 with DatabaseXmlSerializer

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;
}
Also used : MemoryDatabase(org.syncany.database.MemoryDatabase) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer)

Example 4 with DatabaseXmlSerializer

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;
    }
}
Also used : ArrayList(java.util.ArrayList) MemoryDatabase(org.syncany.database.MemoryDatabase) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) StorageException(org.syncany.plugins.transfer.StorageException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DatabaseVersion(org.syncany.database.DatabaseVersion)

Example 5 with DatabaseXmlSerializer

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);
    }
}
Also used : ArrayList(java.util.ArrayList) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) StorageException(org.syncany.plugins.transfer.StorageException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) MemoryDatabase(org.syncany.database.MemoryDatabase) OutputStreamWriter(java.io.OutputStreamWriter) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) DatabaseVersion(org.syncany.database.DatabaseVersion) PrintWriter(java.io.PrintWriter)

Aggregations

DatabaseXmlSerializer (org.syncany.database.dao.DatabaseXmlSerializer)9 File (java.io.File)6 ArrayList (java.util.ArrayList)5 DatabaseVersion (org.syncany.database.DatabaseVersion)5 MemoryDatabase (org.syncany.database.MemoryDatabase)5 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)3 MultichunkRemoteFile (org.syncany.plugins.transfer.files.MultichunkRemoteFile)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 AbstractTransferOperation (org.syncany.operations.AbstractTransferOperation)2 UpOperation (org.syncany.operations.up.UpOperation)2 LocalTransferSettings (org.syncany.plugins.local.LocalTransferSettings)2 StorageException (org.syncany.plugins.transfer.StorageException)2 TransactionRemoteFile (org.syncany.plugins.transfer.files.TransactionRemoteFile)2 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Random (java.util.Random)1 ChunkEntry (org.syncany.database.ChunkEntry)1