CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hash_filter.h
Go to the documentation of this file.
1 
9 #ifndef CVMFS_GARBAGE_COLLECTION_HASH_FILTER_H_
10 #define CVMFS_GARBAGE_COLLECTION_HASH_FILTER_H_
11 
12 #include <set>
13 
14 #include "hash.h"
15 #include "smallhash.h"
16 
21  public:
22  virtual ~AbstractHashFilter() {}
23 
29  virtual void Fill(const shash::Any &hash) = 0;
30 
41  virtual bool Contains(const shash::Any &hash) const = 0;
42 
49  virtual void Freeze() {}
50 
55  virtual size_t Count() const = 0;
56 };
57 
58 
59 //------------------------------------------------------------------------------
60 
61 
67  public:
68  SimpleHashFilter() : frozen_(false) {}
69 
70  void Fill(const shash::Any &hash) {
71  assert(!frozen_);
72  hashes_.insert(hash);
73  }
74 
75  bool Contains(const shash::Any &hash) const {
76  return hashes_.find(hash) != hashes_.end();
77  }
78 
79  void Freeze() { frozen_ = true; }
80  size_t Count() const { return hashes_.size(); }
81 
82  private:
83  std::set<shash::Any> hashes_;
84  bool frozen_;
85 };
86 
87 
88 //------------------------------------------------------------------------------
89 
90 
96  protected:
97  static uint32_t hasher(const shash::Any &key) {
98  // Don't start with the first bytes, because == is using them as well
99  return (uint32_t) *(reinterpret_cast<const uint32_t *>(key.digest) + 1);
100  }
101 
102  public:
104  // zero_element is MD5("unobtanium")
105  shash::Any zero_element(shash::kMd5,
106  shash::HexPtr("d61f853acc5a39e01f3906f73e31d256"));
107  hashmap_.Init(1048576, zero_element, &SmallhashFilter::hasher);
108  }
109 
110  void Fill(const shash::Any &hash) {
111  assert(!frozen_);
112  hashmap_.Insert(hash, true);
113  }
114 
115  bool Contains(const shash::Any &hash) const {
116  return hashmap_.Contains(hash);
117  }
118 
119  void Freeze() { frozen_ = true; }
120  size_t Count() const { return hashmap_.size(); }
121 
122  private:
124  bool frozen_;
125 };
126 
127 #endif // CVMFS_GARBAGE_COLLECTION_HASH_FILTER_H_
bool Contains(const shash::Any &hash) const
Definition: hash_filter.h:115
virtual void Freeze()
Definition: hash_filter.h:49
size_t Count() const
Definition: hash_filter.h:80
virtual void Fill(const shash::Any &hash)=0
virtual ~AbstractHashFilter()
Definition: hash_filter.h:22
bool Contains(const shash::Any &hash) const
Definition: hash_filter.h:75
assert((mem||(size==0))&&"Out Of Memory")
unsigned char digest[digest_size_]
Definition: hash.h:123
std::set< shash::Any > hashes_
Definition: hash_filter.h:83
void Fill(const shash::Any &hash)
Definition: hash_filter.h:110
virtual bool Contains(const shash::Any &hash) const =0
void Insert(const Key &key, const Value &value)
Definition: smallhash.h:89
static uint32_t hasher(const shash::Any &key)
Definition: hash_filter.h:97
virtual size_t Count() const =0
size_t Count() const
Definition: hash_filter.h:120
bool Contains(const Key &key) const
Definition: smallhash.h:82
uint32_t size() const
Definition: smallhash.h:279
void Init(uint32_t expected_size, Key empty, uint32_t(*hasher)(const Key &key))
Definition: smallhash.h:60
SmallHashDynamic< shash::Any, bool > hashmap_
Definition: hash_filter.h:123
void Fill(const shash::Any &hash)
Definition: hash_filter.h:70