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

Side by Side Diff: chrome/browser/budget_service/budget_database.cc

Issue 2233783002: Cleanup budget_database based on evolving API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/budget_service/budget_database.h" 5 #include "chrome/browser/budget_service/budget_database.h"
6 6
7 #include "base/containers/adapters.h" 7 #include "base/containers/adapters.h"
8 #include "base/time/clock.h" 8 #include "base/time/clock.h"
9 #include "base/time/default_clock.h" 9 #include "base/time/default_clock.h"
10 #include "chrome/browser/budget_service/budget.pb.h" 10 #include "chrome/browser/budget_service/budget.pb.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 } 65 }
66 66
67 void BudgetDatabase::AddBudget(const GURL& origin, 67 void BudgetDatabase::AddBudget(const GURL& origin,
68 double amount, 68 double amount,
69 const StoreBudgetCallback& callback) { 69 const StoreBudgetCallback& callback) {
70 DCHECK_EQ(origin.GetOrigin(), origin); 70 DCHECK_EQ(origin.GetOrigin(), origin);
71 71
72 // Add a new chunk of budget for the origin at the default expiration time. 72 // Add a new chunk of budget for the origin at the default expiration time.
73 base::Time expiration = 73 base::Time expiration =
74 clock_->Now() + base::TimeDelta::FromHours(kBudgetDurationInHours); 74 clock_->Now() + base::TimeDelta::FromHours(kBudgetDurationInHours);
75 budget_map_[origin.spec()].second.push_back( 75 budget_map_[origin.spec()].push_back(BudgetChunk(amount, expiration));
Peter Beverloo 2016/08/10 15:36:44 nit: could rewrite this as: budget_map_[origin.sp
harkness 2016/08/10 16:41:54 Done.
76 std::make_pair(amount, expiration));
77 76
78 // Now that the cache is updated, write the data to the database. 77 // Now that the cache is updated, write the data to the database.
79 WriteCachedValuesToDatabase(origin, callback); 78 WriteCachedValuesToDatabase(origin, callback);
80 } 79 }
81 80
82 void BudgetDatabase::OnDatabaseInit(bool success) { 81 void BudgetDatabase::OnDatabaseInit(bool success) {
83 // TODO(harkness): Consider caching the budget database now? 82 // TODO(harkness): Consider caching the budget database now?
84 } 83 }
85 84
86 bool BudgetDatabase::IsCached(const GURL& origin) const { 85 bool BudgetDatabase::IsCached(const GURL& origin) const {
87 return budget_map_.find(origin.spec()) != budget_map_.end(); 86 return budget_map_.find(origin.spec()) != budget_map_.end();
88 } 87 }
89 88
90 void BudgetDatabase::AddToCache( 89 void BudgetDatabase::AddToCache(
91 const GURL& origin, 90 const GURL& origin,
92 const AddToCacheCallback& callback, 91 const AddToCacheCallback& callback,
93 bool success, 92 bool success,
94 std::unique_ptr<budget_service::Budget> budget_proto) { 93 std::unique_ptr<budget_service::Budget> budget_proto) {
95 // If the database read failed, there's nothing to add to the cache. 94 // If the database read failed, there's nothing to add to the cache.
96 if (!success || !budget_proto) { 95 if (!success || !budget_proto) {
97 callback.Run(success); 96 callback.Run(success);
98 return; 97 return;
99 } 98 }
100 99
101 // Add the data to the cache, converting from the proto format to an STL 100 // Add the data to the cache, converting from the proto format to an STL
102 // format which is better for removing things from the list. 101 // format which is better for removing things from the list.
103 BudgetChunks chunks; 102 BudgetChunks chunks;
104 for (const auto& chunk : budget_proto->budget()) 103 for (const auto& chunk : budget_proto->budget())
105 chunks.push_back(std::make_pair( 104 chunks.push_back(BudgetChunk(
106 chunk.amount(), base::Time::FromInternalValue(chunk.expiration()))); 105 chunk.amount(), base::Time::FromInternalValue(chunk.expiration())));
Peter Beverloo 2016/08/10 15:36:44 micro nit: {} since this no longer is a single-lin
harkness 2016/08/10 16:41:54 Done.
107 106
108 DCHECK(budget_proto->has_debt()); 107 budget_map_[origin.spec()] = std::move(chunks);
109 budget_map_[origin.spec()] =
110 std::make_pair(budget_proto->debt(), std::move(chunks));
111 108
112 callback.Run(success); 109 callback.Run(success);
113 } 110 }
114 111
115 void BudgetDatabase::DidGetBudget(const GURL& origin, 112 void BudgetDatabase::DidGetBudget(const GURL& origin,
116 const GetBudgetDetailsCallback& callback, 113 const GetBudgetDetailsCallback& callback,
117 bool success) { 114 bool success) {
118 // If the database wasn't able to read the information, return the 115 // If the database wasn't able to read the information, return the
119 // failure and an empty BudgetExpectation. 116 // failure and an empty BudgetExpectation.
120 if (!success) { 117 if (!success) {
121 callback.Run(success, 0, BudgetExpectation()); 118 callback.Run(success, BudgetExpectation());
122 return; 119 return;
123 } 120 }
124 121
125 // First, cleanup any expired budget chunks for the origin. 122 // First, cleanup any expired budget chunks for the origin.
126 CleanupExpiredBudget(origin); 123 CleanupExpiredBudget(origin);
127 124
128 // Now, build up the BudgetExpection. This is different from the format 125 // Now, build up the BudgetExpection. This is different from the format
129 // in which the cache stores the data. The cache stores chunks of budget and 126 // in which the cache stores the data. The cache stores chunks of budget and
130 // when that budget expires. The BudgetExpectation describes a set of times 127 // when that budget expires. The BudgetExpectation describes a set of times
131 // and the budget at those times. 128 // and the budget at those times.
132 BudgetExpectation expectation; 129 BudgetExpectation expectation;
133 double total = 0; 130 double total = 0;
134 double debt = 0;
135 131
136 if (IsCached(origin)) { 132 if (IsCached(origin)) {
137 // Starting with the chunks that expire the farthest in the future, build up 133 // Starting with the chunks that expire the farthest in the future, build up
138 // the budget expectations for those future times. 134 // the budget expectations for those future times.
139 const BudgetInfo& info = budget_map_[origin.spec()]; 135 const BudgetChunks& chunks = budget_map_[origin.spec()];
140 for (const auto& chunk : base::Reversed(info.second)) { 136 for (const auto& chunk : base::Reversed(chunks)) {
141 expectation.push_front(std::make_pair(total, chunk.second)); 137 expectation.push_front(BudgetStatus(total, chunk.expiration));
Peter Beverloo 2016/08/10 15:36:44 dito, but with emplace_front() instead. Also on li
harkness 2016/08/10 16:41:55 Done.
142 total += chunk.first; 138 total += chunk.amount;
143 } 139 }
144 debt = info.first;
145 } 140 }
146 141
147 // Always add one entry at the front of the list for the total budget now. 142 // Always add one entry at the front of the list for the total budget now.
148 expectation.push_front(std::make_pair(total, clock_->Now())); 143 expectation.push_front(BudgetStatus(total, clock_->Now()));
149 144
150 callback.Run(true /* success */, debt, expectation); 145 callback.Run(true /* success */, expectation);
151 } 146 }
152 147
153 void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) { 148 void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) {
154 clock_ = std::move(clock); 149 clock_ = std::move(clock);
155 } 150 }
156 151
157 void BudgetDatabase::WriteCachedValuesToDatabase( 152 void BudgetDatabase::WriteCachedValuesToDatabase(
158 const GURL& origin, 153 const GURL& origin,
159 const StoreBudgetCallback& callback) { 154 const StoreBudgetCallback& callback) {
160 // First, cleanup any expired budget chunks for the origin. 155 // First, cleanup any expired budget chunks for the origin.
161 CleanupExpiredBudget(origin); 156 CleanupExpiredBudget(origin);
162 157
163 // Create the data structures that are passed to the ProtoDatabase. 158 // Create the data structures that are passed to the ProtoDatabase.
164 std::unique_ptr< 159 std::unique_ptr<
165 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> 160 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector>
166 entries(new leveldb_proto::ProtoDatabase< 161 entries(new leveldb_proto::ProtoDatabase<
167 budget_service::Budget>::KeyEntryVector()); 162 budget_service::Budget>::KeyEntryVector());
168 std::unique_ptr<std::vector<std::string>> keys_to_remove( 163 std::unique_ptr<std::vector<std::string>> keys_to_remove(
169 new std::vector<std::string>()); 164 new std::vector<std::string>());
170 165
171 // Each operation can either update the existing budget or remove the origin's 166 // Each operation can either update the existing budget or remove the origin's
172 // budget information. 167 // budget information.
173 if (IsCached(origin)) { 168 if (IsCached(origin)) {
174 // Build the Budget proto object. 169 // Build the Budget proto object.
175 budget_service::Budget budget; 170 budget_service::Budget budget;
176 const BudgetInfo& info = budget_map_[origin.spec()]; 171 const BudgetChunks& chunks = budget_map_[origin.spec()];
177 budget.set_debt(info.first); 172 for (const auto& chunk : chunks) {
178 for (const auto& chunk : info.second) {
179 budget_service::BudgetChunk* budget_chunk = budget.add_budget(); 173 budget_service::BudgetChunk* budget_chunk = budget.add_budget();
180 budget_chunk->set_amount(chunk.first); 174 budget_chunk->set_amount(chunk.amount);
181 budget_chunk->set_expiration(chunk.second.ToInternalValue()); 175 budget_chunk->set_expiration(chunk.expiration.ToInternalValue());
182 } 176 }
183 entries->push_back(std::make_pair(origin.spec(), budget)); 177 entries->push_back(std::make_pair(origin.spec(), budget));
184 } else { 178 } else {
185 // If the origin doesn't exist in the cache, this is a remove operation. 179 // If the origin doesn't exist in the cache, this is a remove operation.
186 keys_to_remove->push_back(origin.spec()); 180 keys_to_remove->push_back(origin.spec());
187 } 181 }
188 182
189 // Send the updates to the database. 183 // Send the updates to the database.
190 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); 184 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback);
191 } 185 }
192 186
193 void BudgetDatabase::CleanupExpiredBudget(const GURL& origin) { 187 void BudgetDatabase::CleanupExpiredBudget(const GURL& origin) {
194 if (!IsCached(origin)) 188 if (!IsCached(origin))
195 return; 189 return;
196 190
197 base::Time now = clock_->Now(); 191 base::Time now = clock_->Now();
198 192
199 BudgetChunks& chunks = budget_map_[origin.spec()].second; 193 BudgetChunks& chunks = budget_map_[origin.spec()];
200 auto cleanup_iter = chunks.begin(); 194 auto cleanup_iter = chunks.begin();
201 195
202 // This relies on the list of chunks being in timestamp order. 196 // This relies on the list of chunks being in timestamp order.
203 while (cleanup_iter != chunks.end() && cleanup_iter->second <= now) 197 while (cleanup_iter != chunks.end() && cleanup_iter->expiration <= now)
204 cleanup_iter = chunks.erase(cleanup_iter); 198 cleanup_iter = chunks.erase(cleanup_iter);
205 199
206 // If the entire budget is empty now, cleanup the map. 200 // If the entire budget is empty now, cleanup the map.
207 if (chunks.empty()) 201 if (chunks.empty())
208 budget_map_.erase(origin.spec()); 202 budget_map_.erase(origin.spec());
209 } 203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698