]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/bloom_filter: make bloom_filter encodable
authorSage Weil <sage@inktank.com>
Thu, 19 Sep 2013 03:07:01 +0000 (20:07 -0700)
committerSage Weil <sage@inktank.com>
Wed, 25 Sep 2013 23:49:40 +0000 (16:49 -0700)
Signed-off-by: Sage Weil <sage@inktank.com>
src/common/Makefile.am
src/common/bloom_filter.cc [new file with mode: 0644]
src/include/bloom_filter.hpp
src/test/encoding/types.h

index 3526118205f8711461e45c6d90258de4321976cf..9a368f91a0749a96ce62adf52cccd870ecba5f97 100644 (file)
@@ -64,7 +64,8 @@ libcommon_la_SOURCES = \
        common/ceph_strings.cc \
        common/ceph_frag.cc \
        common/addr_parsing.c \
-       common/hobject.cc
+       common/hobject.cc \
+       common/bloom_filter.cc
 
 if LINUX
 libcommon_la_SOURCES += common/secret.c
diff --git a/src/common/bloom_filter.cc b/src/common/bloom_filter.cc
new file mode 100644 (file)
index 0000000..f602b80
--- /dev/null
@@ -0,0 +1,76 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "include/types.h"
+#include "common/bloom_filter.hpp"
+
+void bloom_filter::encode(bufferlist& bl) const
+{
+  ENCODE_START(1, 1, bl);
+  ::encode((uint64_t)salt_count_, bl);
+  ::encode((uint64_t)table_size_, bl);
+  ::encode((uint64_t)inserted_element_count_, bl);
+  ::encode((uint64_t)random_seed_, bl);
+  bufferptr bp((const char*)bit_table_, raw_table_size_);
+  ::encode(bp, bl);
+  ENCODE_FINISH(bl);
+}
+
+void bloom_filter::decode(bufferlist::iterator& p)
+{
+  DECODE_START(1, p);
+  uint64_t v;
+  ::decode(v, p);
+  salt_count_ = v;
+  ::decode(v, p);
+  table_size_ = v;
+  ::decode(v, p);
+  inserted_element_count_ = v;
+  ::decode(v, p);
+  random_seed_ = v;
+  bufferlist t;
+  ::decode(t, p);
+
+  salt_.clear();
+  generate_unique_salt();
+  raw_table_size_ = t.length();
+  assert(raw_table_size_ == table_size_ / bits_per_char);
+  delete bit_table_;
+  bit_table_ = new cell_type[raw_table_size_];
+  t.copy(0, raw_table_size_, (char *)bit_table_);
+
+  DECODE_FINISH(p);
+}
+
+void bloom_filter::dump(Formatter *f) const
+{
+  f->dump_unsigned("salt_count", salt_count_);
+  f->dump_unsigned("table_size", table_size_);
+  f->dump_unsigned("raw_table_size", raw_table_size_);
+  f->dump_unsigned("insert_count", inserted_element_count_);
+  f->dump_unsigned("random_seed", random_seed_);
+
+  f->open_array_section("salt_table");
+  for (std::vector<bloom_type>::const_iterator i = salt_.begin(); i != salt_.end(); ++i)
+    f->dump_unsigned("salt", *i);
+  f->close_section();
+
+  f->open_array_section("bit_table");
+  for (unsigned i = 0; i < raw_table_size_; ++i)
+    f->dump_unsigned("byte", (unsigned)bit_table_[i]);
+  f->close_section();
+}
+
+void bloom_filter::generate_test_instances(list<bloom_filter*>& ls)
+{
+  ls.push_back(new bloom_filter(10, .5, 1));
+  ls.push_back(new bloom_filter(10, .5, 1));
+  ls.back()->insert("foo");
+  ls.back()->insert("bar");
+  ls.push_back(new bloom_filter(50, .5, 1));
+  ls.back()->insert("foo");
+  ls.back()->insert("bar");
+  ls.back()->insert("baz");
+  ls.back()->insert("boof");
+  ls.back()->insert("boogggg");
+}
index 59dafc9b5f7693ec6f895db2e48e8ae22c7b4b82..a65543c88ed1130c4df9d5e62b40f00ceef1be1b 100644 (file)
@@ -29,6 +29,8 @@
 #include <string>
 #include <vector>
 
+#include "include/encoding.h"
+#include "common/Formatter.h"
 
 static const std::size_t bits_per_char = 0x08;    // 8 bits in 1 char(unsigned)
 static const unsigned char bit_mask[bits_per_char] = {
@@ -52,6 +54,15 @@ protected:
 
 public:
 
+   bloom_filter()
+      : bit_table_(0),
+       salt_count_(0),
+       table_size_(0),
+       raw_table_size_(0),
+       inserted_element_count_(0),
+       random_seed_(0)
+   {}
+
    bloom_filter(const std::size_t& predicted_inserted_element_count,
                 const double& false_positive_probability,
                 const std::size_t& random_seed)
@@ -61,10 +72,7 @@ public:
    {
       find_optimal_parameters(predicted_inserted_element_count, false_positive_probability,
                              &salt_count_, &table_size_);
-      generate_unique_salt();
-      raw_table_size_ = table_size_ / bits_per_char;
-      bit_table_ = new cell_type[raw_table_size_];
-      std::fill_n(bit_table_,raw_table_size_,0x00);
+      init();
    }
 
    bloom_filter(const std::size_t& salt_count, std::size_t table_size,
@@ -75,6 +83,10 @@ public:
        inserted_element_count_(0),
        random_seed_((random_seed) ? random_seed : 0xA5A5A5A5)
    {
+      init();
+   }
+
+   void init() {
       generate_unique_salt();
       raw_table_size_ = table_size_ / bits_per_char;
       bit_table_ = new cell_type[raw_table_size_];
@@ -453,7 +465,14 @@ protected:
    std::size_t             raw_table_size_;
    std::size_t             inserted_element_count_;
    std::size_t             random_seed_;
+
+public:
+   void encode(bufferlist& bl) const;
+   void decode(bufferlist::iterator& bl);
+   void dump(Formatter *f) const;
+   static void generate_test_instances(list<bloom_filter*>& ls);
 };
+WRITE_CLASS_ENCODER(bloom_filter)
 
 inline bloom_filter operator & (const bloom_filter& a, const bloom_filter& b)
 {
index fe17f077d8eb75b2d44d33bd2ab1510b21927c24..dd1ea4d570c62be604aeb83bd3d45647c1c81e3a 100644 (file)
@@ -4,6 +4,9 @@ TYPE(CompatSet)
 #include "include/filepath.h"
 TYPE(filepath)
 
+#include "include/bloom_filter.hpp"
+TYPE(bloom_filter)
+
 #include "common/snap_types.h"
 TYPE(SnapContext)
 TYPE(SnapRealmInfo)