Search in sources :

Example 6 with DatabaseRemoteFile

use of org.syncany.plugins.transfer.files.DatabaseRemoteFile in project syncany by syncany.

the class ApplicationDaoTest method testShutdown.

@Test
public void testShutdown() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Prepare
    ApplicationSqlDao applicationDao = new ApplicationSqlDao(databaseConnection);
    List<DatabaseRemoteFile> expectedKnownDatabases = Arrays.asList(new DatabaseRemoteFile[] { new DatabaseRemoteFile("database-A-0000000001") });
    applicationDao.writeKnownRemoteDatabases(expectedKnownDatabases);
    // Run & Test
    assertTrue(new File(testConfig.getDatabaseFile() + ".lck").exists());
    applicationDao.shutdown();
    assertFalse(new File(testConfig.getDatabaseFile() + ".lck").exists());
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : Config(org.syncany.config.Config) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) Connection(java.sql.Connection) ApplicationSqlDao(org.syncany.database.dao.ApplicationSqlDao) File(java.io.File) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) Test(org.junit.Test)

Example 7 with DatabaseRemoteFile

use of org.syncany.plugins.transfer.files.DatabaseRemoteFile in project syncany by syncany.

the class ApplicationDaoTest method testGetKnownDatabases.

@Test
public void testGetKnownDatabases() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set3.sql");
    ApplicationSqlDao applicationDao = new ApplicationSqlDao(databaseConnection);
    List<DatabaseRemoteFile> actualKnownDatabases = applicationDao.getKnownDatabases();
    // Test
    assertTrue(CollectionUtil.containsExactly(Arrays.asList(new DatabaseRemoteFile[] { new DatabaseRemoteFile("database-B-0000000001"), new DatabaseRemoteFile("database-B-0000000002"), new DatabaseRemoteFile("database-B-0000000003") }), actualKnownDatabases));
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : Config(org.syncany.config.Config) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) Connection(java.sql.Connection) ApplicationSqlDao(org.syncany.database.dao.ApplicationSqlDao) Test(org.junit.Test)

Example 8 with DatabaseRemoteFile

use of org.syncany.plugins.transfer.files.DatabaseRemoteFile in project syncany by syncany.

the class ApplicationDaoTest method testPersistAndGetKnownDatabases.

@Test
public void testPersistAndGetKnownDatabases() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    ApplicationSqlDao applicationDao = new ApplicationSqlDao(databaseConnection);
    List<DatabaseRemoteFile> expectedKnownDatabases = Arrays.asList(new DatabaseRemoteFile[] { new DatabaseRemoteFile("database-A-0000000001"), new DatabaseRemoteFile("database-V-0000000001"), new DatabaseRemoteFile("database-B-0000000001"), new DatabaseRemoteFile("database-A-0000000002") });
    applicationDao.writeKnownRemoteDatabases(expectedKnownDatabases);
    List<DatabaseRemoteFile> actualKnownDatabases = applicationDao.getKnownDatabases();
    // Test
    assertTrue(CollectionUtil.containsExactly(expectedKnownDatabases, actualKnownDatabases));
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : Config(org.syncany.config.Config) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) Connection(java.sql.Connection) ApplicationSqlDao(org.syncany.database.dao.ApplicationSqlDao) Test(org.junit.Test)

Example 9 with DatabaseRemoteFile

use of org.syncany.plugins.transfer.files.DatabaseRemoteFile in project syncany by syncany.

the class UpOperation method checkPreconditions.

/**
	 * This method checks if:
	 *
	 * <ul>
	 * 	<li>If there are local changes => No need for Up.</li>
	 *  <li>If another clients is running Cleanup => Not allowed to upload.</li>
	 *  <li>If remote changes exist => Should Down first.</li>
	 * </ul>
	 *
	 * @returns boolean true if Up can and should be done, false otherwise.
	 */
private boolean checkPreconditions() throws Exception {
    // Find local changes
    StatusOperation statusOperation = new StatusOperation(config, options.getStatusOptions());
    StatusOperationResult statusOperationResult = statusOperation.execute();
    ChangeSet localChanges = statusOperationResult.getChangeSet();
    result.getStatusResult().setChangeSet(localChanges);
    if (!localChanges.hasChanges()) {
        logger.log(Level.INFO, "Local database is up-to-date (change set). NOTHING TO DO!");
        result.setResultCode(UpResultCode.OK_NO_CHANGES);
        return false;
    }
    // Check if other operations are running
    if (otherRemoteOperationsRunning(CleanupOperation.ACTION_ID)) {
        logger.log(Level.INFO, "* Cleanup running. Skipping down operation.");
        result.setResultCode(UpResultCode.NOK_UNKNOWN_DATABASES);
        return false;
    }
    // Find remote changes (unless --force is enabled)
    if (!options.forceUploadEnabled()) {
        LsRemoteOperationResult lsRemoteOperationResult = new LsRemoteOperation(config, transferManager).execute();
        List<DatabaseRemoteFile> unknownRemoteDatabases = lsRemoteOperationResult.getUnknownRemoteDatabases();
        if (unknownRemoteDatabases.size() > 0) {
            logger.log(Level.INFO, "There are remote changes. Call 'down' first or use --force-upload you must, Luke!");
            logger.log(Level.FINE, "Unknown remote databases are: " + unknownRemoteDatabases);
            result.setResultCode(UpResultCode.NOK_UNKNOWN_DATABASES);
            return false;
        } else {
            logger.log(Level.INFO, "No remote changes, ready to upload.");
        }
    } else {
        logger.log(Level.INFO, "Force (--force-upload) is enabled, ignoring potential remote changes.");
    }
    return true;
}
Also used : StatusOperation(org.syncany.operations.status.StatusOperation) LsRemoteOperationResult(org.syncany.operations.ls_remote.LsRemoteOperationResult) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) ChangeSet(org.syncany.operations.ChangeSet) StatusOperationResult(org.syncany.operations.status.StatusOperationResult) LsRemoteOperation(org.syncany.operations.ls_remote.LsRemoteOperation)

Example 10 with DatabaseRemoteFile

use of org.syncany.plugins.transfer.files.DatabaseRemoteFile in project syncany by syncany.

the class UpOperation method writeAndAddDeltaDatabase.

/**
	 * This method takes the metadata that is to be uploaded, loads it into a {@link MemoryDatabase} and serializes
	 * it to a file. If this is not a resumption of a previous transaction, this file is added to the transaction.
	 * Finally, databaseversions that are uploaded are remembered as known, such that they are not downloaded in future Downs.
	 *
	 * @param newDatabaseVersion {@link DatabaseVersion} containing all metadata that would be locally persisted if the transaction succeeds.
	 * @param resuming boolean indicating if the current transaction is in the process of being resumed.
	 */
private void writeAndAddDeltaDatabase(RemoteTransaction remoteTransaction, DatabaseVersion newDatabaseVersion, boolean resuming) throws InterruptedException, StorageException, IOException, SQLException {
    // Clone database version (necessary, because the original must not be touched)
    DatabaseVersion deltaDatabaseVersion = newDatabaseVersion.clone();
    // New delta database
    MemoryDatabase deltaDatabase = new MemoryDatabase();
    deltaDatabase.addDatabaseVersion(deltaDatabaseVersion);
    // Save delta database locally
    long newestLocalDatabaseVersion = getNewestDatabaseFileVersion(config.getMachineName(), localDatabase.getKnownDatabases());
    DatabaseRemoteFile remoteDeltaDatabaseFile = new DatabaseRemoteFile(config.getMachineName(), newestLocalDatabaseVersion + 1);
    File localDeltaDatabaseFile = config.getCache().getDatabaseFile(remoteDeltaDatabaseFile.getName());
    logger.log(Level.INFO, "Saving local delta database, version {0} to file {1} ... ", new Object[] { deltaDatabaseVersion.getHeader(), localDeltaDatabaseFile });
    saveDeltaDatabase(deltaDatabase, localDeltaDatabaseFile);
    if (!resuming) {
        // Upload delta database, if we are not resuming (in which case the db is in the transaction already)
        logger.log(Level.INFO, "- Uploading local delta database file ...");
        addLocalDatabaseToTransaction(remoteTransaction, localDeltaDatabaseFile, remoteDeltaDatabaseFile);
    }
    // Remember uploaded database as known.
    List<DatabaseRemoteFile> newDatabaseRemoteFiles = new ArrayList<DatabaseRemoteFile>();
    newDatabaseRemoteFiles.add(remoteDeltaDatabaseFile);
    localDatabase.writeKnownRemoteDatabases(newDatabaseRemoteFiles);
}
Also used : DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) MemoryDatabase(org.syncany.database.MemoryDatabase) ArrayList(java.util.ArrayList) 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)

Aggregations

DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)22 File (java.io.File)11 Test (org.junit.Test)8 MultichunkRemoteFile (org.syncany.plugins.transfer.files.MultichunkRemoteFile)5 RemoteFile (org.syncany.plugins.transfer.files.RemoteFile)5 ArrayList (java.util.ArrayList)4 DatabaseVersion (org.syncany.database.DatabaseVersion)4 CleanupRemoteFile (org.syncany.plugins.transfer.files.CleanupRemoteFile)4 Connection (java.sql.Connection)3 TreeMap (java.util.TreeMap)3 Config (org.syncany.config.Config)3 ApplicationSqlDao (org.syncany.database.dao.ApplicationSqlDao)3 PreparedStatement (java.sql.PreparedStatement)2 List (java.util.List)2 CommandLineClient (org.syncany.cli.CommandLineClient)2 DatabaseVersionHeader (org.syncany.database.DatabaseVersionHeader)2 MemoryDatabase (org.syncany.database.MemoryDatabase)2 LsRemoteOperationResult (org.syncany.operations.ls_remote.LsRemoteOperationResult)2 TransferManager (org.syncany.plugins.transfer.TransferManager)2 MasterRemoteFile (org.syncany.plugins.transfer.files.MasterRemoteFile)2