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

Side by Side Diff: components/omnibox/browser/shortcuts_database.cc

Issue 1832173002: [sql] Database recovery system for Shortcuts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: std::count returns ptrdiff_t, various comment changes. Created 4 years, 5 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "components/omnibox/browser/shortcuts_database.h" 5 #include "components/omnibox/browser/shortcuts_database.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h"
9 #include "base/guid.h" 10 #include "base/guid.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
13 #include "components/omnibox/browser/autocomplete_match_type.h" 14 #include "components/omnibox/browser/autocomplete_match_type.h"
14 #include "sql/meta_table.h" 15 #include "sql/meta_table.h"
16 #include "sql/recovery.h"
15 #include "sql/statement.h" 17 #include "sql/statement.h"
16 #include "sql/transaction.h" 18 #include "sql/transaction.h"
17 #include "ui/base/page_transition_types.h" 19 #include "ui/base/page_transition_types.h"
18 20
19 21
20 // Helpers -------------------------------------------------------------------- 22 // Helpers --------------------------------------------------------------------
21 23
22 namespace { 24 namespace {
23 25
24 // Current version number. We write databases at the "current" version number, 26 // Current version number. We write databases at the "current" version number,
(...skipping 23 matching lines...) Expand all
48 bool DeleteShortcut(const char* field_name, 50 bool DeleteShortcut(const char* field_name,
49 const std::string& id, 51 const std::string& id,
50 sql::Connection& db) { 52 sql::Connection& db) {
51 sql::Statement s(db.GetUniqueStatement( 53 sql::Statement s(db.GetUniqueStatement(
52 base::StringPrintf("DELETE FROM omni_box_shortcuts WHERE %s = ?", 54 base::StringPrintf("DELETE FROM omni_box_shortcuts WHERE %s = ?",
53 field_name).c_str())); 55 field_name).c_str()));
54 s.BindString(0, id); 56 s.BindString(0, id);
55 return s.Run(); 57 return s.Run();
56 } 58 }
57 59
60 void DatabaseErrorCallback(sql::Connection* db,
61 const base::FilePath& db_path,
62 int extended_error,
63 sql::Statement* stmt) {
64 if (sql::Recovery::ShouldRecover(extended_error)) {
65 // Prevent reentrant calls.
66 db->reset_error_callback();
67
68 // After this call, the |db| handle is poisoned so that future calls will
69 // return errors until the handle is re-opened.
70 sql::Recovery::RecoverDatabase(db, db_path);
71
72 // Recoverable cases should not be correlated with programmer error, so the
Mark P 2016/06/27 23:20:13 I have trouble understanding this sentence. I don
Scott Hess - ex-Googler 2016/06/29 21:39:30 Probably nerdview. Made an attempt at revision.
Mark P 2016/07/01 22:40:55 Your new comment is much better, thanks.
73 // DLOG(FATAL) below is not helpful. The ignored call signals the
74 // test-expectation framework that the error was handled.
75 ignore_result(sql::Connection::IsExpectedSqliteError(extended_error));
76 return;
77 }
78
79 // The default handling is to assert on debug and to ignore on release.
80 if (!sql::Connection::IsExpectedSqliteError(extended_error))
81 DLOG(FATAL) << db->GetErrorMessage();
82 }
83
58 } // namespace 84 } // namespace
59 85
60 // ShortcutsDatabase::Shortcut::MatchCore ------------------------------------- 86 // ShortcutsDatabase::Shortcut::MatchCore -------------------------------------
61 87
62 ShortcutsDatabase::Shortcut::MatchCore::MatchCore( 88 ShortcutsDatabase::Shortcut::MatchCore::MatchCore(
63 const base::string16& fill_into_edit, 89 const base::string16& fill_into_edit,
64 const GURL& destination_url, 90 const GURL& destination_url,
65 const base::string16& contents, 91 const base::string16& contents,
66 const std::string& contents_class, 92 const std::string& contents_class,
67 const base::string16& description, 93 const base::string16& description,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 142
117 // ShortcutsDatabase ---------------------------------------------------------- 143 // ShortcutsDatabase ----------------------------------------------------------
118 144
119 ShortcutsDatabase::ShortcutsDatabase(const base::FilePath& database_path) 145 ShortcutsDatabase::ShortcutsDatabase(const base::FilePath& database_path)
120 : database_path_(database_path) { 146 : database_path_(database_path) {
121 } 147 }
122 148
123 bool ShortcutsDatabase::Init() { 149 bool ShortcutsDatabase::Init() {
124 db_.set_histogram_tag("Shortcuts"); 150 db_.set_histogram_tag("Shortcuts");
125 151
152 // To recover from corruption.
153 db_.set_error_callback(
154 base::Bind(&DatabaseErrorCallback, &db_, database_path_));
155
126 // Set the database page size to something a little larger to give us 156 // Set the database page size to something a little larger to give us
127 // better performance (we're typically seek rather than bandwidth limited). 157 // better performance (we're typically seek rather than bandwidth limited).
128 // This only has an effect before any tables have been created, otherwise 158 // This only has an effect before any tables have been created, otherwise
129 // this is a NOP. Must be a power of 2 and a max of 8192. 159 // this is a NOP. Must be a power of 2 and a max of 8192.
130 db_.set_page_size(4096); 160 db_.set_page_size(4096);
131 161
132 // Run the database in exclusive mode. Nobody else should be accessing the 162 // Run the database in exclusive mode. Nobody else should be accessing the
133 // database while we're running, and this will give somewhat improved perf. 163 // database while we're running, and this will give somewhat improved perf.
134 db_.set_exclusive_locking(); 164 db_.set_exclusive_locking();
135 165
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 "UPDATE omni_box_shortcuts SET type = %d", 280 "UPDATE omni_box_shortcuts SET type = %d",
251 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && 281 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) &&
252 db_.Execute("ALTER TABLE omni_box_shortcuts " 282 db_.Execute("ALTER TABLE omni_box_shortcuts "
253 "ADD COLUMN keyword VARCHAR") && 283 "ADD COLUMN keyword VARCHAR") &&
254 transaction.Commit())) { 284 transaction.Commit())) {
255 return false; 285 return false;
256 } 286 }
257 } 287 }
258 288
259 if (!sql::MetaTable::DoesTableExist(&db_)) { 289 if (!sql::MetaTable::DoesTableExist(&db_)) {
260 meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber);
261 sql::Transaction transaction(&db_); 290 sql::Transaction transaction(&db_);
262 if (!(transaction.Begin() && 291 if (!(transaction.Begin() &&
263 // Migrate old SEARCH_OTHER_ENGINE values to the new type value. 292 meta_table_.Init(
264 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 293 &db_, kCurrentVersionNumber, kCompatibleVersionNumber) &&
265 "SET type = 13 WHERE type = 9").c_str()) && 294 // Migrate old SEARCH_OTHER_ENGINE values to the new type value.
266 // Migrate old EXTENSION_APP values to the new type value. 295 db_.Execute(
267 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 296 "UPDATE omni_box_shortcuts SET type = 13 WHERE type = 9") &&
268 "SET type = 14 WHERE type = 10").c_str()) && 297 // Migrate old EXTENSION_APP values to the new type value.
269 // Migrate old CONTACT values to the new type value. 298 db_.Execute(
270 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 299 "UPDATE omni_box_shortcuts SET type = 14 WHERE type = 10") &&
271 "SET type = 15 WHERE type = 11").c_str()) && 300 // Migrate old CONTACT values to the new type value.
272 // Migrate old BOOKMARK_TITLE values to the new type value. 301 db_.Execute(
273 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 302 "UPDATE omni_box_shortcuts SET type = 15 WHERE type = 11") &&
274 "SET type = 16 WHERE type = 12").c_str()) && 303 // Migrate old BOOKMARK_TITLE values to the new type value.
275 transaction.Commit())) { 304 db_.Execute(
305 "UPDATE omni_box_shortcuts SET type = 16 WHERE type = 12") &&
306 transaction.Commit())) {
276 return false; 307 return false;
277 } 308 }
278 } 309 }
279 return true; 310 return true;
280 } 311 }
OLDNEW
« no previous file with comments | « no previous file | components/omnibox/browser/shortcuts_database_unittest.cc » ('j') | sql/recovery.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698