Bug 404645: apply an entire safebrowsing update as one transaction, rather than a transaction per chunk of entries. r=tony, blocking-1.9=beltzner
authordcamp@mozilla.com
Mon, 03 Dec 2007 20:35:25 -0800
changeset 8639 e5fc13213dc91028ef56956c2b20570f5fb894e4
parent 8638 ae489c1fda3b71cafb8615eee3aeca3438f29048
child 8640 bb1e03b466a64141c9a347b2c10906f049ce33e7
push id1
push userbsmedberg@mozilla.com
push dateThu, 20 Mar 2008 16:49:24 +0000
treeherdermozilla-central@61007906a1f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstony
bugs404645
milestone1.9b2pre
Bug 404645: apply an entire safebrowsing update as one transaction, rather than a transaction per chunk of entries. r=tony, blocking-1.9=beltzner
toolkit/components/url-classifier/src/nsUrlClassifierDBService.cpp
--- a/toolkit/components/url-classifier/src/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/src/nsUrlClassifierDBService.cpp
@@ -1419,18 +1419,16 @@ nsUrlClassifierDBServiceWorker::AddChunk
   PRIntervalTime clockStart = 0;
   if (LOG_ENABLED()) {
     clockStart = PR_IntervalNow();
   }
 #endif
 
   LOG(("Adding %d entries to chunk %d", entries.Length(), chunkNum));
 
-  mozStorageTransaction transaction(mConnection, PR_FALSE);
-
   nsCAutoString addChunks;
   nsCAutoString subChunks;
 
   HandlePendingLookups();
 
   nsresult rv = GetChunkLists(tableId, addChunks, subChunks);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1475,38 +1473,31 @@ nsUrlClassifierDBServiceWorker::AddChunk
      reinterpret_cast<PRUint8*>(entryIDs.Elements()),
      entryIDs.Length() * sizeof(PRUint32));
 
   HandlePendingLookups();
 
   rv = mAddChunkEntriesStatement->Execute();
   NS_ENSURE_SUCCESS(rv, rv);
 
-  HandlePendingLookups();
-
-  rv = transaction.Commit();
-  NS_ENSURE_SUCCESS(rv, rv);
-
 #if defined(PR_LOGGING)
   if (LOG_ENABLED()) {
     PRIntervalTime clockEnd = PR_IntervalNow();
     printf("adding chunk %d took %dms\n", chunkNum,
            PR_IntervalToMilliseconds(clockEnd - clockStart));
   }
 #endif
 
   return rv;
 }
 
 nsresult
 nsUrlClassifierDBServiceWorker::ExpireAdd(PRUint32 tableId,
                                           PRUint32 chunkNum)
 {
-  mozStorageTransaction transaction(mConnection, PR_FALSE);
-
   LOG(("Expiring chunk %d\n", chunkNum));
 
   nsCAutoString addChunks;
   nsCAutoString subChunks;
 
   HandlePendingLookups();
 
   nsresult rv = GetChunkLists(tableId, addChunks, subChunks);
@@ -1560,28 +1551,24 @@ nsUrlClassifierDBServiceWorker::ExpireAd
   HandlePendingLookups();
 
   mozStorageStatementScoper removeScoper(mDeleteChunkEntriesStatement);
   mDeleteChunkEntriesStatement->BindInt32Parameter(0, tableId);
   mDeleteChunkEntriesStatement->BindInt32Parameter(1, chunkNum);
   rv = mDeleteChunkEntriesStatement->Execute();
   NS_ENSURE_SUCCESS(rv, rv);
 
-  HandlePendingLookups();
-
-  return transaction.Commit();
+  return NS_OK;
 }
 
 nsresult
 nsUrlClassifierDBServiceWorker::SubChunk(PRUint32 tableId,
                                          PRUint32 chunkNum,
                                          nsTArray<nsUrlClassifierEntry>& entries)
 {
-  mozStorageTransaction transaction(mConnection, PR_FALSE);
-
   nsCAutoString addChunks;
   nsCAutoString subChunks;
 
   HandlePendingLookups();
 
   nsresult rv = GetChunkLists(tableId, addChunks, subChunks);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1605,44 +1592,38 @@ nsUrlClassifierDBServiceWorker::SubChunk
       return NS_ERROR_FAILURE;
 
     HandlePendingLookups();
 
     rv = WriteEntry(existingEntry);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  HandlePendingLookups();
-
-  return transaction.Commit();
+  return NS_OK;
 }
 
 nsresult
 nsUrlClassifierDBServiceWorker::ExpireSub(PRUint32 tableId, PRUint32 chunkNum)
 {
-  mozStorageTransaction transaction(mConnection, PR_FALSE);
-
   nsCAutoString addChunks;
   nsCAutoString subChunks;
 
   HandlePendingLookups();
 
   nsresult rv = GetChunkLists(tableId, addChunks, subChunks);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsTArray<PRUint32> subs;
   ParseChunkList(subChunks, subs);
   subs.RemoveElement(chunkNum);
   JoinChunkList(subs, subChunks);
   rv = SetChunkLists(tableId, addChunks, subChunks);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  HandlePendingLookups();
-
-  return transaction.Commit();
+  return NS_OK;
 }
 
 nsresult
 nsUrlClassifierDBServiceWorker::ProcessChunk(PRBool* done)
 {
   // wait until the chunk plus terminating \n has been read
   if (mPendingStreamUpdate.Length() <= static_cast<PRUint32>(mChunkLen)) {
     *done = PR_TRUE;
@@ -1815,16 +1796,26 @@ nsUrlClassifierDBServiceWorker::Update(c
     return NS_ERROR_FAILURE;
   }
 
   // if something has gone wrong during this update, just throw it away
   if (NS_FAILED(mUpdateStatus)) {
     return mUpdateStatus;
   }
 
+  PRBool transaction;
+  if (NS_SUCCEEDED(mConnection->GetTransactionInProgress(&transaction)) &&
+      !transaction) {
+    rv = mConnection->BeginTransaction();
+    if (NS_FAILED(rv)) {
+      mUpdateStatus = rv;
+      return rv;
+    }
+  }
+
   LOG(("Got %s\n", PromiseFlatCString(chunk).get()));
 
   mPendingStreamUpdate.Append(chunk);
 
   PRBool done = PR_FALSE;
   while (!done) {
     if (mState == STATE_CHUNK) {
       rv = ProcessChunk(&done);
@@ -1841,16 +1832,22 @@ nsUrlClassifierDBServiceWorker::Update(c
 }
 
 NS_IMETHODIMP
 nsUrlClassifierDBServiceWorker::Finish(nsIUrlClassifierCallback* aSuccessCallback,
                                        nsIUrlClassifierCallback* aErrorCallback)
 {
   nsCAutoString arg;
   if (NS_SUCCEEDED(mUpdateStatus)) {
+    mUpdateStatus = mConnection->CommitTransaction();
+  } else {
+    mConnection->RollbackTransaction();
+  }
+
+  if (NS_SUCCEEDED(mUpdateStatus)) {
     arg.AppendInt(mUpdateWait);
     aSuccessCallback->HandleEvent(arg);
   } else {
     arg.AppendInt(mUpdateStatus);
     aErrorCallback->HandleEvent(arg);
   }
 
   ResetUpdate();