revert to original dao/schemas prior to writing migration

This commit is contained in:
Sean Mac Gillicuddy 2019-05-17 11:23:17 +01:00
parent a892668acb
commit fd31d39158
6 changed files with 126 additions and 200 deletions

View File

@ -1,53 +0,0 @@
package org.kiwix.kiwixmobile.database;
import com.yahoo.squidb.data.SimpleDataChangedNotifier;
import com.yahoo.squidb.sql.Table;
import io.reactivex.Flowable;
import io.reactivex.processors.PublishProcessor;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
import kotlin.Unit;
/*
* Kiwix Android
* Copyright (C) 2018 Kiwix <android.kiwix.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseDao {
private final PublishProcessor<Unit> updates = PublishProcessor.create();
protected final KiwixDatabase kiwixDatabase;
public BaseDao(KiwixDatabase kiwixDatabase, Table table) {
this.kiwixDatabase = kiwixDatabase;
kiwixDatabase.registerDataChangedNotifier(
new SimpleDataChangedNotifier(table) {
@Override
protected void onDataChanged() {
updates.onNext(Unit.INSTANCE);
}
});
updates
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(unit -> {
onUpdateToTable();
}
, Throwable::printStackTrace
);
Flowable.timer(100, TimeUnit.MILLISECONDS).subscribe(aLong -> updates.onNext(Unit.INSTANCE));
}
protected abstract void onUpdateToTable();
}

View File

@ -17,20 +17,18 @@
*/
package org.kiwix.kiwixmobile.database;
import com.yahoo.squidb.data.SquidCursor;
import com.yahoo.squidb.sql.Query;
import com.yahoo.squidb.sql.TableStatement;
import io.reactivex.Flowable;
import io.reactivex.processors.BehaviorProcessor;
import org.kiwix.kiwixmobile.database.entity.BookDatabaseEntity;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;
import org.kiwix.kiwixmobile.utils.files.FileUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
import org.kiwix.kiwixmobile.database.entity.BookDatabaseEntity;
import org.kiwix.kiwixmobile.downloader.model.BookOnDisk;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;
import static org.kiwix.kiwixmobile.downloader.ChunkUtils.hasParts;
@ -38,105 +36,103 @@ import static org.kiwix.kiwixmobile.downloader.ChunkUtils.hasParts;
* Dao class for books
*/
public class BookDao extends BaseDao {
private final BehaviorProcessor<List<BookOnDisk>> booksProcessor = BehaviorProcessor.create();
public class BookDao {
private KiwixDatabase mDb;
@Inject
public BookDao(KiwixDatabase kiwixDatabase) {
super(kiwixDatabase, BookDatabaseEntity.TABLE);
this.mDb = kiwixDatabase;
}
@Override
protected void onUpdateToTable() {
booksProcessor.onNext(getBooks());
public void setBookDetails(Book book, SquidCursor<BookDatabaseEntity> bookCursor) {
book.id = bookCursor.get(BookDatabaseEntity.BOOK_ID);
book.title = bookCursor.get(BookDatabaseEntity.TITLE);
book.description = bookCursor.get(BookDatabaseEntity.DESCRIPTION);
book.language = bookCursor.get(BookDatabaseEntity.LANGUAGE);
book.creator = bookCursor.get(BookDatabaseEntity.BOOK_CREATOR);
book.publisher = bookCursor.get(BookDatabaseEntity.PUBLISHER);
book.date = bookCursor.get(BookDatabaseEntity.DATE);
book.file = new File(bookCursor.get(BookDatabaseEntity.URL));
book.articleCount = bookCursor.get(BookDatabaseEntity.ARTICLE_COUNT);
book.mediaCount = bookCursor.get(BookDatabaseEntity.MEDIA_COUNT);
book.size = bookCursor.get(BookDatabaseEntity.SIZE);
book.favicon = bookCursor.get(BookDatabaseEntity.FAVICON);
book.bookName = bookCursor.get(BookDatabaseEntity.NAME);
}
public Flowable<List<BookOnDisk>> books() {
return booksProcessor;
public void setBookDatabaseEntity(Book book, BookDatabaseEntity bookDatabaseEntity) {
bookDatabaseEntity.setBookId(book.getId());
bookDatabaseEntity.setTitle(book.getTitle());
bookDatabaseEntity.setDescription(book.getDescription());
bookDatabaseEntity.setLanguage(book.getLanguage());
bookDatabaseEntity.setBookCreator(book.getCreator());
bookDatabaseEntity.setPublisher(book.getPublisher());
bookDatabaseEntity.setDate(book.getDate());
bookDatabaseEntity.setUrl(book.file.getPath());
bookDatabaseEntity.setArticleCount(book.getArticleCount());
bookDatabaseEntity.setMediaCount(book.getMediaCount());
bookDatabaseEntity.setSize(book.getSize());
bookDatabaseEntity.setFavicon(book.getFavicon());
bookDatabaseEntity.setName(book.getName());
String filePath = book.file.getPath();
mDb.deleteWhere(BookDatabaseEntity.class, BookDatabaseEntity.URL.eq(filePath));
mDb.persist(bookDatabaseEntity);
}
public void setBookDatabaseEntity(BookOnDisk bookonDisk, BookDatabaseEntity bookDatabaseEntity) {
final Book book = bookonDisk.getBook();
bookDatabaseEntity
.setFilePath(bookonDisk.getFile().getPath())
.setBookId(book.getId())
.setTitle(book.getTitle())
.setDescription(book.getDescription())
.setLanguage(book.getLanguage())
.setBookCreator(book.getCreator())
.setPublisher(book.getPublisher())
.setDate(book.getDate())
.setUrl(book.getUrl())
.setArticleCount(book.getArticleCount())
.setMediaCount(book.getMediaCount())
.setSize(book.getSize())
.setName(book.getName())
.setFavicon(book.getFavicon());
}
public List<BookOnDisk> getBooks() {
kiwixDatabase.beginTransaction();
ArrayList<BookOnDisk> books = new ArrayList<>();
final BookDatabaseEntity bookDatabaseEntity = new BookDatabaseEntity();
try(SquidCursor<BookDatabaseEntity> bookCursor = kiwixDatabase.query(
public ArrayList<Book> getBooks() {
SquidCursor<BookDatabaseEntity> bookCursor = mDb.query(
BookDatabaseEntity.class,
Query.select())) {
while (bookCursor.moveToNext()) {
bookDatabaseEntity.readPropertiesFromCursor(bookCursor);
final File file = new File(bookDatabaseEntity.getFilePath());
BookOnDisk book = new BookOnDisk(
bookDatabaseEntity.getId(),
toBook(bookDatabaseEntity),
file);
if (!hasParts(file)) {
if (file.exists()) {
books.add(book);
} else {
deleteBook(book.getDatabaseId());
}
Query.select());
ArrayList<Book> books = new ArrayList<>();
while (bookCursor.moveToNext()) {
Book book = new Book();
setBookDetails(book, bookCursor);
if (!hasParts(book.file)) {
if (book.file.exists()) {
books.add(book);
} else {
mDb.deleteWhere(BookDatabaseEntity.class, BookDatabaseEntity.URL.eq(book.file));
}
}
}
kiwixDatabase.setTransactionSuccessful();
kiwixDatabase.endTransaction();
bookCursor.close();
return books;
}
public void saveBooks(Collection<BookOnDisk> books) {
kiwixDatabase.beginTransaction();
for (BookOnDisk book : books) {
public ArrayList<Book> getDownloadingBooks() {
SquidCursor<BookDatabaseEntity> bookCursor = mDb.query(
BookDatabaseEntity.class,
Query.select());
ArrayList<Book> books = new ArrayList<>();
while (bookCursor.moveToNext()) {
Book book = new Book();
setBookDetails(book, bookCursor);
book.remoteUrl = bookCursor.get(BookDatabaseEntity.REMOTE_URL);
if (hasParts(book.file)) {
books.add(book);
}
}
bookCursor.close();
return books;
}
public void saveBooks(ArrayList<Book> books) {
for (Book book : books) {
if (book != null) {
BookDatabaseEntity bookDatabaseEntity = new BookDatabaseEntity();
setBookDatabaseEntity(book, bookDatabaseEntity);
kiwixDatabase.deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.BOOK_ID.eq(bookDatabaseEntity.getBookId()));
kiwixDatabase.persistWithOnConflict(bookDatabaseEntity,
TableStatement.ConflictAlgorithm.REPLACE);
}
}
kiwixDatabase.setTransactionSuccessful();
kiwixDatabase.endTransaction();
}
public void deleteBook(Long id) {
kiwixDatabase.deleteWhere(BookDatabaseEntity.class, BookDatabaseEntity.ID.eq(id));
public void saveBook(Book book) {
BookDatabaseEntity bookDatabaseEntity = new BookDatabaseEntity();
bookDatabaseEntity.setRemoteUrl(book.remoteUrl);
setBookDatabaseEntity(book, bookDatabaseEntity);
}
private LibraryNetworkEntity.Book toBook(BookDatabaseEntity bookDatabaseEntity) {
final LibraryNetworkEntity.Book book = new LibraryNetworkEntity.Book();
book.id = bookDatabaseEntity.getBookId();
book.title = bookDatabaseEntity.getTitle();
book.description = bookDatabaseEntity.getDescription();
book.language = bookDatabaseEntity.getLanguage();
book.creator = bookDatabaseEntity.getBookCreator();
book.publisher = bookDatabaseEntity.getPublisher();
book.date = bookDatabaseEntity.getDate();
book.url = bookDatabaseEntity.getUrl();
book.articleCount = bookDatabaseEntity.getArticleCount();
book.mediaCount = bookDatabaseEntity.getMediaCount();
book.size = bookDatabaseEntity.getSize();
book.bookName = bookDatabaseEntity.getName();
book.favicon = bookDatabaseEntity.getFavicon();
return book;
public void deleteBook(String id) {
mDb.deleteWhere(BookDatabaseEntity.class, BookDatabaseEntity.BOOK_ID.eq(id));
}
}

View File

@ -21,16 +21,11 @@ package org.kiwix.kiwixmobile.database;
import android.content.Context;
import android.util.Log;
import com.yahoo.squidb.data.SquidDatabase;
import com.yahoo.squidb.data.adapter.SQLiteDatabaseWrapper;
import com.yahoo.squidb.sql.Table;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.kiwix.kiwixmobile.ZimContentProvider;
import org.kiwix.kiwixmobile.database.entity.BookDatabaseEntity;
import org.kiwix.kiwixmobile.database.entity.Bookmarks;
@ -39,6 +34,15 @@ import org.kiwix.kiwixmobile.database.entity.NetworkLanguageDatabaseEntity;
import org.kiwix.kiwixmobile.database.entity.RecentSearch;
import org.kiwix.kiwixmobile.utils.UpdateUtils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.inject.Inject;
import javax.inject.Singleton;
import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
@Singleton
@ -65,7 +69,7 @@ public class KiwixDatabase extends SquidDatabase {
LibraryDatabaseEntity.TABLE,
RecentSearch.TABLE,
Bookmarks.TABLE,
NetworkLanguageDatabaseEntity.TABLE,
NetworkLanguageDatabaseEntity.TABLE
};
}
@ -116,7 +120,7 @@ public class KiwixDatabase extends SquidDatabase {
tryCreateTable(RecentSearch.TABLE);
}
if (newVersion >= 12) {
//tryAddColumn(BookDatabaseEntity.REMOTE_URL);
tryAddColumn(BookDatabaseEntity.REMOTE_URL);
}
if (newVersion >= 13) {
tryAddColumn(BookDatabaseEntity.NAME);
@ -173,4 +177,3 @@ public class KiwixDatabase extends SquidDatabase {
}
}

View File

@ -21,73 +21,46 @@ package org.kiwix.kiwixmobile.database;
import com.yahoo.squidb.data.SquidCursor;
import com.yahoo.squidb.sql.Query;
import io.reactivex.Flowable;
import io.reactivex.processors.BehaviorProcessor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.kiwix.kiwixmobile.database.entity.NetworkLanguageDatabaseEntity;
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.Language;
public class NetworkLanguageDao extends BaseDao {
private final BehaviorProcessor<List<Language>> allLanguageProcessor = BehaviorProcessor.create();
public class NetworkLanguageDao {
private KiwixDatabase mDb;
@Inject
public NetworkLanguageDao(KiwixDatabase kiwikDatabase) {
super(kiwikDatabase, NetworkLanguageDatabaseEntity.TABLE);
this.mDb = kiwikDatabase;
}
@Override
protected void onUpdateToTable() {
allLanguageProcessor.onNext(fetchAllLanguages());
}
public Flowable<List<Language>> allLanguages() {
return allLanguageProcessor;
}
public List<Language> fetchAllLanguages() {
return fetchWith(Query.select());
}
@NotNull private List<Language> fetchWith(Query query) {
ArrayList<Language> result = new ArrayList<>();
final NetworkLanguageDatabaseEntity databaseEntity =
new NetworkLanguageDatabaseEntity();
try (SquidCursor<NetworkLanguageDatabaseEntity> languageCursor = kiwixDatabase.query(
public ArrayList<Language> getFilteredLanguages() {
SquidCursor<NetworkLanguageDatabaseEntity> languageCursor = mDb.query(
NetworkLanguageDatabaseEntity.class,
query)) {
Query.select());
ArrayList<Language> result = new ArrayList<>();
try {
while (languageCursor.moveToNext()) {
databaseEntity.readPropertiesFromCursor(languageCursor);
result.add(
new Language(
databaseEntity.getLanguageISO3(),
databaseEntity.isEnabled(),
databaseEntity.getNumberOfOccurences()
)
);
String languageCode = languageCursor.get(NetworkLanguageDatabaseEntity.LANGUAGE_I_S_O_3);
boolean enabled = languageCursor.get(NetworkLanguageDatabaseEntity.ENABLED);
result.add(new Language(languageCode, enabled,0));
}
} finally {
languageCursor.close();
}
return result;
}
public void saveFilteredLanguages(List<Language> languages) {
if (languages.size() > 0) {
kiwixDatabase.beginTransaction();
kiwixDatabase.deleteAll(NetworkLanguageDatabaseEntity.class);
for (int i = 0; i < languages.size(); i++) {
Language language = languages.get(i);
NetworkLanguageDatabaseEntity networkLanguageDatabaseEntity =
new NetworkLanguageDatabaseEntity();
networkLanguageDatabaseEntity.setLanguageISO3(language.getLanguageCode());
networkLanguageDatabaseEntity.setIsEnabled(language.getActive());
networkLanguageDatabaseEntity.setNumberOfOccurences(language.getOccurencesOfLanguage());
kiwixDatabase.persist(networkLanguageDatabaseEntity);
}
kiwixDatabase.setTransactionSuccessful();
kiwixDatabase.endTransaction();
public void saveFilteredLanguages(List<Language> languages){
mDb.deleteAll(NetworkLanguageDatabaseEntity.class);
Collections.sort(languages, (language, t1) -> language.getLanguage().compareTo(t1.getLanguage()));
for (Language language : languages){
NetworkLanguageDatabaseEntity networkLanguageDatabaseEntity = new NetworkLanguageDatabaseEntity();
networkLanguageDatabaseEntity.setLanguageISO3(language.getLanguageCode());
networkLanguageDatabaseEntity.setIsEnabled(language.getActive());
mDb.persist(networkLanguageDatabaseEntity);
}
}
}

View File

@ -24,8 +24,6 @@ import com.yahoo.squidb.annotations.TableModelSpec;
@TableModelSpec(className = "BookDatabaseEntity", tableName = "book")
public class BookDataSource {
public String filePath;
public String bookId;
public String title;
@ -41,6 +39,9 @@ public class BookDataSource {
public String date;
public String url;
public String remoteUrl;
public String articleCount;
public String mediaCount;
@ -51,5 +52,6 @@ public class BookDataSource {
public String name;
public boolean downloaded;
}

View File

@ -18,6 +18,7 @@
*/
package org.kiwix.kiwixmobile.library.entity;
import java.io.File;
import java.io.Serializable;
import java.util.LinkedList;
import org.simpleframework.xml.Attribute;
@ -89,6 +90,10 @@ public class LibraryNetworkEntity {
public String tags;
public int searchMatches = 0;
@Deprecated
public File file;
@Deprecated
public String remoteUrl;
public String getId() {
return this.id;