Browse Source

Faster database access

main
M66B 6 years ago
parent
commit
37c226c74d
5 changed files with 43 additions and 34 deletions
  1. +11
    -9
      app/schemas/eu.faircode.email.DB/5.json
  2. +11
    -2
      app/src/main/java/eu/faircode/email/DB.java
  3. +15
    -20
      app/src/main/java/eu/faircode/email/DaoMessage.java
  4. +1
    -0
      app/src/main/java/eu/faircode/email/EntityMessage.java
  5. +5
    -3
      app/src/main/java/eu/faircode/email/FragmentMessages.java

+ 11
- 9
app/schemas/eu.faircode.email.DB/5.json View File

@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "f6603ac36cf520d5bfc2662bf7853c70",
"identityHash": "b14a2bd7371400904e10cb3dd32a5d98",
"entities": [
{
"tableName": "identity",
@ -86,7 +86,7 @@
},
{
"tableName": "account",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT, `host` TEXT NOT NULL, `port` INTEGER NOT NULL, `user` TEXT NOT NULL, `password` TEXT NOT NULL, `primary` INTEGER NOT NULL, `synchronize` INTEGER NOT NULL, `capabilities` TEXT NOT NULL)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT, `host` TEXT NOT NULL, `port` INTEGER NOT NULL, `user` TEXT NOT NULL, `password` TEXT NOT NULL, `primary` INTEGER NOT NULL, `synchronize` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
@ -135,12 +135,6 @@
"columnName": "synchronize",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "capabilities",
"columnName": "capabilities",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
@ -456,6 +450,14 @@
"ui_seen"
],
"createSql": "CREATE INDEX `index_message_ui_seen` ON `${TABLE_NAME}` (`ui_seen`)"
},
{
"name": "index_message_ui_hide",
"unique": false,
"columnNames": [
"ui_hide"
],
"createSql": "CREATE INDEX `index_message_ui_hide` ON `${TABLE_NAME}` (`ui_hide`)"
}
],
"foreignKeys": [
@ -659,7 +661,7 @@
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"f6603ac36cf520d5bfc2662bf7853c70\")"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"b14a2bd7371400904e10cb3dd32a5d98\")"
]
}
}

+ 11
- 2
app/src/main/java/eu/faircode/email/DB.java View File

@ -41,7 +41,7 @@ import android.util.Log;
EntityAttachment.class,
EntityOperation.class
},
version = 4,
version = 5,
exportSchema = true
)
@ -78,6 +78,7 @@ public abstract class DB extends RoomDatabase {
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_2_3)
.addMigrations(MIGRATION_3_4)
.addMigrations(MIGRATION_4_5)
.build();
}
@ -109,7 +110,15 @@ public abstract class DB extends RoomDatabase {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("CREATE INDEX `index_message_ui_seen` ON `message` (`ui_seen`)");
db.execSQL("CREATE INDEX `index_message_ui_seen` ON `message` (`ui_seen`)");
}
};
private static final Migration MIGRATION_4_5 = new Migration(4, 5) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("CREATE INDEX `index_message_ui_hide` ON `message` (`ui_hide`)");
}
};


+ 15
- 20
app/src/main/java/eu/faircode/email/DaoMessage.java View File

@ -32,44 +32,39 @@ import java.util.List;
@Dao
public interface DaoMessage {
@Query("SELECT message.*, folder.name as folderName, folder.type as folderType" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread) AS count" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread AND NOT m.ui_seen) AS unseen" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread AND NOT m.ui_hide) AS count" +
", SUM(CASE WHEN message.ui_seen THEN 0 ELSE 1 END) AS unseen" +
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
" FROM folder" +
" JOIN message ON folder = folder.id" +
" WHERE folder.type = '" + EntityFolder.TYPE_INBOX + "'" +
" AND (NOT ui_hide OR :debug)" +
" AND received IN (SELECT MAX(m.received) FROM message m WHERE m.folder = message.folder" +
" GROUP BY CASE WHEN m.thread IS NULL THEN m.id ELSE m.thread END)" +
" ORDER BY received DESC")
" GROUP BY thread" +
" ORDER BY message.received DESC")
// in theory the message id and thread could be the same
DataSource.Factory<Integer, TupleMessageEx> pagedUnifiedInbox(boolean debug);
@Query("SELECT message.*, folder.name as folderName, folder.type as folderType" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread) AS count" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread AND NOT m.ui_seen) AS unseen" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread AND NOT m.ui_hide) AS count" +
", SUM(CASE WHEN message.ui_seen THEN 0 ELSE 1 END) AS unseen" +
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
" FROM folder" +
" JOIN message ON folder = folder.id" +
" FROM message" +
" JOIN folder ON folder.id = message.folder" +
" WHERE folder.id = :folder" +
" AND (NOT ui_hide OR :debug)" +
" AND received IN (SELECT MAX(m.received) FROM message m WHERE m.folder = message.folder" +
" GROUP BY CASE WHEN m.thread IS NULL THEN m.id ELSE m.thread END)" +
" ORDER BY CASE WHEN folder.type IN " +
"('" + EntityFolder.TYPE_OUTBOX + "'" +
", '" + EntityFolder.TYPE_DRAFTS + "'" +
", '" + EntityFolder.TYPE_SENT + "')" +
" THEN sent ELSE received END DESC")
" GROUP BY thread" +
" ORDER BY received DESC, sent DESC")
DataSource.Factory<Integer, TupleMessageEx> pagedFolder(long folder, boolean debug);
@Query("SELECT message.*, folder.name as folderName, folder.type as folderType" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread) AS count" +
", (SELECT COUNT(m.id) FROM message m WHERE m.account = message.account AND m.thread = message.thread AND NOT m.ui_seen) AS unseen" +
", 1 AS count" +
", CASE WHEN message.ui_seen THEN 0 ELSE 1 END AS unseen" +
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
" FROM message" +
" JOIN folder ON folder.id = message.folder" +
" JOIN message m1 ON m1.id = :msgid AND m1.account = message.account AND m1.thread = message.thread" +
" WHERE NOT message.ui_hide OR :debug" +
" WHERE message.account = (SELECT m1.account FROM message m1 WHERE m1.id = :msgid)" +
" AND message.thread = (SELECT m2.thread FROM message m2 WHERE m2.id = :msgid)" +
" AND (NOT ui_hide OR :debug)" +
" ORDER BY received DESC")
DataSource.Factory<Integer, TupleMessageEx> pagedThread(long msgid, boolean debug);


+ 1
- 0
app/src/main/java/eu/faircode/email/EntityMessage.java View File

@ -46,6 +46,7 @@ import static android.arch.persistence.room.ForeignKey.CASCADE;
@Index(value = {"thread"}),
@Index(value = {"received"}),
@Index(value = {"ui_seen"}),
@Index(value = {"ui_hide"})
}
)
public class EntityMessage {


+ 5
- 3
app/src/main/java/eu/faircode/email/FragmentMessages.java View File

@ -52,6 +52,8 @@ public class FragmentMessages extends FragmentEx {
private AdapterMessage adapter;
private static final int PAGE_SIZE = 100;
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -101,7 +103,7 @@ public class FragmentMessages extends FragmentEx {
if (thread < 0)
if (folder < 0) {
setSubtitle(R.string.title_folder_unified);
messages = new LivePagedListBuilder<>(db.message().pagedUnifiedInbox(debug), 20).build();
messages = new LivePagedListBuilder<>(db.message().pagedUnifiedInbox(debug), PAGE_SIZE).build();
} else {
db.folder().liveFolderEx(folder).observe(this, new Observer<TupleFolderEx>() {
@Override
@ -109,11 +111,11 @@ public class FragmentMessages extends FragmentEx {
setSubtitle(folder == null ? null : Helper.localizeFolderName(getContext(), folder.name));
}
});
messages = new LivePagedListBuilder<>(db.message().pagedFolder(folder, debug), 20).build();
messages = new LivePagedListBuilder<>(db.message().pagedFolder(folder, debug), PAGE_SIZE).build();
}
else {
setSubtitle(R.string.title_folder_thread);
messages = new LivePagedListBuilder<>(db.message().pagedThread(thread, debug), 20).build();
messages = new LivePagedListBuilder<>(db.message().pagedThread(thread, debug), PAGE_SIZE).build();
}
messages.observe(this, new Observer<PagedList<TupleMessageEx>>() {


Loading…
Cancel
Save