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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <fstream> | 9 #include <fstream> |
10 | 10 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 if ((cursor_position != kInvalid) && | 81 if ((cursor_position != kInvalid) && |
82 (cursor_position < lower_string->length()) && (cursor_position > 0)) { | 82 (cursor_position < lower_string->length()) && (cursor_position > 0)) { |
83 lower_string->insert(cursor_position, base::ASCIIToUTF16(" ")); | 83 lower_string->insert(cursor_position, base::ASCIIToUTF16(" ")); |
84 } | 84 } |
85 | 85 |
86 *lower_terms = base::SplitString(*lower_string, base::kWhitespaceUTF16, | 86 *lower_terms = base::SplitString(*lower_string, base::kWhitespaceUTF16, |
87 base::KEEP_WHITESPACE, | 87 base::KEEP_WHITESPACE, |
88 base::SPLIT_WANT_NONEMPTY); | 88 base::SPLIT_WANT_NONEMPTY); |
89 } | 89 } |
90 | 90 |
91 constexpr size_t kItemsToScoreLimit = 500; | |
dyaroshev
2017/02/22 22:21:51
Make constants local.
dyaroshev
2017/02/24 01:27:03
Done.
| |
92 constexpr int kLowVisitCount = 20; | |
93 constexpr int kHighVisitCount = 200; | |
94 constexpr int kLowTypedCount = 2; | |
95 constexpr int kHighTypedCount = 100; | |
96 | |
97 constexpr int kMinRowId = 5000; | |
98 | |
99 base::Time OldVisitTime() { | |
Peter Kasting
2017/02/23 01:04:45
Nit: I would make these const temps in the caller
dyaroshev
2017/02/23 01:41:26
I think think that calling now is good because we
dyaroshev
2017/02/24 01:27:03
I've done smth. Is it ok?
| |
100 return base::Time::Now() - base::TimeDelta::FromDays(15); | |
101 }; | |
102 base::Time RecentVisitTime() { | |
103 return base::Time::Now() - base::TimeDelta::FromDays(2); | |
104 }; | |
105 | |
106 HistoryIDSet FillHistoryIDSet(HistoryID min, HistoryID max) { | |
107 HistoryIDSet res; | |
108 // All ids are inserted in the end so the implicit hint would work. | |
109 for (HistoryID id = min; id < max; ++id) | |
110 res.insert(id); | |
111 return res; | |
112 } | |
113 | |
91 } // namespace | 114 } // namespace |
92 | 115 |
93 // ----------------------------------------------------------------------------- | 116 // ----------------------------------------------------------------------------- |
94 | 117 |
95 // Observer class so the unit tests can wait while the cache is being saved. | 118 // Observer class so the unit tests can wait while the cache is being saved. |
96 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver { | 119 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver { |
97 public: | 120 public: |
98 explicit CacheFileSaverObserver(const base::Closure& task); | 121 explicit CacheFileSaverObserver(const base::Closure& task); |
99 | 122 |
100 bool succeeded() { return succeeded_; } | 123 bool succeeded() { return succeeded_; } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 base::CancelableTaskTracker* GetPrivateDataTracker() const; | 176 base::CancelableTaskTracker* GetPrivateDataTracker() const; |
154 void ClearPrivateData(); | 177 void ClearPrivateData(); |
155 void set_history_dir(const base::FilePath& dir_path); | 178 void set_history_dir(const base::FilePath& dir_path); |
156 bool GetCacheFilePath(base::FilePath* file_path) const; | 179 bool GetCacheFilePath(base::FilePath* file_path) const; |
157 void PostRestoreFromCacheFileTask(); | 180 void PostRestoreFromCacheFileTask(); |
158 void PostSaveToCacheFileTask(); | 181 void PostSaveToCacheFileTask(); |
159 const SchemeSet& scheme_whitelist(); | 182 const SchemeSet& scheme_whitelist(); |
160 | 183 |
161 // Pass-through functions to simplify our friendship with URLIndexPrivateData. | 184 // Pass-through functions to simplify our friendship with URLIndexPrivateData. |
162 bool UpdateURL(const history::URLRow& row); | 185 bool UpdateURL(const history::URLRow& row); |
186 void AddHistoryEntry(history::URLID row_id, | |
187 int typed_count, | |
188 int visit_count, | |
189 base::Time last_visit); | |
163 bool DeleteURL(const GURL& url); | 190 bool DeleteURL(const GURL& url); |
164 | 191 |
165 // Data verification helper functions. | 192 // Data verification helper functions. |
166 void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); | 193 void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); |
167 void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); | 194 void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); |
168 void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, | 195 void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, |
169 const URLIndexPrivateData& actual); | 196 const URLIndexPrivateData& actual); |
170 | 197 |
171 base::MessageLoop message_loop_; | 198 base::MessageLoop message_loop_; |
172 base::SequencedWorkerPoolOwner pool_owner_; | 199 base::SequencedWorkerPoolOwner pool_owner_; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 const SchemeSet& InMemoryURLIndexTest::scheme_whitelist() { | 246 const SchemeSet& InMemoryURLIndexTest::scheme_whitelist() { |
220 return url_index_->scheme_whitelist(); | 247 return url_index_->scheme_whitelist(); |
221 } | 248 } |
222 | 249 |
223 bool InMemoryURLIndexTest::UpdateURL(const history::URLRow& row) { | 250 bool InMemoryURLIndexTest::UpdateURL(const history::URLRow& row) { |
224 return GetPrivateData()->UpdateURL( | 251 return GetPrivateData()->UpdateURL( |
225 history_service_.get(), row, url_index_->scheme_whitelist_, | 252 history_service_.get(), row, url_index_->scheme_whitelist_, |
226 GetPrivateDataTracker()); | 253 GetPrivateDataTracker()); |
227 } | 254 } |
228 | 255 |
256 void InMemoryURLIndexTest::AddHistoryEntry(history::URLID row_id, | |
257 int typed_count, | |
258 int visit_count, | |
259 base::Time last_visit) { | |
260 history::URLRow new_row( | |
261 GURL("http://www.fake_url" + std::to_string(row_id) + ".com"), row_id); | |
262 new_row.set_visit_count(visit_count); | |
263 new_row.set_typed_count(typed_count); | |
264 new_row.set_last_visit(last_visit); | |
265 UpdateURL(std::move(new_row)); | |
266 }; | |
267 | |
229 bool InMemoryURLIndexTest::DeleteURL(const GURL& url) { | 268 bool InMemoryURLIndexTest::DeleteURL(const GURL& url) { |
230 return GetPrivateData()->DeleteURL(url); | 269 return GetPrivateData()->DeleteURL(url); |
231 } | 270 } |
232 | 271 |
233 void InMemoryURLIndexTest::SetUp() { | 272 void InMemoryURLIndexTest::SetUp() { |
234 // We cannot access the database until the backend has been loaded. | 273 // We cannot access the database until the backend has been loaded. |
235 if (history_dir_.CreateUniqueTempDir()) | 274 if (history_dir_.CreateUniqueTempDir()) |
236 history_service_ = | 275 history_service_ = |
237 history::CreateHistoryService(history_dir_.GetPath(), true); | 276 history::CreateHistoryService(history_dir_.GetPath(), true); |
238 ASSERT_TRUE(history_service_); | 277 ASSERT_TRUE(history_service_); |
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
703 ASCIIToUTF16("atdmt view"), base::string16::npos, kMaxMatches); | 742 ASCIIToUTF16("atdmt view"), base::string16::npos, kMaxMatches); |
704 EXPECT_EQ(1U, matches.size()); | 743 EXPECT_EQ(1U, matches.size()); |
705 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view"), | 744 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view"), |
706 base::string16::npos, kMaxMatches); | 745 base::string16::npos, kMaxMatches); |
707 EXPECT_EQ(0U, matches.size()); | 746 EXPECT_EQ(0U, matches.size()); |
708 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt"), | 747 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt"), |
709 base::string16::npos, kMaxMatches); | 748 base::string16::npos, kMaxMatches); |
710 EXPECT_EQ(1U, matches.size()); | 749 EXPECT_EQ(1U, matches.size()); |
711 } | 750 } |
712 | 751 |
752 TEST_F(InMemoryURLIndexTest, DontTrimMoreThanLimit) { | |
Peter Kasting
2017/02/23 01:04:45
Nit: Especially for the second test you add, but r
dyaroshev
2017/02/24 01:27:03
Done?
| |
753 history::URLID row_id = kMinRowId; | |
754 AddHistoryEntry(row_id, kLowTypedCount, kLowVisitCount, OldVisitTime()); | |
755 for (++row_id; size_t(row_id) < kItemsToScoreLimit - 1; ++row_id) { | |
Peter Kasting
2017/02/23 01:04:45
I don't think this works right? kMinRowId = 5000
dyaroshev
2017/02/24 01:27:03
Not applicable.
| |
756 AddHistoryEntry(row_id, kLowTypedCount, kLowVisitCount, OldVisitTime()); | |
757 } | |
dyaroshev
2017/02/22 22:21:51
One extra addition is unnecessary.
Peter Kasting
2017/02/23 01:04:45
Not sure what this is saying. Is it explaining th
dyaroshev
2017/02/23 01:41:26
It's more for me - to avoid extra patches, if I se
Peter Kasting
2017/02/23 01:46:06
OK. I still don't really get the "- 1" in the loo
dyaroshev
2017/02/24 01:27:03
Not applicable.
| |
758 | |
759 auto history_id_set = FillHistoryIDSet(kMinRowId, row_id); | |
dyaroshev
2017/02/22 22:21:52
Check that all ids are in
| |
760 | |
761 EXPECT_FALSE(GetPrivateData()->TrimHistoryIdsPool(&history_id_set)); | |
762 EXPECT_EQ(size_t(row_id - kMinRowId), history_id_set.size()); | |
763 } | |
764 | |
765 TEST_F(InMemoryURLIndexTest, TrimByProrities) { | |
766 int expected_worst = 1; | |
Peter Kasting
2017/02/23 01:04:45
Nit: I can't instantly tell what this code is doin
dyaroshev
2017/02/24 01:27:03
Rewrote the test.
| |
767 int expected_recent = 2; | |
768 int expected_visit_count = 3; | |
769 int expected_typed_count = kItemsToScoreLimit - expected_visit_count - | |
770 expected_recent - expected_worst; | |
771 | |
772 const history::URLID last_high_typed_count = kMinRowId + expected_typed_count; | |
773 const history::URLID last_high_visit_count = | |
774 last_high_typed_count + expected_visit_count; | |
775 const history::URLID last_recent = last_high_visit_count + expected_recent; | |
776 const history::URLID last_worst = last_recent + kItemsToScoreLimit; | |
777 | |
778 history::URLID row_id = kMinRowId; | |
779 for (; row_id < last_high_typed_count; ++row_id) | |
780 AddHistoryEntry(row_id, kHighTypedCount, kLowVisitCount, OldVisitTime()); | |
781 | |
782 for (; row_id < last_high_visit_count; ++row_id) | |
783 AddHistoryEntry(row_id, kLowTypedCount, kHighVisitCount, OldVisitTime()); | |
784 | |
785 for (; row_id < last_recent; ++row_id) | |
786 AddHistoryEntry(row_id, kLowTypedCount, kLowVisitCount, RecentVisitTime()); | |
787 | |
788 for (; row_id < last_worst; ++row_id) | |
789 AddHistoryEntry(row_id, kLowTypedCount, kLowVisitCount, OldVisitTime()); | |
790 | |
791 auto history_id_set = FillHistoryIDSet(kMinRowId, row_id); | |
792 EXPECT_TRUE(GetPrivateData()->TrimHistoryIdsPool(&history_id_set)); | |
793 EXPECT_EQ(kItemsToScoreLimit, history_id_set.size()); | |
794 | |
795 for (HistoryID id : history_id_set) { | |
dyaroshev
2017/02/22 22:21:52
We could do the checks more efficiently for the se
| |
796 if (id < last_high_typed_count) | |
797 --expected_typed_count; | |
798 else if (id < last_high_visit_count) | |
799 --expected_visit_count; | |
800 else if (id < last_recent) | |
801 --expected_recent; | |
802 else if (id < last_worst) | |
803 --expected_worst; | |
804 } | |
805 | |
806 EXPECT_EQ(0, expected_typed_count); | |
807 EXPECT_EQ(0, expected_visit_count); | |
808 EXPECT_EQ(0, expected_recent); | |
809 EXPECT_EQ(0, expected_worst); | |
810 } | |
811 | |
713 TEST_F(InMemoryURLIndexTest, HugeResultSet) { | 812 TEST_F(InMemoryURLIndexTest, HugeResultSet) { |
714 // Create a huge set of qualifying history items. | 813 // Create a huge set of qualifying history items. |
715 for (history::URLID row_id = 5000; row_id < 6000; ++row_id) { | 814 for (history::URLID row_id = 5000; row_id < 6000; ++row_id) { |
716 history::URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), | 815 history::URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), |
717 row_id); | 816 row_id); |
718 new_row.set_last_visit(base::Time::Now()); | 817 new_row.set_last_visit(base::Time::Now()); |
719 EXPECT_TRUE(UpdateURL(new_row)); | 818 EXPECT_TRUE(UpdateURL(new_row)); |
720 } | 819 } |
721 | 820 |
722 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms( | 821 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms( |
723 ASCIIToUTF16("b"), base::string16::npos, kMaxMatches); | 822 ASCIIToUTF16("b"), base::string16::npos, kMaxMatches); |
724 URLIndexPrivateData& private_data(*GetPrivateData()); | |
725 EXPECT_EQ(kMaxMatches, matches.size()); | 823 EXPECT_EQ(kMaxMatches, matches.size()); |
726 // There are 7 matches already in the database. | |
727 EXPECT_EQ(1008U, private_data.pre_filter_item_count_); | |
728 EXPECT_EQ(500U, private_data.post_filter_item_count_); | |
729 EXPECT_EQ(kMaxMatches, private_data.post_scoring_item_count_); | |
730 } | 824 } |
731 | 825 |
732 TEST_F(InMemoryURLIndexTest, TitleSearch) { | 826 TEST_F(InMemoryURLIndexTest, TitleSearch) { |
733 // Signal if someone has changed the test DB. | 827 // Signal if someone has changed the test DB. |
734 EXPECT_EQ(30U, GetPrivateData()->history_info_map_.size()); | 828 EXPECT_EQ(30U, GetPrivateData()->history_info_map_.size()); |
735 | 829 |
736 // Ensure title is being searched. | 830 // Ensure title is being searched. |
737 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms( | 831 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms( |
738 ASCIIToUTF16("MORTGAGE RATE DROPS"), base::string16::npos, kMaxMatches); | 832 ASCIIToUTF16("MORTGAGE RATE DROPS"), base::string16::npos, kMaxMatches); |
739 ASSERT_EQ(1U, matches.size()); | 833 ASSERT_EQ(1U, matches.size()); |
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1345 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); | 1439 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); |
1346 std::vector<base::FilePath::StringType> actual_parts; | 1440 std::vector<base::FilePath::StringType> actual_parts; |
1347 full_file_path.GetComponents(&actual_parts); | 1441 full_file_path.GetComponents(&actual_parts); |
1348 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 1442 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
1349 size_t count = expected_parts.size(); | 1443 size_t count = expected_parts.size(); |
1350 for (size_t i = 0; i < count; ++i) | 1444 for (size_t i = 0; i < count; ++i) |
1351 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 1445 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
1352 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 1446 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
1353 set_history_dir(base::FilePath()); | 1447 set_history_dir(base::FilePath()); |
1354 } | 1448 } |
OLD | NEW |