Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1397)

Unified Diff: components/rappor/bloom_filter.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: BrowserProcess member Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/rappor/bloom_filter.cc
diff --git a/components/rappor/bloom_filter.cc b/components/rappor/bloom_filter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6a0c7c36934a9fa146fd9ca75bdd5c870dade121
--- /dev/null
+++ b/components/rappor/bloom_filter.cc
@@ -0,0 +1,56 @@
+// Copyright (c) 2013 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 <string>
+
+#include "base/logging.h"
+#include "components/rappor/bloom_filter.h"
+#include "third_party/smhasher/src/MurmurHash3.h"
+
+namespace {
+
+// NOTE: These were just generated from /dev/urandom. May be unecessary
+// for murmur hash, or may be better to select carefully to help the
+// hash function distribute its inputs.
+const uint32_t hashSeedCount = 20;
jwd 2013/12/16 17:04:03 Use constant naming style kConstantName.
Steven Holte 2013/12/16 23:02:16 Done.
+const uint32_t mockHashSeeds[hashSeedCount] = {
jwd 2013/12/16 17:04:03 Dito.
jwd 2013/12/16 17:04:03 This could use a better name I think. The current
Steven Holte 2013/12/16 23:02:16 Done.
+ 0xd123957d, 0x6752fc9b, 0xcb6a0102, 0x1a82ea95, 0x55cb27bd,
+ 0x0d23a17e, 0xbfb2beac, 0xbabca478, 0x6d0b103c, 0x5e98bc37,
+ 0xe73ccb9d, 0xe60c1150, 0xa5f070a8, 0x91cc68c2, 0x12c919ff,
+ 0xfec1d371, 0x01b6bf4c, 0xbbe5cf2b, 0x6bd30801, 0x292956d3};
+
+inline uint32_t MurmurHash3String(const std::string& str, uint32_t seed) {
jwd 2013/12/16 17:04:03 Usually, only simple accessors are asked to be inl
Steven Holte 2013/12/16 23:02:16 Done.
+ uint32_t output = 0;
+ MurmurHash3_x86_32(str.data(), str.size(), seed, &output);
+ return output;
+}
+
+} // namespace
+
+namespace rappor {
+
+BloomFilter::BloomFilter(uint32_t bytes_size, uint32_t hash_count)
+ : bytes_(bytes_size), hash_count_(hash_count) {
+ DCHECK(hash_count < hashSeedCount);
+}
+
+void BloomFilter::Add(const std::string& str) {
+ for (size_t j = 0; j < hash_count_; ++j) {
+ uint32_t index = MurmurHash3String(str, mockHashSeeds[j]);
+ uint32_t byte_index = (index / 8) % bytes_.size();
+ uint32_t bit_index = index % 8;
+ bytes_[byte_index] |= 1 << bit_index;
+ }
+}
+
+void BloomFilter::Add(const std::vector<std::string>& strings) {
+ for (size_t i = 0, len = strings.size(); i < len; ++i)
+ Add(strings[i]);
+}
+
+const ByteVector& BloomFilter::bytes() const { return bytes_; }
+
+uint32_t BloomFilter::hash_count() const { return hash_count_; }
+
+} // namespace rappor

Powered by Google App Engine
This is Rietveld 408576698