File: test_inmemory_strike_database.cc

package info (click to toggle)
chromium 145.0.7632.109-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,974,804 kB
  • sloc: cpp: 36,197,696; ansic: 7,602,761; javascript: 3,563,590; python: 1,649,324; xml: 838,427; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,022; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (109 lines) | stat: -rw-r--r-- 3,381 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/strike_database/test_inmemory_strike_database.h"

#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "components/strike_database/strike_data.pb.h"

namespace strike_database {

TestInMemoryStrikeDatabase::TestInMemoryStrikeDatabase() = default;

TestInMemoryStrikeDatabase::~TestInMemoryStrikeDatabase() = default;

int TestInMemoryStrikeDatabase::AddStrikes(int strikes_increase,
                                           const std::string& key) {
  DCHECK_GT(strikes_increase, 0);
  int num_strikes =
      strike_map_cache_.contains(key)
          ? strike_map_cache_[key].num_strikes() + strikes_increase
          : strikes_increase;
  SetStrikeData(key, num_strikes);
  return num_strikes;
}

int TestInMemoryStrikeDatabase::RemoveStrikes(int strikes_decrease,
                                              const std::string& key) {
  int num_strikes = GetStrikes(key);
  num_strikes = std::max(0, num_strikes - strikes_decrease);
  SetStrikeData(key, num_strikes);
  return num_strikes;
}

int TestInMemoryStrikeDatabase::GetStrikes(const std::string& key) {
  auto iter = strike_map_cache_.find(key);
  return (iter != strike_map_cache_.end()) ? iter->second.num_strikes() : 0;
}

void TestInMemoryStrikeDatabase::ClearStrikes(const std::string& key) {
  strike_map_cache_.erase(key);
}

std::map<std::string, StrikeData>&
TestInMemoryStrikeDatabase::GetStrikeCache() {
  return strike_map_cache_;
}

std::vector<std::string> TestInMemoryStrikeDatabase::GetAllStrikeKeysForProject(
    const std::string& project_prefix) {
  std::vector<std::string> project_keys;
  for (const auto& [key, data] : strike_map_cache_) {
    if (key.find(project_prefix) == 0) {
      project_keys.push_back(key);
    }
  }
  return project_keys;
}

void TestInMemoryStrikeDatabase::ClearAllStrikesForProject(
    const std::string& project_prefix) {
  ClearStrikesForKeys(GetAllStrikeKeysForProject(project_prefix));
}

void TestInMemoryStrikeDatabase::ClearStrikesForKeys(
    const std::vector<std::string>& keys_to_remove) {
  for (const auto& key : keys_to_remove) {
    strike_map_cache_.erase(key);
  }
}

void TestInMemoryStrikeDatabase::ClearAllStrikes() {
  strike_map_cache_.clear();
}

std::string TestInMemoryStrikeDatabase::GetPrefixFromKey(
    const std::string& key) const {
  return key.substr(0, key.find(kKeyDeliminator));
}

void TestInMemoryStrikeDatabase::SetStrikeData(const std::string& key,
                                               int num_strikes) {
  if (num_strikes == 0) {
    ClearStrikes(key);
    return;
  }
  StrikeData data;
  data.set_num_strikes(num_strikes);
  data.set_last_update_timestamp(
      base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
  strike_map_cache_[key] = data;
}

base::Time TestInMemoryStrikeDatabase::GetLastUpdatedTimestamp(
    const std::string& key) {
  auto iter = strike_map_cache_.find(key);
  return iter != strike_map_cache_.end()
             ? base::Time::FromDeltaSinceWindowsEpoch(
                   base::Microseconds(iter->second.last_update_timestamp()))
             : base::Time();
}

}  // namespace strike_database