Create hash policies through factory.

This commit is contained in:
Matthias Vallentin 2013-06-17 16:26:35 -07:00
parent 85668e7054
commit e6e5f4926f
4 changed files with 15 additions and 6 deletions

View file

@ -34,8 +34,6 @@ BloomFilter* BloomFilter::Unserialize(UnserialInfo* info)
bool BloomFilter::DoSerialize(SerialInfo* info) const bool BloomFilter::DoSerialize(SerialInfo* info) const
{ {
DO_SERIALIZE(SER_BLOOMFILTER, SerialObj); DO_SERIALIZE(SER_BLOOMFILTER, SerialObj);
// FIXME: Since we have a fixed hashing policy, we just serialize the
// information needed to reconstruct it.
if ( ! SERIALIZE(static_cast<uint16>(hash_->K())) ) if ( ! SERIALIZE(static_cast<uint16>(hash_->K())) )
return false; return false;
return SERIALIZE_STR(hash_->Name().c_str(), hash_->Name().size()); return SERIALIZE_STR(hash_->Name().c_str(), hash_->Name().size());
@ -50,8 +48,7 @@ bool BloomFilter::DoUnserialize(UnserialInfo* info)
const char* name; const char* name;
if ( ! UNSERIALIZE_STR(&name, 0) ) if ( ! UNSERIALIZE_STR(&name, 0) )
return false; return false;
// FIXME: for now Bloom filters always use double hashing. hash_ = HashPolicy::Create(k, name);
hash_ = new DefaultHashing(k, name);
return true; return true;
} }

View file

@ -32,6 +32,11 @@ size_t Hasher::compute_seed(size_t seed, const std::string& extra)
} }
HashPolicy* HashPolicy::Create(size_t k, const std::string& name)
{
return new DefaultHashing(k, name);
}
HashPolicy::HashPolicy(size_t k, const std::string& name) HashPolicy::HashPolicy(size_t k, const std::string& name)
: k_(k), name_(name) : k_(k), name_(name)
{ {

View file

@ -42,6 +42,13 @@ private:
*/ */
class HashPolicy { class HashPolicy {
public: public:
/**
* Constructs the hashing policy used by the implementation. This factory
* function exists because the HashingPolicy class hierachy is not yet
* serializable.
*/
static HashPolicy* Create(size_t k, const std::string& name);
typedef Hasher::hash_type hash_type; typedef Hasher::hash_type hash_type;
typedef std::vector<hash_type> hash_vector; typedef std::vector<hash_type> hash_vector;

View file

@ -5008,7 +5008,7 @@ function bloomfilter_basic_init%(fp: double, capacity: count,
size_t cells = BasicBloomFilter::M(fp, capacity); size_t cells = BasicBloomFilter::M(fp, capacity);
size_t optimal_k = BasicBloomFilter::K(cells, capacity); size_t optimal_k = BasicBloomFilter::K(cells, capacity);
const HashPolicy* hp = new DefaultHashing(optimal_k, name->CheckString()); const HashPolicy* hp = HashPolicy::Create(optimal_k, name->CheckString());
return new BloomFilterVal(new BasicBloomFilter(hp, cells)); return new BloomFilterVal(new BasicBloomFilter(hp, cells));
%} %}
@ -5029,7 +5029,7 @@ function bloomfilter_basic_init%(fp: double, capacity: count,
function bloomfilter_counting_init%(k: count, cells: count, max: count, function bloomfilter_counting_init%(k: count, cells: count, max: count,
name: string &default=""%): opaque of bloomfilter name: string &default=""%): opaque of bloomfilter
%{ %{
const HashPolicy* hp = new DefaultHashing(k, name->CheckString()); const HashPolicy* hp = HashPolicy::Create(k, name->CheckString());
uint16 width = 0; uint16 width = 0;
while ( max >>= 1 ) while ( max >>= 1 )
++width; ++width;