Bug 808212 - Expire thumbnails that we don't need. r=rnewman

This commit is contained in:
Wes Johnston 2012-11-07 09:39:55 -08:00
Родитель 3738ed83b2
Коммит a1b76d0ab6
2 изменённых файлов: 71 добавлений и 9 удалений

Просмотреть файл

@ -89,6 +89,8 @@ public class BrowserProvider extends ContentProvider {
// 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.
static final int DEFAULT_EXPIRY_THUMBNAIL_COUNT = 15;
static final String TABLE_BOOKMARKS = "bookmarks";
static final String TABLE_HISTORY = "history";
@ -1427,22 +1429,21 @@ public class BrowserProvider extends ContentProvider {
*/
public void expireHistory(final SQLiteDatabase db, final int retain, final long keepAfter) {
final long rows = DatabaseUtils.queryNumEntries(db, TABLE_HISTORY);
if (retain >= rows) {
debug("Not expiring history: only have " + rows + " rows.");
return;
}
final String sortOrder = BrowserContract.getFrecencySortOrder(false, true);
final long toRemove = rows - retain;
debug("Expiring at most " + toRemove + " rows earlier than " + keepAfter + ".");
final String sortOrder = BrowserContract.getFrecencySortOrder(false, true);
final String sql;
if (keepAfter > 0) {
// If we don't bind these paramaters dynamically, the WHERE clause here can return null
sql = "DELETE FROM " + TABLE_HISTORY + " " +
sql = "DELETE FROM " + TABLE_HISTORY + " " +
"WHERE MAX(" + History.DATE_LAST_VISITED + ", " + History.DATE_MODIFIED +") < " + keepAfter + " " +
" AND " + History._ID + " " + "IN ( SELECT " +
" AND " + History._ID + " IN ( SELECT " +
History._ID + " FROM " + TABLE_HISTORY + " " +
"ORDER BY " + sortOrder + " LIMIT " + toRemove +
")";
@ -1451,11 +1452,30 @@ public class BrowserProvider extends ContentProvider {
"IN ( SELECT " + History._ID + " FROM " + TABLE_HISTORY + " " +
"ORDER BY " + sortOrder + " LIMIT " + toRemove + ")";
}
trace("Deleting using query: " + sql);
db.execSQL(sql);
}
/**
* Remove any thumbnails that for sites that aren't likely to be ever shown.
* Items will be removed according to a frecency calculation.
*
* Call this method within a transaction.
*/
public void expireThumbnails(final SQLiteDatabase db) {
final String sortOrder = BrowserContract.getFrecencySortOrder(true, false);
final String sql = "UPDATE " + TABLE_IMAGES +
" SET " + Images.THUMBNAIL + " = NULL " +
" WHERE " + Images.URL + " NOT IN ( " +
" SELECT " + Combined.URL +
" FROM " + VIEW_COMBINED_WITH_IMAGES +
" ORDER BY " + sortOrder +
" LIMIT " + DEFAULT_EXPIRY_THUMBNAIL_COUNT +
")";
trace("Clear thumbs using query: " + sql);
db.execSQL(sql);
}
private boolean isCallerSync(Uri uri) {
String isSync = uri.getQueryParameter(BrowserContract.PARAM_IS_SYNC);
return !TextUtils.isEmpty(isSync);
@ -1606,6 +1626,8 @@ public class BrowserProvider extends ContentProvider {
retainCount = AGGRESSIVE_EXPIRY_RETAIN_COUNT;
}
expireHistory(db, retainCount, keepAfter);
expireThumbnails(db);
deleteUnusedImages(uri);
break;
}

Просмотреть файл

@ -1593,7 +1593,7 @@ public class testBrowserProvider extends ContentProviderTest {
class TestExpireHistory extends Test {
private void createFakeHistory(long timeShift, int count) {
// Insert a bunch of very new entries
ContentValues allVals[] = new ContentValues[count];
ContentValues[] allVals = new ContentValues[count];
long time = System.currentTimeMillis() - timeShift;
for (int i = 0; i < count; i++) {
allVals[i] = new ContentValues();
@ -1604,7 +1604,7 @@ public class testBrowserProvider extends ContentProviderTest {
}
int inserts = mProvider.bulkInsert(mHistoryUri, allVals);
mAsserter.is(inserts, count, "Excepted number of inserts matches");
mAsserter.is(inserts, count, "Expected number of inserts matches");
// inserting a new entry sets the date created and modified automatically
// reset all of them
@ -1618,10 +1618,26 @@ public class testBrowserProvider extends ContentProviderTest {
Cursor c = mProvider.query(mHistoryUri, null, "", null, null);
mAsserter.is(c.getCount(), count, count + " history entries found");
// add thumbnails for each entry
allVals = new ContentValues[count];
for (int i = 0; i < count; i++) {
allVals[i] = new ContentValues();
allVals[i].put(mImagesThumbnailCol, i);
allVals[i].put(mImagesUrlCol, "http://www.test.org/" + i);
allVals[i].put(mImagesFaviconCol, i);
}
inserts = mProvider.bulkInsert(mImagesUri, allVals);
mAsserter.is(inserts, count, "Expected number of inserts matches");
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
mAsserter.is(c.getCount(), count, count + " thumbnails entries found");
}
public void test() throws Exception {
final int count = 3000;
final int thumbCount = 15;
// insert a bunch of new entries
createFakeHistory(0, count);
@ -1632,14 +1648,25 @@ public class testBrowserProvider extends ContentProviderTest {
Cursor c = mProvider.query(mHistoryUri, null, "", null, null);
mAsserter.is(c.getCount(), count, count + " history entries found");
// expiring with a normal priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
// insert a bunch of new entries
createFakeHistory(0, count);
// expiring with a aggressive priority should leave 500 entries
url = appendUriParam(mHistoryOldUri, "PARAM_EXPIRE_PRIORITY", "AGGRESSIVE");
mProvider.delete(url, null, null);
c = mProvider.query(mHistoryUri, null, "", null, null);
mAsserter.is(c.getCount(), 500, "500 history entries found");
ensureEmptyDatabase();
// expiring with a aggressive priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
// insert a bunch of entries with an old time created/modified
long time = 1000L * 60L * 60L * 24L * 30L * 3L;
createFakeHistory(time, count);
@ -1651,12 +1678,25 @@ public class testBrowserProvider extends ContentProviderTest {
c = mProvider.query(mHistoryUri, null, "", null, null);
mAsserter.is(c.getCount(), 2000, "2000 history entries found");
// expiring with a normal priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
// insert a bunch of entries with an old time created/modified
time = 1000L * 60L * 60L * 24L * 30L * 3L;
createFakeHistory(time, count);
// expiring with an agressive priority should remove old
// entries leaving at least 500
url = appendUriParam(mHistoryOldUri, "PARAM_EXPIRE_PRIORITY", "AGGRESSIVE");
mProvider.delete(url, null, null);
c = mProvider.query(mHistoryUri, null, "", null, null);
mAsserter.is(c.getCount(), 500, "500 history entries found");
// expiring with a aggressive priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
}
}
}