Cleanup old local contacts

pull/153/head
M66B 6 years ago
parent a2d03f7af8
commit 40f1832ab7

File diff suppressed because it is too large Load Diff

@ -50,7 +50,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 56,
version = 57,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -615,6 +615,15 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `contact` ADD COLUMN `favorite` INTEGER NOT NULL DEFAULT 0");
}
})
.addMigrations(new Migration(56, 57) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("CREATE INDEX `index_contact_times_contacted` ON `contact` (`times_contacted`)");
db.execSQL("CREATE INDEX `index_contact_last_contacted` ON `contact` (`last_contacted`)");
db.execSQL("CREATE INDEX `index_contact_favorite` ON `contact` (`favorite`)");
}
})
.build();
}

@ -72,6 +72,12 @@ public interface DaoContact {
@Query("DELETE FROM contact WHERE id= :id")
int deleteContact(long id);
@Query("DELETE FROM contact" +
" WHERE last_contacted IS NOT NULL" +
" AND last_contacted < :before" +
" AND NOT favorite")
int deleteContacts(long before);
@Query("DELETE FROM contact")
int clearContacts();
}

@ -40,6 +40,9 @@ import androidx.room.PrimaryKey;
indices = {
@Index(value = {"email", "type"}, unique = true),
@Index(value = {"name", "type"}),
@Index(value = {"times_contacted"}),
@Index(value = {"last_contacted"}),
@Index(value = {"favorite"})
}
)
public class EntityContact implements Serializable {

@ -22,6 +22,7 @@ import androidx.work.WorkerParameters;
public class WorkerCleanup extends Worker {
private static final int CLEANUP_INTERVAL = 4; // hours
private static final long CACHE_IMAGE_DURATION = 3 * 24 * 3600 * 1000L; // milliseconds
private static final long KEEP_CONTACTS_DURATION = 180 * 24 * 3600 * 1000L; // milliseconds
private static final long KEEP_LOG_DURATION = 24 * 3600 * 1000L; // milliseconds
public WorkerCleanup(@NonNull Context context, @NonNull WorkerParameters workerParams) {
@ -112,9 +113,12 @@ public class WorkerCleanup extends Worker {
Log.w("Error deleting " + file);
}
Log.i("Cleanup contacts");
int contacts = db.contact().deleteContacts(now - KEEP_CONTACTS_DURATION);
Log.i("Deleted contacts=" + contacts);
Log.i("Cleanup log");
long before = now - KEEP_LOG_DURATION;
int logs = db.log().deleteLogs(before);
int logs = db.log().deleteLogs(now - KEEP_LOG_DURATION);
Log.i("Deleted logs=" + logs);
db.setTransactionSuccessful();

Loading…
Cancel
Save