diff --git a/src/org/geometerplus/android/fbreader/synchroniser/SynchroniserService.java b/src/org/geometerplus/android/fbreader/synchroniser/SynchroniserService.java index 1e18f80fe..13bea8f7b 100644 --- a/src/org/geometerplus/android/fbreader/synchroniser/SynchroniserService.java +++ b/src/org/geometerplus/android/fbreader/synchroniser/SynchroniserService.java @@ -42,7 +42,8 @@ public class SynchroniserService extends Service implements IBookCollection.List private final List myQueue = Collections.synchronizedList(new LinkedList()); private final Set myProcessed = new HashSet(); - private final Set myHashesFromServer = new HashSet(); + private final Set myActualHashesFromServer = new HashSet(); + private final Set myDeletedHashesFromServer = new HashSet(); @Override public IBinder onBind(Intent intent) { @@ -66,21 +67,24 @@ public class SynchroniserService extends Service implements IBookCollection.List public void run() { try { System.err.println("HELLO THREAD"); - myHashesFromServer.clear(); + myActualHashesFromServer.clear(); + myDeletedHashesFromServer.clear(); try { myNetworkContext.perform(new PostRequest("all.hashes", null) { @Override public void processResponse(Object response) { - myHashesFromServer.addAll((List)response); + final Map> map = (Map>)response; + myActualHashesFromServer.addAll(map.get("actual")); + myDeletedHashesFromServer.addAll(map.get("deleted")); } }); - System.err.println("RECEIVED: " + myHashesFromServer.size() + " HASHES"); + System.err.println(String.format("RECEIVED: %s/%s HASHES", myActualHashesFromServer.size(), myDeletedHashesFromServer.size())); } catch (Exception e) { e.printStackTrace(); System.err.println("DO NOT SYNCHRONIZE: ALL HASHES REQUEST FAILED"); return; } - System.err.println("START SYNCRONIZATION"); + System.err.println("START SYNCRONIZATION " + new Date()); for (BookQuery q = new BookQuery(new Filter.Empty(), 20);; q = q.next()) { final List books = myCollection.books(q); if (books.isEmpty()) { @@ -100,7 +104,7 @@ public class SynchroniserService extends Service implements IBookCollection.List uploadBookToServer(book); } } finally { - System.err.println("BYE-BYE THREAD"); + System.err.println("BYE-BYE THREAD " + new Date()); ourSynchronizationThread = null; } } @@ -157,7 +161,7 @@ public class SynchroniserService extends Service implements IBookCollection.List System.err.println("UPLOAD FAILURE: " + error); } else if (id != null && hashes != null) { System.err.println("UPLOADED SUCCESSFULLY: " + id); - myHashesFromServer.addAll(hashes); + myActualHashesFromServer.addAll(hashes); } else { System.err.println("UNEXPECED RESPONSE: " + response); } @@ -174,16 +178,20 @@ public class SynchroniserService extends Service implements IBookCollection.List System.err.println("Failed: checksum not computed"); return; } - if (myHashesFromServer.contains(hash)) { - System.err.println("HASH ALREADY IN THE TABLE"); + if (myActualHashesFromServer.contains(hash)) { + System.err.println("BOOK ALREADY UPLOADED"); + return; + } + if (myDeletedHashesFromServer.contains(hash)) { + System.err.println("BOOK ALREADY UPLOADED AND DELETED"); return; } final Map result = new HashMap(); final PostRequest verificationRequest = - new PostRequest("books.by.hash", Collections.singletonMap("sha1", hash)) { + new PostRequest("book.status.by.hash", Collections.singletonMap("sha1", hash)) { @Override public void processResponse(Object response) { - result.put("result", response); + result.putAll((Map)response); } }; try { @@ -192,10 +200,9 @@ public class SynchroniserService extends Service implements IBookCollection.List e.printStackTrace(); } final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken"); - final Object response = result.get("result"); try { - final List> responseList = (List>)response; - if (responseList.isEmpty()) { + final String status = (String)result.get("status"); + if ("not found".equals(status)) { try { final UploadRequest uploadRequest = new UploadRequest(file.javaFile()); uploadRequest.addHeader("Referer", verificationRequest.getURL()); @@ -205,13 +212,17 @@ public class SynchroniserService extends Service implements IBookCollection.List e.printStackTrace(); } } else { - for (Map bookInfo : responseList) { - System.err.println("BOOK ALREADY UPLOADED: " + bookInfo.get("id")); - myHashesFromServer.addAll((List)bookInfo.get("hashes")); + final List hashes = (List)result.get("hashes"); + if ("found".equals(status)) { + System.err.println("BOOK ALREADY UPLOADED"); + myActualHashesFromServer.addAll(hashes); + } else /* if ("deleted".equals(status)) */ { + System.err.println("BOOK ALREADY UPLOADED AND DELETED"); + myDeletedHashesFromServer.addAll(hashes); } } } catch (Exception e) { - System.err.println("UNEXPECTED RESPONSE: " + response); + System.err.println("UNEXPECTED RESPONSE: " + result); } } diff --git a/src/org/geometerplus/fbreader/book/BookCollection.java b/src/org/geometerplus/fbreader/book/BookCollection.java index 0a44ef67c..3b62190fd 100644 --- a/src/org/geometerplus/fbreader/book/BookCollection.java +++ b/src/org/geometerplus/fbreader/book/BookCollection.java @@ -728,7 +728,6 @@ public class BookCollection extends AbstractBookCollection { return null; } String hash = myDatabase.getHash(book.getId(), file.javaFile().lastModified()); - System.err.println("HASH FOR " + book + " FROM DB = " + hash); if (hash == null) { final UID uid = BookUtil.createUid(file, "SHA-1"); if (uid == null) {