Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(909)

Unified Diff: content/browser/cache_storage/cache_storage_index.cc

Issue 2416713002: Write out CacheStorageCache size to index file. (Closed)
Patch Set: Scheduling index writes + lots of other fixes. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/cache_storage/cache_storage_index.cc
diff --git a/content/browser/cache_storage/cache_storage_index.cc b/content/browser/cache_storage/cache_storage_index.cc
new file mode 100644
index 0000000000000000000000000000000000000000..40ae847d2bf456eebde68045247863ced38b4683
--- /dev/null
+++ b/content/browser/cache_storage/cache_storage_index.cc
@@ -0,0 +1,113 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/cache_storage/cache_storage_index.h"
+
+#include <utility>
+
+namespace content {
+
+CacheStorageIndex::CacheStorageIndex(const CacheStorageIndex& other) {
+ for (const auto& cache_metadata : other.ordered_cache_metadata_)
+ Insert(cache_metadata);
+}
+
+CacheStorageIndex::CacheStorageIndex() = default;
+
+CacheStorageIndex::~CacheStorageIndex() = default;
+
+CacheStorageIndex& CacheStorageIndex::operator=(CacheStorageIndex&& rhs) {
+ ordered_cache_metadata_ = std::move(rhs.ordered_cache_metadata_);
+ cache_metadata_map_ = std::move(rhs.cache_metadata_map_);
+ storage_size_ = rhs.storage_size_;
+ rhs.storage_size_ = CacheStorage::kSizeUnknown;
+ return *this;
+}
+
+CacheStorageIndex& CacheStorageIndex::operator=(const CacheStorageIndex& rhs) {
+ ordered_cache_metadata_.clear();
+ cache_metadata_map_.clear();
+ for (const auto& metadata : rhs.ordered_cache_metadata_)
+ Insert(metadata);
+ storage_size_ = rhs.storage_size_;
+ return *this;
+}
+
+void CacheStorageIndex::Insert(const CacheMetadata& cache_metadata) {
+ DCHECK(cache_metadata_map_.find(cache_metadata.name) ==
+ cache_metadata_map_.end());
+ ordered_cache_metadata_.push_back(cache_metadata);
+ cache_metadata_map_[cache_metadata.name] = --ordered_cache_metadata_.end();
+ storage_size_ = CacheStorage::kSizeUnknown;
+}
+
+void CacheStorageIndex::Delete(const std::string& cache_name) {
+ auto it = cache_metadata_map_.find(cache_name);
+ DCHECK(it != cache_metadata_map_.end());
+ ordered_cache_metadata_.erase(it->second);
+ cache_metadata_map_.erase(it);
+ storage_size_ = CacheStorage::kSizeUnknown;
+}
+
+// Sets the cache size. Returns true if the new size is different than the
+// current size else false.
+bool CacheStorageIndex::SetCacheSize(const std::string& cache_name,
+ int64_t size) {
+ auto it = cache_metadata_map_.find(cache_name);
+ if (it == cache_metadata_map_.end()) {
+ // Deleted (but still referenced) caches can still run.
jkarlin 2016/12/01 17:43:31 Hmm, this means that we might be getting a size up
cmumford 2016/12/05 17:19:02 Agreed. I added a check in CacheStorage::CacheSize
+ return false;
+ }
+ if (it->second->size == size)
+ return false;
+ it->second->size = size;
+ storage_size_ = CacheStorage::kSizeUnknown;
+ return true;
+}
+
+// The cache was modified, increasing/decreasing cache size by |size_delta|.
+void CacheStorageIndex::SetCacheSizeModified(const std::string& cache_name,
jkarlin 2016/12/01 17:43:31 Is this still called?
cmumford 2016/12/05 17:19:02 Only used by a lingering test - deleted it (and th
+ int64_t size_delta) {
+ DCHECK_NE(size_delta, 0);
+
+ auto it = cache_metadata_map_.find(cache_name);
+ if (it == cache_metadata_map_.end()) {
+ // Deleted (but still referenced) caches can still run.
+ return;
+ }
+ DCHECK_NE(it->second->size, CacheStorage::kSizeUnknown);
+ it->second->size += size_delta;
+ DCHECK_GE(it->second->size, 0U);
+ if (storage_size_ == CacheStorage::kSizeUnknown)
+ return;
+ DCHECK_NE(storage_size_, CacheStorage::kSizeUnknown);
+ storage_size_ += size_delta;
+ DCHECK_GE(storage_size_, 0U);
+}
+
+int64_t CacheStorageIndex::GetCacheSize(const std::string& cache_name) const {
+ const auto& it = cache_metadata_map_.find(cache_name);
+ if (it == cache_metadata_map_.end())
+ return CacheStorage::kSizeUnknown;
+ return it->second->size;
+}
+
+int64_t CacheStorageIndex::GetStorageSize() {
+ if (storage_size_ == CacheStorage::kSizeUnknown)
+ UpdateStorageSize();
+ return storage_size_;
+}
+
+void CacheStorageIndex::UpdateStorageSize() {
+ int64_t storage_size = 0;
+ storage_size_ = CacheStorage::kSizeUnknown;
+ for (const CacheMetadata& info : ordered_cache_metadata_) {
+ if (info.size == CacheStorage::kSizeUnknown)
+ return;
+ storage_size += info.size;
+ }
+ storage_size_ = storage_size;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698