Fix cardinalitycounter deserialization.

This one took me way too long to admit. Values were pushed back on
deserialization - instead of assigned. Meaning they were added to the
end of the already 0-assigned vector.

The mean thing here is that estimation still worked - just merging
resulted in 0. And estimation still was correct because m, V, alpha_m
are enough for this - and those were correctly copied...

With this change, all tests pass.
This commit is contained in:
Johanna Amann 2019-06-18 08:59:31 -07:00
parent 618f0802f4
commit ca28b98fd4
6 changed files with 19 additions and 6 deletions

View file

@ -201,10 +201,10 @@ broker::expected<broker::data> CardinalityCounter::Serialize() const
broker::vector v = {m, V, alpha_m};
v.reserve(3 + m);
for ( size_t i = 0; i < m; ++i )
for ( size_t i = 0; i < m; ++i )
v.emplace_back(static_cast<uint64>(buckets[i]));
return {v};
return {v};
}
std::unique_ptr<CardinalityCounter> CardinalityCounter::Unserialize(const broker::data& data)
@ -219,19 +219,22 @@ std::unique_ptr<CardinalityCounter> CardinalityCounter::Unserialize(const broker
if ( ! (m && V && alpha_m) )
return nullptr;
if ( v->size() != 3 + *m )
return nullptr;
auto cc = std::unique_ptr<CardinalityCounter>(new CardinalityCounter(*m, *V, *alpha_m));
if ( *m != cc->m )
return nullptr;
if ( cc->buckets.size() != * m )
return nullptr;
for ( size_t i = 0; i < *m; ++i )
for ( size_t i = 0; i < *m; ++i )
{
auto x = caf::get_if<uint64>(&(*v)[3 + i]);
if ( ! x )
return nullptr;
cc->buckets.push_back(*x);
cc->buckets[i] = *x;
}
return cc;

View file

@ -90,7 +90,7 @@ function hll_cardinality_merge_into%(handle1: opaque of cardinality, handle2: op
bool res = h1->Merge(h2);
if ( ! res )
{
reporter->Error("Carinality counters with different parameters cannot be merged");
reporter->Error("Cardinality counters with different parameters cannot be merged");
return val_mgr->GetBool(0);
}

View file

@ -4,6 +4,7 @@
============ HLL
3.000069
3.000069
3.000069
============ Bloom
0
1

View file

@ -4,6 +4,7 @@
============ HLL
3.000069
3.000069
3.000069
============ Bloom
0
1

View file

@ -26,6 +26,10 @@ event zeek_init()
hll_cardinality_add(c1, 2004);
print hll_cardinality_estimate(c2);
local c3 = hll_cardinality_init(0.01, 0.95);
hll_cardinality_merge_into(c3, c2);
print hll_cardinality_estimate(c3);
print "============ Bloom";
local bf_cnt = bloomfilter_basic_init(0.1, 1000);
bloomfilter_add(bf_cnt, 42);

View file

@ -27,6 +27,10 @@ event zeek_init()
hll_cardinality_add(c1, 2004);
print hll_cardinality_estimate(c2);
local c3 = hll_cardinality_init(0.01, 0.95);
hll_cardinality_merge_into(c3, c2);
print hll_cardinality_estimate(c3);
print "============ Bloom";
local bf_cnt = bloomfilter_basic_init(0.1, 1000);
bloomfilter_add(bf_cnt, 42);