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

@ -219,11 +219,14 @@ std::unique_ptr<CardinalityCounter> CardinalityCounter::Unserialize(const broker
if ( ! (m && V && alpha_m) ) if ( ! (m && V && alpha_m) )
return nullptr; return nullptr;
if ( v->size() != 3 + *m ) if ( v->size() != 3 + *m )
return nullptr; return nullptr;
auto cc = std::unique_ptr<CardinalityCounter>(new CardinalityCounter(*m, *V, *alpha_m)); 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 )
{ {
@ -231,7 +234,7 @@ std::unique_ptr<CardinalityCounter> CardinalityCounter::Unserialize(const broker
if ( ! x ) if ( ! x )
return nullptr; return nullptr;
cc->buckets.push_back(*x); cc->buckets[i] = *x;
} }
return cc; return cc;

View file

@ -90,7 +90,7 @@ function hll_cardinality_merge_into%(handle1: opaque of cardinality, handle2: op
bool res = h1->Merge(h2); bool res = h1->Merge(h2);
if ( ! res ) 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); return val_mgr->GetBool(0);
} }

View file

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

View file

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

View file

@ -26,6 +26,10 @@ event zeek_init()
hll_cardinality_add(c1, 2004); hll_cardinality_add(c1, 2004);
print hll_cardinality_estimate(c2); 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"; print "============ Bloom";
local bf_cnt = bloomfilter_basic_init(0.1, 1000); local bf_cnt = bloomfilter_basic_init(0.1, 1000);
bloomfilter_add(bf_cnt, 42); bloomfilter_add(bf_cnt, 42);

View file

@ -27,6 +27,10 @@ event zeek_init()
hll_cardinality_add(c1, 2004); hll_cardinality_add(c1, 2004);
print hll_cardinality_estimate(c2); 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"; print "============ Bloom";
local bf_cnt = bloomfilter_basic_init(0.1, 1000); local bf_cnt = bloomfilter_basic_init(0.1, 1000);
bloomfilter_add(bf_cnt, 42); bloomfilter_add(bf_cnt, 42);