Chromium Code Reviews| 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..7edb6fd4340a423828971d2f970c452936e169b9 |
| --- /dev/null |
| +++ b/components/data_reduction_proxy/core/browser/data_usage_storage_helper.cc |
| @@ -0,0 +1,188 @@ |
| +// 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 "components/data_reduction_proxy/core/browser/data_usage_storage_helper.h" |
| + |
| +#include <stdlib.h> |
| + |
| +#include <algorithm> |
| +#include <map> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/strings/stringprintf.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_store.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 kMinutesInHour = 60; |
| +const int kMinutesInDay = 24 * kMinutesInHour; |
| + |
| +// Time interval for each DataUsageBucket. |
| +const int kDataUsageBucketLengthInMinutes = 15; |
| +static_assert(kDataUsageBucketLengthInMinutes > 0, |
| + "Length of time should be positive"); |
| +static_assert(kMinutesInHour % kDataUsageBucketLengthInMinutes == 0, |
| + "kDataUsageBucketLengthMins must be a factor of kMinsInHour"); |
| + |
| +// 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 / kDataUsageBucketLengthInMinutes; |
| + |
| +std::string DbKeyForBucketIndex(int index) { |
| + DCHECK_GE(index, 0); |
| + DCHECK_LT(index, kNumDataUsageBuckets); |
| + |
| + return base::StringPrintf("%s%d", kBucketKeyPrefix, index); |
| +} |
| + |
| +base::Time BucketLowerBoundary(base::Time time) { |
| + base::Time::Exploded exploded; |
| + time.UTCExplode(&exploded); |
| + exploded.minute -= exploded.minute % kDataUsageBucketLengthInMinutes; |
| + |
| + return base::Time::FromUTCExploded(exploded); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace data_reduction_proxy { |
| + |
| +DataUsageStorageHelper::DataUsageStorageHelper( |
| + scoped_refptr<DataReductionProxyStore> db) |
| + : db_(db), current_bucket_index_(-1) { |
| + thread_checker_.DetachFromThread(); |
| +} |
| + |
| +DataUsageStorageHelper::~DataUsageStorageHelper() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| +} |
| + |
| +scoped_ptr<DataUsageBucket> |
| +DataUsageStorageHelper::LoadCurrentDataUsageBucket() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + scoped_ptr<DataUsageBucket> current(new DataUsageBucket()); |
| + |
| + std::string current_index_string; |
| + DataReductionProxyStore::Status index_read_status = |
| + db_->Get(kCurrentBucketIndexKey, ¤t_index_string); |
| + |
| + if (index_read_status == DataReductionProxyStore::Status::OK) |
| + current_bucket_index_ = atoi(current_index_string.c_str()); |
| + else |
| + current_bucket_index_ = 0; |
| + |
| + DCHECK_GE(current_bucket_index_, 0); |
| + DCHECK_LT(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); |
| + if ((bucket_read_status != DataReductionProxyStore::Status::OK && |
| + bucket_read_status != DataReductionProxyStore::NOT_FOUND)) { |
| + LOG(WARNING) << "Failed to read data usage bucket from LevelDB: " |
| + << bucket_read_status; |
| + } |
| + |
| + if (bucket_read_status == DataReductionProxyStore::Status::OK) { |
| + DCHECK(current->ParseFromString(bucket_as_string)); |
| + current_bucket_last_updated_ = |
| + base::Time::FromInternalValue(current->last_updated_timestamp()); |
| + } |
| + return current.Pass(); |
| +} |
| + |
| +void DataUsageStorageHelper::StoreCurrentDataUsageBucket( |
| + const DataUsageBucket* current) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(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->has_last_updated_timestamp()) |
| + return; |
| + |
| + int prev_current_bucket_index = current_bucket_index_; |
| + base::Time prev_current_bucket_last_updated_ = current_bucket_last_updated_; |
| + // 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->last_updated_timestamp()); |
| + std::map<std::string, std::string> buckets_to_save; |
| + int num_buckets_since_last_saved = NumBucketsSinceLastSaved(last_updated); |
| + for (int i = 0; i < num_buckets_since_last_saved; ++i) { |
| + DataUsageBucket bucket; |
| + AddBucketToMap(&bucket, &buckets_to_save); |
| + |
| + current_bucket_index_++; |
| + DCHECK(current_bucket_index_ <= kNumDataUsageBuckets); |
| + if (current_bucket_index_ == kNumDataUsageBuckets) |
| + current_bucket_index_ = 0; |
| + } |
| + |
| + AddBucketToMap(current, &buckets_to_save); |
| + current_bucket_last_updated_ = |
| + base::Time::FromInternalValue(current->last_updated_timestamp()); |
| + |
| + std::stringstream current_index_string; |
| + current_index_string << current_bucket_index_; |
| + buckets_to_save.insert(std::pair<std::string, std::string>( |
| + kCurrentBucketIndexKey, current_index_string.str())); |
| + |
| + DataReductionProxyStore::Status status = db_->Put(buckets_to_save); |
| + if (status != DataReductionProxyStore::Status::OK) { |
| + current_bucket_index_ = prev_current_bucket_index; |
| + current_bucket_last_updated_ = prev_current_bucket_last_updated_; |
| + LOG(WARNING) << "Failed to write data usage buckets to LevelDB" << status; |
| + } |
| +} |
| + |
| +void DataUsageStorageHelper::AddBucketToMap( |
| + const DataUsageBucket* bucket, |
| + std::map<std::string, std::string>* map) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
|
jeremyim
2015/06/30 01:59:19
Not necessary, since it's a private method called
Not at Google. Contact bengr
2015/07/01 17:13:49
Done.
|
| + |
| + std::string bucket_key = DbKeyForBucketIndex(current_bucket_index_); |
| + |
| + std::string bucket_value; |
| + bool success = bucket->SerializeToString(&bucket_value); |
| + |
| + DataUsageBucket test; |
| + DCHECK(test.ParseFromString(bucket_value)); |
| + |
| + DCHECK(success); |
| + |
| + map->insert(std::pair<std::string, std::string>(bucket_key, bucket_value)); |
| +} |
| + |
| +int DataUsageStorageHelper::NumBucketsSinceLastSaved( |
| + base::Time new_last_updated_timestamp) const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
|
jeremyim
2015/06/30 01:59:18
DCHECK unnecessary
Not at Google. Contact bengr
2015/07/01 17:13:49
Done.
|
| + |
| + 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() / kDataUsageBucketLengthInMinutes; |
| + return std::min(num_buckets_skipped, kNumDataUsageBuckets - 1); |
| +} |
| + |
| +} // namespace data_reduction_proxy |