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

Side by Side Diff: chrome/browser/history/android/android_cache_database.cc

Issue 9549031: The implementation of AndroidCacheDatabase. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Sync and remove the Created 8 years, 9 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/history/android/android_cache_database.h"
6
7 #include "base/file_util.h"
8 #include "sql/statement.h"
9
10 using base::Time;
11 using base::TimeDelta;
12
13 namespace history {
14
15 namespace {
16
17 int64 ToMilliseconds(const Time& time) {
18 TimeDelta delta = time - Time::UnixEpoch();
19 return delta.InMilliseconds();
20 }
21
22 } // namespace.
23
24 AndroidCacheDatabase::AndroidCacheDatabase() {
25 }
26
27 AndroidCacheDatabase::~AndroidCacheDatabase() {
28 }
29
30 sql::InitStatus AndroidCacheDatabase::InitAndroidCacheDatabase(
31 const FilePath& db_name) {
32 if (!CreateDatabase(db_name))
33 return sql::INIT_FAILURE;
34
35 if (!Attach())
36 return sql::INIT_FAILURE;
37
38 if (!CreateBookmarkCacheTable())
39 return sql::INIT_FAILURE;
40
41 return sql::INIT_OK;
42 }
43
44 bool AndroidCacheDatabase::AddBookmarkCacheRow(const Time& created_time,
45 const Time& last_visit_time,
46 URLID url_id) {
47 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
48 "INSERT INTO android_cache_db.bookmark_cache (created_time, "
49 "last_visit_time, url_id) VALUES (?, ?, ?)"));
50
51 statement.BindInt64(0, ToMilliseconds(created_time));
52 statement.BindInt64(1, ToMilliseconds(last_visit_time));
53 statement.BindInt64(2, url_id);
54
55 if (!statement.Run()) {
56 LOG(ERROR) << GetDB().GetErrorMessage();
57 return false;
58 }
59
60 return true;
61 }
62
63 bool AndroidCacheDatabase::ClearAllBookmarkCache() {
64 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
65 "DELETE FROM android_cache_db.bookmark_cache"));
66 if (!statement.Run()) {
67 LOG(ERROR) << GetDB().GetErrorMessage();
68 return false;
69 }
70 return true;
71 }
72
73 bool AndroidCacheDatabase::MarkURLsAsBookmarked(
74 const std::vector<URLID>& url_ids) {
75 bool has_id = false;
76 std::ostringstream oss;
77 for (std::vector<URLID>::const_iterator i = url_ids.begin();
78 i != url_ids.end(); ++i) {
79 if (has_id)
80 oss << ", ";
81 else
82 has_id = true;
83 oss << *i;
84 }
85
86 if (!has_id)
87 return true;
88
89 std::string sql("UPDATE android_cache_db.bookmark_cache "
90 "SET bookmark = 1 WHERE url_id in (");
91 sql.append(oss.str());
92 sql.append(")");
93 if (!GetDB().Execute(sql.c_str())) {
94 LOG(ERROR) << GetDB().GetErrorMessage();
95 return false;
96 }
97 return true;
98 }
99
100 bool AndroidCacheDatabase::SetFaviconID(URLID url_id, FaviconID favicon_id) {
101 sql::Statement update_statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
102 "UPDATE android_cache_db.bookmark_cache "
103 "SET favicon_id = ? WHERE url_id = ? "));
104
105 update_statement.BindInt64(0, favicon_id);
106 update_statement.BindInt64(1, url_id);
107 if (!update_statement.Run()) {
108 LOG(ERROR) << GetDB().GetErrorMessage();
109 return false;
110 }
111 return true;
112 }
113
114 bool AndroidCacheDatabase::CreateDatabase(const FilePath& db_name) {
115 db_name_ = db_name;
116 if (file_util::PathExists(db_name_))
117 file_util::Delete(db_name_, false);
118
119 // Using a new connection, otherwise we can not create the database.
120 sql::Connection connection;
121
122 // The db doesn't store too much data, so we don't need that big a page
123 // size or cache.
124 connection.set_page_size(2048);
125 connection.set_cache_size(32);
126
127 // Run the database in exclusive mode. Nobody else should be accessing the
128 // database while we're running, and this will give somewhat improved perf.
129 connection.set_exclusive_locking();
130
131 if (!connection.Open(db_name_)) {
132 LOG(ERROR) << connection.GetErrorMessage();
133 return false;
134 }
135 connection.Close();
136 return true;
137 }
138
139 bool AndroidCacheDatabase::CreateBookmarkCacheTable() {
140 const char* name = "android_cache_db.bookmark_cache";
141 DCHECK(!GetDB().DoesTableExist(name));
142
143 std::string sql;
144 sql.append("CREATE TABLE ");
145 sql.append(name);
146 sql.append("("
147 "id INTEGER PRIMARY KEY,"
148 "created_time INTEGER NOT NULL," // Time in millisecond.
149 "last_visit_time INTEGER NOT NULL," // Time in millisecond.
150 "url_id INTEGER NOT NULL," // url id in urls table.
151 "favicon_id INTEGER DEFAULT NULL," // favicon id.
152 "bookmark INTEGER DEFAULT 0" // whether is bookmark.
153 ")");
154 if (!GetDB().Execute(sql.c_str())) {
155 LOG(ERROR) << GetDB().GetErrorMessage();
156 return false;
157 }
158
159 sql.assign("CREATE INDEX ");
160 sql.append("android_cache_db.bookmark_cache_url_id_idx ON "
161 "bookmark_cache(url_id)");
162 if (!GetDB().Execute(sql.c_str())) {
163 LOG(ERROR) << GetDB().GetErrorMessage();
164 return false;
165 }
166 return true;
167 }
168
169 bool AndroidCacheDatabase::Attach() {
170 // Commit all open transactions to make attach succeed.
171 if (GetDB().transaction_nesting())
172 GetDB().CommitTransaction();
173
174 bool result = DoAttach();
175
176 // No matter the attach succeed or not, we need to begin a new transaction.
177 GetDB().BeginTransaction();
178 return result;
179 }
180
181 bool AndroidCacheDatabase::DoAttach() {
182 std::string sql("ATTACH ? AS android_cache_db");
183 sql::Statement attach(GetDB().GetUniqueStatement(sql.c_str()));
184 if (!attach.is_valid())
185 // Keep the transaction open, even though we failed.
186 return false;
187
188 attach.BindString(0, db_name_.value());
189 if (!attach.Run())
190 return false;
191
192 return true;
193 }
194
195 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698