Switching to lazy database for history
This commit is contained in:
parent
0823f41188
commit
ca8024d04d
@ -8,6 +8,7 @@ import android.database.sqlite.SQLiteDatabase;
|
|||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.annotation.WorkerThread;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.anthonycr.bonsai.Completable;
|
import com.anthonycr.bonsai.Completable;
|
||||||
@ -56,7 +57,6 @@ public class BookmarkDatabase extends SQLiteOpenHelper implements BookmarkModel
|
|||||||
private static final String KEY_FOLDER = "folder";
|
private static final String KEY_FOLDER = "folder";
|
||||||
private static final String KEY_POSITION = "position";
|
private static final String KEY_POSITION = "position";
|
||||||
|
|
||||||
|
|
||||||
@NonNull private final String DEFAULT_BOOKMARK_TITLE;
|
@NonNull private final String DEFAULT_BOOKMARK_TITLE;
|
||||||
|
|
||||||
@Nullable private SQLiteDatabase mDatabase;
|
@Nullable private SQLiteDatabase mDatabase;
|
||||||
@ -73,9 +73,10 @@ public class BookmarkDatabase extends SQLiteOpenHelper implements BookmarkModel
|
|||||||
*
|
*
|
||||||
* @return a non null writable database.
|
* @return a non null writable database.
|
||||||
*/
|
*/
|
||||||
|
@WorkerThread
|
||||||
@NonNull
|
@NonNull
|
||||||
private SQLiteDatabase lazyDatabase() {
|
private SQLiteDatabase lazyDatabase() {
|
||||||
if (mDatabase == null) {
|
if (mDatabase == null || !mDatabase.isOpen()) {
|
||||||
mDatabase = getWritableDatabase();
|
mDatabase = getWritableDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,15 +68,6 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
onCreate(db);
|
onCreate(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized void close() {
|
|
||||||
if (mDatabase != null) {
|
|
||||||
mDatabase.close();
|
|
||||||
mDatabase = null;
|
|
||||||
}
|
|
||||||
super.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private static HistoryItem fromCursor(@NonNull Cursor cursor) {
|
private static HistoryItem fromCursor(@NonNull Cursor cursor) {
|
||||||
HistoryItem historyItem = new HistoryItem();
|
HistoryItem historyItem = new HistoryItem();
|
||||||
@ -89,7 +80,7 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
@NonNull
|
@NonNull
|
||||||
private SQLiteDatabase openIfNecessary() {
|
private SQLiteDatabase lazyDatabase() {
|
||||||
if (mDatabase == null || !mDatabase.isOpen()) {
|
if (mDatabase == null || !mDatabase.isOpen()) {
|
||||||
mDatabase = this.getWritableDatabase();
|
mDatabase = this.getWritableDatabase();
|
||||||
}
|
}
|
||||||
@ -98,30 +89,26 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
synchronized void deleteHistory() {
|
synchronized void deleteHistory() {
|
||||||
mDatabase = openIfNecessary();
|
lazyDatabase().delete(TABLE_HISTORY, null, null);
|
||||||
mDatabase.delete(TABLE_HISTORY, null, null);
|
lazyDatabase().close();
|
||||||
mDatabase.close();
|
|
||||||
mDatabase = this.getWritableDatabase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
synchronized void deleteHistoryItem(@NonNull String url) {
|
synchronized void deleteHistoryItem(@NonNull String url) {
|
||||||
mDatabase = openIfNecessary();
|
lazyDatabase().delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url});
|
||||||
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) {
|
synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) {
|
||||||
mDatabase = openIfNecessary();
|
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(KEY_TITLE, title == null ? "" : title);
|
values.put(KEY_TITLE, title == null ? "" : title);
|
||||||
values.put(KEY_TIME_VISITED, System.currentTimeMillis());
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis());
|
||||||
|
|
||||||
Cursor cursor = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL},
|
Cursor cursor = lazyDatabase().query(false, TABLE_HISTORY, new String[]{KEY_URL},
|
||||||
KEY_URL + " = ?", new String[]{url}, null, null, null, "1");
|
KEY_URL + " = ?", new String[]{url}, null, null, null, "1");
|
||||||
|
|
||||||
if (cursor.getCount() > 0) {
|
if (cursor.getCount() > 0) {
|
||||||
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url});
|
lazyDatabase().update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url});
|
||||||
} else {
|
} else {
|
||||||
addHistoryItem(new HistoryItem(url, title == null ? "" : title));
|
addHistoryItem(new HistoryItem(url, title == null ? "" : title));
|
||||||
}
|
}
|
||||||
@ -131,19 +118,17 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
private synchronized void addHistoryItem(@NonNull HistoryItem item) {
|
private synchronized void addHistoryItem(@NonNull HistoryItem item) {
|
||||||
mDatabase = openIfNecessary();
|
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(KEY_URL, item.getUrl());
|
values.put(KEY_URL, item.getUrl());
|
||||||
values.put(KEY_TITLE, item.getTitle());
|
values.put(KEY_TITLE, item.getTitle());
|
||||||
values.put(KEY_TIME_VISITED, System.currentTimeMillis());
|
values.put(KEY_TIME_VISITED, System.currentTimeMillis());
|
||||||
mDatabase.insert(TABLE_HISTORY, null, values);
|
lazyDatabase().insert(TABLE_HISTORY, null, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
@Nullable
|
@Nullable
|
||||||
synchronized String getHistoryItem(@NonNull String url) {
|
synchronized String getHistoryItem(@NonNull String url) {
|
||||||
mDatabase = openIfNecessary();
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE},
|
||||||
Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE},
|
|
||||||
KEY_URL + " = ?", new String[]{url}, null, null, null, "1");
|
KEY_URL + " = ?", new String[]{url}, null, null, null, "1");
|
||||||
String m = null;
|
String m = null;
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
@ -158,7 +143,6 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
@WorkerThread
|
@WorkerThread
|
||||||
@NonNull
|
@NonNull
|
||||||
synchronized List<HistoryItem> findItemsContaining(@Nullable String search) {
|
synchronized List<HistoryItem> findItemsContaining(@Nullable String search) {
|
||||||
mDatabase = openIfNecessary();
|
|
||||||
List<HistoryItem> itemList = new ArrayList<>(5);
|
List<HistoryItem> itemList = new ArrayList<>(5);
|
||||||
if (search == null) {
|
if (search == null) {
|
||||||
return itemList;
|
return itemList;
|
||||||
@ -166,7 +150,7 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
search = '%' + search + '%';
|
search = '%' + search + '%';
|
||||||
|
|
||||||
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, KEY_TITLE + " LIKE ? OR " + KEY_URL + " LIKE ?",
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, null, KEY_TITLE + " LIKE ? OR " + KEY_URL + " LIKE ?",
|
||||||
new String[]{search, search}, null, null, KEY_TIME_VISITED + " DESC", "5");
|
new String[]{search, search}, null, null, KEY_TIME_VISITED + " DESC", "5");
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
@ -181,9 +165,8 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
@WorkerThread
|
@WorkerThread
|
||||||
@NonNull
|
@NonNull
|
||||||
synchronized List<HistoryItem> getLastHundredItems() {
|
synchronized List<HistoryItem> getLastHundredItems() {
|
||||||
mDatabase = openIfNecessary();
|
|
||||||
List<HistoryItem> itemList = new ArrayList<>(100);
|
List<HistoryItem> itemList = new ArrayList<>(100);
|
||||||
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC", "100");
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC", "100");
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
itemList.add(fromCursor(cursor));
|
itemList.add(fromCursor(cursor));
|
||||||
@ -197,10 +180,9 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
|||||||
@WorkerThread
|
@WorkerThread
|
||||||
@NonNull
|
@NonNull
|
||||||
synchronized List<HistoryItem> getAllHistoryItems() {
|
synchronized List<HistoryItem> getAllHistoryItems() {
|
||||||
mDatabase = openIfNecessary();
|
|
||||||
List<HistoryItem> itemList = new ArrayList<>();
|
List<HistoryItem> itemList = new ArrayList<>();
|
||||||
|
|
||||||
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC");
|
Cursor cursor = lazyDatabase().query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC");
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
itemList.add(fromCursor(cursor));
|
itemList.add(fromCursor(cursor));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user