Search in sources :

Example 1 with DatabaseVersion

use of org.syncany.database.DatabaseVersion in project syncany by syncany.

the class DownOperation method persistDatabaseVersion.

/**
	 * Persists a regular database version to the local database by using
	 * {@link SqlDatabase#writeDatabaseVersion(DatabaseVersion)}.
	 */
private void persistDatabaseVersion(MemoryDatabase winnersDatabase, DatabaseVersionHeader currentDatabaseVersionHeader) {
    logger.log(Level.INFO, "  + Applying database version " + currentDatabaseVersionHeader.getVectorClock());
    DatabaseVersion applyDatabaseVersion = winnersDatabase.getDatabaseVersion(currentDatabaseVersionHeader.getVectorClock());
    logger.log(Level.FINE, "  + Contents: " + applyDatabaseVersion);
    localDatabase.writeDatabaseVersion(applyDatabaseVersion);
}
Also used : DatabaseVersion(org.syncany.database.DatabaseVersion)

Example 2 with DatabaseVersion

use of org.syncany.database.DatabaseVersion 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 3 with DatabaseVersion

use of org.syncany.database.DatabaseVersion in project syncany by syncany.

the class DownOperation method populateDatabaseBranches.

/**
	 * This methods takes a Map containing DatabaseVersions (headers only) and loads these headers into {@link DatabaseBranches}.
	 * In addition, the local branch is added to this. The resulting DatabaseBranches will contain all headers exactly once,
	 * for the client that created that version.
	 *
	 * @param localBranch {@link DatabaseBranch} containing the locally known headers.
	 * @param remoteDatabaseHeaders Map from {@link DatabaseRemoteFile}s (important for client names) to the {@link DatabaseVersion}s that are
	 *        contained in these files.
	 *
	 * @return DatabaseBranches filled with all the headers that originated from either of the parameters.
	 */
private DatabaseBranches populateDatabaseBranches(DatabaseBranch localBranch, SortedMap<DatabaseRemoteFile, List<DatabaseVersion>> remoteDatabaseHeaders) {
    DatabaseBranches allBranches = new DatabaseBranches();
    allBranches.put(config.getMachineName(), localBranch.clone());
    for (DatabaseRemoteFile remoteDatabaseFile : remoteDatabaseHeaders.keySet()) {
        // Populate branches
        DatabaseBranch remoteClientBranch = allBranches.getBranch(remoteDatabaseFile.getClientName(), true);
        for (DatabaseVersion remoteDatabaseVersion : remoteDatabaseHeaders.get(remoteDatabaseFile)) {
            DatabaseVersionHeader header = remoteDatabaseVersion.getHeader();
            remoteClientBranch.add(header);
        }
    }
    logger.log(Level.INFO, "Populated unknown branches: " + allBranches);
    return allBranches;
}
Also used : DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) DatabaseVersion(org.syncany.database.DatabaseVersion)

Example 4 with DatabaseVersion

use of org.syncany.database.DatabaseVersion in project syncany by syncany.

the class XmlDatabaseDaoTest method testWriteAndReadMultipleDatabaseVersions.

@Test
public void testWriteAndReadMultipleDatabaseVersions() throws IOException {
    MemoryDatabase writtenDatabase = new MemoryDatabase();
    List<DatabaseVersion> writtenDatabaseVersions = new ArrayList<DatabaseVersion>();
    for (int i = 0; i < 10; i++) {
        DatabaseVersion basedOnDatabaseVersion = (i > 0) ? writtenDatabaseVersions.get(i - 1) : null;
        DatabaseVersion newDatabaseVersion = createDatabaseVersion(basedOnDatabaseVersion);
        // Some random chunks
        newDatabaseVersion.addChunk(new ChunkEntry(new ChunkChecksum(TestFileUtil.createRandomArray(20)), 32 * 1024));
        newDatabaseVersion.addChunk(new ChunkEntry(new ChunkChecksum(TestFileUtil.createRandomArray(20)), 32 * 1024));
        newDatabaseVersion.addChunk(new ChunkEntry(new ChunkChecksum(TestFileUtil.createRandomArray(20)), 32 * 1024));
        // Add to database
        writtenDatabase.addDatabaseVersion(newDatabaseVersion);
        // Add to test array
        writtenDatabaseVersions.add(newDatabaseVersion);
    }
    // Write database to disk, read it again, and compare them
    writeReadAndCompareDatabase(writtenDatabase);
}
Also used : ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) MemoryDatabase(org.syncany.database.MemoryDatabase) ArrayList(java.util.ArrayList) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 5 with DatabaseVersion

use of org.syncany.database.DatabaseVersion in project syncany by syncany.

the class XmlDatabaseDaoTest method testWriteAndReadChunks.

@Test
public void testWriteAndReadChunks() throws IOException {
    // Prepare
    MemoryDatabase newDatabase = new MemoryDatabase();
    DatabaseVersion newDatabaseVersion = createDatabaseVersion();
    // Create chunks
    ChunkEntry chunkA1 = new ChunkEntry(new ChunkChecksum(new byte[] { 1, 2, 3, 4, 5, 7, 8, 9, 0 }), 12);
    ChunkEntry chunkA2 = new ChunkEntry(new ChunkChecksum(new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 }), 34);
    ChunkEntry chunkA3 = new ChunkEntry(new ChunkChecksum(new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }), 56);
    ChunkEntry chunkA4 = new ChunkEntry(new ChunkChecksum(new byte[] { 2, 2, 2, 2, 2, 2, 2, 2, 2 }), 78);
    newDatabaseVersion.addChunk(chunkA1);
    newDatabaseVersion.addChunk(chunkA2);
    newDatabaseVersion.addChunk(chunkA3);
    newDatabaseVersion.addChunk(chunkA4);
    // Add database version
    newDatabase.addDatabaseVersion(newDatabaseVersion);
    // Write database to disk, read it again, and compare them
    MemoryDatabase loadedDatabase = writeReadAndCompareDatabase(newDatabase);
    // Check chunks
    assertEquals("Chunk not found in database loaded.", chunkA1, loadedDatabase.getChunk(chunkA1.getChecksum()));
    assertEquals("Chunk not found in database loaded.", chunkA2, loadedDatabase.getChunk(chunkA2.getChecksum()));
    assertEquals("Chunk not found in database loaded.", chunkA3, loadedDatabase.getChunk(chunkA3.getChecksum()));
    assertEquals("Chunk not found in database loaded.", chunkA4, loadedDatabase.getChunk(chunkA4.getChecksum()));
}
Also used : ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) MemoryDatabase(org.syncany.database.MemoryDatabase) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Aggregations

DatabaseVersion (org.syncany.database.DatabaseVersion)43 Test (org.junit.Test)22 MemoryDatabase (org.syncany.database.MemoryDatabase)20 MultiChunkEntry (org.syncany.database.MultiChunkEntry)12 PartialFileHistory (org.syncany.database.PartialFileHistory)12 ChunkEntry (org.syncany.database.ChunkEntry)11 FileVersion (org.syncany.database.FileVersion)10 ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)9 ArrayList (java.util.ArrayList)8 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)8 File (java.io.File)7 Config (org.syncany.config.Config)7 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)6 ChunkSqlDao (org.syncany.database.dao.ChunkSqlDao)6 DatabaseVersionSqlDao (org.syncany.database.dao.DatabaseVersionSqlDao)6 FileContentSqlDao (org.syncany.database.dao.FileContentSqlDao)6 FileHistorySqlDao (org.syncany.database.dao.FileHistorySqlDao)6 FileVersionSqlDao (org.syncany.database.dao.FileVersionSqlDao)6 MultiChunkSqlDao (org.syncany.database.dao.MultiChunkSqlDao)6 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)6