Merge remote-tracking branch 'origin/topic/bernhard/topk' into topic/robin/topk-merge

* origin/topic/bernhard/topk:
  update documentation, rename get* to Get* and make hasher persistent

Conflicts:
	src/probabilistic/Topk.cc
	src/probabilistic/Topk.h
	src/probabilistic/top-k.bif
This commit is contained in:
Robin Sommer 2013-08-01 14:13:25 -07:00
commit f6e5de91fa
4 changed files with 178 additions and 86 deletions

View file

@ -19,19 +19,22 @@ static void topk_element_hash_delete_func(void* val)
Element::~Element()
{
Unref(value);
value = 0;
}
void TopkVal::Typify(BroType* t)
{
assert(!hash && !type);
type = t->Ref();
TypeList* tl = new TypeList(t);
tl->Append(t->Ref());
hash = new CompositeHash(tl);
Unref(tl);
}
HashKey* TopkVal::GetHash(Val* v) const
{
TypeList* tl = new TypeList(v->Type());
tl->Append(v->Type()->Ref());
CompositeHash* topk_hash = new CompositeHash(tl);
Unref(tl);
HashKey* key = topk_hash->ComputeHash(v, 1);
HashKey* key = hash->ComputeHash(v, 1);
assert(key);
delete topk_hash;
return key;
}
@ -43,6 +46,7 @@ TopkVal::TopkVal(uint64 arg_size) : OpaqueVal(topk_type)
type = 0;
numElements = 0;
pruned = false;
hash = 0;
}
TopkVal::TopkVal() : OpaqueVal(topk_type)
@ -52,6 +56,7 @@ TopkVal::TopkVal() : OpaqueVal(topk_type)
size = 0;
type = 0;
numElements = 0;
hash = 0;
}
TopkVal::~TopkVal()
@ -68,7 +73,7 @@ TopkVal::~TopkVal()
}
Unref(type);
type = 0;
delete hash;
}
void TopkVal::Merge(const TopkVal* value, bool doPrune)
@ -76,7 +81,7 @@ void TopkVal::Merge(const TopkVal* value, bool doPrune)
if ( type == 0 )
{
assert(numElements == 0);
type = value->type->Ref();
Typify(value->type);
}
else
@ -230,7 +235,10 @@ bool TopkVal::DoUnserialize(UnserialInfo* info)
v &= UNSERIALIZE(&type_present);
if ( type_present )
{
type = BroType::Unserialize(info);
BroType* deserialized_type = BroType::Unserialize(info);
Typify(deserialized_type);
Unref(deserialized_type);
assert(type);
}
else
@ -270,7 +278,7 @@ bool TopkVal::DoUnserialize(UnserialInfo* info)
}
VectorVal* TopkVal::getTopK(int k) const // returns vector
VectorVal* TopkVal::GetTopK(int k) const // returns vector
{
if ( numElements == 0 )
{
@ -311,14 +319,14 @@ VectorVal* TopkVal::getTopK(int k) const // returns vector
return t;
}
uint64_t TopkVal::getCount(Val* value) const
uint64_t TopkVal::GetCount(Val* value) const
{
HashKey* key = GetHash(value);
Element* e = (Element*) elementDict->Lookup(key);
if ( e == 0 )
{
reporter->Error("getCount for element that is not in top-k");
reporter->Error("GetCount for element that is not in top-k");
return 0;
}
@ -326,14 +334,14 @@ uint64_t TopkVal::getCount(Val* value) const
return e->parent->count;
}
uint64_t TopkVal::getEpsilon(Val* value) const
uint64_t TopkVal::GetEpsilon(Val* value) const
{
HashKey* key = GetHash(value);
Element* e = (Element*) elementDict->Lookup(key);
if ( e == 0 )
{
reporter->Error("getEpsilon for element that is not in top-k");
reporter->Error("GetEpsilon for element that is not in top-k");
return 0;
}
@ -341,7 +349,7 @@ uint64_t TopkVal::getEpsilon(Val* value) const
return e->epsilon;
}
uint64_t TopkVal::getSum() const
uint64_t TopkVal::GetSum() const
{
uint64_t sum = 0;
@ -353,8 +361,8 @@ uint64_t TopkVal::getSum() const
it++;
}
if ( pruned )
reporter->Warning("TopkVal::getSum() was used on a pruned data structure. Result values do not represent total element count");
if ( pruned )
reporter->Warning("TopkVal::GetSum() was used on a pruned data structure. Result values do not represent total element count");
return sum;
}
@ -362,11 +370,9 @@ uint64_t TopkVal::getSum() const
void TopkVal::Encountered(Val* encountered)
{
// ok, let's see if we already know this one.
//printf("NumElements: %d\n", numElements);
// check type compatibility
if ( numElements == 0 )
type = encountered->Type()->Ref();
if ( numElements == 0 )
Typify(encountered->Type());
else
if ( ! same_type(type, encountered->Type()) )
{