FTS single instance

pull/172/head
M66B 5 years ago
parent b7f1be88d0
commit 04716fc06f

@ -189,10 +189,8 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
boolean fts = prefs.getBoolean("fts", false);
if (fts && seen == null && flagged == null && snoozed == null && encrypted == null) {
if (state.ids == null) {
FtsDbHelper ftsDb = new FtsDbHelper(context);
try (SQLiteDatabase sdb = ftsDb.getReadableDatabase()) {
state.ids = ftsDb.match(sdb, folder, query);
}
SQLiteDatabase sdb = FtsDbHelper.getInstance(context);
state.ids = FtsDbHelper.match(sdb, folder, query);
}
try {

@ -35,13 +35,21 @@ import io.requery.android.database.sqlite.SQLiteOpenHelper;
// https://www.sqlite.org/fts5.html
public class FtsDbHelper extends SQLiteOpenHelper {
private static FtsDbHelper instance = null;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "fts.db";
FtsDbHelper(Context context) {
private FtsDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
static SQLiteDatabase getInstance(Context context) {
if (instance == null)
instance = new FtsDbHelper(context);
return instance.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("FTS create");
@ -54,8 +62,8 @@ public class FtsDbHelper extends SQLiteOpenHelper {
// Do nothing
}
void insert(SQLiteDatabase db, EntityMessage message, String text) {
Log.i("FTS insert id=" + message.id + " subject=" + message.subject + " text=" + text);
static void insert(SQLiteDatabase db, EntityMessage message, String text) {
Log.i("FTS insert id=" + message.id);
List<Address> address = new ArrayList<>();
if (message.from != null)
address.addAll(Arrays.asList(message.from));
@ -64,34 +72,24 @@ public class FtsDbHelper extends SQLiteOpenHelper {
if (message.cc != null)
address.addAll(Arrays.asList(message.cc));
try {
db.beginTransaction();
delete(db, message.id);
ContentValues cv = new ContentValues();
cv.put("rowid", message.id);
cv.put("folder", message.folder);
cv.put("time", message.received);
cv.put("address", MessageHelper.formatAddresses(address.toArray(new Address[0]), true, false));
cv.put("subject", message.subject == null ? "" : message.subject);
cv.put("keyword", TextUtils.join(", ", message.keywords));
cv.put("text", text);
db.insert("message", SQLiteDatabase.CONFLICT_FAIL, cv);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(ex);
} finally {
db.endTransaction();
}
delete(db, message.id);
ContentValues cv = new ContentValues();
cv.put("rowid", message.id);
cv.put("folder", message.folder);
cv.put("time", message.received);
cv.put("address", MessageHelper.formatAddresses(address.toArray(new Address[0]), true, false));
cv.put("subject", message.subject == null ? "" : message.subject);
cv.put("keyword", TextUtils.join(", ", message.keywords));
cv.put("text", text);
db.insert("message", SQLiteDatabase.CONFLICT_FAIL, cv);
}
void delete(SQLiteDatabase db, long id) {
static void delete(SQLiteDatabase db, long id) {
db.delete("message", "rowid = ?", new Object[]{id});
}
List<Long> match(SQLiteDatabase db, Long folder, String search) {
static List<Long> match(SQLiteDatabase db, Long folder, String search) {
Log.i("FTS folder=" + folder + " search=" + search);
List<Long> result = new ArrayList<>();
try (Cursor cursor = db.query(
@ -106,7 +104,7 @@ public class FtsDbHelper extends SQLiteOpenHelper {
return result;
}
Cursor getIds(SQLiteDatabase db) {
static Cursor getIds(SQLiteDatabase db) {
return db.query(
"message", new String[]{"rowid"},
null, null,

@ -195,17 +195,15 @@ public class WorkerCleanup extends Worker {
Log.i("Cleanup FTS");
int fts = 0;
FtsDbHelper ftsDb = new FtsDbHelper(context);
try (SQLiteDatabase sdb = ftsDb.getWritableDatabase()) {
try (Cursor cursor = ftsDb.getIds(sdb)) {
while (cursor.moveToNext()) {
long rowid = cursor.getLong(0);
EntityMessage message = db.message().getMessage(rowid);
if (message == null) {
Log.i("Deleting rowid" + rowid);
ftsDb.delete(sdb, rowid);
fts++;
}
SQLiteDatabase sdb = FtsDbHelper.getInstance(context);
try (Cursor cursor = FtsDbHelper.getIds(sdb)) {
while (cursor.moveToNext()) {
long rowid = cursor.getLong(0);
EntityMessage message = db.message().getMessage(rowid);
if (message == null) {
Log.i("Deleting rowid" + rowid);
FtsDbHelper.delete(sdb, rowid);
fts++;
}
}
}

@ -55,24 +55,23 @@ public class WorkerFts extends Worker {
Log.i("FTS index");
int indexed = 0;
FtsDbHelper ftsDb = new FtsDbHelper(getApplicationContext());
try (SQLiteDatabase sdb = ftsDb.getWritableDatabase()) {
DB db = DB.getInstance(getApplicationContext());
try (Cursor cursor = db.message().getMessageFts()) {
while (cursor.moveToNext()) {
EntityMessage message = db.message().getMessage(cursor.getLong(0));
if (message != null)
try {
Log.i("FTS index=" + message.id);
File file = message.getFile(getApplicationContext());
String html = Helper.readText(file);
ftsDb.insert(sdb, message, HtmlHelper.getText(html));
db.message().setMessageFts(message.id, true);
indexed++;
} catch (Throwable ex) {
Log.e(ex);
}
}
DB db = DB.getInstance(getApplicationContext());
SQLiteDatabase sdb = FtsDbHelper.getInstance(getApplicationContext());
try (Cursor cursor = db.message().getMessageFts()) {
while (cursor.moveToNext()) {
EntityMessage message = db.message().getMessage(cursor.getLong(0));
if (message != null)
try {
Log.i("FTS index=" + message.id);
File file = message.getFile(getApplicationContext());
String html = Helper.readText(file);
String text = HtmlHelper.getText(html);
FtsDbHelper.insert(sdb, message, text);
db.message().setMessageFts(message.id, true);
indexed++;
} catch (Throwable ex) {
Log.e(ex);
}
}
}

Loading…
Cancel
Save