1
0
Fork 0
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:
M66B 2019-07-24 14:49:55 +02:00
parent 752aee0a3b
commit 3be40ef641
6 changed files with 1860 additions and 9 deletions

File diff suppressed because it is too large Load diff

View file

@ -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);

View file

@ -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();
}

View file

@ -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" +

View file

@ -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) &&

View file

@ -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) &&