mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 02:39:23 +02:00
BookHash table used for faster synchronization (we do not recompute SHA-1 hash every time)
This commit is contained in:
parent
5aa57778f2
commit
8832b902bd
8 changed files with 71 additions and 13 deletions
|
@ -322,6 +322,17 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
|
|||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
public String getHash(Book book) {
|
||||
if (myInterface == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return myInterface.getHash(SerializerUtil.serialize(book));
|
||||
} catch (RemoteException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized ZLTextPosition getStoredPosition(long bookId) {
|
||||
if (myInterface == null) {
|
||||
return null;
|
||||
|
|
|
@ -52,4 +52,6 @@ interface LibraryInterface {
|
|||
void saveHighlightingStyle(in String style);
|
||||
|
||||
void rescan(in String path);
|
||||
|
||||
String getHash(in String book);
|
||||
}
|
||||
|
|
|
@ -320,6 +320,10 @@ public class LibraryService extends Service {
|
|||
public void rescan(String path) {
|
||||
myCollection.rescan(path);
|
||||
}
|
||||
|
||||
public String getHash(String book) {
|
||||
return myCollection.getHash(SerializerUtil.deserializeBook(book));
|
||||
}
|
||||
}
|
||||
|
||||
private volatile LibraryImplementation myLibrary;
|
||||
|
|
|
@ -23,8 +23,7 @@ import java.util.*;
|
|||
import java.math.BigDecimal;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
import android.database.sqlite.*;
|
||||
import android.database.SQLException;
|
||||
import android.database.Cursor;
|
||||
|
||||
|
@ -975,14 +974,29 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
|||
return progress;
|
||||
}
|
||||
|
||||
//@Override
|
||||
String getHash(long bookId, long lastModified) {
|
||||
@Override
|
||||
protected String getHash(long bookId, long lastModified) {
|
||||
final SQLiteStatement statement = get(
|
||||
"SELECT hash FROM BookHash WHERE bookId=? AND timestamp>?"
|
||||
"SELECT hash FROM BookHash WHERE book_id=? AND timestamp>?"
|
||||
);
|
||||
statement.bindLong(1, bookId);
|
||||
statement.bindLong(2, lastModified);
|
||||
return statement.simpleQueryForString();
|
||||
try {
|
||||
return statement.simpleQueryForString();
|
||||
} catch (SQLiteDoneException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setHash(long bookId, String hash) {
|
||||
final SQLiteStatement statement = get(
|
||||
"INSERT OR REPLACE INTO BookHash (book_id,timestamp,hash) VALUES (?,?,?)"
|
||||
);
|
||||
statement.bindLong(1, bookId);
|
||||
statement.bindLong(2, System.currentTimeMillis());
|
||||
statement.bindString(3, hash);
|
||||
statement.executeInsert();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -97,7 +97,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
|||
}
|
||||
myProcessed.add(book);
|
||||
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
|
||||
uploadBookToServer(book.File.getPhysicalFile());
|
||||
uploadBookToServer(book);
|
||||
}
|
||||
} finally {
|
||||
System.err.println("BYE-BYE THREAD");
|
||||
|
@ -164,19 +164,23 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
|||
}
|
||||
}
|
||||
|
||||
private void uploadBookToServer(ZLPhysicalFile file) {
|
||||
final UID uid = BookUtil.createUid(file, "SHA-1");
|
||||
if (uid == null) {
|
||||
System.err.println("Failed: SHA-1 checksum not computed");
|
||||
private void uploadBookToServer(Book book) {
|
||||
final ZLPhysicalFile file = book.File.getPhysicalFile();
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
if (myHashesFromServer.contains(uid.Id.toLowerCase())) {
|
||||
final String hash = myCollection.getHash(book);
|
||||
if (hash == null) {
|
||||
System.err.println("Failed: checksum not computed");
|
||||
return;
|
||||
}
|
||||
if (myHashesFromServer.contains(hash)) {
|
||||
System.err.println("HASH ALREADY IN THE TABLE");
|
||||
return;
|
||||
}
|
||||
final Map<String,Object> result = new HashMap<String,Object>();
|
||||
final PostRequest verificationRequest =
|
||||
new PostRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) {
|
||||
new PostRequest("books.by.hash", Collections.singletonMap("sha1", hash)) {
|
||||
@Override
|
||||
public void processResponse(Object response) {
|
||||
result.put("result", response);
|
||||
|
|
|
@ -711,4 +711,22 @@ public class BookCollection extends AbstractBookCollection {
|
|||
myDatabase.saveStyle(style);
|
||||
fireBookEvent(BookEvent.BookmarkStyleChanged, null);
|
||||
}
|
||||
|
||||
public String getHash(Book book) {
|
||||
final ZLPhysicalFile file = book.File.getPhysicalFile();
|
||||
if (file == null) {
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
hash = uid.Id.toLowerCase();
|
||||
myDatabase.setHash(book.getId(), hash);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,4 +126,7 @@ public abstract class BooksDatabase {
|
|||
|
||||
protected abstract Collection<String> loadVisitedHyperlinks(long bookId);
|
||||
protected abstract void addVisitedHyperlink(long bookId, String hyperlinkId);
|
||||
|
||||
protected abstract String getHash(long bookId, long lastModified);
|
||||
protected abstract void setHash(long bookId, String hash);
|
||||
}
|
||||
|
|
|
@ -73,6 +73,8 @@ public interface IBookCollection {
|
|||
boolean saveBook(Book book);
|
||||
void removeBook(Book book, boolean deleteFromDisk);
|
||||
|
||||
String getHash(Book book);
|
||||
|
||||
ZLTextPosition getStoredPosition(long bookId);
|
||||
void storePosition(long bookId, ZLTextPosition position);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue