Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Test and fixes for handling failed upload and downloads (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloyan-raev authored Nov 28, 2017
1 parent f37a3fc commit a52e4d6
Show file tree
Hide file tree
Showing 8 changed files with 765 additions and 58 deletions.
51 changes: 20 additions & 31 deletions src/main/java/io/goobox/sync/storj/CheckStateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,28 @@ private void processFiles(File[] files) {
try {
if (DB.contains(file)) {
SyncFile syncFile = DB.get(file.getName());
boolean cloudChanged = cloudChanged(syncFile, file);
if (localPath == null) {
if (syncFile.getState() == SyncState.FOR_DOWNLOAD
if (cloudChanged || syncFile.getState() == SyncState.FOR_DOWNLOAD
&& syncFile.getLocalModifiedTime() == 0) {
addForDownload(file);
} else if (syncFile.getState() == SyncState.DOWNLOAD_FAILED) {
if (syncFile.getLocalModifiedTime() == 0) {
DB.setDownloadFailed(file, localPath);
} else {
addForDownload(file);
}
} else {
setForCloudDelete(file);
}
} else {
boolean cloudChanged = cloudChanged(syncFile, file);
boolean localChanged = localChanged(syncFile, localPath);
if (cloudChanged && localChanged || syncFile.getState() == SyncState.FOR_DOWNLOAD) {
resolveConflict(file, localPath);
} else if (cloudChanged) {
addForDownload(file);
addForDownload(file, localPath);
} else if (localChanged) {
addForUpload(localPath);
addForUpload(file, localPath);
} else {
// no change - do nothing
}
Expand Down Expand Up @@ -134,20 +140,13 @@ private void processFiles(File[] files) {
try {
if (DB.contains(path)) {
SyncFile syncFile = DB.get(path.getFileName());
if (syncFile.getState().isSynced()) {
setForLocalDelete(path);
} else if (syncFile.getState() == SyncState.FOR_UPLOAD) {
if (syncFile.getStorjCreatedTime() == 0) {
addForUpload(path);
} else {
setForLocalDelete(path);
}
} else if (syncFile.getState() == SyncState.FOR_LOCAL_DELETE
|| syncFile.getState() == SyncState.FOR_DOWNLOAD) {
setForLocalDelete(path);
} else if (syncFile.getState() == SyncState.UPLOAD_FAILED
&& syncFile.getLocalModifiedTime() != getLocalTimestamp(path)) {
if (localChanged(syncFile, path)
|| syncFile.getState() == SyncState.FOR_UPLOAD && syncFile.getStorjCreatedTime() == 0) {
addForUpload(path);
} else if (syncFile.getState() == SyncState.UPLOAD_FAILED && syncFile.getStorjCreatedTime() == 0) {
DB.setUploadFailed(path);
} else {
setForLocalDelete(path);
}
} else {
addForUpload(path);
Expand Down Expand Up @@ -197,13 +196,11 @@ private long getLocalTimestamp(Path path) throws IOException {
}

private boolean cloudChanged(SyncFile syncFile, File file) throws ParseException {
return syncFile.getState() != SyncState.UPLOAD_FAILED
&& syncFile.getStorjCreatedTime() != getCloudTimestamp(file);
return syncFile.getStorjCreatedTime() != getCloudTimestamp(file);
}

private boolean localChanged(SyncFile syncFile, Path path) throws IOException {
return syncFile.getState() != SyncState.DOWNLOAD_FAILED
&& syncFile.getLocalModifiedTime() != getLocalTimestamp(path);
return syncFile.getLocalModifiedTime() != getLocalTimestamp(path);
}

private void resolveConflict(File file, Path path) throws IOException, ParseException {
Expand Down Expand Up @@ -243,21 +240,13 @@ private void setForCloudDelete(File file) {
tasks.add(new DeleteCloudFileTask(gooboxBucket, file));
}

private void setForLocalDelete(Path path) {
private void setForLocalDelete(Path path) throws IOException {
DB.setForLocalDelete(path);
tasks.add(new DeleteLocalFileTask(path));
}

private void cleanDeletedFilesFromDB(File[] files, List<Path> localPaths) {
cleanDeletedFilesFromDB(SyncState.FOR_DOWNLOAD, files, localPaths);
cleanDeletedFilesFromDB(SyncState.FOR_UPLOAD, files, localPaths);
cleanDeletedFilesFromDB(SyncState.FOR_CLOUD_DELETE, files, localPaths);
cleanDeletedFilesFromDB(SyncState.FOR_LOCAL_DELETE, files, localPaths);
}

private void cleanDeletedFilesFromDB(SyncState state, File[] files, List<Path> localPaths) {
List<SyncFile> syncFiles = DB.getWithState(state);
for (SyncFile syncFile : syncFiles) {
for (SyncFile syncFile : DB.all()) {
String fileName = syncFile.getName();
File storjFile = getStorjFile(fileName, files);
Path localPath = getLocalPath(fileName, localPaths);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/io/goobox/sync/storj/DownloadFileTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.goobox.sync.storj;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import io.goobox.sync.storj.db.DB;
Expand Down Expand Up @@ -60,9 +61,14 @@ public void onComplete(File file, String localPath) {

@Override
public void onError(File file, String message) {
DB.setDownloadFailed(file);
DB.commit();
System.out.println(" " + message);
Path localPath = Utils.getSyncDir().resolve(file.getName());
try {
DB.setDownloadFailed(file, localPath);
DB.commit();
System.out.println(" " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/goobox/sync/storj/UploadFileTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ public void onError(String message) {

@Override
public void onError(String filePath, String message) {
DB.setUploadFailed(path);
DB.commit();
System.out.println(" " + message);
try {
DB.setUploadFailed(path);
DB.commit();
System.out.println(" " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Expand Down
32 changes: 20 additions & 12 deletions src/main/java/io/goobox/sync/storj/db/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.goobox.sync.storj.db;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

Expand Down Expand Up @@ -47,10 +48,6 @@ private static ObjectFilter withName(String fileName) {
return ObjectFilters.eq("name", fileName);
}

private static ObjectFilter withState(SyncState state) {
return ObjectFilters.eq("state", state);
}

private static Nitrite open() {
Path dbPath = Utils.getDataDir().resolve("sync.db");
return Nitrite.builder()
Expand All @@ -67,6 +64,10 @@ public synchronized static void commit() {
db().commit();
}

public synchronized static List<SyncFile> all() {
return repo().find().toList();
}

public synchronized static boolean contains(File file) {
return contains(file.getName());
}
Expand Down Expand Up @@ -134,6 +135,7 @@ public synchronized static void setSynced(File storjFile, Path localFile) throws
}

public synchronized static void addForDownload(File file) {
remove(file);
SyncFile syncFile = getOrCreate(file);
syncFile.setCloudData(file);
syncFile.setState(SyncState.FOR_DOWNLOAD);
Expand All @@ -149,6 +151,7 @@ public synchronized static void addForDownload(File storjFile, Path localFile) t
}

public synchronized static void addForUpload(Path path) throws IOException {
remove(path);
SyncFile syncFile = getOrCreate(path);
syncFile.setLocalData(path);
syncFile.setState(SyncState.FOR_UPLOAD);
Expand All @@ -163,26 +166,35 @@ public synchronized static void addForUpload(File storjFile, Path localFile) thr
repo().update(syncFile);
}

public synchronized static void setDownloadFailed(File file) {
SyncFile syncFile = get(file);
public synchronized static void setDownloadFailed(File storjFile, Path localFile) throws IOException {
SyncFile syncFile = get(storjFile);
syncFile.setCloudData(storjFile);
if (Files.exists(localFile)) {
syncFile.setLocalData(localFile);
}
syncFile.setState(SyncState.DOWNLOAD_FAILED);
repo().update(syncFile);
}

public synchronized static void setUploadFailed(Path path) {
public synchronized static void setUploadFailed(Path path) throws IOException {
SyncFile syncFile = get(path);
if (Files.exists(path)) {
syncFile.setLocalData(path);
}
syncFile.setState(SyncState.UPLOAD_FAILED);
repo().update(syncFile);
}

public synchronized static void setForLocalDelete(Path path) {
public synchronized static void setForLocalDelete(Path path) throws IOException {
SyncFile syncFile = get(path);
syncFile.setLocalData(path);
syncFile.setState(SyncState.FOR_LOCAL_DELETE);
repo().update(syncFile);
}

public synchronized static void setForCloudDelete(File file) {
SyncFile syncFile = get(file);
syncFile.setCloudData(file);
syncFile.setState(SyncState.FOR_CLOUD_DELETE);
repo().update(syncFile);
}
Expand All @@ -195,10 +207,6 @@ public synchronized static void setConflict(File storjFile, Path localFile) thro
repo().update(syncFile);
}

public synchronized static List<SyncFile> getWithState(SyncState state) {
return repo().find(withState(state)).toList();
}

public static void main(String[] args) {
List<SyncFile> files = repo().find().toList();
for (SyncFile file : files) {
Expand Down
Loading

0 comments on commit a52e4d6

Please sign in to comment.