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

Commit

Permalink
Unignoring deletes in transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 14, 2014
1 parent 42f32c5 commit 7e7fd38
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import org.syncany.plugins.transfer.files.MultiChunkRemoteFile;
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 @@ -119,6 +121,10 @@ public void download(RemoteFile remoteFile, File localFile) throws StorageExcept
connect();

File repoFile = getRemoteFile(remoteFile);

if (!repoFile.exists()) {
repoFile = getRemoteFile(new TempRemoteFile(remoteFile.getName()));
}

if (!repoFile.exists()) {
throw new StorageException("No such file in local repository: " + repoFile);
Expand Down Expand Up @@ -197,13 +203,15 @@ public boolean delete(RemoteFile remoteFile) throws StorageException {
public <T extends RemoteFile> Map<String, T> list(Class<T> remoteFileClass) throws StorageException {
connect();

Set<RemoteFile> filesToIgnore;
if (remoteFileClass.equals(TransactionRemoteFile.class)) {
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
filesToIgnore = new HashSet<RemoteFile>();
}
else {
filesToIgnore = getFilesInTransactions();
transactions = getTransactionTOs().keySet();
filesToIgnore = getFilesInTransactions(transactions);
dummyDeletedFiles = getDummyDeletedFiles(transactions);
}

// List folder
Expand All @@ -217,6 +225,13 @@ 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 = (T) deletedFile;
remoteFiles.put(deletedTFile.getName(), deletedTFile);
}
}

for (File file : files) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @author Philipp C. Heckel <philipp.heckel@gmail.com>
*/
public abstract class AbstractTransferManager implements TransferManager {
private static final Logger logger = Logger.getLogger(AbstractTransferManager.class.getSimpleName());
private static final Logger logger = Logger.getLogger(AbstractTransferManager.class.getSimpleName());
private TransferSettings settings;

public AbstractTransferManager(TransferSettings settings) {
Expand All @@ -63,13 +63,13 @@ protected File createTempFile(String name) throws IOException {

@Override
public StorageTestResult test(boolean testCreateTarget) {
logger.log(Level.INFO, "Performing storage test TM.test() ...");
logger.log(Level.INFO, "Performing storage test TM.test() ...");
StorageTestResult result = new StorageTestResult();

try {
logger.log(Level.INFO, "- Running connect() ...");
connect();

result.setTargetExists(testTargetExists());
result.setTargetCanWrite(testTargetCanWrite());
result.setRepoFileExists(testRepoFileExists());
Expand All @@ -85,13 +85,13 @@ public StorageTestResult test(boolean testCreateTarget) {
result.setTargetCanCreate(false);
}
}

result.setTargetCanConnect(true);
}
catch (StorageException e) {
result.setTargetCanConnect(false);
result.setException(e);

logger.log(Level.INFO, "-> Testing storage failed. Returning " + result, e);
}
finally {
Expand All @@ -102,10 +102,10 @@ public StorageTestResult test(boolean testCreateTarget) {
// Don't care
}
}

return result;
}

public void cleanTransactions(Config config) throws StorageException {
Map<TransactionTO, TransactionRemoteFile> transactions = getTransactionTOs();
RemoteTransaction remoteTransaction = new RemoteTransaction(config, this);
Expand All @@ -116,48 +116,63 @@ public void cleanTransactions(Config config) throws StorageException {
remoteTransaction.delete(action.getRemoteFile());
remoteTransaction.delete(action.getTempRemoteFile());
}

// Get corresponding remote file of transaction and delete it.
remoteTransaction.delete(transactions.get(transaction));
}
}
remoteTransaction.commit();
}

/**
* Returns a Set of all files that are not temporary, but are listed in a
* transaction file. These belong to an unfinished transaction and should be
* ignored.
*/
protected Set<RemoteFile> getFilesInTransactions() throws StorageException {
protected Set<RemoteFile> getFilesInTransactions(Set<TransactionTO> transactions) throws StorageException {
Set<RemoteFile> filesInTransaction = new HashSet<RemoteFile>();
Set<TransactionTO> transactions = getTransactionTOs().keySet();

for (TransactionTO transaction : transactions) {
for (ActionTO action : transaction.getActions()) {
filesInTransaction.add(action.getRemoteFile());
if (action.getType().equals(ActionTO.TYPE_UPLOAD)) {
filesInTransaction.add(action.getRemoteFile());
}
}
}

return filesInTransaction;
}

private Map<TransactionTO, TransactionRemoteFile> getTransactionTOs() throws StorageException{

protected Set<RemoteFile> getDummyDeletedFiles(Set<TransactionTO> transactions) {
Set<RemoteFile> dummyDeletedFiles = new HashSet<RemoteFile>();

for (TransactionTO transaction : transactions) {
for (ActionTO action : transaction.getActions()) {
if (action.getType().equals(ActionTO.TYPE_DELETE)) {
dummyDeletedFiles.add(action.getRemoteFile());
}
}
}

return dummyDeletedFiles;
}

protected 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");

// Download transaction file
download(transaction, transactionFile);
String transactionFileStr = FileUtils.readFileToString(transactionFile);

// Deserialize it
Serializer serializer = new Persister();
TransactionTO transactionTO = serializer.read(TransactionTO.class, transactionFileStr);

// Extract final locations
transactions.put(transactionTO, transaction);
transactionFile.delete();
Expand Down

0 comments on commit 7e7fd38

Please sign in to comment.