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/history_backend.h" | 5 #include "chrome/browser/history/history_backend.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 1264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1275 } | 1275 } |
1276 | 1276 |
1277 // Basic time-based querying of history. | 1277 // Basic time-based querying of history. |
1278 void HistoryBackend::QueryHistoryBasic(URLDatabase* url_db, | 1278 void HistoryBackend::QueryHistoryBasic(URLDatabase* url_db, |
1279 VisitDatabase* visit_db, | 1279 VisitDatabase* visit_db, |
1280 const QueryOptions& options, | 1280 const QueryOptions& options, |
1281 QueryResults* result) { | 1281 QueryResults* result) { |
1282 // First get all visits. | 1282 // First get all visits. |
1283 VisitVector visits; | 1283 VisitVector visits; |
1284 visit_db->GetVisibleVisitsInRange(options.begin_time, options.end_time, | 1284 visit_db->GetVisibleVisitsInRange(options.begin_time, options.end_time, |
1285 options.max_count, &visits); | 1285 options.max_count, &visits, true); |
1286 DCHECK(options.max_count == 0 || | 1286 DCHECK(options.max_count == 0 || |
1287 static_cast<int>(visits.size()) <= options.max_count); | 1287 static_cast<int>(visits.size()) <= options.max_count); |
1288 | 1288 |
1289 // Now add them and the URL rows to the results. | 1289 // Now add them and the URL rows to the results. |
1290 URLResult url_result; | 1290 URLResult url_result; |
1291 for (size_t i = 0; i < visits.size(); i++) { | 1291 for (size_t i = 0; i < visits.size(); i++) { |
1292 const VisitRow visit = visits[i]; | 1292 const VisitRow visit = visits[i]; |
1293 | 1293 |
1294 // Add a result row for this visit, get the URL info from the DB. | 1294 // Add a result row for this visit, get the URL info from the DB. |
1295 if (!url_db->GetURLRow(visit.url_id, &url_result)) { | 1295 if (!url_db->GetURLRow(visit.url_id, &url_result)) { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1475 VisitVector visits; | 1475 VisitVector visits; |
1476 db_->GetVisibleVisitsDuringTimes(filter, 0, &visits); | 1476 db_->GetVisibleVisitsDuringTimes(filter, 0, &visits); |
1477 | 1477 |
1478 std::map<VisitID, std::pair<VisitID, URLID> > segment_ids; | 1478 std::map<VisitID, std::pair<VisitID, URLID> > segment_ids; |
1479 for (size_t i = 0; i < visits.size(); ++i) { | 1479 for (size_t i = 0; i < visits.size(); ++i) { |
1480 segment_ids[visits[i].visit_id] = | 1480 segment_ids[visits[i].visit_id] = |
1481 std::make_pair(visits[i].referring_visit, visits[i].segment_id); | 1481 std::make_pair(visits[i].referring_visit, visits[i].segment_id); |
1482 } | 1482 } |
1483 | 1483 |
1484 std::map<URLID, double> score_map; | 1484 std::map<URLID, double> score_map; |
1485 const double kLn2 = 0.6931471805599453; | |
1486 base::Time now = base::Time::Now(); | |
1487 for (size_t i = 0; i < visits.size(); ++i) { | 1485 for (size_t i = 0; i < visits.size(); ++i) { |
1488 URLID segment_id = visits[i].segment_id; | 1486 URLID segment_id = visits[i].segment_id; |
1489 for (VisitID visit_id = visits[i].visit_id; !segment_id && visit_id;) { | 1487 for (VisitID visit_id = visits[i].visit_id; !segment_id && visit_id;) { |
1490 std::map<VisitID, std::pair<VisitID, URLID> >::iterator vi = | 1488 std::map<VisitID, std::pair<VisitID, URLID> >::iterator vi = |
1491 segment_ids.find(visit_id); | 1489 segment_ids.find(visit_id); |
1492 if (vi == segment_ids.end()) { | 1490 if (vi == segment_ids.end()) { |
1493 VisitRow visit_row; | 1491 VisitRow visit_row; |
1494 if (!db_->GetRowForVisit(visit_id, &visit_row)) | 1492 if (!db_->GetRowForVisit(visit_id, &visit_row)) |
1495 break; | 1493 break; |
1496 segment_ids[visit_id] = | 1494 segment_ids[visit_id] = |
1497 std::make_pair(visit_row.referring_visit, visit_row.segment_id); | 1495 std::make_pair(visit_row.referring_visit, visit_row.segment_id); |
1498 segment_id = visit_row.segment_id; | 1496 segment_id = visit_row.segment_id; |
1499 visit_id = visit_row.referring_visit; | 1497 visit_id = visit_row.referring_visit; |
1500 } else { | 1498 } else { |
1501 visit_id = vi->second.first; | 1499 visit_id = vi->second.first; |
1502 segment_id = vi->second.second; | 1500 segment_id = vi->second.second; |
1503 } | 1501 } |
1504 } | 1502 } |
1505 if (!segment_id) | 1503 if (!segment_id) |
1506 continue; | 1504 continue; |
1507 double score = 0.0; | 1505 double score = filter.GetVisitScore(visits[i]); |
1508 switch (filter.sorting_order()) { | |
1509 case VisitFilter::ORDER_BY_RECENCY: { | |
1510 // Decay score by half each week. | |
1511 base::TimeDelta time_passed = now - visits[i].visit_time; | |
1512 // Clamp to 0 in case time jumps backwards (e.g. due to DST). | |
1513 double decay_exponent = std::max(0.0, kLn2 * static_cast<double>( | |
1514 time_passed.InMicroseconds()) / base::Time::kMicrosecondsPerWeek); | |
1515 score = 1.0 / exp(decay_exponent); | |
1516 } break; | |
1517 case VisitFilter::ORDER_BY_VISIT_COUNT: | |
1518 score = 1.0; // Every visit counts the same. | |
1519 break; | |
1520 case VisitFilter::ORDER_BY_DURATION_SPENT: | |
1521 NOTREACHED() << "Not implemented!"; | |
1522 break; | |
1523 } | |
1524 | 1506 |
1525 std::map<URLID, double>::iterator it = score_map.find(visits[i].segment_id); | 1507 std::map<URLID, double>::iterator it = score_map.find(visits[i].segment_id); |
1526 if (it == score_map.end()) | 1508 if (it == score_map.end()) |
1527 score_map[visits[i].segment_id] = score; | 1509 score_map[visits[i].segment_id] = score; |
1528 else | 1510 else |
1529 it->second += score; | 1511 it->second += score; |
1530 } | 1512 } |
1531 | 1513 |
1532 // TODO(georgey): experiment with visit_segment database granularity (it is | 1514 // TODO(georgey): experiment with visit_segment database granularity (it is |
1533 // currently 24 hours) to use it directly instead of using visits database, | 1515 // currently 24 hours) to use it directly instead of using visits database, |
(...skipping 975 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2509 info.url_id = visit.url_id; | 2491 info.url_id = visit.url_id; |
2510 info.time = visit.visit_time; | 2492 info.time = visit.visit_time; |
2511 info.transition = visit.transition; | 2493 info.transition = visit.transition; |
2512 // If we don't have a delegate yet during setup or shutdown, we will drop | 2494 // If we don't have a delegate yet during setup or shutdown, we will drop |
2513 // these notifications. | 2495 // these notifications. |
2514 if (delegate_.get()) | 2496 if (delegate_.get()) |
2515 delegate_->NotifyVisitDBObserversOnAddVisit(info); | 2497 delegate_->NotifyVisitDBObserversOnAddVisit(info); |
2516 } | 2498 } |
2517 | 2499 |
2518 } // namespace history | 2500 } // namespace history |
OLD | NEW |