/** * Downloads the previously identified new/unknown remote databases to the local cache * and returns a map with the local cache files mapped to the given remote database * files. The method additionally fires events for every database it downloads. */ private SortedMap<File, DatabaseRemoteFile> downloadUnknownRemoteDatabases(List<DatabaseRemoteFile> unknownRemoteDatabases) throws StorageException { logger.log(Level.INFO, "Downloading unknown databases."); SortedMap<File, DatabaseRemoteFile> unknownRemoteDatabasesInCache = new TreeMap<File, DatabaseRemoteFile>(); int downloadFileIndex = 0; for (DatabaseRemoteFile remoteFile : unknownRemoteDatabases) { File unknownRemoteDatabaseFileInCache = config.getCache().getDatabaseFile(remoteFile.getName()); DatabaseRemoteFile unknownDatabaseRemoteFile = new DatabaseRemoteFile(remoteFile.getName()); logger.log(Level.INFO, "- Downloading {0} to local cache at {1}", new Object[] { remoteFile.getName(), unknownRemoteDatabaseFileInCache }); eventBus.post(new DownDownloadFileSyncExternalEvent(config.getLocalDir().getAbsolutePath(), "database", ++downloadFileIndex, unknownRemoteDatabases.size())); transferManager.download(unknownDatabaseRemoteFile, unknownRemoteDatabaseFileInCache); unknownRemoteDatabasesInCache.put(unknownRemoteDatabaseFileInCache, unknownDatabaseRemoteFile); result.getDownloadedUnknownDatabases().add(remoteFile.getName()); } return unknownRemoteDatabasesInCache; }
/** * This method writes the file with merged databases for a single client and adds it to a Map containing all merged * database files. This is done by querying the local database for all {@link DatabaseVersion}s by this client and * serializing them. * * @param clientName for which we want to write the merged dataabse file. * @param allMergedDatabaseFiles Map where we add the merged file once it is written. */ private void writeMergeFile(String clientName, Map<File, DatabaseRemoteFile> allMergedDatabaseFiles) throws StorageException, IOException { // Increment the version by 1, to signal cleanup has occurred long lastClientVersion = getNewestDatabaseFileVersion(clientName, localDatabase.getKnownDatabases()); DatabaseRemoteFile newRemoteMergeDatabaseFile = new DatabaseRemoteFile(clientName, lastClientVersion + 1); File newLocalMergeDatabaseFile = config.getCache().getDatabaseFile(newRemoteMergeDatabaseFile.getName()); logger.log(Level.INFO, " + Writing new merge file (all files up to {0}) to {1} ...", new Object[] { lastClientVersion, newLocalMergeDatabaseFile }); Iterator<DatabaseVersion> lastNDatabaseVersions = localDatabase.getDatabaseVersionsTo(clientName, lastClientVersion); DatabaseXmlSerializer databaseDAO = new DatabaseXmlSerializer(config.getTransformer()); databaseDAO.save(lastNDatabaseVersions, newLocalMergeDatabaseFile); allMergedDatabaseFiles.put(newLocalMergeDatabaseFile, newRemoteMergeDatabaseFile); }
File localDeltaDatabaseFile = config.getCache().getDatabaseFile(remoteDeltaDatabaseFile.getName());