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

Side by Side Diff: components/offline_pages/background/request_queue_store_sql.cc

Issue 2218403002: Change database scheme - add state and start tracking (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Delete old database if any 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 "components/offline_pages/background/request_queue_store_sql.h" 5 #include "components/offline_pages/background/request_queue_store_sql.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 13 matching lines...) Expand all
24 // it can be used inline in other SQL statements below. 24 // it can be used inline in other SQL statements below.
25 #define REQUEST_QUEUE_TABLE_NAME "request_queue_v1" 25 #define REQUEST_QUEUE_TABLE_NAME "request_queue_v1"
26 const bool kUserRequested = true; 26 const bool kUserRequested = true;
27 27
28 bool CreateRequestQueueTable(sql::Connection* db) { 28 bool CreateRequestQueueTable(sql::Connection* db) {
29 const char kSql[] = "CREATE TABLE IF NOT EXISTS " REQUEST_QUEUE_TABLE_NAME 29 const char kSql[] = "CREATE TABLE IF NOT EXISTS " REQUEST_QUEUE_TABLE_NAME
30 " (request_id INTEGER PRIMARY KEY NOT NULL," 30 " (request_id INTEGER PRIMARY KEY NOT NULL,"
31 " creation_time INTEGER NOT NULL," 31 " creation_time INTEGER NOT NULL,"
32 " activation_time INTEGER NOT NULL DEFAULT 0," 32 " activation_time INTEGER NOT NULL DEFAULT 0,"
33 " last_attempt_time INTEGER NOT NULL DEFAULT 0," 33 " last_attempt_time INTEGER NOT NULL DEFAULT 0,"
34 " attempt_count INTEGER NOT NULL," 34 " started_attempt_count INTEGER NOT NULL,"
35 " completed_attempt_count INTEGER NOT NULL,"
36 " state INTEGER NOT NULL DEFAULT 0,"
35 " url VARCHAR NOT NULL," 37 " url VARCHAR NOT NULL,"
36 " client_namespace VARCHAR NOT NULL," 38 " client_namespace VARCHAR NOT NULL,"
37 " client_id VARCHAR NOT NULL" 39 " client_id VARCHAR NOT NULL"
38 ")"; 40 ")";
39 return db->Execute(kSql); 41 return db->Execute(kSql);
40 } 42 }
41 43
42 bool CreateSchema(sql::Connection* db) { 44 bool CreateSchema(sql::Connection* db) {
43 // TODO(fgorski): Upgrade code goes here and requires transaction. 45 // If there is not already a state column, we need to drop the old table. We
46 // are choosing to drop instead of upgrade since the feature is not yet
47 // released, so we don't use a transaction to protect existing data, or try to
48 // migrate it.
chili 2016/08/08 18:17:01 do we need a todo to add transaction once this fea
Pete Williamson 2016/08/08 18:44:28 We only need a transaction the next time we modify
49 if (!db->DoesColumnExist(REQUEST_QUEUE_TABLE_NAME, "state")) {
50 if (!db->Execute("DROP TABLE IF EXISTS " REQUEST_QUEUE_TABLE_NAME))
51 return false;
52 }
53
44 if (!CreateRequestQueueTable(db)) 54 if (!CreateRequestQueueTable(db))
45 return false; 55 return false;
46 56
47 // TODO(fgorski): Add indices here. 57 // TODO(fgorski): Add indices here.
48 return true; 58 return true;
49 } 59 }
50 60
51 bool DeleteRequestById(sql::Connection* db, int64_t request_id) { 61 bool DeleteRequestById(sql::Connection* db, int64_t request_id) {
52 const char kSql[] = 62 const char kSql[] =
53 "DELETE FROM " REQUEST_QUEUE_TABLE_NAME " WHERE request_id=?"; 63 "DELETE FROM " REQUEST_QUEUE_TABLE_NAME " WHERE request_id=?";
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // all columns present. Columns are in order they are defined in select query 127 // all columns present. Columns are in order they are defined in select query
118 // in |RequestQueueStore::RequestSync| method. 128 // in |RequestQueueStore::RequestSync| method.
119 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) { 129 SavePageRequest MakeSavePageRequest(const sql::Statement& statement) {
120 const int64_t id = statement.ColumnInt64(0); 130 const int64_t id = statement.ColumnInt64(0);
121 const base::Time creation_time = 131 const base::Time creation_time =
122 base::Time::FromInternalValue(statement.ColumnInt64(1)); 132 base::Time::FromInternalValue(statement.ColumnInt64(1));
123 const base::Time activation_time = 133 const base::Time activation_time =
124 base::Time::FromInternalValue(statement.ColumnInt64(2)); 134 base::Time::FromInternalValue(statement.ColumnInt64(2));
125 const base::Time last_attempt_time = 135 const base::Time last_attempt_time =
126 base::Time::FromInternalValue(statement.ColumnInt64(3)); 136 base::Time::FromInternalValue(statement.ColumnInt64(3));
127 const int64_t last_attempt_count = statement.ColumnInt64(4); 137 const int64_t started_attempt_count = statement.ColumnInt64(4);
128 const GURL url(statement.ColumnString(5)); 138 const int64_t completed_attempt_count = statement.ColumnInt64(5);
129 const ClientId client_id(statement.ColumnString(6), 139 const SavePageRequest::RequestState state =
130 statement.ColumnString(7)); 140 static_cast<SavePageRequest::RequestState>(statement.ColumnInt64(6));
141 const GURL url(statement.ColumnString(7));
142 const ClientId client_id(statement.ColumnString(8),
143 statement.ColumnString(9));
131 144
132 DVLOG(2) << "making save page request - id " << id << " url " << url 145 DVLOG(2) << "making save page request - id " << id << " url " << url
133 << " client_id " << client_id.name_space << "-" << client_id.id 146 << " client_id " << client_id.name_space << "-" << client_id.id
134 << " creation time " << creation_time << " user requested " 147 << " creation time " << creation_time << " user requested "
135 << kUserRequested; 148 << kUserRequested;
136 149
137 SavePageRequest request( 150 SavePageRequest request(
138 id, url, client_id, creation_time, activation_time, kUserRequested); 151 id, url, client_id, creation_time, activation_time, kUserRequested);
139 request.set_last_attempt_time(last_attempt_time); 152 request.set_last_attempt_time(last_attempt_time);
140 request.set_attempt_count(last_attempt_count); 153 request.set_started_attempt_count(started_attempt_count);
154 request.set_completed_attempt_count(completed_attempt_count);
155 request.set_request_state(state);
141 return request; 156 return request;
142 } 157 }
143 158
144 RequestQueueStore::UpdateStatus InsertOrReplace( 159 RequestQueueStore::UpdateStatus InsertOrReplace(
145 sql::Connection* db, 160 sql::Connection* db,
146 const SavePageRequest& request) { 161 const SavePageRequest& request) {
147 // In order to use the enums in the Bind* methods, keep the order of fields 162 // In order to use the enums in the Bind* methods, keep the order of fields
148 // the same as in the definition/select query. 163 // the same as in the definition/select query.
149 const char kInsertSql[] = 164 const char kInsertSql[] =
150 "INSERT OR REPLACE INTO " REQUEST_QUEUE_TABLE_NAME 165 "INSERT OR REPLACE INTO " REQUEST_QUEUE_TABLE_NAME
151 " (request_id, creation_time, activation_time, last_attempt_time, " 166 " (request_id, creation_time, activation_time, last_attempt_time, "
152 " attempt_count, url, client_namespace, client_id) " 167 " started_attempt_count, completed_attempt_count, state, url, "
168 " client_namespace, client_id) "
153 " VALUES " 169 " VALUES "
154 " (?, ?, ?, ?, ?, ?, ?, ?)"; 170 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
155 171
156 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kInsertSql)); 172 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kInsertSql));
157 statement.BindInt64(0, request.request_id()); 173 statement.BindInt64(0, request.request_id());
158 statement.BindInt64(1, request.creation_time().ToInternalValue()); 174 statement.BindInt64(1, request.creation_time().ToInternalValue());
159 statement.BindInt64(2, request.activation_time().ToInternalValue()); 175 statement.BindInt64(2, request.activation_time().ToInternalValue());
160 statement.BindInt64(3, request.last_attempt_time().ToInternalValue()); 176 statement.BindInt64(3, request.last_attempt_time().ToInternalValue());
161 statement.BindInt64(4, request.attempt_count()); 177 statement.BindInt64(4, request.started_attempt_count());
162 statement.BindString(5, request.url().spec()); 178 statement.BindInt64(5, request.completed_attempt_count());
163 statement.BindString(6, request.client_id().name_space); 179 statement.BindInt64(6, static_cast<int64_t>(request.request_state()));
164 statement.BindString(7, request.client_id().id); 180 statement.BindString(7, request.url().spec());
181 statement.BindString(8, request.client_id().name_space);
182 statement.BindString(9, request.client_id().id);
165 183
166 // TODO(fgorski): Replace the UpdateStatus with boolean in the 184 // TODO(fgorski): Replace the UpdateStatus with boolean in the
167 // RequestQueueStore interface and update this code. 185 // RequestQueueStore interface and update this code.
168 return statement.Run() ? RequestQueueStore::UpdateStatus::UPDATED 186 return statement.Run() ? RequestQueueStore::UpdateStatus::UPDATED
169 : RequestQueueStore::UpdateStatus::FAILED; 187 : RequestQueueStore::UpdateStatus::FAILED;
170 } 188 }
171 189
172 bool InitDatabase(sql::Connection* db, const base::FilePath& path) { 190 bool InitDatabase(sql::Connection* db, const base::FilePath& path) {
173 db->set_page_size(4096); 191 db->set_page_size(4096);
174 db->set_cache_size(500); 192 db->set_cache_size(500);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 runner->PostTask(FROM_HERE, base::Bind(callback, success)); 229 runner->PostTask(FROM_HERE, base::Bind(callback, success));
212 } 230 }
213 231
214 // static 232 // static
215 void RequestQueueStoreSQL::GetRequestsSync( 233 void RequestQueueStoreSQL::GetRequestsSync(
216 sql::Connection* db, 234 sql::Connection* db,
217 scoped_refptr<base::SingleThreadTaskRunner> runner, 235 scoped_refptr<base::SingleThreadTaskRunner> runner,
218 const GetRequestsCallback& callback) { 236 const GetRequestsCallback& callback) {
219 const char kSql[] = 237 const char kSql[] =
220 "SELECT request_id, creation_time, activation_time," 238 "SELECT request_id, creation_time, activation_time,"
221 " last_attempt_time, attempt_count, url, client_namespace, client_id" 239 " last_attempt_time, started_attempt_count, completed_attempt_count,"
240 " state, url, client_namespace, client_id"
222 " FROM " REQUEST_QUEUE_TABLE_NAME; 241 " FROM " REQUEST_QUEUE_TABLE_NAME;
223 242
224 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); 243 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
225 244
226 std::vector<SavePageRequest> result; 245 std::vector<SavePageRequest> result;
227 while (statement.Step()) 246 while (statement.Step())
228 result.push_back(MakeSavePageRequest(statement)); 247 result.push_back(MakeSavePageRequest(statement));
229 248
230 runner->PostTask(FROM_HERE, 249 runner->PostTask(FROM_HERE,
231 base::Bind(callback, statement.Succeeded(), result)); 250 base::Bind(callback, statement.Succeeded(), result));
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 } 413 }
395 414
396 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback, 415 void RequestQueueStoreSQL::OnResetDone(const ResetCallback& callback,
397 bool success) { 416 bool success) {
398 // Complete connection initialization post reset. 417 // Complete connection initialization post reset.
399 OnOpenConnectionDone(success); 418 OnOpenConnectionDone(success);
400 callback.Run(success); 419 callback.Run(success);
401 } 420 }
402 421
403 } // namespace offline_pages 422 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698