Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Making plugins almost entirely tx-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 17, 2014
1 parent 30a9d48 commit 6530e07
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -37,8 +35,6 @@
import org.syncany.plugins.transfer.files.RemoteFile;
import org.syncany.plugins.transfer.files.RepoRemoteFile;
import org.syncany.plugins.transfer.files.TempRemoteFile;
import org.syncany.plugins.transfer.files.TransactionRemoteFile;
import org.syncany.plugins.transfer.to.TransactionTO;

/**
* Implements a {@link TransferManager} based on a local storage backend for the
Expand Down Expand Up @@ -106,7 +102,7 @@ public void init(boolean createIfRequired) throws StorageException {
if (!databasesPath.mkdir()) {
throw new StorageException("Cannot create databases directory: " + databasesPath);
}

if (!actionsPath.mkdir()) {
throw new StorageException("Cannot create actions directory: " + databasesPath);
}
Expand All @@ -117,7 +113,7 @@ public void download(RemoteFile remoteFile, File localFile) throws StorageExcept
connect();

File repoFile = getRemoteFile(remoteFile);

if (!repoFile.exists()) {
repoFile = getRemoteFile(new TempRemoteFile(remoteFile.getName()));
}
Expand All @@ -140,14 +136,14 @@ public void download(RemoteFile remoteFile, File localFile) throws StorageExcept
throw new StorageException("Unable to copy file " + repoFile + " from local repository to " + localFile, ex);
}
}

@Override
public void move(RemoteFile sourceFile, RemoteFile targetFile) throws StorageException {
connect();

File sourceRemoteFile = getRemoteFile(sourceFile);
File targetRemoteFile = getRemoteFile(targetFile);

if (!sourceRemoteFile.exists()) {
throw new StorageMoveException("Unable to move file " + sourceFile + " because it does not exist.");
}
Expand All @@ -159,7 +155,7 @@ public void move(RemoteFile sourceFile, RemoteFile targetFile) throws StorageExc
throw new StorageException("Unable to move file " + sourceRemoteFile + " to destination " + targetRemoteFile, ex);
}
}

@Override
public void upload(File localFile, RemoteFile remoteFile) throws StorageException {
connect();
Expand Down Expand Up @@ -203,17 +199,6 @@ public boolean delete(RemoteFile remoteFile) throws StorageException {
public <T extends RemoteFile> Map<String, T> list(Class<T> remoteFileClass) throws StorageException {
connect();

Set<TransactionTO> transactions = new HashSet<TransactionTO>();
Set<RemoteFile> dummyDeletedFiles = new HashSet<RemoteFile>();
Set<RemoteFile> filesToIgnore = new HashSet<RemoteFile>();

if (!remoteFileClass.equals(TransactionRemoteFile.class)) {
// If we are listing transaction files, we don't want to ignore any
transactions = getTransactionTOs().keySet();
filesToIgnore = getFilesInTransactions(transactions);
dummyDeletedFiles = getDummyDeletedFiles(transactions);
}

// List folder
File remoteFilePath = getRemoteFilePath(remoteFileClass);
File[] files = remoteFilePath.listFiles();
Expand All @@ -225,28 +210,19 @@ public <T extends RemoteFile> Map<String, T> list(Class<T> remoteFileClass) thro

// Create RemoteFile objects
Map<String, T> remoteFiles = new HashMap<String, T>();

for (RemoteFile deletedFile : dummyDeletedFiles) {
if (deletedFile.getClass().equals(remoteFileClass)) {
T deletedTFile = remoteFileClass.cast(deletedFile);
remoteFiles.put(deletedTFile.getName(), deletedTFile);
}
}

for (File file : files) {
try {
T remoteFile = RemoteFile.createRemoteFile(file.getName(), remoteFileClass);
if (!filesToIgnore.contains(remoteFile)) {
remoteFiles.put(file.getName(), remoteFile);
}
remoteFiles.put(file.getName(), remoteFile);
}
catch (Exception e) {
logger.log(Level.INFO, "Cannot create instance of " + remoteFileClass.getSimpleName() + " for file " + file
+ "; maybe invalid file name pattern. Ignoring file.");
}
}

return remoteFiles;
return addAndFilterFilesInTransaction(remoteFileClass, remoteFiles);
}

private File getRemoteFile(RemoteFile remoteFile) {
Expand Down Expand Up @@ -309,7 +285,7 @@ public boolean testTargetExists() {
public boolean testRepoFileExists() {
try {
File repoFile = getRemoteFile(new RepoRemoteFile());

if (repoFile.exists()) {
logger.log(Level.INFO, "testRepoFileExists: Repo file exists, list(syncany) returned one result.");
return true;
Expand All @@ -321,7 +297,7 @@ public boolean testRepoFileExists() {
}
catch (Exception e) {
logger.log(Level.INFO, "testRepoFileExists: Repo file DOES NOT exist. Exception occurred.", e);
return false;
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public StorageTestResult test(boolean testCreateTarget) {
public void cleanTransactions(Config config) throws StorageException {
Map<TransactionTO, TransactionRemoteFile> transactions = getTransactionTOs();
RemoteTransaction remoteTransaction = new RemoteTransaction(config, this);

for (TransactionTO transaction : transactions.keySet()) {
if (transaction.getMachineName().equals(config.getMachineName())) {
// Delete all permanent or temporary files in this transaction.
Expand Down Expand Up @@ -143,7 +143,38 @@ protected Set<RemoteFile> getFilesInTransactions(Set<TransactionTO> transactions
return filesInTransaction;
}

protected Set<RemoteFile> getDummyDeletedFiles(Set<TransactionTO> transactions) throws StorageException {
protected <T extends RemoteFile> Map<String, T> addAndFilterFilesInTransaction(Class<T> remoteFileClass, Map<String, T> remoteFiles)
throws StorageException {
Map<String, T> filteredFiles = new HashMap<String, T>();

Set<TransactionTO> transactions = new HashSet<TransactionTO>();
Set<RemoteFile> dummyDeletedFiles = new HashSet<RemoteFile>();
Set<RemoteFile> filesToIgnore = new HashSet<RemoteFile>();

if (!remoteFileClass.equals(TransactionRemoteFile.class)) {
// If we are listing transaction files, we don't want to ignore any
transactions = getTransactionTOs().keySet();
filesToIgnore = getFilesInTransactions(transactions);
dummyDeletedFiles = getDummyDeletedFiles(transactions);
}

for (RemoteFile deletedFile : dummyDeletedFiles) {
if (deletedFile.getClass().equals(remoteFileClass)) {
T deletedTFile = remoteFileClass.cast(deletedFile);
filteredFiles.put(deletedTFile.getName(), deletedTFile);
}
}

for (String fileName : remoteFiles.keySet()) {
if (!filesToIgnore.contains(remoteFiles.get(fileName))) {
filteredFiles.put(fileName, remoteFiles.get(fileName));
}
}

return filteredFiles;
}

private Set<RemoteFile> getDummyDeletedFiles(Set<TransactionTO> transactions) throws StorageException {
Set<RemoteFile> dummyDeletedFiles = new HashSet<RemoteFile>();

for (TransactionTO transaction : transactions) {
Expand All @@ -157,10 +188,10 @@ protected Set<RemoteFile> getDummyDeletedFiles(Set<TransactionTO> transactions)
return dummyDeletedFiles;
}

protected Map<TransactionTO, TransactionRemoteFile> getTransactionTOs() throws StorageException {
private Map<TransactionTO, TransactionRemoteFile> getTransactionTOs() throws StorageException {
Map<String, TransactionRemoteFile> transactionFiles = list(TransactionRemoteFile.class);
Map<TransactionTO, TransactionRemoteFile> transactions = new HashMap<TransactionTO, TransactionRemoteFile>();

for (TransactionRemoteFile transaction : transactionFiles.values()) {
try {
File transactionFile = createTempFile("transaction");
Expand All @@ -181,7 +212,7 @@ protected Map<TransactionTO, TransactionRemoteFile> getTransactionTOs() throws S
throw new StorageException("Failed to read transactionFile", e);
}
}

return transactions;
}
}

0 comments on commit 6530e07

Please sign in to comment.