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

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

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.h
diff --git a/components/data_reduction_proxy/core/browser/data_usage_storage_helper.h b/components/data_reduction_proxy/core/browser/data_usage_storage_helper.h
new file mode 100644
index 0000000000000000000000000000000000000000..aa73f51da3addf2ca175aea7d25cf0adc5d2221d
--- /dev/null
+++ b/components/data_reduction_proxy/core/browser/data_usage_storage_helper.h
@@ -0,0 +1,89 @@
+// 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.
+
+#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_USAGE_STORAGE_HELPER_H_
+#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_USAGE_STORAGE_HELPER_H_
+
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+#include "base/threading/thread_checker.h"
+#include "base/time/time.h"
+
+namespace data_reduction_proxy {
+class DataReductionProxyStore;
+class DataUsageBucket;
+
+// Refcounted wrapper around DataUsageBucket. Using ref counting allows us to
+// pass DataUsageBucket across threads.
+class DataUsageBucketWrapper
+ : base::RefCountedThreadSafe<DataUsageBucketWrapper> {
+ public:
+ DataUsageBucketWrapper(DataUsageBucket* bucket);
+
+ inline DataUsageBucket* bucket() { return bucket_.get(); }
+
+ inline void set_bucket(DataUsageBucket* bucket) {
+ DCHECK(bucket_.get() == nullptr);
+ bucket_.reset(bucket);
+ }
+
+ private:
+ ~DataUsageBucketWrapper();
+
+ scoped_ptr<DataUsageBucket> bucket_;
+};
+
+// Store for detailed data usage stats. Data usage from every
+// |kDataUsageBucketLengthMins| interval is stored in a DataUsageBucket.
+class DataUsageStorageHelper
+ : public base::SupportsWeakPtr<DataUsageStorageHelper> {
+ public:
+ DataUsageStorageHelper(scoped_refptr<DataReductionProxyStore> db);
+ ~DataUsageStorageHelper();
+
+ // Loads the data usage bucket for the current interval. This method must be
+ // called atleast once before any calls to |StoreCurrentDataUsageBucket|.
+ void LoadCurrentDataUsageBucket(
+ scoped_refptr<DataUsageBucketWrapper> current_bucket);
+
+ // Stores the data usage bucket for the current interval. This will overwrite
+ // the current data usage bucket in the DB if they are for the same interval.
+ // It will also backfill any missed intervals with empty data. Intervals might
+ // be missed because Chrome was not running, or there was no network activity
+ // during an interval.
+ void StoreCurrentDataUsageBucket(
+ scoped_refptr<DataUsageBucketWrapper> current_bucket);
+
+ private:
+ // Converts the given |bucket| into a string format for persistance to
+ // |DataReductionProxyStore| and add to the map.
+ void AddBucketToMap(std::map<std::string, std::string>* map,
+ DataUsageBucket* bucket);
+
+ // Returns the number of buckets between the current interval bucket and the
+ // last bucket that was persisted to the store.
+ int NumBucketsSinceLastSaved(base::Time current);
+
+ // The store to persist data usage information.
+ scoped_refptr<DataReductionProxyStore> db_;
+
+ // The index of the last bucket persisted in the DB. |DataUsageBucket| is
+ // stored in the DB as a circular array. This index points to the array
+ // position corresponding to the current bucket.
+ int current_bucket_index_;
+
+ // The time when the current bucket was last written to DB. This field is
+ // used to determine if a DataUsageBucket to be saved belongs to the same
+ // interval, or a more recent interval.
+ base::Time current_bucket_last_updated_;
+
+ base::ThreadChecker thread_checker_;
+
+ DISALLOW_COPY_AND_ASSIGN(DataUsageStorageHelper);
+};
+
+} // namespace data_reduction_proxy
+#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_USAGE_STORAGE_HELPER_H_

Powered by Google App Engine
This is Rietveld 408576698