| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/contextual_search/browser/weekly_activity_storage.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace { |
| 10 |
| 11 // Keys for ChromePreferenceManager storage of oldest and newest week written. |
| 12 const char kOldestWeekWrittenKey[] = "contextual_search_oldest_week"; |
| 13 const char kNewestWeekWrittenKey[] = "contextual_search_newest_week"; |
| 14 // Prefixes for ChromePreferenceManager storage keyed by week. |
| 15 const char kClicksWeekKeyPrefix[] = "contextual_search_clicks_week_"; |
| 16 const char kImpressionsWeekKeyPrefix[] = "contextual_search_impressions_week_"; |
| 17 |
| 18 // Used for validation in debug build. Week numbers are > 2300 as of year 2016. |
| 19 const int kReasonableMinWeek = 2000; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 namespace contextual_search { |
| 24 |
| 25 WeeklyActivityStorage::WeeklyActivityStorage(int weeks_needed) { |
| 26 weeks_needed_ = weeks_needed; |
| 27 } |
| 28 |
| 29 WeeklyActivityStorage::~WeeklyActivityStorage() {} |
| 30 |
| 31 int WeeklyActivityStorage::ReadClicks(int week_number) { |
| 32 std::string key = GetWeekClicksKey(week_number); |
| 33 return ReadInt(key); |
| 34 } |
| 35 |
| 36 void WeeklyActivityStorage::WriteClicks(int week_number, int value) { |
| 37 std::string key = GetWeekClicksKey(week_number); |
| 38 WriteInt(key, value); |
| 39 } |
| 40 |
| 41 int WeeklyActivityStorage::ReadImpressions(int week_number) { |
| 42 std::string key = GetWeekImpressionsKey(week_number); |
| 43 return ReadInt(key); |
| 44 } |
| 45 |
| 46 void WeeklyActivityStorage::WriteImpressions(int week_number, int value) { |
| 47 std::string key = GetWeekImpressionsKey(week_number); |
| 48 WriteInt(key, value); |
| 49 } |
| 50 |
| 51 bool WeeklyActivityStorage::HasActivity(int week_number) { |
| 52 return ReadInt(kOldestWeekWrittenKey) <= week_number && |
| 53 ReadInt(kNewestWeekWrittenKey) >= week_number; |
| 54 } |
| 55 |
| 56 void WeeklyActivityStorage::ClearActivity(int week_number) { |
| 57 WriteImpressions(week_number, 0); |
| 58 WriteClicks(week_number, 0); |
| 59 } |
| 60 |
| 61 void WeeklyActivityStorage::InitActivity(int week_number) { |
| 62 EnsureHasActivity(week_number); |
| 63 } |
| 64 |
| 65 // private |
| 66 |
| 67 std::string WeeklyActivityStorage::GetWeekImpressionsKey(int which_week) { |
| 68 return kImpressionsWeekKeyPrefix + GetWeekKey(which_week); |
| 69 } |
| 70 |
| 71 std::string WeeklyActivityStorage::GetWeekClicksKey(int which_week) { |
| 72 return kClicksWeekKeyPrefix + GetWeekKey(which_week); |
| 73 } |
| 74 |
| 75 // Round-robin implementation: |
| 76 // GetWeekKey and EnsureHasActivity are implemented with a round-robin |
| 77 // implementation that simply recycles usage of the last N weeks, where N is |
| 78 // less than weeks_needed_. |
| 79 |
| 80 std::string WeeklyActivityStorage::GetWeekKey(int which_week) { |
| 81 return std::to_string(which_week % (weeks_needed_ + 1)); |
| 82 } |
| 83 |
| 84 void WeeklyActivityStorage::EnsureHasActivity(int which_week) { |
| 85 DCHECK(which_week > kReasonableMinWeek); |
| 86 |
| 87 // If still on the newest week we're done! |
| 88 int newest_week = ReadInt(kNewestWeekWrittenKey); |
| 89 if (newest_week == which_week) |
| 90 return; |
| 91 |
| 92 // Update the newest and oldest week written. |
| 93 if (which_week > newest_week) { |
| 94 WriteInt(kNewestWeekWrittenKey, which_week); |
| 95 } |
| 96 int oldest_week = ReadInt(kOldestWeekWrittenKey); |
| 97 if (oldest_week == 0 || oldest_week > which_week) |
| 98 WriteInt(kOldestWeekWrittenKey, which_week); |
| 99 |
| 100 // Any stale weeks to update? |
| 101 if (newest_week == 0) |
| 102 return; |
| 103 |
| 104 // Moved to some new week beyond the newest previously recorded. |
| 105 // Since we recycle storage we must clear the new week and all that we |
| 106 // may have skipped since our last access. |
| 107 int weeks_to_clear = std::min(which_week - newest_week, weeks_needed_); |
| 108 int week = which_week; |
| 109 while (weeks_to_clear > 0) { |
| 110 WriteInt(GetWeekImpressionsKey(week), 0); |
| 111 WriteInt(GetWeekClicksKey(week), 0); |
| 112 week--; |
| 113 weeks_to_clear--; |
| 114 } |
| 115 } |
| 116 |
| 117 // Storage access bottlenecks |
| 118 |
| 119 int WeeklyActivityStorage::ReadInt(std::string storage_bucket) { |
| 120 return ReadStorage(storage_bucket); |
| 121 } |
| 122 |
| 123 void WeeklyActivityStorage::WriteInt(std::string storage_bucket, int value) { |
| 124 WriteStorage(storage_bucket, value); |
| 125 } |
| 126 |
| 127 } // namespace contextual_search |
| OLD | NEW |