From c19ddbdfc3b124f501e2dcfc2d239f0751404732 Mon Sep 17 00:00:00 2001 From: Andrzej Hunt Date: Wed, 21 Sep 2016 14:57:11 -0700 Subject: [PATCH] Bug 1298783 - Implement expiry for activity stream blocklist r=Grisha MozReview-Commit-ID: CfBIvX1ml05 --HG-- extra : rebase_source : 3ae36abf11a66607d1d7f71affda3e164f21cdac --- .../org/mozilla/gecko/db/BrowserProvider.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mobile/android/base/java/org/mozilla/gecko/db/BrowserProvider.java b/mobile/android/base/java/org/mozilla/gecko/db/BrowserProvider.java index 8d476e74d2b1..1c507b3f9579 100644 --- a/mobile/android/base/java/org/mozilla/gecko/db/BrowserProvider.java +++ b/mobile/android/base/java/org/mozilla/gecko/db/BrowserProvider.java @@ -71,6 +71,9 @@ public class BrowserProvider extends SharedBrowserDatabaseProvider { static final int DEFAULT_EXPIRY_RETAIN_COUNT = 2000; static final int AGGRESSIVE_EXPIRY_RETAIN_COUNT = 500; + // Factor used to determine the minimum number of records to keep when expiring the activity stream blocklist + static final int ACTIVITYSTREAM_BLOCKLIST_EXPIRY_FACTOR = 5; + // Minimum duration to keep when expiring. static final long DEFAULT_EXPIRY_PRESERVE_WINDOW = 1000L * 60L * 60L * 24L * 28L; // Four weeks. // Minimum number of thumbnails to keep around. @@ -383,6 +386,30 @@ public class BrowserProvider extends SharedBrowserDatabaseProvider { } } + /** + * Remove enough activity stream blocklist items to bring the database count below retain. + * + * Items will be removed according to their creation date, oldest being removed first. + */ + private void expireActivityStreamBlocklist(final SQLiteDatabase db, final int retain) { + Log.d(LOGTAG, "Expiring highlights blocklist."); + final long rows = DatabaseUtils.queryNumEntries(db, TABLE_ACTIVITY_STREAM_BLOCKLIST); + + if (retain >= rows) { + debug("Not expiring highlights blocklist: only have " + rows + " rows."); + return; + } + + final long toRemove = rows - retain; + + final String statement = "DELETE FROM " + TABLE_ACTIVITY_STREAM_BLOCKLIST + " WHERE " + ActivityStreamBlocklist._ID + " IN " + + " ( SELECT " + ActivityStreamBlocklist._ID + " FROM " + TABLE_ACTIVITY_STREAM_BLOCKLIST + " " + + "ORDER BY " + ActivityStreamBlocklist.CREATED + " ASC LIMIT " + toRemove + ")"; + + beginWrite(db); + db.execSQL(statement); + } + /** * Remove enough history items to bring the database count below retain, * removing no items with a modified time after keepAfter. @@ -555,6 +582,7 @@ public class BrowserProvider extends SharedBrowserDatabaseProvider { retainCount = AGGRESSIVE_EXPIRY_RETAIN_COUNT; } expireHistory(db, retainCount, keepAfter); + expireActivityStreamBlocklist(db, retainCount / ACTIVITYSTREAM_BLOCKLIST_EXPIRY_FACTOR); expireThumbnails(db); deleteUnusedImages(uri); break;