OLD | NEW |
---|---|
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/offline_page_metadata_store_sql.h" | 5 #include "components/offline_pages/offline_page_metadata_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 15 matching lines...) Expand all Loading... | |
26 // it can be used inline in other SQL statements below. | 26 // it can be used inline in other SQL statements below. |
27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" | 27 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" |
28 | 28 |
29 bool CreateOfflinePagesTable(sql::Connection* db) { | 29 bool CreateOfflinePagesTable(sql::Connection* db) { |
30 const char kSql[] = "CREATE TABLE IF NOT EXISTS " OFFLINE_PAGES_TABLE_NAME | 30 const char kSql[] = "CREATE TABLE IF NOT EXISTS " OFFLINE_PAGES_TABLE_NAME |
31 "(offline_id INTEGER PRIMARY KEY NOT NULL," | 31 "(offline_id INTEGER PRIMARY KEY NOT NULL," |
32 " creation_time INTEGER NOT NULL," | 32 " creation_time INTEGER NOT NULL," |
33 " file_size INTEGER NOT NULL," | 33 " file_size INTEGER NOT NULL," |
34 " last_access_time INTEGER NOT NULL," | 34 " last_access_time INTEGER NOT NULL," |
35 " access_count INTEGER NOT NULL," | 35 " access_count INTEGER NOT NULL," |
36 " expiration_time INTEGER NOT NULL DEFAULT 0," | |
37 " client_namespace VARCHAR NOT NULL," | 36 " client_namespace VARCHAR NOT NULL," |
38 " client_id VARCHAR NOT NULL," | 37 " client_id VARCHAR NOT NULL," |
39 " online_url VARCHAR NOT NULL," | 38 " online_url VARCHAR NOT NULL," |
40 " file_path VARCHAR NOT NULL," | 39 " file_path VARCHAR NOT NULL," |
41 " title VARCHAR NOT NULL DEFAULT ''," | 40 " title VARCHAR NOT NULL DEFAULT ''," |
42 " original_url VARCHAR NOT NULL DEFAULT ''" | 41 " original_url VARCHAR NOT NULL DEFAULT ''" |
43 ")"; | 42 ")"; |
44 return db->Execute(kSql); | 43 return db->Execute(kSql); |
45 } | 44 } |
46 | 45 |
(...skipping 22 matching lines...) Expand all Loading... | |
69 "access_count, client_namespace, client_id, " | 68 "access_count, client_namespace, client_id, " |
70 "online_url, file_path " | 69 "online_url, file_path " |
71 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | 70 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; |
72 return UpgradeWithQuery(db, kSql); | 71 return UpgradeWithQuery(db, kSql); |
73 } | 72 } |
74 | 73 |
75 bool UpgradeFrom53(sql::Connection* db) { | 74 bool UpgradeFrom53(sql::Connection* db) { |
76 const char kSql[] = | 75 const char kSql[] = |
77 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | 76 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME |
78 " (offline_id, creation_time, file_size, last_access_time, " | 77 " (offline_id, creation_time, file_size, last_access_time, " |
79 "access_count, expiration_time, client_namespace, client_id, " | 78 "access_count, client_namespace, client_id, " |
fgorski
2016/11/18 00:13:04
this will revive expired pages.
Shouldn't we dele
romax
2016/11/18 20:50:49
I lean towards letting consistency check do the wo
| |
80 "online_url, file_path) " | 79 "online_url, file_path) " |
81 "SELECT " | 80 "SELECT " |
82 "offline_id, creation_time, file_size, last_access_time, " | 81 "offline_id, creation_time, file_size, last_access_time, " |
83 "access_count, expiration_time, client_namespace, client_id, " | 82 "access_count, client_namespace, client_id, " |
84 "online_url, file_path " | 83 "online_url, file_path " |
85 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | 84 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; |
86 return UpgradeWithQuery(db, kSql); | 85 return UpgradeWithQuery(db, kSql); |
87 } | 86 } |
88 | 87 |
89 bool UpgradeFrom54(sql::Connection* db) { | 88 bool UpgradeFrom54(sql::Connection* db) { |
90 const char kSql[] = | 89 const char kSql[] = |
91 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | 90 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME |
92 " (offline_id, creation_time, file_size, last_access_time, " | 91 " (offline_id, creation_time, file_size, last_access_time, " |
93 "access_count, expiration_time, client_namespace, client_id, " | 92 "access_count, client_namespace, client_id, " |
fgorski
2016/11/18 00:13:04
ditto
romax
2016/11/18 20:50:49
Done.
| |
94 "online_url, file_path, title) " | 93 "online_url, file_path, title) " |
95 "SELECT " | 94 "SELECT " |
96 "offline_id, creation_time, file_size, last_access_time, " | 95 "offline_id, creation_time, file_size, last_access_time, " |
97 "access_count, expiration_time, client_namespace, client_id, " | 96 "access_count, client_namespace, client_id, " |
98 "online_url, file_path, title " | 97 "online_url, file_path, title " |
99 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | 98 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; |
100 return UpgradeWithQuery(db, kSql); | 99 return UpgradeWithQuery(db, kSql); |
101 } | 100 } |
102 | 101 |
103 bool UpgradeFrom55(sql::Connection* db) { | 102 bool UpgradeFrom55(sql::Connection* db) { |
104 const char kSql[] = | 103 const char kSql[] = |
105 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME | 104 "INSERT INTO " OFFLINE_PAGES_TABLE_NAME |
106 " (offline_id, creation_time, file_size, last_access_time, " | 105 " (offline_id, creation_time, file_size, last_access_time, " |
107 "access_count, expiration_time, client_namespace, client_id, " | 106 "access_count, client_namespace, client_id, online_url, " |
108 "online_url, file_path, title) " | 107 "file_path, title) " |
109 "SELECT " | 108 "SELECT " |
110 "offline_id, creation_time, file_size, last_access_time, " | 109 "offline_id, creation_time, file_size, last_access_time, " |
111 "access_count, expiration_time, client_namespace, client_id, " | 110 "access_count, client_namespace, client_id, online_url, " |
112 "online_url, file_path, title " | 111 "file_path, title " |
113 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; | 112 "FROM temp_" OFFLINE_PAGES_TABLE_NAME; |
114 return UpgradeWithQuery(db, kSql); | 113 return UpgradeWithQuery(db, kSql); |
115 } | 114 } |
116 | 115 |
117 bool CreateSchema(sql::Connection* db) { | 116 bool CreateSchema(sql::Connection* db) { |
118 // If you create a transaction but don't Commit() it is automatically | 117 // If you create a transaction but don't Commit() it is automatically |
119 // rolled back by its destructor when it falls out of scope. | 118 // rolled back by its destructor when it falls out of scope. |
120 sql::Transaction transaction(db); | 119 sql::Transaction transaction(db); |
121 if (!transaction.Begin()) | 120 if (!transaction.Begin()) |
122 return false; | 121 return false; |
123 | 122 |
124 if (!db->DoesTableExist(OFFLINE_PAGES_TABLE_NAME)) { | 123 if (!db->DoesTableExist(OFFLINE_PAGES_TABLE_NAME)) { |
125 if (!CreateOfflinePagesTable(db)) | 124 if (!CreateOfflinePagesTable(db)) |
126 return false; | 125 return false; |
127 } | 126 } |
128 | 127 |
129 // Upgrade section. Details are described in the header file. | 128 // Upgrade section. Details are described in the header file. |
130 if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "expiration_time")) { | 129 if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "expiration_time") && |
jianli
2016/11/18 00:15:48
M56 table doesn't have expiration_time and title.
romax
2016/11/18 20:50:49
M56 should have title. It was added in M54
| |
130 !db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "title")) { | |
131 if (!UpgradeFrom52(db)) | 131 if (!UpgradeFrom52(db)) |
132 return false; | 132 return false; |
133 } else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "title")) { | 133 } else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "title")) { |
134 if (!UpgradeFrom53(db)) | 134 if (!UpgradeFrom53(db)) |
135 return false; | 135 return false; |
136 } else if (db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "offline_url")) { | 136 } else if (db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "offline_url")) { |
137 if (!UpgradeFrom54(db)) | 137 if (!UpgradeFrom54(db)) |
138 return false; | 138 return false; |
139 } else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "original_url")) { | 139 } else if (!db->DoesColumnExist(OFFLINE_PAGES_TABLE_NAME, "original_url")) { |
140 if (!UpgradeFrom55(db)) | 140 if (!UpgradeFrom55(db)) |
141 return false; | 141 return false; |
142 } | 142 } |
fgorski
2016/11/18 00:13:04
your expiration_time check should go here...
it m
romax
2016/11/18 20:50:49
Done.
| |
143 | 143 |
144 // TODO(fgorski): Add indices here. | 144 // TODO(fgorski): Add indices here. |
145 return transaction.Commit(); | 145 return transaction.Commit(); |
146 } | 146 } |
147 | 147 |
148 bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) { | 148 bool DeleteByOfflineId(sql::Connection* db, int64_t offline_id) { |
149 static const char kSql[] = | 149 static const char kSql[] = |
150 "DELETE FROM " OFFLINE_PAGES_TABLE_NAME " WHERE offline_id=?"; | 150 "DELETE FROM " OFFLINE_PAGES_TABLE_NAME " WHERE offline_id=?"; |
151 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 151 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
152 statement.BindInt64(0, offline_id); | 152 statement.BindInt64(0, offline_id); |
(...skipping 23 matching lines...) Expand all Loading... | |
176 // Create an offline page item from a SQL result. Expects complete rows with | 176 // Create an offline page item from a SQL result. Expects complete rows with |
177 // all columns present. | 177 // all columns present. |
178 OfflinePageItem MakeOfflinePageItem(sql::Statement* statement) { | 178 OfflinePageItem MakeOfflinePageItem(sql::Statement* statement) { |
179 int64_t id = statement->ColumnInt64(0); | 179 int64_t id = statement->ColumnInt64(0); |
180 base::Time creation_time = | 180 base::Time creation_time = |
181 base::Time::FromInternalValue(statement->ColumnInt64(1)); | 181 base::Time::FromInternalValue(statement->ColumnInt64(1)); |
182 int64_t file_size = statement->ColumnInt64(2); | 182 int64_t file_size = statement->ColumnInt64(2); |
183 base::Time last_access_time = | 183 base::Time last_access_time = |
184 base::Time::FromInternalValue(statement->ColumnInt64(3)); | 184 base::Time::FromInternalValue(statement->ColumnInt64(3)); |
185 int access_count = statement->ColumnInt(4); | 185 int access_count = statement->ColumnInt(4); |
186 base::Time expiration_time = | 186 ClientId client_id(statement->ColumnString(5), statement->ColumnString(6)); |
187 base::Time::FromInternalValue(statement->ColumnInt64(5)); | 187 GURL url(statement->ColumnString(7)); |
188 ClientId client_id(statement->ColumnString(6), statement->ColumnString(7)); | 188 base::FilePath path(GetPathFromUTF8String(statement->ColumnString(8))); |
189 GURL url(statement->ColumnString(8)); | 189 base::string16 title = statement->ColumnString16(9); |
190 base::FilePath path(GetPathFromUTF8String(statement->ColumnString(9))); | 190 GURL original_url(statement->ColumnString(10)); |
191 base::string16 title = statement->ColumnString16(10); | |
192 GURL original_url(statement->ColumnString(11)); | |
193 | 191 |
194 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); | 192 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); |
195 item.last_access_time = last_access_time; | 193 item.last_access_time = last_access_time; |
196 item.access_count = access_count; | 194 item.access_count = access_count; |
197 item.expiration_time = expiration_time; | |
198 item.title = title; | 195 item.title = title; |
199 item.original_url = original_url; | 196 item.original_url = original_url; |
200 return item; | 197 return item; |
201 } | 198 } |
202 | 199 |
203 ItemActionStatus Insert(sql::Connection* db, const OfflinePageItem& item) { | 200 ItemActionStatus Insert(sql::Connection* db, const OfflinePageItem& item) { |
204 // Using 'INSERT OR FAIL' or 'INSERT OR ABORT' in the query below causes debug | 201 // Using 'INSERT OR FAIL' or 'INSERT OR ABORT' in the query below causes debug |
205 // builds to DLOG. | 202 // builds to DLOG. |
206 const char kSql[] = | 203 const char kSql[] = |
207 "INSERT OR IGNORE INTO " OFFLINE_PAGES_TABLE_NAME | 204 "INSERT OR IGNORE INTO " OFFLINE_PAGES_TABLE_NAME |
208 " (offline_id, online_url, client_namespace, client_id, file_path, " | 205 " (offline_id, online_url, client_namespace, client_id, file_path, " |
209 "file_size, creation_time, last_access_time, access_count, " | 206 "file_size, creation_time, last_access_time, access_count, " |
210 "expiration_time, title, original_url)" | 207 "title, original_url)" |
211 " VALUES " | 208 " VALUES " |
212 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | 209 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
213 | 210 |
214 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 211 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
215 statement.BindInt64(0, item.offline_id); | 212 statement.BindInt64(0, item.offline_id); |
216 statement.BindString(1, item.url.spec()); | 213 statement.BindString(1, item.url.spec()); |
217 statement.BindString(2, item.client_id.name_space); | 214 statement.BindString(2, item.client_id.name_space); |
218 statement.BindString(3, item.client_id.id); | 215 statement.BindString(3, item.client_id.id); |
219 statement.BindString(4, GetUTF8StringFromPath(item.file_path)); | 216 statement.BindString(4, GetUTF8StringFromPath(item.file_path)); |
220 statement.BindInt64(5, item.file_size); | 217 statement.BindInt64(5, item.file_size); |
221 statement.BindInt64(6, item.creation_time.ToInternalValue()); | 218 statement.BindInt64(6, item.creation_time.ToInternalValue()); |
222 statement.BindInt64(7, item.last_access_time.ToInternalValue()); | 219 statement.BindInt64(7, item.last_access_time.ToInternalValue()); |
223 statement.BindInt(8, item.access_count); | 220 statement.BindInt(8, item.access_count); |
224 statement.BindInt64(9, item.expiration_time.ToInternalValue()); | 221 statement.BindString16(9, item.title); |
225 statement.BindString16(10, item.title); | 222 statement.BindString(10, item.original_url.spec()); |
226 statement.BindString(11, item.original_url.spec()); | |
227 if (!statement.Run()) | 223 if (!statement.Run()) |
228 return ItemActionStatus::STORE_ERROR; | 224 return ItemActionStatus::STORE_ERROR; |
229 if (db->GetLastChangeCount() == 0) | 225 if (db->GetLastChangeCount() == 0) |
230 return ItemActionStatus::ALREADY_EXISTS; | 226 return ItemActionStatus::ALREADY_EXISTS; |
231 return ItemActionStatus::SUCCESS; | 227 return ItemActionStatus::SUCCESS; |
232 } | 228 } |
233 | 229 |
234 bool Update(sql::Connection* db, const OfflinePageItem& item) { | 230 bool Update(sql::Connection* db, const OfflinePageItem& item) { |
235 const char kSql[] = | 231 const char kSql[] = |
236 "UPDATE OR IGNORE " OFFLINE_PAGES_TABLE_NAME | 232 "UPDATE OR IGNORE " OFFLINE_PAGES_TABLE_NAME |
237 " SET online_url = ?, client_namespace = ?, client_id = ?, file_path = ?," | 233 " SET online_url = ?, client_namespace = ?, client_id = ?, file_path = ?," |
238 " file_size = ?, creation_time = ?, last_access_time = ?," | 234 " file_size = ?, creation_time = ?, last_access_time = ?," |
239 " access_count = ?, expiration_time = ?, title = ?, original_url = ?" | 235 " access_count = ?, title = ?, original_url = ?" |
240 " WHERE offline_id = ?"; | 236 " WHERE offline_id = ?"; |
241 | 237 |
242 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 238 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
243 statement.BindString(0, item.url.spec()); | 239 statement.BindString(0, item.url.spec()); |
244 statement.BindString(1, item.client_id.name_space); | 240 statement.BindString(1, item.client_id.name_space); |
245 statement.BindString(2, item.client_id.id); | 241 statement.BindString(2, item.client_id.id); |
246 statement.BindString(3, GetUTF8StringFromPath(item.file_path)); | 242 statement.BindString(3, GetUTF8StringFromPath(item.file_path)); |
247 statement.BindInt64(4, item.file_size); | 243 statement.BindInt64(4, item.file_size); |
248 statement.BindInt64(5, item.creation_time.ToInternalValue()); | 244 statement.BindInt64(5, item.creation_time.ToInternalValue()); |
249 statement.BindInt64(6, item.last_access_time.ToInternalValue()); | 245 statement.BindInt64(6, item.last_access_time.ToInternalValue()); |
250 statement.BindInt(7, item.access_count); | 246 statement.BindInt(7, item.access_count); |
251 statement.BindInt64(8, item.expiration_time.ToInternalValue()); | 247 statement.BindString16(8, item.title); |
252 statement.BindString16(9, item.title); | 248 statement.BindString(9, item.original_url.spec()); |
253 statement.BindString(10, item.original_url.spec()); | 249 statement.BindInt64(10, item.offline_id); |
254 statement.BindInt64(11, item.offline_id); | |
255 return statement.Run() && db->GetLastChangeCount() > 0; | 250 return statement.Run() && db->GetLastChangeCount() > 0; |
256 } | 251 } |
257 | 252 |
258 bool InitDatabase(sql::Connection* db, base::FilePath path) { | 253 bool InitDatabase(sql::Connection* db, base::FilePath path) { |
259 db->set_page_size(4096); | 254 db->set_page_size(4096); |
260 db->set_cache_size(500); | 255 db->set_cache_size(500); |
261 db->set_histogram_tag("OfflinePageMetadata"); | 256 db->set_histogram_tag("OfflinePageMetadata"); |
262 db->set_exclusive_locking(); | 257 db->set_exclusive_locking(); |
263 | 258 |
264 base::File::Error err; | 259 base::File::Error err; |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 | 595 |
601 bool OfflinePageMetadataStoreSQL::CheckDb(const base::Closure& callback) { | 596 bool OfflinePageMetadataStoreSQL::CheckDb(const base::Closure& callback) { |
602 if (!db_.get() || state_ != StoreState::LOADED) { | 597 if (!db_.get() || state_ != StoreState::LOADED) { |
603 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 598 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
604 return false; | 599 return false; |
605 } | 600 } |
606 return true; | 601 return true; |
607 } | 602 } |
608 | 603 |
609 } // namespace offline_pages | 604 } // namespace offline_pages |
OLD | NEW |