Index: components/rappor/sample.cc |
diff --git a/components/rappor/sample.cc b/components/rappor/sample.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c509729373172358f1cb06c6535e6517d49d2a3b |
--- /dev/null |
+++ b/components/rappor/sample.cc |
@@ -0,0 +1,66 @@ |
+// Copyright 2014 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/rappor/sample.h" |
+ |
+#include <map> |
+#include <string> |
+ |
+#include "components/metrics/metrics_hashes.h" |
+#include "components/rappor/bloom_filter.h" |
+#include "components/rappor/byte_vector_utils.h" |
+#include "components/rappor/proto/rappor_metric.pb.h" |
+#include "components/rappor/reports.h" |
+ |
+namespace rappor { |
+ |
+Sample::Sample(int32_t cohort_seed, const RapporParameters& parameters) |
+ : parameters_(parameters), |
+ bloom_offset_((cohort_seed % parameters_.num_cohorts) * |
+ parameters_.bloom_filter_hash_function_count) { |
+} |
+ |
+Sample::~Sample() { |
+ |
+} |
+ |
+void Sample::SetStringField(const std::string& fieldName, |
+ const std::string& value) { |
+ fields[fieldName] = internal::GetBloomBits( |
+ parameters_.bloom_filter_size_bytes, |
+ parameters_.bloom_filter_hash_function_count, |
+ bloom_offset_, |
+ value); |
+ sizes[fieldName] = parameters_.bloom_filter_size_bytes; |
+} |
+ |
+void Sample::SetFlagsField(const std::string& fieldName, |
+ uint64_t flags) { |
+ fields[fieldName] = flags; |
+ 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
|
+} |
+ |
+void Sample::ExportMetrics(const std::string& secret, |
+ const std::string& metricName, |
+ RapporReports* reports) const { |
+ 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.
|
+ it != fields.end(); |
+ ++it) { |
+ uint64_t value = it->second; |
+ size_t size = sizes.at(it->first); |
+ ByteVector value_bytes(size); |
+ for (size_t i = 0; i < size; i++) { |
+ value_bytes[i] = (value & (0xff << (i * 8))) >> (i * 8); |
+ } |
+ ByteVector report_bytes = internal::GenerateReport( |
+ secret, parameters_, value_bytes); |
+ |
+ RapporReports::Report* report = reports->add_report(); |
+ report->set_name_hash(metrics::HashMetricName( |
+ metricName + "." + it->first)); |
+ report->set_bits(std::string(report_bytes.begin(), report_bytes.end())); |
+ } |
+} |
+ |
+} // namespace rappor |