@Override public OperationResult execute() throws Exception { logger.log(Level.INFO, "Determine file system actions ..."); FileSystemActionReconciliator actionReconciliator = new FileSystemActionReconciliator(config, result.getChangeSet()); List<FileSystemAction> actions; if (cleanupOccurred) { actions = actionReconciliator.determineFileSystemActions( winnersDatabase, true, preDeleteFileHistoriesWithLastVersion); } else { actions = actionReconciliator.determineFileSystemActions(winnersDatabase); } Set<MultiChunkId> unknownMultiChunks = determineRequiredMultiChunks(actions, winnersDatabase); downloader.downloadAndDecryptMultiChunks(unknownMultiChunks); result.getDownloadedMultiChunks().addAll(unknownMultiChunks); applyFileSystemActions(actions); return null; }
private void applyWinnersBranch(Branch winnersBranch, List<File> unknownRemoteDatabasesInCache) throws Exception { Branch winnersApplyBranch = databaseReconciliator.findWinnersApplyBranch(localBranch, winnersBranch); logger.log(Level.INFO, "- Database versions to APPLY locally: " + winnersApplyBranch); if (winnersApplyBranch.size() == 0) { logger.log(Level.WARNING, " + Nothing to update. Nice!"); result.setResultCode(DownResultCode.OK_NO_REMOTE_CHANGES); } else { logger.log(Level.INFO, "- Loading winners database ..."); Database winnersDatabase = readWinnersDatabase(winnersApplyBranch, unknownRemoteDatabasesInCache); FileSystemActionReconciliator actionReconciliator = new FileSystemActionReconciliator(config, localDatabase, result); List<FileSystemAction> actions = actionReconciliator.determineFileSystemActions(winnersDatabase); Set<MultiChunkEntry> unknownMultiChunks = determineRequiredMultiChunks(actions, winnersDatabase); downloadAndDecryptMultiChunks(unknownMultiChunks); applyFileSystemActions(actions); // Add winners database to local database // Note: This must happen AFTER the file system stuff, because we compare the winners database // with the local database! for (DatabaseVersionHeader applyDatabaseVersionHeader : winnersApplyBranch.getAll()) { logger.log( Level.INFO, " + Applying database version " + applyDatabaseVersionHeader.getVectorClock()); DatabaseVersion applyDatabaseVersion = winnersDatabase.getDatabaseVersion(applyDatabaseVersionHeader.getVectorClock()); localDatabase.addDatabaseVersion(applyDatabaseVersion); } logger.log(Level.INFO, "- Saving local database to " + config.getDatabaseFile() + " ..."); saveLocalDatabase(localDatabase, config.getDatabaseFile()); result.setResultCode(DownResultCode.OK_WITH_REMOTE_CHANGES); } }
public OperationResult execute() throws Exception { logger.log(Level.INFO, ""); logger.log(Level.INFO, "Running 'Sync down' at client " + config.getMachineName() + " ..."); logger.log(Level.INFO, "--------------------------------------------"); // 0. Load database and create TM initOperationVariables(); // 1. Check which remote databases to download based on the last local vector clock List<RemoteFile> unknownRemoteDatabases = listUnknownRemoteDatabases(localDatabase, transferManager); if (unknownRemoteDatabases.isEmpty()) { logger.log(Level.INFO, "* Nothing new. Skipping down operation."); result.setResultCode(DownResultCode.OK_NO_REMOTE_CHANGES); return result; } // 2. Download the remote databases to the local cache folder List<File> unknownRemoteDatabasesInCache = downloadUnknownRemoteDatabases(transferManager, unknownRemoteDatabases); // 3. Read version headers (vector clocks) Branches unknownRemoteBranches = readUnknownDatabaseVersionHeaders(unknownRemoteDatabasesInCache); // 4. Determine winner branch Branch winnersBranch = determineWinnerBranch(localDatabase, unknownRemoteBranches); logger.log(Level.INFO, "We have a winner! Now determine what to do locally ..."); // 5. Prune local stuff (if local conflicts exist) pruneConflictingLocalBranch(winnersBranch); // 6. Apply winner's branch applyWinnersBranch(winnersBranch, unknownRemoteDatabasesInCache); // 7. Write names of newly analyzed remote databases (so we don't download them again) writeAlreadyDownloadedDatabasesListFromFile(unknownRemoteDatabases); logger.log(Level.INFO, "Sync down done."); return result; }
private List<File> downloadUnknownRemoteDatabases( TransferManager transferManager, List<RemoteFile> unknownRemoteDatabases) throws StorageException { logger.log(Level.INFO, "Downloading unknown databases."); List<File> unknownRemoteDatabasesInCache = new ArrayList<File>(); for (RemoteFile remoteFile : unknownRemoteDatabases) { File unknownRemoteDatabaseFileInCache = config.getCache().getDatabaseFile(remoteFile.getName()); logger.log( Level.INFO, "- Downloading {0} to local cache at {1}", new Object[] {remoteFile.getName(), unknownRemoteDatabaseFileInCache}); transferManager.download( new DatabaseRemoteFile(remoteFile.getName()), unknownRemoteDatabaseFileInCache); unknownRemoteDatabasesInCache.add(unknownRemoteDatabaseFileInCache); result.getDownloadedUnknownDatabases().add(remoteFile.getName()); } return unknownRemoteDatabasesInCache; }