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();
|
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) {
|
public synchronized ZLTextPosition getStoredPosition(long bookId) {
|
||||||
if (myInterface == null) {
|
if (myInterface == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -52,4 +52,6 @@ interface LibraryInterface {
|
||||||
void saveHighlightingStyle(in String style);
|
void saveHighlightingStyle(in String style);
|
||||||
|
|
||||||
void rescan(in String path);
|
void rescan(in String path);
|
||||||
|
|
||||||
|
String getHash(in String book);
|
||||||
}
|
}
|
||||||
|
|
|
@ -320,6 +320,10 @@ public class LibraryService extends Service {
|
||||||
public void rescan(String path) {
|
public void rescan(String path) {
|
||||||
myCollection.rescan(path);
|
myCollection.rescan(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getHash(String book) {
|
||||||
|
return myCollection.getHash(SerializerUtil.deserializeBook(book));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private volatile LibraryImplementation myLibrary;
|
private volatile LibraryImplementation myLibrary;
|
||||||
|
|
|
@ -23,8 +23,7 @@ import java.util.*;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.*;
|
||||||
import android.database.sqlite.SQLiteStatement;
|
|
||||||
import android.database.SQLException;
|
import android.database.SQLException;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
|
||||||
|
@ -975,14 +974,29 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
return progress;
|
return progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Override
|
@Override
|
||||||
String getHash(long bookId, long lastModified) {
|
protected String getHash(long bookId, long lastModified) {
|
||||||
final SQLiteStatement statement = get(
|
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(1, bookId);
|
||||||
statement.bindLong(2, lastModified);
|
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
|
@Override
|
||||||
|
|
|
@ -97,7 +97,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
}
|
}
|
||||||
myProcessed.add(book);
|
myProcessed.add(book);
|
||||||
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
|
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
|
||||||
uploadBookToServer(book.File.getPhysicalFile());
|
uploadBookToServer(book);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
System.err.println("BYE-BYE THREAD");
|
System.err.println("BYE-BYE THREAD");
|
||||||
|
@ -164,19 +164,23 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void uploadBookToServer(ZLPhysicalFile file) {
|
private void uploadBookToServer(Book book) {
|
||||||
final UID uid = BookUtil.createUid(file, "SHA-1");
|
final ZLPhysicalFile file = book.File.getPhysicalFile();
|
||||||
if (uid == null) {
|
if (file == null) {
|
||||||
System.err.println("Failed: SHA-1 checksum not computed");
|
|
||||||
return;
|
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");
|
System.err.println("HASH ALREADY IN THE TABLE");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Map<String,Object> result = new HashMap<String,Object>();
|
final Map<String,Object> result = new HashMap<String,Object>();
|
||||||
final PostRequest verificationRequest =
|
final PostRequest verificationRequest =
|
||||||
new PostRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) {
|
new PostRequest("books.by.hash", Collections.singletonMap("sha1", hash)) {
|
||||||
@Override
|
@Override
|
||||||
public void processResponse(Object response) {
|
public void processResponse(Object response) {
|
||||||
result.put("result", response);
|
result.put("result", response);
|
||||||
|
|
|
@ -711,4 +711,22 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
myDatabase.saveStyle(style);
|
myDatabase.saveStyle(style);
|
||||||
fireBookEvent(BookEvent.BookmarkStyleChanged, null);
|
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 Collection<String> loadVisitedHyperlinks(long bookId);
|
||||||
protected abstract void addVisitedHyperlink(long bookId, String hyperlinkId);
|
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);
|
boolean saveBook(Book book);
|
||||||
void removeBook(Book book, boolean deleteFromDisk);
|
void removeBook(Book book, boolean deleteFromDisk);
|
||||||
|
|
||||||
|
String getHash(Book book);
|
||||||
|
|
||||||
ZLTextPosition getStoredPosition(long bookId);
|
ZLTextPosition getStoredPosition(long bookId);
|
||||||
void storePosition(long bookId, ZLTextPosition position);
|
void storePosition(long bookId, ZLTextPosition position);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue