OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/rappor/sample.h" | |
6 | |
7 #include <map> | |
8 #include <string> | |
9 | |
10 #include "components/metrics/metrics_hashes.h" | |
11 #include "components/rappor/bloom_filter.h" | |
12 #include "components/rappor/byte_vector_utils.h" | |
13 #include "components/rappor/proto/rappor_metric.pb.h" | |
14 #include "components/rappor/reports.h" | |
15 | |
16 namespace rappor { | |
17 | |
18 Sample::Sample(int32_t cohort_seed, const RapporParameters& parameters) | |
19 : parameters_(parameters), | |
20 bloom_offset_((cohort_seed % parameters_.num_cohorts) * | |
21 parameters_.bloom_filter_hash_function_count) { | |
22 } | |
23 | |
24 Sample::~Sample() { | |
25 | |
26 } | |
27 | |
28 void Sample::SetStringField(const std::string& fieldName, | |
29 const std::string& value) { | |
30 fields[fieldName] = internal::GetBloomBits( | |
31 parameters_.bloom_filter_size_bytes, | |
32 parameters_.bloom_filter_hash_function_count, | |
33 bloom_offset_, | |
34 value); | |
35 sizes[fieldName] = parameters_.bloom_filter_size_bytes; | |
36 } | |
37 | |
38 void Sample::SetFlagsField(const std::string& fieldName, | |
39 uint64_t flags) { | |
40 fields[fieldName] = flags; | |
41 sizes[fieldName] = parameters_.flag_bytes; | |
Alexei Svitkine (slow)
2015/04/16 15:09:43
Hmm, this flag_bytes field now specified the value
Steven Holte
2015/04/22 19:24:28
The main drawback is that we would generate unnece
Alexei Svitkine (slow)
2015/04/22 20:26:30
I see, how about having the user specify the numbe
| |
42 } | |
43 | |
44 void Sample::ExportMetrics(const std::string& secret, | |
45 const std::string& metricName, | |
46 RapporReports* reports) const { | |
47 for (std::map<std::string, uint64_t>::const_iterator it = fields.begin(); | |
Alexei Svitkine (slow)
2015/04/16 15:09:43
Nit: Use C++ for loop syntax.
Steven Holte
2015/04/22 19:24:28
Done.
| |
48 it != fields.end(); | |
49 ++it) { | |
50 uint64_t value = it->second; | |
51 size_t size = sizes.at(it->first); | |
52 ByteVector value_bytes(size); | |
53 for (size_t i = 0; i < size; i++) { | |
54 value_bytes[i] = (value & (0xff << (i * 8))) >> (i * 8); | |
55 } | |
56 ByteVector report_bytes = internal::GenerateReport( | |
57 secret, parameters_, value_bytes); | |
58 | |
59 RapporReports::Report* report = reports->add_report(); | |
60 report->set_name_hash(metrics::HashMetricName( | |
61 metricName + "." + it->first)); | |
62 report->set_bits(std::string(report_bytes.begin(), report_bytes.end())); | |
63 } | |
64 } | |
65 | |
66 } // namespace rappor | |
OLD | NEW |