Chromium Code Reviews| Index: chrome/browser/history/download_database.cc |
| diff --git a/chrome/browser/history/download_database.cc b/chrome/browser/history/download_database.cc |
| index c9881156db31829652ffccc9ffd972e32777b381..1c5b7852867ca95761445c353200df9f8826bfdf 100644 |
| --- a/chrome/browser/history/download_database.cc |
| +++ b/chrome/browser/history/download_database.cc |
| @@ -14,16 +14,17 @@ |
| #include "base/time.h" |
| #include "base/utf_string_conversions.h" |
| #include "build/build_config.h" |
| +#include "chrome/browser/history/download_persistent_store_info.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/download_item.h" |
| -#include "content/public/browser/download_persistent_store_info.h" |
| #include "sql/statement.h" |
| using content::DownloadItem; |
| -using content::DownloadPersistentStoreInfo; |
| namespace history { |
| +const int64 DownloadDatabase::kUninitializedHandle = -1; |
| + |
| namespace { |
| static const char kSchema[] = |
| @@ -112,15 +113,6 @@ DownloadDatabase::DownloadDatabase() |
| DownloadDatabase::~DownloadDatabase() { |
| } |
| -void DownloadDatabase::CheckThread() { |
| - if (owning_thread_set_) { |
| - DCHECK(owning_thread_ == base::PlatformThread::CurrentId()); |
| - } else { |
| - owning_thread_ = base::PlatformThread::CurrentId(); |
| - owning_thread_set_ = true; |
| - } |
| -} |
| - |
| bool DownloadDatabase::EnsureColumnExists( |
| const std::string& name, const std::string& type) { |
| std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; |
| @@ -137,7 +129,6 @@ bool DownloadDatabase::MigrateDownloadsState() { |
| } |
| bool DownloadDatabase::InitDownloadTable() { |
| - CheckThread(); |
| GetMetaTable().GetValue(kNextDownloadId, &next_id_); |
| if (GetDB().DoesTableExist("downloads")) { |
| return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
| @@ -148,13 +139,11 @@ bool DownloadDatabase::InitDownloadTable() { |
| } |
| bool DownloadDatabase::DropDownloadTable() { |
| - CheckThread(); |
| return GetDB().Execute("DROP TABLE downloads"); |
| } |
| void DownloadDatabase::QueryDownloads( |
| std::vector<DownloadPersistentStoreInfo>* results) { |
| - CheckThread(); |
| results->clear(); |
| if (next_db_handle_ < 1) |
| next_db_handle_ = 1; |
| @@ -194,7 +183,6 @@ void DownloadDatabase::QueryDownloads( |
| } |
| bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) { |
| - CheckThread(); |
| DCHECK(data.db_handle > 0); |
| int state = StateToInt(data.state); |
| if (state == kStateInvalid) { |
| @@ -203,30 +191,20 @@ bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) { |
| } |
| sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| "UPDATE downloads " |
| - "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?")); |
| - statement.BindInt64(0, data.received_bytes); |
| - statement.BindInt(1, state); |
| - statement.BindInt64(2, data.end_time.ToTimeT()); |
| - statement.BindInt(3, (data.opened ? 1 : 0)); |
| - statement.BindInt64(4, data.db_handle); |
| - |
| - return statement.Run(); |
| -} |
| - |
| -bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, |
| - DownloadID db_handle) { |
| - CheckThread(); |
| - DCHECK(db_handle > 0); |
| - sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| - "UPDATE downloads SET full_path=? WHERE id=?")); |
| - BindFilePath(statement, path, 0); |
| - statement.BindInt64(1, db_handle); |
| + "SET full_path=?, received_bytes=?, state=?, end_time=?, total_bytes=?, " |
| + "opened=? WHERE id=?")); |
| + BindFilePath(statement, data.path, 0); |
| + statement.BindInt64(1, data.received_bytes); |
| + statement.BindInt(2, state); |
| + statement.BindInt64(3, data.end_time.ToTimeT()); |
| + statement.BindInt(4, data.total_bytes); |
| + statement.BindInt(5, (data.opened ? 1 : 0)); |
| + statement.BindInt64(6, data.db_handle); |
| return statement.Run(); |
| } |
| bool DownloadDatabase::CleanUpInProgressEntries() { |
| - CheckThread(); |
| sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| "UPDATE downloads SET state=? WHERE state=?")); |
| statement.BindInt(0, kStateCancelled); |
| @@ -237,8 +215,6 @@ bool DownloadDatabase::CleanUpInProgressEntries() { |
| int64 DownloadDatabase::CreateDownload( |
| const DownloadPersistentStoreInfo& info) { |
| - CheckThread(); |
| - |
| if (next_db_handle_ == 0) { |
| // This is unlikely. All current known tests and users already call |
| // QueryDownloads() before CreateDownload(). |
| @@ -249,7 +225,7 @@ int64 DownloadDatabase::CreateDownload( |
| int state = StateToInt(info.state); |
| if (state == kStateInvalid) |
| - return false; |
| + return kUninitializedHandle; |
| sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| "INSERT INTO downloads " |
| @@ -275,63 +251,24 @@ int64 DownloadDatabase::CreateDownload( |
| return db_handle; |
| } |
| - return 0; |
| -} |
| - |
| -void DownloadDatabase::RemoveDownload(DownloadID db_handle) { |
| - CheckThread(); |
| - |
| - sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| - "DELETE FROM downloads WHERE id=?")); |
| - statement.BindInt64(0, db_handle); |
| - |
| - statement.Run(); |
| + return kUninitializedHandle; |
| } |
| -bool DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin, |
| - base::Time delete_end) { |
| - CheckThread(); |
| - time_t start_time = delete_begin.ToTimeT(); |
| - time_t end_time = delete_end.ToTimeT(); |
| - |
| - int num_downloads_deleted = -1; |
| - { |
| - sql::Statement count(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| - "SELECT count(*) FROM downloads WHERE start_time >= ? " |
| - "AND start_time < ? AND (State = ? OR State = ? OR State = ?)")); |
| - count.BindInt64(0, start_time); |
| - count.BindInt64( |
| - 1, |
| - end_time ? end_time : std::numeric_limits<int64>::max()); |
| - count.BindInt(2, kStateComplete); |
| - count.BindInt(3, kStateCancelled); |
| - count.BindInt(4, kStateInterrupted); |
| - if (count.Step()) |
| - num_downloads_deleted = count.ColumnInt(0); |
| - } |
| - |
| - |
| - bool success = false; |
| +void DownloadDatabase::RemoveDownloads(const std::set<DownloadID>& handles) { |
|
sky
2012/11/08 20:55:17
If this is going to delete a download at a time, i
benjhayden
2012/11/08 21:40:03
Can you say more?
It looks to me like HistoryBacke
sky
2012/11/09 16:45:50
We have generally kept the DataBase implementation
benjhayden
2012/11/12 18:44:16
Done.
|
| + int downloads_count_before = CountDownloads(); |
| base::TimeTicks started_removing = base::TimeTicks::Now(); |
| - { |
| - // This does not use an index. We currently aren't likely to have enough |
| - // downloads where an index by time will give us a lot of benefit. |
| + // HistoryBackend uses a long-running Transaction that is committed |
| + // periodically, so this loop doesn't actually hit the disk too hard. |
| + for (std::set<DownloadID>::const_iterator it = handles.begin(); |
| + it != handles.end(); ++it) { |
| sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| - "DELETE FROM downloads WHERE start_time >= ? AND start_time < ? " |
| - "AND (State = ? OR State = ? OR State = ?)")); |
| - statement.BindInt64(0, start_time); |
| - statement.BindInt64( |
| - 1, |
| - end_time ? end_time : std::numeric_limits<int64>::max()); |
| - statement.BindInt(2, kStateComplete); |
| - statement.BindInt(3, kStateCancelled); |
| - statement.BindInt(4, kStateInterrupted); |
| - |
| - success = statement.Run(); |
| + "DELETE FROM downloads WHERE id=?")); |
| + statement.BindInt64(0, *it); |
| + statement.Run(); |
| } |
| - |
| base::TimeTicks finished_removing = base::TimeTicks::Now(); |
| - |
| + int downloads_count_after = CountDownloads(); |
| + int num_downloads_deleted = downloads_count_before - downloads_count_after; |
| if (num_downloads_deleted >= 0) { |
| UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount", |
| num_downloads_deleted); |
| @@ -342,8 +279,18 @@ bool DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin, |
| (1000 * micros) / num_downloads_deleted); |
| } |
| } |
| + int num_downloads_not_deleted = handles.size() - num_downloads_deleted; |
| + if (num_downloads_not_deleted >= 0) { |
| + UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCountNotRemoved", |
| + num_downloads_not_deleted); |
| + } |
| +} |
| - return success; |
| +int DownloadDatabase::CountDownloads() { |
| + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| + "SELECT count(*) from downloads")); |
| + statement.Step(); |
| + return statement.ColumnInt(0); |
| } |
| } // namespace history |