Bug 744961 - Move expiration sort into a static reusable function. r=lucasr
authorWes Johnston <wjohnston@mozilla.com>
Fri, 19 Oct 2012 17:35:43 -0700
changeset 110979 5d9a4a7b8818bd6947b912eea257a27e5ba6abbe
parent 110978 8ab4bbf4b815795452b58dcdc9d2de571ad8c8a5
child 110980 65d560d91cb08925819f815b6ae6efe70ce8003f
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerslucasr
bugs744961
milestone19.0a1
Bug 744961 - Move expiration sort into a static reusable function. r=lucasr
mobile/android/base/db/BrowserContract.java.in
mobile/android/base/db/BrowserProvider.java.in
mobile/android/base/db/LocalBrowserDB.java
--- a/mobile/android/base/db/BrowserContract.java.in
+++ b/mobile/android/base/db/BrowserContract.java.in
@@ -31,16 +31,29 @@ public class BrowserContract {
     public static final String PARAM_INCREMENT_VISITS = "increment_visits";
     public static final String PARAM_EXPIRE_PRIORITY = "priority";
 
     static public enum ExpirePriority {
         NORMAL,
         AGGRESSIVE
     }
 
+    static public String getFrecencySortOrder(boolean includesBookmarks, boolean asc) {
+        final String age = "(" + Combined.DATE_LAST_VISITED + " - " + System.currentTimeMillis() + ") / 86400000";
+
+        StringBuilder order = new StringBuilder(Combined.VISITS + " * MAX(1, 100 * 225 / (" + age + "*" + age + " + 225)) ");
+
+        if (includesBookmarks) {
+            order.insert(0, "(CASE WHEN " + Combined.BOOKMARK_ID + " > -1 THEN 100 ELSE 0 END) + ");
+        }
+
+        order.append(asc ? " ASC" : " DESC");
+        return order.toString();
+    }
+
     public interface CommonColumns {
         public static final String _ID = "_id";
     }
 
     public interface SyncColumns {
         public static final String GUID = "guid";
         public static final String DATE_CREATED = "created";
         public static final String DATE_MODIFIED = "modified";
--- a/mobile/android/base/db/BrowserProvider.java.in
+++ b/mobile/android/base/db/BrowserProvider.java.in
@@ -1347,21 +1347,19 @@ public class BrowserProvider extends Con
     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 long toRemove = rows - retain;
-        final long now = System.currentTimeMillis();
         debug("Expiring at most " + toRemove + " rows earlier than " + keepAfter + ".");
 
-        final String age = "(" + Combined.DATE_LAST_VISITED + " - " + now + ") / 86400000";
-        final String sortOrder = Combined.VISITS + " * MAX(1, 100 * 225 / (" + age + "*" + age + " + 225)) ASC";
+        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 + " " + 
                   "WHERE MAX(" + History.DATE_LAST_VISITED + ", " + History.DATE_MODIFIED +") < " + keepAfter + " " +
                   " AND " + History._ID + " " + "IN ( SELECT " +
                     History._ID + " FROM " + TABLE_HISTORY + " " +
--- a/mobile/android/base/db/LocalBrowserDB.java
+++ b/mobile/android/base/db/LocalBrowserDB.java
@@ -160,19 +160,17 @@ public class LocalBrowserDB implements B
 
         // Our version of frecency is computed by scaling the number of visits by a multiplier
         // that approximates Gaussian decay, based on how long ago the entry was last visited.
         // Since we're limited by the math we can do with sqlite, we're calculating this
         // approximation using the Cauchy distribution: multiplier = 15^2 / (age^2 + 15^2).
         // Using 15 as our scale parameter, we get a constant 15^2 = 225. Following this math,
         // frecencyScore = numVisits * max(1, 100 * 225 / (age*age + 225)). (See bug 704977)
         // We also give bookmarks an extra bonus boost by adding 100 points to their frecency score.
-        final String age = "(" + Combined.DATE_LAST_VISITED + " - " + System.currentTimeMillis() + ") / 86400000";
-        final String sortOrder = "(CASE WHEN " + Combined.BOOKMARK_ID + " > -1 THEN 100 ELSE 0 END) + " +
-                                 Combined.VISITS + " * MAX(1, 100 * 225 / (" + age + "*" + age + " + 225)) DESC";
+        final String sortOrder = BrowserContract.getFrecencySortOrder(true, false);
 
         Cursor c = cr.query(combinedUriWithLimit(limit),
                             projection,
                             selection,
                             selectionArgs,
                             sortOrder);
 
         return new LocalDBCursor(c);