Search in sources :

Example 6 with DatabaseXmlSerializer

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);
}
Also used : ArrayList(java.util.ArrayList) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) UpOperationResult(org.syncany.operations.up.UpOperationResult) AbstractTransferOperation(org.syncany.operations.AbstractTransferOperation) UpOperation(org.syncany.operations.up.UpOperation) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) File(java.io.File)

Example 7 with DatabaseXmlSerializer

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);
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) ArrayList(java.util.ArrayList) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) PartialFileHistory(org.syncany.database.PartialFileHistory) AbstractTransferOperation(org.syncany.operations.AbstractTransferOperation) UpOperation(org.syncany.operations.up.UpOperation) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) SqlDatabase(org.syncany.database.SqlDatabase) File(java.io.File) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 8 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)

Example 9 with DatabaseXmlSerializer

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());
}
Also used : Random(java.util.Random) 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) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) File(java.io.File) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

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