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; package org.kiwix.kiwixmobile.database;
import com.yahoo.squidb.data.SquidCursor; import com.yahoo.squidb.data.SquidCursor;
import com.yahoo.squidb.sql.Query; import com.yahoo.squidb.sql.Query;
import com.yahoo.squidb.sql.TableStatement;
import io.reactivex.Flowable; import org.kiwix.kiwixmobile.database.entity.BookDatabaseEntity;
import io.reactivex.processors.BehaviorProcessor; import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;
import org.kiwix.kiwixmobile.utils.files.FileUtils;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject; 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; import static org.kiwix.kiwixmobile.downloader.ChunkUtils.hasParts;
@ -38,105 +36,103 @@ import static org.kiwix.kiwixmobile.downloader.ChunkUtils.hasParts;
* Dao class for books * Dao class for books
*/ */
public class BookDao extends BaseDao { public class BookDao {
private final BehaviorProcessor<List<BookOnDisk>> booksProcessor = BehaviorProcessor.create(); private KiwixDatabase mDb;
@Inject @Inject
public BookDao(KiwixDatabase kiwixDatabase) { public BookDao(KiwixDatabase kiwixDatabase) {
super(kiwixDatabase, BookDatabaseEntity.TABLE); this.mDb = kiwixDatabase;
} }
@Override
protected void onUpdateToTable() { public void setBookDetails(Book book, SquidCursor<BookDatabaseEntity> bookCursor) {
booksProcessor.onNext(getBooks()); 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() { public void setBookDatabaseEntity(Book book, BookDatabaseEntity bookDatabaseEntity) {
return booksProcessor; 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) { public ArrayList<Book> getBooks() {
final Book book = bookonDisk.getBook(); SquidCursor<BookDatabaseEntity> bookCursor = mDb.query(
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(
BookDatabaseEntity.class, BookDatabaseEntity.class,
Query.select())) { Query.select());
ArrayList<Book> books = new ArrayList<>();
while (bookCursor.moveToNext()) { while (bookCursor.moveToNext()) {
bookDatabaseEntity.readPropertiesFromCursor(bookCursor); Book book = new Book();
final File file = new File(bookDatabaseEntity.getFilePath()); setBookDetails(book, bookCursor);
BookOnDisk book = new BookOnDisk( if (!hasParts(book.file)) {
bookDatabaseEntity.getId(), if (book.file.exists()) {
toBook(bookDatabaseEntity),
file);
if (!hasParts(file)) {
if (file.exists()) {
books.add(book); books.add(book);
} else { } else {
deleteBook(book.getDatabaseId()); mDb.deleteWhere(BookDatabaseEntity.class, BookDatabaseEntity.URL.eq(book.file));
} }
} }
} }
} bookCursor.close();
kiwixDatabase.setTransactionSuccessful();
kiwixDatabase.endTransaction();
return books; return books;
} }
public void saveBooks(Collection<BookOnDisk> books) { public ArrayList<Book> getDownloadingBooks() {
kiwixDatabase.beginTransaction(); SquidCursor<BookDatabaseEntity> bookCursor = mDb.query(
for (BookOnDisk book : books) { 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) { if (book != null) {
BookDatabaseEntity bookDatabaseEntity = new BookDatabaseEntity(); BookDatabaseEntity bookDatabaseEntity = new BookDatabaseEntity();
setBookDatabaseEntity(book, 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) { public void saveBook(Book book) {
kiwixDatabase.deleteWhere(BookDatabaseEntity.class, BookDatabaseEntity.ID.eq(id)); BookDatabaseEntity bookDatabaseEntity = new BookDatabaseEntity();
bookDatabaseEntity.setRemoteUrl(book.remoteUrl);
setBookDatabaseEntity(book, bookDatabaseEntity);
} }
private LibraryNetworkEntity.Book toBook(BookDatabaseEntity bookDatabaseEntity) { public void deleteBook(String id) {
final LibraryNetworkEntity.Book book = new LibraryNetworkEntity.Book(); mDb.deleteWhere(BookDatabaseEntity.class, BookDatabaseEntity.BOOK_ID.eq(id));
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;
} }
} }

View File

@ -21,16 +21,11 @@ package org.kiwix.kiwixmobile.database;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import com.yahoo.squidb.data.SquidDatabase; import com.yahoo.squidb.data.SquidDatabase;
import com.yahoo.squidb.data.adapter.SQLiteDatabaseWrapper; import com.yahoo.squidb.data.adapter.SQLiteDatabaseWrapper;
import com.yahoo.squidb.sql.Table; 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.ZimContentProvider;
import org.kiwix.kiwixmobile.database.entity.BookDatabaseEntity; import org.kiwix.kiwixmobile.database.entity.BookDatabaseEntity;
import org.kiwix.kiwixmobile.database.entity.Bookmarks; 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.database.entity.RecentSearch;
import org.kiwix.kiwixmobile.utils.UpdateUtils; 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; import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
@Singleton @Singleton
@ -65,7 +69,7 @@ public class KiwixDatabase extends SquidDatabase {
LibraryDatabaseEntity.TABLE, LibraryDatabaseEntity.TABLE,
RecentSearch.TABLE, RecentSearch.TABLE,
Bookmarks.TABLE, Bookmarks.TABLE,
NetworkLanguageDatabaseEntity.TABLE, NetworkLanguageDatabaseEntity.TABLE
}; };
} }
@ -116,7 +120,7 @@ public class KiwixDatabase extends SquidDatabase {
tryCreateTable(RecentSearch.TABLE); tryCreateTable(RecentSearch.TABLE);
} }
if (newVersion >= 12) { if (newVersion >= 12) {
//tryAddColumn(BookDatabaseEntity.REMOTE_URL); tryAddColumn(BookDatabaseEntity.REMOTE_URL);
} }
if (newVersion >= 13) { if (newVersion >= 13) {
tryAddColumn(BookDatabaseEntity.NAME); 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.data.SquidCursor;
import com.yahoo.squidb.sql.Query; import com.yahoo.squidb.sql.Query;
import io.reactivex.Flowable;
import io.reactivex.processors.BehaviorProcessor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.kiwix.kiwixmobile.database.entity.NetworkLanguageDatabaseEntity; import org.kiwix.kiwixmobile.database.entity.NetworkLanguageDatabaseEntity;
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.Language; import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.Language;
public class NetworkLanguageDao extends BaseDao { public class NetworkLanguageDao {
private KiwixDatabase mDb;
private final BehaviorProcessor<List<Language>> allLanguageProcessor = BehaviorProcessor.create();
@Inject @Inject
public NetworkLanguageDao(KiwixDatabase kiwikDatabase) { public NetworkLanguageDao(KiwixDatabase kiwikDatabase) {
super(kiwikDatabase, NetworkLanguageDatabaseEntity.TABLE); this.mDb = kiwikDatabase;
} }
@Override public ArrayList<Language> getFilteredLanguages() {
protected void onUpdateToTable() { SquidCursor<NetworkLanguageDatabaseEntity> languageCursor = mDb.query(
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(
NetworkLanguageDatabaseEntity.class, NetworkLanguageDatabaseEntity.class,
query)) { Query.select());
ArrayList<Language> result = new ArrayList<>();
try {
while (languageCursor.moveToNext()) { while (languageCursor.moveToNext()) {
databaseEntity.readPropertiesFromCursor(languageCursor); String languageCode = languageCursor.get(NetworkLanguageDatabaseEntity.LANGUAGE_I_S_O_3);
result.add( boolean enabled = languageCursor.get(NetworkLanguageDatabaseEntity.ENABLED);
new Language( result.add(new Language(languageCode, enabled,0));
databaseEntity.getLanguageISO3(),
databaseEntity.isEnabled(),
databaseEntity.getNumberOfOccurences()
)
);
} }
} finally {
languageCursor.close();
} }
return result; return result;
} }
public void saveFilteredLanguages(List<Language> languages) { public void saveFilteredLanguages(List<Language> languages){
if (languages.size() > 0) { mDb.deleteAll(NetworkLanguageDatabaseEntity.class);
kiwixDatabase.beginTransaction(); Collections.sort(languages, (language, t1) -> language.getLanguage().compareTo(t1.getLanguage()));
kiwixDatabase.deleteAll(NetworkLanguageDatabaseEntity.class); for (Language language : languages){
for (int i = 0; i < languages.size(); i++) { NetworkLanguageDatabaseEntity networkLanguageDatabaseEntity = new NetworkLanguageDatabaseEntity();
Language language = languages.get(i);
NetworkLanguageDatabaseEntity networkLanguageDatabaseEntity =
new NetworkLanguageDatabaseEntity();
networkLanguageDatabaseEntity.setLanguageISO3(language.getLanguageCode()); networkLanguageDatabaseEntity.setLanguageISO3(language.getLanguageCode());
networkLanguageDatabaseEntity.setIsEnabled(language.getActive()); networkLanguageDatabaseEntity.setIsEnabled(language.getActive());
networkLanguageDatabaseEntity.setNumberOfOccurences(language.getOccurencesOfLanguage()); mDb.persist(networkLanguageDatabaseEntity);
kiwixDatabase.persist(networkLanguageDatabaseEntity);
}
kiwixDatabase.setTransactionSuccessful();
kiwixDatabase.endTransaction();
} }
} }
} }

View File

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

View File

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