use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class SplitSyncUpOperationTest method testUploadLocalDatabase.
private void testUploadLocalDatabase(int fileSize, int fileAmount, int expectedTransactions, UpOperationOptions options) throws Exception {
List<File> originalFiles = TestFileUtil.createRandomFilesInDirectory(testConfig.getLocalDir(), fileSize, fileAmount);
// Run!
AbstractTransferOperation op = new UpOperation(testConfig, options);
UpOperationResult opResult = (UpOperationResult) op.execute();
// Ensure that the expected number of transactions has been completed to upload the files
assertNotNull(opResult);
assertTrue(opResult.getTransactionsCompleted() == expectedTransactions);
// Get databases (for comparison)
LocalTransferSettings localConnection = (LocalTransferSettings) testConfig.getConnection();
File localDatabaseDir = testConfig.getDatabaseDir();
assertNotNull(localDatabaseDir.listFiles());
assertTrue(localDatabaseDir.listFiles().length > 0);
List<File> remoteDatabaseFiles = new ArrayList<>();
for (int transaction = 1; transaction <= expectedTransactions; transaction++) {
String databaseVersion = String.format("%010d", transaction);
File remoteDatabaseFile = new File(localConnection.getPath() + "/databases/database-" + testConfig.getMachineName() + "-" + databaseVersion);
assertTrue(remoteDatabaseFile.exists());
remoteDatabaseFiles.add(remoteDatabaseFile);
}
// Import remote databases into memory database
DatabaseXmlSerializer dDAO = new DatabaseXmlSerializer(testConfig.getTransformer());
MemoryDatabase remoteDatabase = new MemoryDatabase();
for (File remoteDatabaseFile : remoteDatabaseFiles) {
dDAO.load(remoteDatabase, remoteDatabaseFile, null, null, DatabaseXmlSerializer.DatabaseReadType.FULL);
}
// Open local SQL Database
SqlDatabase localDatabase = new SqlDatabase(testConfig);
// Compare!
assertEquals(localDatabase.getLastDatabaseVersionHeader(), remoteDatabase.getLastDatabaseVersion().getHeader());
Map<PartialFileHistory.FileHistoryId, PartialFileHistory> localFileHistories = localDatabase.getFileHistoriesWithFileVersions();
Collection<PartialFileHistory> remoteFileHistories = remoteDatabase.getFileHistories();
assertEquals(localDatabase.getCurrentFileTree().size(), fileAmount);
assertEquals(localFileHistories.size(), remoteDatabase.getFileHistories().size());
List<FileVersion> remoteFileVersions = new ArrayList<FileVersion>();
List<FileVersion> localFileVersions = new ArrayList<FileVersion>();
for (PartialFileHistory partialFileHistory : remoteFileHistories) {
remoteFileVersions.add(partialFileHistory.getLastVersion());
assertNotNull(localFileHistories.get(partialFileHistory.getFileHistoryId()));
}
for (PartialFileHistory partialFileHistory : localFileHistories.values()) {
localFileVersions.add(partialFileHistory.getLastVersion());
}
assertTrue(CollectionUtil.containsExactly(localFileVersions, remoteFileVersions));
compareFileVersionsAgainstOriginalFiles(originalFiles, localFileVersions);
compareFileVersionsAgainstOriginalFiles(originalFiles, remoteFileVersions);
}
use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class UpOperationTest method testUploadLocalDatabase.
@Test
public void testUploadLocalDatabase() throws Exception {
int fileSize = 1230 * 1024;
int fileAmount = 3;
List<File> originalFiles = TestFileUtil.createRandomFilesInDirectory(testConfig.getLocalDir(), fileSize, fileAmount);
// Run!
AbstractTransferOperation op = new UpOperation(testConfig);
op.execute();
// Get databases (for comparison)
LocalTransferSettings localConnection = (LocalTransferSettings) testConfig.getConnection();
File localDatabaseDir = testConfig.getDatabaseDir();
File remoteDatabaseFile = new File(localConnection.getPath() + "/databases/database-" + testConfig.getMachineName() + "-0000000001");
assertNotNull(localDatabaseDir.listFiles());
assertTrue(localDatabaseDir.listFiles().length > 0);
assertTrue(remoteDatabaseFile.exists());
// - Memory database
DatabaseXmlSerializer dDAO = new DatabaseXmlSerializer(testConfig.getTransformer());
MemoryDatabase remoteDatabase = new MemoryDatabase();
dDAO.load(remoteDatabase, remoteDatabaseFile, null, null, DatabaseReadType.FULL);
DatabaseVersion remoteDatabaseVersion = remoteDatabase.getLastDatabaseVersion();
// - Sql Database
SqlDatabase localDatabase = new SqlDatabase(testConfig);
Map<FileHistoryId, PartialFileHistory> localFileHistories = localDatabase.getFileHistoriesWithFileVersions();
// Compare!
assertEquals(localDatabase.getLastDatabaseVersionHeader(), remoteDatabaseVersion.getHeader());
assertEquals(localFileHistories.size(), fileAmount);
assertEquals(localDatabase.getFileHistoriesWithFileVersions().size(), remoteDatabaseVersion.getFileHistories().size());
Collection<PartialFileHistory> remoteFileHistories = remoteDatabaseVersion.getFileHistories();
List<FileVersion> remoteFileVersions = new ArrayList<FileVersion>();
List<FileVersion> localFileVersions = new ArrayList<FileVersion>();
for (PartialFileHistory partialFileHistory : remoteFileHistories) {
remoteFileVersions.add(partialFileHistory.getLastVersion());
assertNotNull(localFileHistories.get(partialFileHistory.getFileHistoryId()));
}
for (PartialFileHistory partialFileHistory : localFileHistories.values()) {
localFileVersions.add(partialFileHistory.getLastVersion());
}
assertTrue(CollectionUtil.containsExactly(localFileVersions, remoteFileVersions));
compareFileVersionsAgainstOriginalFiles(originalFiles, localFileVersions);
compareFileVersionsAgainstOriginalFiles(originalFiles, remoteFileVersions);
}
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);
}
}
use of org.syncany.database.dao.DatabaseXmlSerializer in project syncany by syncany.
the class XmlDatabaseDaoTest method testWritePartialDatabaseOneToFive.
@Test
public void testWritePartialDatabaseOneToFive() 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
File writtenDatabaseFile = new File(tempDir + "/db-" + Math.random() + "-" + Math.abs(new Random().nextInt(Integer.MAX_VALUE)));
DatabaseXmlSerializer writeDAO = new DatabaseXmlSerializer();
writeDAO.save(writtenDatabase.getDatabaseVersions(), writtenDatabaseFile);
// Read again
MemoryDatabase readDatabase = new MemoryDatabase();
DatabaseXmlSerializer readDAO = new DatabaseXmlSerializer();
readDAO.load(readDatabase, writtenDatabaseFile, null, null, DatabaseReadType.FULL);
for (int i = 0; i < 10; i++) {
DatabaseVersion writtenDatabaseVersion = writtenDatabaseVersions.get(i);
DatabaseVersion readDatabaseVersion = readDatabase.getDatabaseVersion(writtenDatabaseVersion.getVectorClock());
assertNotNull(readDatabaseVersion);
assertDatabaseVersionEquals(writtenDatabaseVersion, readDatabaseVersion);
}
assertEquals(10, readDatabase.getDatabaseVersions().size());
}
Aggregations