Bug 1290481 - P9: Implement a function to get overall padding size for an origin from cache.sqlite. r=bkelly
authorTom Tung <shes050117@gmail.com>
Tue, 18 Jul 2017 18:58:56 +0800
changeset 429029 885e9a3b821e752a8df0a97a456d02bb1138076c
parent 429028 de5a573de6632f96368f646ff3eba7ac9e2707cc
child 429030 69c4ad5e1f2db67c9857dcbc8f176dd5443f42a5
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1290481
milestone57.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 1290481 - P9: Implement a function to get overall padding size for an origin from cache.sqlite. r=bkelly MozReview-Commit-ID: KlVsaGhpABk
dom/cache/DBSchema.cpp
dom/cache/DBSchema.h
dom/cache/FileUtils.cpp
--- a/dom/cache/DBSchema.cpp
+++ b/dom/cache/DBSchema.cpp
@@ -694,16 +694,47 @@ FindOrphanedCacheIds(mozIStorageConnecti
     if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
     aOrphanedListOut.AppendElement(cacheId);
   }
 
   return rv;
 }
 
 nsresult
+FindOverallPaddingSize(mozIStorageConnection* aConn,
+                       int64_t* aOverallPaddingSizeOut)
+{
+  MOZ_DIAGNOSTIC_ASSERT(aConn);
+  MOZ_DIAGNOSTIC_ASSERT(aOverallPaddingSizeOut);
+
+  nsCOMPtr<mozIStorageStatement> state;
+  nsresult rv = aConn->CreateStatement(NS_LITERAL_CSTRING(
+    "SELECT response_padding_size FROM entries "
+    "WHERE response_padding_size IS NOT NULL;"
+  ), getter_AddRefs(state));
+  if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
+
+  int64_t overallPaddingSize = 0;
+  bool hasMoreData = false;
+  while (NS_SUCCEEDED(state->ExecuteStep(&hasMoreData)) && hasMoreData) {
+    int64_t padding_size = 0;
+    rv = state->GetInt64(0, &padding_size);
+    if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
+
+    MOZ_DIAGNOSTIC_ASSERT(padding_size >= 0);
+    MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - padding_size >= overallPaddingSize);
+    overallPaddingSize += padding_size;
+  }
+
+  *aOverallPaddingSizeOut = overallPaddingSize;
+
+  return rv;
+}
+
+nsresult
 GetKnownBodyIds(mozIStorageConnection* aConn, nsTArray<nsID>& aBodyIdListOut)
 {
   MOZ_ASSERT(!NS_IsMainThread());
   MOZ_DIAGNOSTIC_ASSERT(aConn);
 
   nsCOMPtr<mozIStorageStatement> state;
   nsresult rv = aConn->CreateStatement(NS_LITERAL_CSTRING(
     "SELECT request_body_id, response_body_id FROM entries;"
--- a/dom/cache/DBSchema.h
+++ b/dom/cache/DBSchema.h
@@ -50,16 +50,20 @@ nsresult
 IsCacheOrphaned(mozIStorageConnection* aConn, CacheId aCacheId,
                 bool* aOrphanedOut);
 
 nsresult
 FindOrphanedCacheIds(mozIStorageConnection* aConn,
                      nsTArray<CacheId>& aOrphanedListOut);
 
 nsresult
+FindOverallPaddingSize(mozIStorageConnection* aConn,
+                       int64_t* aOverallPaddingSizeOut);
+
+nsresult
 GetKnownBodyIds(mozIStorageConnection* aConn, nsTArray<nsID>& aBodyIdListOut);
 
 nsresult
 CacheMatch(mozIStorageConnection* aConn, CacheId aCacheId,
            const CacheRequest& aRequest, const CacheQueryParams& aParams,
            bool* aFoundResponseOut, SavedResponse* aSavedResponseOut);
 
 nsresult
--- a/dom/cache/FileUtils.cpp
+++ b/dom/cache/FileUtils.cpp
@@ -804,27 +804,38 @@ LockedMaybeUpdateDirectoryPaddingFile(ns
     if (rv != NS_ERROR_FILE_NOT_FOUND &&
         rv != NS_ERROR_FILE_TARGET_DOES_NOT_EXIST) {
       // Not delete the temporary padding file here, because we're going to
       // overwrite it below anyway.
       rv = LockedDirectoryPaddingDeleteFile(aBaseDir, DirPaddingFile::FILE);
       if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
     }
 
-    // XXXtt: will have a function to retore from db.
+    // We don't need to add the aIncreaseSize or aDecreaseSize here, because
+    // it's already encompassed within the database.
+    rv = db::FindOverallPaddingSize(aConn, &currentPaddingSize);
+    if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
   } else {
     if (aIncreaseSize > 0) {
       MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - currentPaddingSize >= aIncreaseSize);
       currentPaddingSize += aIncreaseSize;
     }
 
     if (aDecreaseSize > 0) {
       MOZ_DIAGNOSTIC_ASSERT(currentPaddingSize >= aDecreaseSize);
       currentPaddingSize -= aDecreaseSize;
     }
+
+#ifdef DEBUG
+    int64_t paddingSizeFromDB = 0;
+    rv = db::FindOverallPaddingSize(aConn, &paddingSizeFromDB);
+    if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
+
+    MOZ_DIAGNOSTIC_ASSERT(paddingSizeFromDB == currentPaddingSize);
+#endif // DEBUG
   }
 
   MOZ_DIAGNOSTIC_ASSERT(currentPaddingSize >= 0);
 
   rv = LockedDirectoryPaddingTemporaryWrite(aBaseDir, currentPaddingSize);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   *aUpdatedOut = true;
@@ -877,17 +888,23 @@ LockedDirectoryPaddingRestore(nsIFile* a
   // The content of padding file is untrusted, so remove it here.
   nsresult rv = LockedDirectoryPaddingDeleteFile(aBaseDir,
                                                  DirPaddingFile::TMP_FILE);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   rv = LockedDirectoryPaddingDeleteFile(aBaseDir, DirPaddingFile::FILE);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
-  // XXXtt: will have a function to retore from db.
+  int64_t paddingSize = 0;
+  rv = db::FindOverallPaddingSize(aConn, &paddingSize);
+  if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
+
+  MOZ_DIAGNOSTIC_ASSERT(paddingSize >= 0);
+
+  LockedDirectoryPaddingWrite(aBaseDir, DirPaddingFile::FILE, paddingSize);
 
   return rv;
 }
 
 // static
 nsresult
 LockedDirectoryPaddingDeleteFile(nsIFile* aBaseDir,
                                  DirPaddingFile aPaddingFileType)