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

Side by Side Diff: components/precache/core/precache_url_table.cc

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Added field trials Created 7 years, 1 month 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 2013 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 "components/precache/core/precache_url_table.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "sql/connection.h"
13 #include "sql/statement.h"
14 #include "url/gurl.h"
15
16 using sql::Statement;
17
18 namespace {
19
20 const char kUrlTableName[] = "precache_urls";
21
22 } // namespace
23
24 namespace precache {
25
26 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {}
27
28 PrecacheURLTable::~PrecacheURLTable() {}
29
30 void PrecacheURLTable::Init(sql::Connection* db) {
31 DCHECK(!db_); // Init must only be called once.
32 DCHECK(db); // The database connection must be non-NULL.
33 db_ = db;
34 CreateTableIfNonExistent();
35 }
36
37 void PrecacheURLTable::AddURL(const GURL& url,
38 const base::Time& precache_time) {
39 DCHECK(db_);
40
41 Statement statement(db_->GetCachedStatement(
42 SQL_FROM_HERE,
43 base::StringPrintf("INSERT OR REPLACE INTO %s (url, time) VALUES(?,?)",
44 kUrlTableName).c_str()));
45 statement.BindString(0, GetKey(url));
46 statement.BindInt64(1, precache_time.ToInternalValue());
47
48 statement.Run();
49 }
50
51 bool PrecacheURLTable::HasURL(const GURL& url) {
52 DCHECK(db_);
53
54 Statement statement(db_->GetCachedStatement(
55 SQL_FROM_HERE, base::StringPrintf("SELECT time FROM %s WHERE url=?",
56 kUrlTableName).c_str()));
57 statement.BindString(0, GetKey(url));
58
59 return statement.Step();
60 }
61
62 void PrecacheURLTable::DeleteURL(const GURL& url) {
63 DCHECK(db_);
64
65 Statement statement(db_->GetCachedStatement(
66 SQL_FROM_HERE,
67 base::StringPrintf("DELETE FROM %s WHERE url=?", kUrlTableName).c_str()));
68 statement.BindString(0, GetKey(url));
69
70 statement.Run();
71 }
72
73 void PrecacheURLTable::DeleteAllPrecachedBetween(const base::Time& delete_begin,
74 const base::Time& delete_end) {
75 DCHECK(db_);
76
77 Statement statement(db_->GetCachedStatement(
78 SQL_FROM_HERE,
79 base::StringPrintf("DELETE FROM %s WHERE time >= ? AND time <= ?",
80 kUrlTableName).c_str()));
81
82 statement.BindInt64(0, delete_begin.ToInternalValue());
83 statement.BindInt64(1, delete_end.ToInternalValue());
mmenke 2013/10/31 16:45:00 Are internal values guaranteed to be consistent be
sclittle 2013/11/06 01:51:42 There aren't any consistency guarantees on interna
84
85 statement.Run();
86 }
87
88 void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) {
mmenke 2013/10/31 16:45:00 I suggest clearing the map first, so we have a wel
sclittle 2013/11/06 01:51:42 Done.
89 DCHECK(db_);
90
91 Statement statement(db_->GetCachedStatement(
92 SQL_FROM_HERE,
93 base::StringPrintf("SELECT url, time FROM %s", kUrlTableName).c_str()));
94
95 while (statement.Step()) {
96 GURL url = GURL(statement.ColumnString(0));
97 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1));
98 }
99 }
100
101 // static
102 std::string PrecacheURLTable::GetKey(const GURL& url) {
103 return url.spec();
104 }
mmenke 2013/10/31 16:45:00 This doesn't need to be a member function. Sugges
sclittle 2013/11/06 01:51:42 Done.
105
106 void PrecacheURLTable::CreateTableIfNonExistent() {
107 DCHECK(db_);
108
109 if (db_->DoesTableExist(kUrlTableName))
110 return;
111
112 if (!db_->Execute(base::StringPrintf(
113 "CREATE TABLE %s (url TEXT, time INTEGER, PRIMARY KEY(url))",
114 kUrlTableName).c_str())) {
115 DLOG(WARNING) << "Could not create precache URL table in database.";
116 }
117 }
118
119 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698