Bug 743923 - Don't keep history in memory during Profile Migration. r=lucasr
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Thu, 12 Apr 2012 19:28:50 +0200
changeset 94858 d0c617586fcf9bda17a14ace2db1e356b44ef2af
parent 94857 428c6bf76857c038b36a8aa1ab10141af2d7bb95
child 94859 1c3e7cbe44eeb5d45534d134e651cfb1e040665a
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr
bugs743923
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 743923 - Don't keep history in memory during Profile Migration. r=lucasr
mobile/android/base/ProfileMigrator.java
--- a/mobile/android/base/ProfileMigrator.java
+++ b/mobile/android/base/ProfileMigrator.java
@@ -39,17 +39,16 @@ package org.mozilla.gecko;
 
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserContract.History;
 import org.mozilla.gecko.db.BrowserContract.ImageColumns;
 import org.mozilla.gecko.db.BrowserContract.Images;
 import org.mozilla.gecko.db.BrowserContract.URLColumns;
 import org.mozilla.gecko.db.BrowserContract.SyncColumns;
-import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.sqlite.SQLiteBridge;
 import org.mozilla.gecko.sqlite.SQLiteBridgeException;
 
 import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.ContentProviderResult;
 import android.content.ContentProviderOperation;
@@ -318,95 +317,52 @@ public class ProfileMigrator {
                 }
                 cursor.close();
             } catch (SQLiteBridgeException e) {
                 Log.e(LOGTAG, "Failed to get bookmark roots: ", e);
                 return;
             }
         }
 
-        // Get a list of the last times an URL was accessed
-        protected Map<String, Long> gatherBrowserDBHistory() {
-            Map<String, Long> history = new HashMap<String, Long>();
-
-            Cursor cursor =
-                BrowserDB.getRecentHistory(mCr, BrowserDB.getMaxHistoryCount());
-            final int urlCol =
-                cursor.getColumnIndexOrThrow(BrowserDB.URLColumns.URL);
-            final int dateCol =
-                cursor.getColumnIndexOrThrow(BrowserDB.URLColumns.DATE_LAST_VISITED);
-
-            cursor.moveToFirst();
-            while (!cursor.isAfterLast()) {
-                String url = cursor.getString(urlCol);
-                Long date = cursor.getLong(dateCol);
-                // getRecentHistory returns newest-to-oldest, which means
-                // we remember the most recent access
-                if (!history.containsKey(url)) {
-                    history.put(url, date);
-                }
-                cursor.moveToNext();
-            }
-            cursor.close();
-
-            return history;
-        }
-
-        protected void addHistory(Map<String, Long> browserDBHistory,
-                                  String url, String title, long date, int visits) {
-            boolean allowUpdate = false;
-
-            if (!browserDBHistory.containsKey(url)) {
-                // BrowserDB doesn't know the URL, allow it to be
-                // inserted with places date.
-                allowUpdate = true;
-            } else {
-                long androidDate = browserDBHistory.get(url);
-                if (androidDate < date) {
-                    // Places URL hit is newer than BrowserDB,
-                    // allow it to be updated with places date.
-                    allowUpdate = true;
-                }
-            }
-
-            if (allowUpdate) {
-                updateBrowserHistory(url, title, date, visits);
-            }
-        }
-
         protected void updateBrowserHistory(String url, String title,
                                             long date, int visits) {
             Cursor cursor = null;
 
             try {
                 final String[] projection = new String[] {
                     History._ID,
-                    History.VISITS
+                    History.VISITS,
+                    History.DATE_LAST_VISITED
                 };
 
                 cursor = mCr.query(getHistoryUri(),
                                    projection,
                                    History.URL + " = ?",
                                    new String[] { url },
                                    null);
 
                 ContentValues values = new ContentValues();
                 ContentProviderOperation.Builder builder = null;
-                values.put(History.DATE_LAST_VISITED, date);
                 // Restore deleted record if possible
                 values.put(History.IS_DELETED, 0);
 
                 if (cursor.moveToFirst()) {
                     int visitsCol = cursor.getColumnIndexOrThrow(History.VISITS);
+                    int dateCol = cursor.getColumnIndexOrThrow(History.DATE_LAST_VISITED);
                     int oldVisits = cursor.getInt(visitsCol);
+                    long oldDate = cursor.getLong(dateCol);
 
                     values.put(History.VISITS, oldVisits + visits);
                     if (title != null) {
                         values.put(History.TITLE, title);
                     }
+                    // Only update last visited if newer.
+                    if (date > oldDate) {
+                        values.put(History.DATE_LAST_VISITED, date);
+                    }
 
                     int idCol = cursor.getColumnIndexOrThrow(History._ID);
                     // We use default profile anyway
                     Uri historyUri = ContentUris.withAppendedId(getHistoryUri(),
                                                                 cursor.getLong(idCol));
 
                     // Update
                     builder = ContentProviderOperation.newUpdate(historyUri);
@@ -416,16 +372,17 @@ public class ProfileMigrator {
                 } else {
                     values.put(History.URL, url);
                     values.put(History.VISITS, visits);
                     if (title != null) {
                         values.put(History.TITLE, title);
                     } else {
                         values.put(History.TITLE, url);
                     }
+                    values.put(History.DATE_LAST_VISITED, date);
 
                     // Insert
                     builder = ContentProviderOperation.newInsert(getHistoryUri());
                     builder.withValues(values);
                 }
 
                 // Queue the operation
                 mOperations.add(builder.build());
@@ -511,17 +468,16 @@ public class ProfileMigrator {
                 mOperations.add(builder.build());
             } catch (SQLException e) {
                 Log.i(LOGTAG, "Migrating favicon failed: " + mime + " URL: " + url
                       + " error:" + e.getMessage());
             }
         }
 
         protected void doMigrateHistoryBatch(SQLiteBridge db,
-                                             Map<String, Long> browserDBHistory,
                                              int maxEntries, int currentEntries) {
             final ArrayList<String> placesHistory = new ArrayList<String>();
             mOperations = new ArrayList<ContentProviderOperation>();
             int queryResultEntries = 0;
 
             try {
                 final String currentTime = Long.toString(System.currentTimeMillis());
                 final String[] queryParams = new String[] {
@@ -553,17 +509,17 @@ public class ProfileMigrator {
                     String faviconMime = cursor.getString(faviconMimeCol);
                     String faviconUrl = cursor.getString(faviconUrlCol);
                     String faviconGuid = cursor.getString(faviconGuidCol);
 
                     try {
                         placesHistory.add(url);
                         addFavicon(url, faviconUrl, faviconGuid,
                                    faviconMime, faviconDataBuff);
-                        addHistory(browserDBHistory, url, title, date, visits);
+                        updateBrowserHistory(url, title, date, visits);
                     } catch (Exception e) {
                         Log.e(LOGTAG, "Error adding history entry: ", e);
                     }
                     cursor.moveToNext();
                 }
                 cursor.close();
             } catch (SQLiteBridgeException e) {
                 Log.e(LOGTAG, "Failed to get history: ", e);
@@ -588,27 +544,24 @@ public class ProfileMigrator {
                         for (String url : placesHistory) {
                             GlobalHistory.getInstance().addToGeckoOnly(url);
                         }
                     }
              });
         }
 
         protected void migrateHistory(SQLiteBridge db) {
-            Map<String, Long> browserDBHistory = gatherBrowserDBHistory();
-
             for (int i = 0; i < mMaxEntries; i += HISTORY_MAX_BATCH) {
                 int currentEntries = getMigratedHistoryEntries();
                 int fetchEntries = Math.min(mMaxEntries, HISTORY_MAX_BATCH);
 
                 Log.i(LOGTAG, "Processed " + currentEntries + " history entries");
                 Log.i(LOGTAG, "Fetching " + fetchEntries + " more history entries");
 
-                doMigrateHistoryBatch(db, browserDBHistory,
-                                      fetchEntries, currentEntries);
+                doMigrateHistoryBatch(db, fetchEntries, currentEntries);
             }
         }
 
         protected void addBookmark(String url, String title, String guid,
                                    long parent, long added,
                                    long modified, long position,
                                    boolean folder) {
             ContentValues values = new ContentValues();