OLD | NEW |
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 "chrome/browser/history/visit_database.h" | 5 #include "chrome/browser/history/visit_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 111 |
112 while (statement.Step()) { | 112 while (statement.Step()) { |
113 history::VisitRow visit; | 113 history::VisitRow visit; |
114 FillVisitRow(statement, &visit); | 114 FillVisitRow(statement, &visit); |
115 visits->push_back(visit); | 115 visits->push_back(visit); |
116 } | 116 } |
117 | 117 |
118 return statement.Succeeded(); | 118 return statement.Succeeded(); |
119 } | 119 } |
120 | 120 |
| 121 // static |
| 122 bool VisitDatabase::FillVisitVectorWithOptions(sql::Statement& statement, |
| 123 const QueryOptions& options, |
| 124 VisitVector* visits) { |
| 125 std::set<URLID> found_urls; |
| 126 |
| 127 // Keeps track of the day that |found_urls| is holding the URLs for, in order |
| 128 // to handle removing per-day duplicates. |
| 129 base::Time found_urls_midnight; |
| 130 |
| 131 while (statement.Step()) { |
| 132 VisitRow visit; |
| 133 FillVisitRow(statement, &visit); |
| 134 |
| 135 if (options.duplicate_policy != QueryOptions::KEEP_ALL_DUPLICATES) { |
| 136 if (options.duplicate_policy == QueryOptions::REMOVE_DUPLICATES_PER_DAY && |
| 137 found_urls_midnight != visit.visit_time.LocalMidnight()) { |
| 138 found_urls.clear(); |
| 139 found_urls_midnight = visit.visit_time.LocalMidnight(); |
| 140 } |
| 141 // Make sure the URL this visit corresponds to is unique. |
| 142 if (found_urls.find(visit.url_id) != found_urls.end()) |
| 143 continue; |
| 144 found_urls.insert(visit.url_id); |
| 145 } |
| 146 |
| 147 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) |
| 148 return true; |
| 149 visits->push_back(visit); |
| 150 } |
| 151 return false; |
| 152 } |
| 153 |
121 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { | 154 VisitID VisitDatabase::AddVisit(VisitRow* visit, VisitSource source) { |
122 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 155 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
123 "INSERT INTO visits " | 156 "INSERT INTO visits " |
124 "(url, visit_time, from_visit, transition, segment_id, is_indexed, " | 157 "(url, visit_time, from_visit, transition, segment_id, is_indexed, " |
125 "visit_duration) VALUES (?,?,?,?,?,?,?)")); | 158 "visit_duration) VALUES (?,?,?,?,?,?,?)")); |
126 statement.BindInt64(0, visit->url_id); | 159 statement.BindInt64(0, visit->url_id); |
127 statement.BindInt64(1, visit->visit_time.ToInternalValue()); | 160 statement.BindInt64(1, visit->visit_time.ToInternalValue()); |
128 statement.BindInt64(2, visit->referring_visit); | 161 statement.BindInt64(2, visit->referring_visit); |
129 statement.BindInt64(3, visit->transition); | 162 statement.BindInt64(3, visit->transition); |
130 statement.BindInt64(4, visit->segment_id); | 163 statement.BindInt64(4, visit->segment_id); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 visits->clear(); | 271 visits->clear(); |
239 | 272 |
240 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 273 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
241 "SELECT" HISTORY_VISIT_ROW_FIELDS | 274 "SELECT" HISTORY_VISIT_ROW_FIELDS |
242 "FROM visits " | 275 "FROM visits " |
243 "WHERE url=? AND is_indexed=1")); | 276 "WHERE url=? AND is_indexed=1")); |
244 statement.BindInt64(0, url_id); | 277 statement.BindInt64(0, url_id); |
245 return FillVisitVector(statement, visits); | 278 return FillVisitVector(statement, visits); |
246 } | 279 } |
247 | 280 |
| 281 bool VisitDatabase::GetVisitsForURLWithOptions(URLID url_id, |
| 282 const QueryOptions& options, |
| 283 VisitVector* visits) { |
| 284 visits->clear(); |
| 285 |
| 286 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 287 "SELECT" HISTORY_VISIT_ROW_FIELDS |
| 288 "FROM visits " |
| 289 "WHERE url=? " |
| 290 "ORDER BY visit_time ASC")); |
| 291 statement.BindInt64(0, url_id); |
| 292 |
| 293 return FillVisitVectorWithOptions(statement, options, visits); |
| 294 } |
248 | 295 |
249 bool VisitDatabase::GetVisitsForTimes(const std::vector<base::Time>& times, | 296 bool VisitDatabase::GetVisitsForTimes(const std::vector<base::Time>& times, |
250 VisitVector* visits) { | 297 VisitVector* visits) { |
251 visits->clear(); | 298 visits->clear(); |
252 | 299 |
253 for (std::vector<base::Time>::const_iterator it = times.begin(); | 300 for (std::vector<base::Time>::const_iterator it = times.begin(); |
254 it != times.end(); ++it) { | 301 it != times.end(); ++it) { |
255 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 302 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
256 "SELECT" HISTORY_VISIT_ROW_FIELDS "FROM visits " | 303 "SELECT" HISTORY_VISIT_ROW_FIELDS "FROM visits " |
257 "WHERE visit_time == ?")); | 304 "WHERE visit_time == ?")); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 "ORDER BY visit_time DESC, id DESC")); | 373 "ORDER BY visit_time DESC, id DESC")); |
327 | 374 |
328 statement.BindInt64(0, options.EffectiveBeginTime()); | 375 statement.BindInt64(0, options.EffectiveBeginTime()); |
329 statement.BindInt64(1, options.EffectiveEndTime()); | 376 statement.BindInt64(1, options.EffectiveEndTime()); |
330 statement.BindInt(2, content::PAGE_TRANSITION_CHAIN_END); | 377 statement.BindInt(2, content::PAGE_TRANSITION_CHAIN_END); |
331 statement.BindInt(3, content::PAGE_TRANSITION_CORE_MASK); | 378 statement.BindInt(3, content::PAGE_TRANSITION_CORE_MASK); |
332 statement.BindInt(4, content::PAGE_TRANSITION_AUTO_SUBFRAME); | 379 statement.BindInt(4, content::PAGE_TRANSITION_AUTO_SUBFRAME); |
333 statement.BindInt(5, content::PAGE_TRANSITION_MANUAL_SUBFRAME); | 380 statement.BindInt(5, content::PAGE_TRANSITION_MANUAL_SUBFRAME); |
334 statement.BindInt(6, content::PAGE_TRANSITION_KEYWORD_GENERATED); | 381 statement.BindInt(6, content::PAGE_TRANSITION_KEYWORD_GENERATED); |
335 | 382 |
336 std::set<URLID> found_urls; | 383 return FillVisitVectorWithOptions(statement, options, visits); |
337 | |
338 // Keeps track of the day that |found_urls| is holding the URLs for, in order | |
339 // to handle removing per-day duplicates. | |
340 base::Time found_urls_midnight; | |
341 | |
342 while (statement.Step()) { | |
343 VisitRow visit; | |
344 FillVisitRow(statement, &visit); | |
345 | |
346 if (options.duplicate_policy != QueryOptions::KEEP_ALL_DUPLICATES) { | |
347 if (options.duplicate_policy == QueryOptions::REMOVE_DUPLICATES_PER_DAY && | |
348 found_urls_midnight != visit.visit_time.LocalMidnight()) { | |
349 found_urls.clear(); | |
350 found_urls_midnight = visit.visit_time.LocalMidnight(); | |
351 } | |
352 // Make sure the URL this visit corresponds to is unique. | |
353 if (found_urls.find(visit.url_id) != found_urls.end()) | |
354 continue; | |
355 found_urls.insert(visit.url_id); | |
356 } | |
357 | |
358 if (static_cast<int>(visits->size()) >= options.EffectiveMaxCount()) | |
359 return true; | |
360 visits->push_back(visit); | |
361 } | |
362 return false; | |
363 } | 384 } |
364 | 385 |
365 void VisitDatabase::GetDirectVisitsDuringTimes(const VisitFilter& time_filter, | 386 void VisitDatabase::GetDirectVisitsDuringTimes(const VisitFilter& time_filter, |
366 int max_results, | 387 int max_results, |
367 VisitVector* visits) { | 388 VisitVector* visits) { |
368 visits->clear(); | 389 visits->clear(); |
369 if (max_results) | 390 if (max_results) |
370 visits->reserve(max_results); | 391 visits->reserve(max_results); |
371 for (VisitFilter::TimeVector::const_iterator it = time_filter.times().begin(); | 392 for (VisitFilter::TimeVector::const_iterator it = time_filter.times().begin(); |
372 it != time_filter.times().end(); ++it) { | 393 it != time_filter.times().end(); ++it) { |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 while (statement.Step()) { | 628 while (statement.Step()) { |
608 BriefVisitInfo info; | 629 BriefVisitInfo info; |
609 info.url_id = statement.ColumnInt64(0); | 630 info.url_id = statement.ColumnInt64(0); |
610 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); | 631 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
611 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); | 632 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); |
612 result_vector->push_back(info); | 633 result_vector->push_back(info); |
613 } | 634 } |
614 } | 635 } |
615 | 636 |
616 } // namespace history | 637 } // namespace history |
OLD | NEW |