OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/rappor/rappor_metric.h" | 5 #include "components/rappor/rappor_metric.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 | 9 |
10 namespace rappor { | 10 namespace rappor { |
11 | 11 |
12 RapporMetric::RapporMetric(const std::string& metric_name, | 12 RapporMetric::RapporMetric(const std::string& metric_name, |
13 const RapporParameters& parameters, | 13 const RapporParameters& parameters, |
14 int32_t cohort_seed) | 14 int32_t cohort_seed) |
15 : metric_name_(metric_name), | 15 : metric_name_(metric_name), |
16 parameters_(parameters), | 16 parameters_(parameters), |
17 sample_count_(0), | 17 sample_count_(0), |
18 bloom_filter_(parameters.bloom_filter_size_bytes, | 18 bloom_filter_(parameters.bloom_filter_size_bytes, |
19 parameters.bloom_filter_hash_function_count, | 19 parameters.bloom_filter_hash_function_count, |
20 (cohort_seed % parameters.num_cohorts) * | 20 (cohort_seed % parameters.num_cohorts) * |
21 parameters.bloom_filter_hash_function_count) { | 21 parameters.bloom_filter_hash_function_count) { |
22 DCHECK_GE(cohort_seed, 0); | 22 DCHECK_GE(cohort_seed, 0); |
23 DCHECK_LT(cohort_seed, RapporParameters::kMaxCohorts); | 23 DCHECK_LT(cohort_seed, RapporParameters::kMaxCohorts); |
24 // Since cohort_seed is in the range [0, kMaxCohorts), num_cohorts should | |
25 // divide kMaxCohorts for each cohort to have equal weight. | |
26 DCHECK_EQ(RapporParameters::kMaxCohorts % parameters.num_cohorts, 0); | |
Alexei Svitkine (slow)
2014/10/03 20:28:50
Nit: Expected value goes on the left for DCHECK_EQ
Steven Holte
2014/10/03 21:41:11
Done.
| |
24 } | 27 } |
25 | 28 |
26 RapporMetric::~RapporMetric() {} | 29 RapporMetric::~RapporMetric() {} |
27 | 30 |
28 void RapporMetric::AddSample(const std::string& str) { | 31 void RapporMetric::AddSample(const std::string& str) { |
29 ++sample_count_; | 32 ++sample_count_; |
30 // Replace the previous sample with a 1 in sample_count_ chance so that each | 33 // Replace the previous sample with a 1 in sample_count_ chance so that each |
31 // sample has equal probability of being reported. | 34 // sample has equal probability of being reported. |
32 if (base::RandGenerator(sample_count_) == 0) { | 35 if (base::RandGenerator(sample_count_) == 0) { |
33 bloom_filter_.SetString(str); | 36 bloom_filter_.SetString(str); |
(...skipping 30 matching lines...) Expand all Loading... | |
64 // the outcome of flipping a zero coin for the zero bits in that data, and of | 67 // the outcome of flipping a zero coin for the zero bits in that data, and of |
65 // flipping a one coin for the one bits in that data, as the final report. | 68 // flipping a one coin for the one bits in that data, as the final report. |
66 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); | 69 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); |
67 } | 70 } |
68 | 71 |
69 void RapporMetric::SetBytesForTesting(const ByteVector& bytes) { | 72 void RapporMetric::SetBytesForTesting(const ByteVector& bytes) { |
70 bloom_filter_.SetBytesForTesting(bytes); | 73 bloom_filter_.SetBytesForTesting(bytes); |
71 } | 74 } |
72 | 75 |
73 } // namespace rappor | 76 } // namespace rappor |
OLD | NEW |