File: weekly_activity_storage.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (129 lines) | stat: -rw-r--r-- 4,006 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/contextual_search/browser/weekly_activity_storage.h"

#include <algorithm>  // std::min

#include "base/logging.h"

namespace {

// Keys for ChromePreferenceManager storage of oldest and newest week written.
const char kOldestWeekWrittenKey[] = "contextual_search_oldest_week";
const char kNewestWeekWrittenKey[] = "contextual_search_newest_week";
// Prefixes for ChromePreferenceManager storage keyed by week.
const char kClicksWeekKeyPrefix[] = "contextual_search_clicks_week_";
const char kImpressionsWeekKeyPrefix[] = "contextual_search_impressions_week_";

// Used for validation in debug build.  Week numbers are > 2300 as of year 2016.
const int kReasonableMinWeek = 2000;

}  // namespace

namespace contextual_search {

WeeklyActivityStorage::WeeklyActivityStorage(int weeks_needed) {
  weeks_needed_ = weeks_needed;
}

WeeklyActivityStorage::~WeeklyActivityStorage() {}

int WeeklyActivityStorage::ReadClicks(int week_number) {
  std::string key = GetWeekClicksKey(week_number);
  return ReadInt(key);
}

void WeeklyActivityStorage::WriteClicks(int week_number, int value) {
  std::string key = GetWeekClicksKey(week_number);
  WriteInt(key, value);
}

int WeeklyActivityStorage::ReadImpressions(int week_number) {
  std::string key = GetWeekImpressionsKey(week_number);
  return ReadInt(key);
}

void WeeklyActivityStorage::WriteImpressions(int week_number, int value) {
  std::string key = GetWeekImpressionsKey(week_number);
  WriteInt(key, value);
}

bool WeeklyActivityStorage::HasData(int week_number) {
  return ReadInt(kOldestWeekWrittenKey) <= week_number &&
         ReadInt(kNewestWeekWrittenKey) >= week_number;
}

void WeeklyActivityStorage::ClearData(int week_number) {
  WriteImpressions(week_number, 0);
  WriteClicks(week_number, 0);
}

void WeeklyActivityStorage::AdvanceToWeek(int week_number) {
  EnsureHasActivity(week_number);
}

// private

std::string WeeklyActivityStorage::GetWeekImpressionsKey(int which_week) {
  return kImpressionsWeekKeyPrefix + GetWeekKey(which_week);
}

std::string WeeklyActivityStorage::GetWeekClicksKey(int which_week) {
  return kClicksWeekKeyPrefix + GetWeekKey(which_week);
}

// Round-robin implementation:
// GetWeekKey and EnsureHasActivity are implemented with a round-robin
// implementation that simply recycles usage of the last N weeks, where N is
// less than weeks_needed_.

std::string WeeklyActivityStorage::GetWeekKey(int which_week) {
  return std::to_string(which_week % (weeks_needed_ + 1));
}

void WeeklyActivityStorage::EnsureHasActivity(int which_week) {
  DCHECK(which_week > kReasonableMinWeek);

  // If still on the newest week we're done!
  int newest_week = ReadInt(kNewestWeekWrittenKey);
  if (newest_week == which_week)
    return;

  // Update the newest and oldest week written.
  if (which_week > newest_week) {
    WriteInt(kNewestWeekWrittenKey, which_week);
  }
  int oldest_week = ReadInt(kOldestWeekWrittenKey);
  if (oldest_week == 0 || oldest_week > which_week)
    WriteInt(kOldestWeekWrittenKey, which_week);

  // Any stale weeks to update?
  if (newest_week == 0)
    return;

  // Moved to some new week beyond the newest previously recorded.
  // Since we recycle storage we must clear the new week and all that we
  // may have skipped since our last access.
  int weeks_to_clear = std::min(which_week - newest_week, weeks_needed_);
  int week = which_week;
  while (weeks_to_clear > 0) {
    WriteInt(GetWeekImpressionsKey(week), 0);
    WriteInt(GetWeekClicksKey(week), 0);
    week--;
    weeks_to_clear--;
  }
}

// Storage access bottlenecks

int WeeklyActivityStorage::ReadInt(std::string storage_bucket) {
  return ReadStorage(storage_bucket);
}

void WeeklyActivityStorage::WriteInt(std::string storage_bucket, int value) {
  WriteStorage(storage_bucket, value);
}

}  // namespace contextual_search