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

Side by Side Diff: chrome/browser/history/download_database.cc

Issue 9071014: Database usage adjustment for .../history (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/history/download_database.h" 5 #include "chrome/browser/history/download_database.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 void DownloadDatabase::QueryDownloads( 108 void DownloadDatabase::QueryDownloads(
109 std::vector<DownloadPersistentStoreInfo>* results) { 109 std::vector<DownloadPersistentStoreInfo>* results) {
110 CheckThread(); 110 CheckThread();
111 results->clear(); 111 results->clear();
112 112
113 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 113 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
114 "SELECT id, full_path, url, start_time, received_bytes, " 114 "SELECT id, full_path, url, start_time, received_bytes, "
115 "total_bytes, state, end_time, opened " 115 "total_bytes, state, end_time, opened "
116 "FROM downloads " 116 "FROM downloads "
117 "ORDER BY start_time")); 117 "ORDER BY start_time"));
118 if (!statement)
119 return;
120 118
121 while (statement.Step()) { 119 while (statement.Step()) {
122 DownloadPersistentStoreInfo info; 120 DownloadPersistentStoreInfo info;
123 info.db_handle = statement.ColumnInt64(0); 121 info.db_handle = statement.ColumnInt64(0);
124 122
125 info.path = ColumnFilePath(statement, 1); 123 info.path = ColumnFilePath(statement, 1);
126 info.url = GURL(statement.ColumnString(2)); 124 info.url = GURL(statement.ColumnString(2));
127 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); 125 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3));
128 info.received_bytes = statement.ColumnInt64(4); 126 info.received_bytes = statement.ColumnInt64(4);
129 info.total_bytes = statement.ColumnInt64(5); 127 info.total_bytes = statement.ColumnInt64(5);
(...skipping 11 matching lines...) Expand all
141 returned_ids_.insert(info.db_handle); 139 returned_ids_.insert(info.db_handle);
142 } 140 }
143 } 141 }
144 142
145 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) { 143 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) {
146 CheckThread(); 144 CheckThread();
147 DCHECK(data.db_handle > 0); 145 DCHECK(data.db_handle > 0);
148 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 146 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
149 "UPDATE downloads " 147 "UPDATE downloads "
150 "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?")); 148 "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?"));
151 if (!statement)
152 return false;
153
154 statement.BindInt64(0, data.received_bytes); 149 statement.BindInt64(0, data.received_bytes);
155 statement.BindInt(1, data.state); 150 statement.BindInt(1, data.state);
156 statement.BindInt64(2, data.end_time.ToTimeT()); 151 statement.BindInt64(2, data.end_time.ToTimeT());
157 statement.BindInt(3, (data.opened ? 1 : 0)); 152 statement.BindInt(3, (data.opened ? 1 : 0));
158 statement.BindInt64(4, data.db_handle); 153 statement.BindInt64(4, data.db_handle);
154
159 return statement.Run(); 155 return statement.Run();
160 } 156 }
161 157
162 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, 158 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path,
163 DownloadID db_handle) { 159 DownloadID db_handle) {
164 CheckThread(); 160 CheckThread();
165 DCHECK(db_handle > 0); 161 DCHECK(db_handle > 0);
166 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 162 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
167 "UPDATE downloads SET full_path=? WHERE id=?")); 163 "UPDATE downloads SET full_path=? WHERE id=?"));
168 if (!statement)
169 return false;
170
171 BindFilePath(statement, path, 0); 164 BindFilePath(statement, path, 0);
172 statement.BindInt64(1, db_handle); 165 statement.BindInt64(1, db_handle);
166
173 return statement.Run(); 167 return statement.Run();
174 } 168 }
175 169
176 bool DownloadDatabase::CleanUpInProgressEntries() { 170 bool DownloadDatabase::CleanUpInProgressEntries() {
177 CheckThread(); 171 CheckThread();
178 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 172 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
179 "UPDATE downloads SET state=? WHERE state=?")); 173 "UPDATE downloads SET state=? WHERE state=?"));
180 if (!statement)
181 return false;
182 statement.BindInt(0, DownloadItem::CANCELLED); 174 statement.BindInt(0, DownloadItem::CANCELLED);
183 statement.BindInt(1, DownloadItem::IN_PROGRESS); 175 statement.BindInt(1, DownloadItem::IN_PROGRESS);
176
184 return statement.Run(); 177 return statement.Run();
185 } 178 }
186 179
187 int64 DownloadDatabase::CreateDownload( 180 int64 DownloadDatabase::CreateDownload(
188 const DownloadPersistentStoreInfo& info) { 181 const DownloadPersistentStoreInfo& info) {
189 CheckThread(); 182 CheckThread();
190 183
191 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 184 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
192 "INSERT INTO downloads " 185 "INSERT INTO downloads "
193 "(full_path, url, start_time, received_bytes, total_bytes, state, " 186 "(full_path, url, start_time, received_bytes, total_bytes, state, "
194 "end_time, opened) " 187 "end_time, opened) "
195 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)")); 188 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"));
196 if (!statement)
197 return 0;
198
199 BindFilePath(statement, info.path, 0); 189 BindFilePath(statement, info.path, 0);
200 statement.BindString(1, info.url.spec()); 190 statement.BindString(1, info.url.spec());
201 statement.BindInt64(2, info.start_time.ToTimeT()); 191 statement.BindInt64(2, info.start_time.ToTimeT());
202 statement.BindInt64(3, info.received_bytes); 192 statement.BindInt64(3, info.received_bytes);
203 statement.BindInt64(4, info.total_bytes); 193 statement.BindInt64(4, info.total_bytes);
204 statement.BindInt(5, info.state); 194 statement.BindInt(5, info.state);
205 statement.BindInt64(6, info.end_time.ToTimeT()); 195 statement.BindInt64(6, info.end_time.ToTimeT());
206 statement.BindInt(7, info.opened ? 1 : 0); 196 statement.BindInt(7, info.opened ? 1 : 0);
207 197
208 if (statement.Run()) { 198 if (statement.Run()) {
209 int64 id = GetDB().GetLastInsertRowId(); 199 int64 id = GetDB().GetLastInsertRowId();
210 200
211 // TODO(rdsmith): Remove when http://crbug.com/96627 is resolved. 201 // TODO(rdsmith): Remove when http://crbug.com/96627 is resolved.
212 if (returned_ids_.count(id) != 0) { 202 if (returned_ids_.count(id) != 0) {
213 // We have an invariant violation and we're going to crash. Take a 203 // We have an invariant violation and we're going to crash. Take a
214 // moment more before crashing to figure out if it's a returned_ids_/DB 204 // moment more before crashing to figure out if it's a returned_ids_/DB
215 // inconsistency, or an inconsistency inside the DB. 205 // inconsistency, or an inconsistency inside the DB.
216 sql::Statement dbg_statement(GetDB().GetCachedStatement( 206 sql::Statement dbg_statement(GetDB().GetCachedStatement(
217 SQL_FROM_HERE, 207 SQL_FROM_HERE,
218 "SELECT id FROM downloads;")); 208 "SELECT id FROM downloads;"));
219 CHECK(dbg_statement); 209 CHECK(dbg_statement.is_valid());
220 210
221 std::set<int64> database_ids; 211 std::set<int64> database_ids;
222 while (dbg_statement.Step()) { 212 while (dbg_statement.Step()) {
223 bool success = database_ids.insert(dbg_statement.ColumnInt64(0)).second; 213 bool success = database_ids.insert(dbg_statement.ColumnInt64(0)).second;
224 CHECK(success); 214 CHECK(success);
225 } 215 }
226 CHECK(false); 216 CHECK(false);
227 } 217 }
228 218
229 returned_ids_.insert(id); 219 returned_ids_.insert(id);
230 220
231 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} 221 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;}
232 meta_table_.SetValue(kNextDownloadId, ++next_id_); 222 meta_table_.SetValue(kNextDownloadId, ++next_id_);
233 223
234 return id; 224 return id;
235 } 225 }
236 return 0; 226 return 0;
237 } 227 }
238 228
239 void DownloadDatabase::RemoveDownload(DownloadID db_handle) { 229 void DownloadDatabase::RemoveDownload(DownloadID db_handle) {
240 CheckThread(); 230 CheckThread();
241 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 231 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
242 "DELETE FROM downloads WHERE id=?")); 232 "DELETE FROM downloads WHERE id=?"));
243 if (!statement) 233 statement.BindInt64(0, db_handle);
234
235 // TODO(rdsmith): Remove when http://crbug.com/96627 is resolved.
Scott Hess - ex-Googler 2012/01/03 23:41:57 The target of this TODO seems ambiguous, now. May
Greg Billock 2012/01/04 19:20:20 I think I misplaced it. Should be by the erase I t
Randy Smith (Not in Mondays) 2012/01/04 19:34:18 Yes, it should.
236 if (!statement.Run())
244 return; 237 return;
245 238
246 statement.BindInt64(0, db_handle); 239 returned_ids_.erase(db_handle);
247 if (statement.Run())
248 // TODO(rdsmith): Remove when http://crbug.com/96627 is resolved.
249 returned_ids_.erase(db_handle);
250 } 240 }
251 241
252 void DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin, 242 bool DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin,
253 base::Time delete_end) { 243 base::Time delete_end) {
254 CheckThread(); 244 CheckThread();
255 time_t start_time = delete_begin.ToTimeT(); 245 time_t start_time = delete_begin.ToTimeT();
256 time_t end_time = delete_end.ToTimeT(); 246 time_t end_time = delete_end.ToTimeT();
257 247
258 // TODO(rdsmith): Remove when http://crbug.com/96627 is resolved. 248 // TODO(rdsmith): Remove when http://crbug.com/96627 is resolved.
259 { 249 {
260 sql::Statement dbg_statement(GetDB().GetCachedStatement( 250 sql::Statement dbg_statement(GetDB().GetCachedStatement(
261 SQL_FROM_HERE, 251 SQL_FROM_HERE,
262 "SELECT id FROM downloads WHERE start_time >= ? AND start_time < ? " 252 "SELECT id FROM downloads WHERE start_time >= ? AND start_time < ? "
263 "AND (State = ? OR State = ? OR State = ?)")); 253 "AND (State = ? OR State = ? OR State = ?)"));
264 if (!dbg_statement)
265 return;
266 dbg_statement.BindInt64(0, start_time); 254 dbg_statement.BindInt64(0, start_time);
267 dbg_statement.BindInt64( 255 dbg_statement.BindInt64(
268 1, 256 1,
269 end_time ? end_time : std::numeric_limits<int64>::max()); 257 end_time ? end_time : std::numeric_limits<int64>::max());
270 dbg_statement.BindInt(2, DownloadItem::COMPLETE); 258 dbg_statement.BindInt(2, DownloadItem::COMPLETE);
271 dbg_statement.BindInt(3, DownloadItem::CANCELLED); 259 dbg_statement.BindInt(3, DownloadItem::CANCELLED);
272 dbg_statement.BindInt(4, DownloadItem::INTERRUPTED); 260 dbg_statement.BindInt(4, DownloadItem::INTERRUPTED);
261
262 if (!dbg_statement.is_valid())
263 return false;
264
273 while (dbg_statement.Step()) { 265 while (dbg_statement.Step()) {
274 int64 id_to_delete = dbg_statement.ColumnInt64(0); 266 int64 id_to_delete = dbg_statement.ColumnInt64(0);
275 returned_ids_.erase(id_to_delete); 267 returned_ids_.erase(id_to_delete);
276 } 268 }
277 CHECK(dbg_statement.Succeeded()); 269 CHECK(dbg_statement.Succeeded());
278 } 270 }
279 271
280 // This does not use an index. We currently aren't likely to have enough 272 // This does not use an index. We currently aren't likely to have enough
281 // downloads where an index by time will give us a lot of benefit. 273 // downloads where an index by time will give us a lot of benefit.
282 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 274 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
283 "DELETE FROM downloads WHERE start_time >= ? AND start_time < ? " 275 "DELETE FROM downloads WHERE start_time >= ? AND start_time < ? "
284 "AND (State = ? OR State = ? OR State = ?)")); 276 "AND (State = ? OR State = ? OR State = ?)"));
285 if (!statement)
286 return;
287
288 statement.BindInt64(0, start_time); 277 statement.BindInt64(0, start_time);
289 statement.BindInt64( 278 statement.BindInt64(
290 1, 279 1,
291 end_time ? end_time : std::numeric_limits<int64>::max()); 280 end_time ? end_time : std::numeric_limits<int64>::max());
292 statement.BindInt(2, DownloadItem::COMPLETE); 281 statement.BindInt(2, DownloadItem::COMPLETE);
293 statement.BindInt(3, DownloadItem::CANCELLED); 282 statement.BindInt(3, DownloadItem::CANCELLED);
294 statement.BindInt(4, DownloadItem::INTERRUPTED); 283 statement.BindInt(4, DownloadItem::INTERRUPTED);
295 statement.Run(); 284
285 return statement.Run();
296 } 286 }
297 287
298 } // namespace history 288 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698