OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/rappor/sample.h" | |
6 | |
7 #include <map> | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 #include "components/metrics/metrics_hashes.h" | |
12 #include "components/rappor/bloom_filter.h" | |
13 #include "components/rappor/byte_vector_utils.h" | |
14 #include "components/rappor/proto/rappor_metric.pb.h" | |
15 #include "components/rappor/reports.h" | |
16 | |
17 namespace rappor { | |
18 | |
19 Sample::Sample(int32_t cohort_seed, const RapporParameters& parameters) | |
20 : parameters_(parameters), | |
21 bloom_offset_((cohort_seed % parameters_.num_cohorts) * | |
22 parameters_.bloom_filter_hash_function_count) { | |
23 } | |
24 | |
25 Sample::~Sample() { | |
26 } | |
27 | |
28 void Sample::SetStringField(const std::string& field_name, | |
29 const std::string& value) { | |
30 DCHECK_EQ(0u, sizes_[field_name]); | |
31 fields_[field_name] = internal::GetBloomBits( | |
32 parameters_.bloom_filter_size_bytes, | |
33 parameters_.bloom_filter_hash_function_count, | |
34 bloom_offset_, | |
35 value); | |
36 sizes_[field_name] = parameters_.bloom_filter_size_bytes; | |
37 } | |
38 | |
39 void Sample::SetFlagsField(const std::string& field_name, | |
40 uint64_t flags, | |
41 size_t num_flags) { | |
42 DCHECK_EQ(0u, sizes_[field_name]); | |
43 DCHECK_GT(num_flags, 0u); | |
44 DCHECK_LE(num_flags, 64u); | |
45 DCHECK(num_flags == 64u || flags >> num_flags == 0); | |
46 fields_[field_name] = flags; | |
47 sizes_[field_name] = (num_flags + 7) / 8; | |
48 } | |
49 | |
50 void Sample::ExportMetrics(const std::string& secret, | |
51 const std::string& metric_name, | |
52 RapporReports* reports) const { | |
53 for (const auto& kv : fields_) { | |
54 uint64_t value = kv.second; | |
55 size_t size = sizes_.at(kv.first); | |
56 ByteVector value_bytes(size); | |
57 for (size_t i = 0; i < size; i++) { | |
58 // Get the value of the i-th smallest byte and copy it to the byte vector. | |
59 uint64_t byte_mask = 0xff << (i * 8); | |
Alexei Svitkine (slow)
2015/04/24 18:10:01
Nit: i * 8 is repeated - make a variable, e.g. |sh
Steven Holte
2015/04/24 18:39:57
Done.
| |
60 value_bytes[i] = (value & byte_mask) >> (i * 8); | |
61 } | |
62 ByteVector report_bytes = internal::GenerateReport( | |
63 secret, parameters_, value_bytes); | |
64 | |
65 RapporReports::Report* report = reports->add_report(); | |
66 report->set_name_hash(metrics::HashMetricName( | |
67 metric_name + "." + kv.first)); | |
68 report->set_bits(std::string(report_bytes.begin(), report_bytes.end())); | |
69 } | |
70 } | |
71 | |
72 } // namespace rappor | |
OLD | NEW |