Search in sources :

Example 1 with TimeUnit

use of org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit in project syncany by syncany.

the class CleanupInterruptedTest method testUnreliableCleanup_Test2_databaseFileMerge.

@Test
public void testUnreliableCleanup_Test2_databaseFileMerge() throws Exception {
    // Setup
    UnreliableLocalTransferSettings testConnection = TestConfigUtil.createTestUnreliableLocalConnection(Arrays.asList(new String[] { // << 3 retries!!
    "rel=(11|12|13).+upload.+database" }));
    TestClient clientA = new TestClient("A", testConnection);
    clientA.createNewFile("file");
    clientA.up();
    clientA.changeFile("file");
    clientA.upWithForceChecksum();
    CleanupOperationOptions cleanupOptions = new CleanupOperationOptions();
    cleanupOptions.setMaxDatabaseFiles(1);
    cleanupOptions.setPurgeFileVersionSettings(new TreeMap<Long, TimeUnit>());
    boolean cleanupFailed = false;
    try {
        clientA.cleanup(cleanupOptions);
    } catch (StorageException e) {
        cleanupFailed = true;
    }
    assertTrue(cleanupFailed);
    TransferManager transferManagerA = TransferManagerFactory.build(clientA.getConfig()).withFeature(TransactionAware.class).asDefault();
    assertEquals(2, transferManagerA.list(MultichunkRemoteFile.class).size());
    assertEquals(2, new File(testConnection.getPath(), "multichunks").list().length);
    // Note that the list here differs from the actual files, because the transaction fails
    // while deletions have been done
    assertEquals(2, transferManagerA.list(DatabaseRemoteFile.class).size());
    assertEquals(0, new File(testConnection.getPath(), "databases").list().length);
    assertEquals(1, transferManagerA.list(TransactionRemoteFile.class).size());
    assertEquals(1, new File(testConnection.getPath(), "transactions").list().length);
    assertEquals(1, transferManagerA.list(ActionRemoteFile.class).size());
    assertEquals(1, new File(testConnection.getPath(), "actions").list().length);
    File tempDir = TestFileUtil.createTempDirectoryInSystemTemp();
    File tempFile = File.createTempFile("multichunk", "", tempDir);
    for (RemoteFile remoteFile : transferManagerA.list(DatabaseRemoteFile.class).values()) {
        transferManagerA.download(remoteFile, tempFile);
        assertTrue(tempFile.exists());
        tempFile.delete();
    }
    // Cleanup should have merged the two files.
    CleanupOperationResult result = clientA.cleanup(cleanupOptions);
    assertEquals(CleanupResultCode.OK, result.getResultCode());
    assertEquals(2, result.getMergedDatabaseFilesCount());
    assertEquals(2, transferManagerA.list(MultichunkRemoteFile.class).size());
    assertEquals(2, new File(testConnection.getPath(), "multichunks").list().length);
    assertEquals(1, transferManagerA.list(DatabaseRemoteFile.class).size());
    assertEquals(1, new File(testConnection.getPath(), "databases").list(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            return name.startsWith("database-");
        }
    }).length);
    assertEquals(0, transferManagerA.list(TransactionRemoteFile.class).size());
    assertEquals(0, new File(testConnection.getPath(), "transactions").list().length);
    assertEquals(0, transferManagerA.list(ActionRemoteFile.class).size());
    assertEquals(0, new File(testConnection.getPath(), "actions").list().length);
    clientA.deleteTestData();
}
Also used : TransferManager(org.syncany.plugins.transfer.TransferManager) CleanupOperationResult(org.syncany.operations.cleanup.CleanupOperationResult) CleanupOperationOptions(org.syncany.operations.cleanup.CleanupOperationOptions) FilenameFilter(java.io.FilenameFilter) TransactionAware(org.syncany.plugins.transfer.features.TransactionAware) UnreliableLocalTransferSettings(org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings) TestClient(org.syncany.tests.util.TestClient) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TimeUnit(org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit) StorageException(org.syncany.plugins.transfer.StorageException) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) ActionRemoteFile(org.syncany.plugins.transfer.files.ActionRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) ActionRemoteFile(org.syncany.plugins.transfer.files.ActionRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) Test(org.junit.Test)

Example 2 with TimeUnit

use of org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit in project syncany by syncany.

the class CleanupOperationTest method testCleanupNoChanges.

@Test
public void testCleanupNoChanges() throws Exception {
    // Setup
    LocalTransferSettings testConnection = (LocalTransferSettings) TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);
    CleanupOperationOptions options = new CleanupOperationOptions();
    options.setRemoveOldVersions(true);
    options.setPurgeFileVersionSettings(new TreeMap<Long, TimeUnit>());
    // Run
    // A: Create some file versions
    clientA.createNewFile("file.jpg");
    for (int i = 1; i <= 4; i++) {
        clientA.changeFile("file.jpg");
        clientA.upWithForceChecksum();
    }
    // B: Sync down, add something
    clientB.down();
    // B: Cleanup
    CleanupOperationResult cleanupOperationResult = clientB.cleanup(options);
    assertEquals(CleanupResultCode.OK_NOTHING_DONE, cleanupOperationResult.getResultCode());
    assertEquals(0, cleanupOperationResult.getMergedDatabaseFilesCount());
    assertEquals(0, cleanupOperationResult.getRemovedMultiChunksCount());
    assertEquals(0, cleanupOperationResult.getRemovedOldVersionsCount());
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : CleanupOperationResult(org.syncany.operations.cleanup.CleanupOperationResult) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) UnreliableLocalTransferSettings(org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings) TestClient(org.syncany.tests.util.TestClient) CleanupOperationOptions(org.syncany.operations.cleanup.CleanupOperationOptions) TimeUnit(org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit) Test(org.junit.Test)

Example 3 with TimeUnit

use of org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit in project syncany by syncany.

the class CleanupOperationTest method testCleanupMaxDatabaseFiles.

@Test
public void testCleanupMaxDatabaseFiles() throws Exception {
    // Setup
    LocalTransferSettings testConnection = (LocalTransferSettings) TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    CleanupOperationOptions options = new CleanupOperationOptions();
    options.setMinSecondsBetweenCleanups(0);
    options.setPurgeFileVersionSettings(new TreeMap<Long, TimeUnit>());
    options.setRemoveOldVersions(true);
    options.setMaxDatabaseFiles(3);
    // Run
    // A: Create some file versions
    clientA.createNewFile("file.jpg");
    for (int i = 1; i <= 4; i++) {
        clientA.changeFile("file.jpg");
        clientA.upWithForceChecksum();
    }
    // B: Cleanup
    CleanupOperationResult cleanupOperationResult = clientA.cleanup(options);
    assertEquals(CleanupResultCode.OK, cleanupOperationResult.getResultCode());
    assertEquals(4, cleanupOperationResult.getMergedDatabaseFilesCount());
    assertEquals(0, cleanupOperationResult.getRemovedMultiChunksCount());
    assertEquals(0, cleanupOperationResult.getRemovedOldVersionsCount());
    TestClient clientB = new TestClient("B", testConnection);
    clientB.down();
    // B: Create some file versions
    clientB.createNewFile("file-B.jpg");
    for (int i = 1; i <= 6; i++) {
        clientB.changeFile("file-B.jpg");
        clientB.upWithForceChecksum();
    }
    // B: Cleanup (2 clients, so 7 databases is too much)
    cleanupOperationResult = clientB.cleanup(options);
    assertEquals(CleanupResultCode.OK, cleanupOperationResult.getResultCode());
    assertEquals(7, cleanupOperationResult.getMergedDatabaseFilesCount());
    assertEquals(0, cleanupOperationResult.getRemovedMultiChunksCount());
    assertEquals(0, cleanupOperationResult.getRemovedOldVersionsCount());
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : CleanupOperationResult(org.syncany.operations.cleanup.CleanupOperationResult) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) UnreliableLocalTransferSettings(org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings) TestClient(org.syncany.tests.util.TestClient) CleanupOperationOptions(org.syncany.operations.cleanup.CleanupOperationOptions) TimeUnit(org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit) Test(org.junit.Test)

Example 4 with TimeUnit

use of org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit in project syncany by syncany.

the class CleanupOperation method collectPurgableFileVersions.

/**
 * For all time intervals defined in the purge file settings, determine the eligible file
 * versions to be purged -- namely all but the newest one.
 *
 * @see CleanupOperation
 * @see CleanupOperationOptions#getPurgeFileVersionSettings()
 * @see FileVersionSqlDao#getFileHistoriesToPurgeInInterval(long, long, TimeUnit)
 */
private Map<FileHistoryId, List<FileVersion>> collectPurgableFileVersions() {
    Map<FileHistoryId, List<FileVersion>> purgeFileVersions = new HashMap<FileHistoryId, List<FileVersion>>();
    long currentTime = System.currentTimeMillis();
    long previousTruncateIntervalTimeMultiplier = 0;
    for (Map.Entry<Long, TimeUnit> purgeFileVersionSetting : options.getPurgeFileVersionSettings().entrySet()) {
        Long truncateIntervalMultiplier = purgeFileVersionSetting.getKey();
        TimeUnit truncateIntervalTimeUnit = purgeFileVersionSetting.getValue();
        long beginIntervalTimestamp = currentTime - truncateIntervalMultiplier * 1000;
        long endIntervalTimestamp = currentTime - previousTruncateIntervalTimeMultiplier * 1000;
        Map<FileHistoryId, List<FileVersion>> newPurgeFileVersions = localDatabase.getFileHistoriesToPurgeInInterval(beginIntervalTimestamp, endIntervalTimestamp, truncateIntervalTimeUnit);
        putAllFileVersionsInMap(newPurgeFileVersions, purgeFileVersions);
        previousTruncateIntervalTimeMultiplier = truncateIntervalMultiplier;
    }
    return purgeFileVersions;
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) HashMap(java.util.HashMap) FileVersion(org.syncany.database.FileVersion) TimeUnit(org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 5 with TimeUnit

use of org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit in project syncany by syncany.

the class CleanupOperationTest method testFullFileVersionDeletion.

@Test
public void testFullFileVersionDeletion() throws Exception {
    // Setup
    LocalTransferSettings testConnection = (LocalTransferSettings) TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    java.sql.Connection databaseConnectionA = clientA.getConfig().createDatabaseConnection();
    CleanupOperationOptions options = new CleanupOperationOptions();
    options.setRemoveOldVersions(true);
    options.setPurgeFileVersionSettings(new TreeMap<Long, TimeUnit>());
    options.setMinKeepSeconds(0);
    options.setMinSecondsBetweenCleanups(0);
    // More than a month back
    clientA.createNewFile("file.jpg", 1024);
    clientA.upWithForceChecksum();
    // Less than a month back
    clientA.changeFile("file.jpg");
    clientA.upWithForceChecksum();
    clientA.cleanup(options);
    assertEquals("1", TestSqlUtil.runSqlSelect("select count(*) from fileversion", databaseConnectionA));
    // Tear down
    clientA.deleteTestData();
}
Also used : LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) UnreliableLocalTransferSettings(org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings) TestClient(org.syncany.tests.util.TestClient) CleanupOperationOptions(org.syncany.operations.cleanup.CleanupOperationOptions) TimeUnit(org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit) Test(org.junit.Test)

Aggregations

TimeUnit (org.syncany.operations.cleanup.CleanupOperationOptions.TimeUnit)6 Test (org.junit.Test)5 CleanupOperationOptions (org.syncany.operations.cleanup.CleanupOperationOptions)5 UnreliableLocalTransferSettings (org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings)5 TestClient (org.syncany.tests.util.TestClient)5 LocalTransferSettings (org.syncany.plugins.local.LocalTransferSettings)4 CleanupOperationResult (org.syncany.operations.cleanup.CleanupOperationResult)3 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 FileVersion (org.syncany.database.FileVersion)1 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)1 StorageException (org.syncany.plugins.transfer.StorageException)1 TransferManager (org.syncany.plugins.transfer.TransferManager)1 TransactionAware (org.syncany.plugins.transfer.features.TransactionAware)1