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