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);
}
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);
}
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;
}
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);
}
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()));
}
Aggregations