mirror of
https://github.com/M66B/FairEmail.git
synced 2024-12-24 08:44:26 +00:00
Store attachment count for better performance
This commit is contained in:
parent
752aee0a3b
commit
3be40ef641
6 changed files with 1860 additions and 9 deletions
1830
app/schemas/eu.faircode.email.DB/96.json
Normal file
1830
app/schemas/eu.faircode.email.DB/96.json
Normal file
File diff suppressed because it is too large
Load diff
|
@ -3159,6 +3159,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
same = false;
|
||||
Log.i("size changed id=" + next.id);
|
||||
}
|
||||
if (!Objects.equals(prev.attachments, next.attachments)) {
|
||||
same = false;
|
||||
Log.i("attachments changed id=" + next.id);
|
||||
}
|
||||
if (!prev.content.equals(next.content)) {
|
||||
same = false;
|
||||
Log.i("content changed id=" + next.id);
|
||||
|
|
|
@ -57,7 +57,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
|
|||
// https://developer.android.com/topic/libraries/architecture/room.html
|
||||
|
||||
@Database(
|
||||
version = 95,
|
||||
version = 96,
|
||||
entities = {
|
||||
EntityIdentity.class,
|
||||
EntityAccount.class,
|
||||
|
@ -161,7 +161,19 @@ public abstract class DB extends RoomDatabase {
|
|||
.databaseBuilder(context, DB.class, DB_NAME)
|
||||
.openHelperFactory(new RequerySQLiteOpenHelperFactory())
|
||||
.setQueryExecutor(executor)
|
||||
.setJournalMode(JournalMode.WRITE_AHEAD_LOGGING);
|
||||
.setJournalMode(JournalMode.WRITE_AHEAD_LOGGING)
|
||||
.addCallback(new Callback() {
|
||||
@Override
|
||||
public void onOpen(@NonNull SupportSQLiteDatabase db) {
|
||||
Log.i("Creating triggers");
|
||||
db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_insert" +
|
||||
" AFTER INSERT ON attachment" +
|
||||
" BEGIN UPDATE message SET attachments = attachments + 1 WHERE message.id = NEW.message; END");
|
||||
db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_delete" +
|
||||
" AFTER DELETE ON attachment" +
|
||||
" BEGIN UPDATE message SET attachments = attachments - 1 WHERE message.id = OLD.message; END");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static DB migrate(final Context context, RoomDatabase.Builder<DB> builder) {
|
||||
|
@ -956,6 +968,15 @@ public abstract class DB extends RoomDatabase {
|
|||
db.execSQL("ALTER TABLE `identity` ADD COLUMN `sign_key` INTEGER");
|
||||
}
|
||||
})
|
||||
.addMigrations(new Migration(95, 96) {
|
||||
@Override
|
||||
public void migrate(@NonNull SupportSQLiteDatabase db) {
|
||||
Log.i("DB migration from version " + startVersion + " to " + endVersion);
|
||||
db.execSQL("ALTER TABLE `message` ADD COLUMN `attachments` INTEGER NOT NULL DEFAULT 0");
|
||||
db.execSQL("UPDATE message SET attachments =" +
|
||||
" (SELECT COUNT(attachment.id) FROM attachment WHERE attachment.message = message.id)");
|
||||
}
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,6 @@ public interface DaoMessage {
|
|||
", COUNT(message.id) AS count" +
|
||||
", " + unseen_unified + " AS unseen" +
|
||||
", " + unflagged_unified + " AS unflagged" +
|
||||
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
|
||||
", SUM(CASE WHEN folder.type = '" + EntityFolder.DRAFTS + "' THEN 1 ELSE 0 END) AS drafts" +
|
||||
", COUNT(DISTINCT CASE WHEN message.msgid IS NULL THEN message.id ELSE message.msgid END) AS visible" +
|
||||
", SUM(message.size) AS totalSize" +
|
||||
|
@ -99,7 +98,6 @@ public interface DaoMessage {
|
|||
", COUNT(message.id) AS count" +
|
||||
", " + unseen_folder + " AS unseen" +
|
||||
", " + unflagged_folder + " AS unflagged" +
|
||||
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
|
||||
", SUM(CASE WHEN folder.type = '" + EntityFolder.DRAFTS + "' THEN 1 ELSE 0 END) AS drafts" +
|
||||
", COUNT(DISTINCT CASE WHEN message.msgid IS NULL THEN message.id ELSE message.msgid END) AS visible" +
|
||||
", SUM(message.size) AS totalSize" +
|
||||
|
@ -142,7 +140,6 @@ public interface DaoMessage {
|
|||
", 1 AS count" +
|
||||
", CASE WHEN message.ui_seen THEN 0 ELSE 1 END AS unseen" +
|
||||
", CASE WHEN message.ui_flagged THEN 0 ELSE 1 END AS unflagged" +
|
||||
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
|
||||
", CASE WHEN folder.type = '" + EntityFolder.DRAFTS + "' THEN 1 ELSE 0 END AS drafts" +
|
||||
", 1 AS visible" +
|
||||
", message.size AS totalSize" +
|
||||
|
@ -250,7 +247,6 @@ public interface DaoMessage {
|
|||
", 1 AS count" +
|
||||
", CASE WHEN message.ui_seen THEN 0 ELSE 1 END AS unseen" +
|
||||
", CASE WHEN message.ui_flagged THEN 0 ELSE 1 END AS unflagged" +
|
||||
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
|
||||
", CASE WHEN folder.type = '" + EntityFolder.DRAFTS + "' THEN 1 ELSE 0 END AS drafts" +
|
||||
", 1 AS visible" +
|
||||
", message.size AS totalSize" +
|
||||
|
@ -268,7 +264,6 @@ public interface DaoMessage {
|
|||
", 1 AS count" +
|
||||
", 1 AS unseen" +
|
||||
", 0 AS unflagged" +
|
||||
", 0 AS attachments" +
|
||||
", 0 AS drafts" +
|
||||
", 1 AS visible" +
|
||||
", message.size AS totalSize" +
|
||||
|
|
|
@ -113,6 +113,8 @@ public class EntityMessage implements Serializable {
|
|||
public String subject;
|
||||
public Long size;
|
||||
@NonNull
|
||||
public Integer attachments = 0; // performance
|
||||
@NonNull
|
||||
public Boolean content = false;
|
||||
public Boolean plain_only = null;
|
||||
public Boolean encrypt = null;
|
||||
|
@ -266,6 +268,7 @@ public class EntityMessage implements Serializable {
|
|||
Objects.equals(this.raw, other.raw) &&
|
||||
Objects.equals(this.subject, other.subject) &&
|
||||
Objects.equals(this.size, other.size) &&
|
||||
Objects.equals(this.attachments, other.attachments) &&
|
||||
this.content == other.content &&
|
||||
Objects.equals(this.plain_only, other.plain_only) &&
|
||||
Objects.equals(this.preview, other.preview) &&
|
||||
|
|
|
@ -37,7 +37,6 @@ public class TupleMessageEx extends EntityMessage {
|
|||
public int count;
|
||||
public int unseen;
|
||||
public int unflagged;
|
||||
public int attachments;
|
||||
public int drafts;
|
||||
public int visible;
|
||||
public Long totalSize;
|
||||
|
@ -63,7 +62,6 @@ public class TupleMessageEx extends EntityMessage {
|
|||
this.count == other.count &&
|
||||
this.unseen == other.unseen &&
|
||||
this.unflagged == other.unflagged &&
|
||||
this.attachments == other.attachments &&
|
||||
this.drafts == other.drafts &&
|
||||
this.visible == other.visible &&
|
||||
Objects.equals(this.totalSize, other.totalSize) &&
|
||||
|
|
Loading…
Reference in a new issue