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

Unified Diff: components/data_reduction_proxy/core/browser/data_usage_storage_helper.cc

Issue 1173343009: LevelDB storage for data reduction proxy to store data usage stats. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 5 years, 6 months 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: components/data_reduction_proxy/core/browser/data_usage_storage_helper.cc
diff --git a/components/data_reduction_proxy/core/browser/data_usage_storage_helper.cc b/components/data_reduction_proxy/core/browser/data_usage_storage_helper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9424a62a90ad63fd82dcc5b4b6c9c8b52c0a8f46
--- /dev/null
+++ b/components/data_reduction_proxy/core/browser/data_usage_storage_helper.cc
@@ -0,0 +1,159 @@
+// Copyright 2015 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.
+
+// Each |DataUsageBucket| corresponds to data usage for an interval of
+// |kDataUsageBucketLengthMins| minutes. We store data usage for the past
+// |kNumDataUsageBuckets| buckets. Buckets are maintained as a circular array
+// with indexes from 0 to (kNumDataUsageBuckets - 1). To store the circular
+// array in a key-value store, we convert each index to a unique key. The latest
+// bucket persisted to DB overwrites the oldest.
+#include <stdlib.h>
+
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_store.h"
+#include "components/data_reduction_proxy/core/browser/data_usage_storage_helper.h"
+#include "components/data_reduction_proxy/proto/store.pb.h"
+
+namespace {
+const char kCurrentBucketIndexKey[] = "current_bucket_index";
+const char kBucketKeyPrefix[] = "data_usage_bucket:";
+
+const int kMinsInHour = 60;
+const int kMinutesInDay = 24 * kMinsInHour;
+
+// Time interval for each DataUsageBucket.
+const int kDataUsageBucketLengthMins = 15;
+
+// Number of days for which to maintain data usage history.
+const int kDataUsageHistoryNumDays = 60;
+
+// Total number of buckets persisted to DB.
+const int kNumDataUsageBuckets =
+ kDataUsageHistoryNumDays * kMinutesInDay / kDataUsageBucketLengthMins;
+}
+
+namespace data_reduction_proxy {
+
+DataUsageBucketWrapper::DataUsageBucketWrapper(DataUsageBucket* bucket) {
+ bucket_.reset(bucket);
+}
+
+DataUsageBucketWrapper::~DataUsageBucketWrapper() {
+}
+
+DataUsageStorageHelper::DataUsageStorageHelper(
+ scoped_refptr<DataReductionProxyStore> db)
+ : db_(db), current_bucket_index_(-1) {
+}
+
+DataUsageStorageHelper::~DataUsageStorageHelper() {
+}
+
+std::string DbKeyForBucketIndex(int index) {
+ return kBucketKeyPrefix + index;
+}
+
+void DataUsageStorageHelper::LoadCurrentDataUsageBucket(
+ scoped_refptr<DataUsageBucketWrapper> current) {
+ DCHECK(current->bucket() == nullptr);
+
+ std::string current_index_string;
+ DataReductionProxyStore::Status index_read_status =
+ db_->Get(kCurrentBucketIndexKey, current_index_string);
+ DCHECK(index_read_status == DataReductionProxyStore::Status::OK ||
+ index_read_status == DataReductionProxyStore::NOT_FOUND);
+
+ if (index_read_status == DataReductionProxyStore::Status::OK)
+ current_bucket_index_ = atoi(current_index_string.c_str());
+ else if (index_read_status == DataReductionProxyStore::NOT_FOUND)
+ current_bucket_index_ = 0;
+
+ DCHECK(current_bucket_index_ >= 0 &&
+ current_bucket_index_ < kNumDataUsageBuckets);
+
+ std::string bucket_key = DbKeyForBucketIndex(current_bucket_index_);
+ std::string bucket_as_string;
+ DataReductionProxyStore::Status bucket_read_status =
+ db_->Get(bucket_key, bucket_as_string);
+ DCHECK(bucket_read_status == DataReductionProxyStore::Status::OK ||
+ bucket_read_status == DataReductionProxyStore::NOT_FOUND);
+
+ scoped_ptr<DataUsageBucket> current_data_usage(new DataUsageBucket());
+ if (bucket_read_status == DataReductionProxyStore::Status::OK) {
+ DCHECK(current_data_usage->ParseFromString(bucket_as_string));
+ current_bucket_last_updated_ = base::Time::FromInternalValue(
+ current_data_usage->last_updated_timestamp());
+ }
+ current->set_bucket(current_data_usage.release());
+}
+
+void DataUsageStorageHelper::StoreCurrentDataUsageBucket(
+ scoped_refptr<DataUsageBucketWrapper> current) {
+ DCHECK(current_bucket_index_ >= 0 &&
+ current_bucket_index_ < kNumDataUsageBuckets);
+
+ // If current bucket does not have any information, we skip writing to DB.
+ if (!current->bucket()->has_last_updated_timestamp() ||
+ current->bucket()->last_updated_timestamp() == 0)
+ return;
+
+ // We might have skipped saving buckets because Chrome was not being used.
+ // Write empty buckets to those slots to overwrite outdated information.
+ base::Time last_updated = base::Time::FromInternalValue(
+ current->bucket()->last_updated_timestamp());
+ scoped_ptr<std::map<std::string, std::string>> buckets_to_save(
+ new std::map<std::string, std::string>());
+ int num_buckets_since_last_saved = NumBucketsSinceLastSaved(last_updated);
+ for (int i = 0; i < num_buckets_since_last_saved; i++) {
+ scoped_ptr<DataUsageBucket> bucket(new DataUsageBucket());
+ AddBucketToMap(buckets_to_save.get(), bucket.get());
+ }
+
+ AddBucketToMap(buckets_to_save.get(), current->bucket());
+
+ DataReductionProxyStore::Status status = db_->Put(buckets_to_save.get());
+ DCHECK(status == DataReductionProxyStore::Status::OK);
+}
+
+void DataUsageStorageHelper::AddBucketToMap(
+ std::map<std::string, std::string>* map,
+ DataUsageBucket* bucket) {
+ std::string bucket_key = DbKeyForBucketIndex(current_bucket_index_);
+
+ std::string bucket_value;
+ bool success = bucket->SerializeToString(&bucket_value);
+ DCHECK(success);
+
+ map->insert(std::pair<std::string, std::string>(bucket_key, bucket_value));
+
+ current_bucket_index_++;
+ DCHECK(current_bucket_index_ <= kNumDataUsageBuckets);
+ if (current_bucket_index_ == kNumDataUsageBuckets) {
+ current_bucket_index_ = 0;
+ }
+}
+
+base::Time BucketLowerBoundary(base::Time time) {
+ base::Time::Exploded exploded;
+ time.UTCExplode(&exploded);
+ exploded.minute = (exploded.minute / kDataUsageBucketLengthMins) *
+ kDataUsageBucketLengthMins;
+
+ return base::Time::FromUTCExploded(exploded);
+}
+
+int DataUsageStorageHelper::NumBucketsSinceLastSaved(
+ base::Time new_last_updated_timestamp) {
+ DCHECK(kMinsInHour % kDataUsageBucketLengthMins == 0);
+
+ if (current_bucket_last_updated_.is_null())
+ return 0;
+
+ base::TimeDelta time_delta =
+ BucketLowerBoundary(new_last_updated_timestamp) -
+ BucketLowerBoundary(current_bucket_last_updated_);
+ int num_buckets_skipped = time_delta.InMinutes() / kDataUsageBucketLengthMins;
+ return std::min(num_buckets_skipped, kNumDataUsageBuckets - 1);
+}
+
+} // namespace data_reduction_proxy

Powered by Google App Engine
This is Rietveld 408576698