Convert Dictionary types to be templated classes

This has the fortunate side-effect of also making it so we can store
the value objects as typed pointers, instead of void*.
This commit is contained in:
Tim Wojtulewicz 2022-06-27 14:22:21 -07:00
parent 15c0bd9b9d
commit 47e7fe2cd1
15 changed files with 1153 additions and 1284 deletions

File diff suppressed because it is too large Load diff

1232
src/Dict.h

File diff suppressed because it is too large Load diff

View file

@ -1331,7 +1331,7 @@ ValPtr ForStmt::DoExec(Frame* f, Val* v, StmtFlowType& flow)
for ( const auto& lve : *loop_vals ) for ( const auto& lve : *loop_vals )
{ {
auto k = lve.GetHashKey(); auto k = lve.GetHashKey();
auto* current_tev = lve.GetValue<TableEntryVal*>(); auto* current_tev = lve.value;
auto ind_lv = tv->RecreateIndex(*k); auto ind_lv = tv->RecreateIndex(*k);
if ( value_var ) if ( value_var )

View file

@ -493,13 +493,10 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val*
else else
writer.StartObject(); writer.StartObject();
std::unique_ptr<detail::HashKey> k;
TableEntryVal* entry;
for ( const auto& te : *table ) for ( const auto& te : *table )
{ {
entry = te.GetValue<TableEntryVal*>(); auto* entry = te.value;
k = te.GetHashKey(); auto k = te.GetHashKey();
auto lv = tval->RecreateIndex(*k); auto lv = tval->RecreateIndex(*k);
Val* entry_key = lv->Length() == 1 ? lv->Idx(0).get() : lv.get(); Val* entry_key = lv->Length() == 1 ? lv->Idx(0).get() : lv.get();
@ -1465,7 +1462,7 @@ int TableVal::RecursiveSize() const
for ( const auto& ve : *table_val ) for ( const auto& ve : *table_val )
{ {
auto* tv = ve.GetValue<TableEntryVal*>(); auto* tv = ve.value;
if ( tv->GetVal() ) if ( tv->GetVal() )
n += tv->GetVal()->AsTableVal()->RecursiveSize(); n += tv->GetVal()->AsTableVal()->RecursiveSize();
} }
@ -1634,7 +1631,7 @@ bool TableVal::AddTo(Val* val, bool is_first_init, bool propagate_ops) const
for ( const auto& tble : *table_val ) for ( const auto& tble : *table_val )
{ {
auto k = tble.GetHashKey(); auto k = tble.GetHashKey();
auto* v = tble.GetValue<TableEntryVal*>(); auto* v = tble.value;
if ( is_first_init && t->AsTable()->Lookup(k.get()) ) if ( is_first_init && t->AsTable()->Lookup(k.get()) )
{ {
@ -2255,7 +2252,7 @@ std::unordered_map<ValPtr, ValPtr> TableVal::ToMap() const
for ( const auto& iter : *table_val ) for ( const auto& iter : *table_val )
{ {
auto k = iter.GetHashKey(); auto k = iter.GetHashKey();
auto v = iter.GetValue<TableEntryVal*>(); auto v = iter.value;
auto vl = table_hash->RecoverVals(*k); auto vl = table_hash->RecoverVals(*k);
res[std::move(vl)] = v->GetVal(); res[std::move(vl)] = v->GetVal();
@ -2298,7 +2295,7 @@ void TableVal::Describe(ODesc* d) const
reporter->InternalError("hash table underflow in TableVal::Describe"); reporter->InternalError("hash table underflow in TableVal::Describe");
auto k = iter->GetHashKey(); auto k = iter->GetHashKey();
auto* v = iter->GetValue<TableEntryVal*>(); auto* v = iter->value;
auto vl = table_hash->RecoverVals(*k); auto vl = table_hash->RecoverVals(*k);
int dim = vl->Length(); int dim = vl->Length();
@ -2445,7 +2442,7 @@ void TableVal::DoExpire(double t)
i < zeek::detail::table_incremental_step && *expire_iterator != table_val->end_robust(); i < zeek::detail::table_incremental_step && *expire_iterator != table_val->end_robust();
++i, ++(*expire_iterator) ) ++i, ++(*expire_iterator) )
{ {
auto v = (*expire_iterator)->GetValue<TableEntryVal*>(); auto v = (*expire_iterator)->value;
if ( v->ExpireAccessTime() == 0 ) if ( v->ExpireAccessTime() == 0 )
{ {
@ -2624,7 +2621,7 @@ ValPtr TableVal::DoClone(CloneState* state)
for ( const auto& tble : *table_val ) for ( const auto& tble : *table_val )
{ {
auto key = tble.GetHashKey(); auto key = tble.GetHashKey();
auto* val = tble.GetValue<TableEntryVal*>(); auto* val = tble.value;
TableEntryVal* nval = val->Clone(state); TableEntryVal* nval = val->Clone(state);
tv->table_val->Insert(key.get(), nval); tv->table_val->Insert(key.get(), nval);
@ -2664,7 +2661,7 @@ unsigned int TableVal::ComputeFootprint(std::unordered_set<const Val*>* analyzed
{ {
auto k = iter.GetHashKey(); auto k = iter.GetHashKey();
auto vl = table_hash->RecoverVals(*k); auto vl = table_hash->RecoverVals(*k);
auto v = iter.GetValue<TableEntryVal*>()->GetVal(); auto v = iter.value->GetVal();
fp += vl->Footprint(analyzed_vals); fp += vl->Footprint(analyzed_vals);
if ( v ) if ( v )
@ -2711,7 +2708,7 @@ TableVal::ParseTimeTableState TableVal::DumpTableState()
for ( const auto& tble : *table_val ) for ( const auto& tble : *table_val )
{ {
auto key = tble.GetHashKey(); auto key = tble.GetHashKey();
auto* val = tble.GetValue<TableEntryVal*>(); auto* val = tble.value;
rval.emplace_back(RecreateIndex(*key), val->GetVal()); rval.emplace_back(RecreateIndex(*key), val->GetVal());
} }

View file

@ -1044,7 +1044,7 @@ protected:
detail::ExprPtr expire_time; detail::ExprPtr expire_time;
detail::ExprPtr expire_func; detail::ExprPtr expire_func;
TableValTimer* timer; TableValTimer* timer;
RobustDictIterator* expire_iterator; RobustDictIterator<TableEntryVal>* expire_iterator;
detail::PrefixTable* subnets; detail::PrefixTable* subnets;
ValPtr def_val; ValPtr def_val;
detail::ExprPtr change_func; detail::ExprPtr change_func;

View file

@ -924,8 +924,6 @@ broker::expected<broker::data> val_to_data(const Val* v)
for ( const auto& te : *table ) for ( const auto& te : *table )
{ {
auto hk = te.GetHashKey(); auto hk = te.GetHashKey();
auto* entry = te.GetValue<TableEntryVal*>();
auto vl = table_val->RecreateIndex(*hk); auto vl = table_val->RecreateIndex(*hk);
broker::vector composite_key; broker::vector composite_key;
@ -952,7 +950,7 @@ broker::expected<broker::data> val_to_data(const Val* v)
get<broker::set>(rval).emplace(move(key)); get<broker::set>(rval).emplace(move(key));
else else
{ {
auto val = val_to_data(entry->GetVal().get()); auto val = val_to_data(te.value->GetVal().get());
if ( ! val ) if ( ! val )
return broker::ec::invalid_data; return broker::ec::invalid_data;

View file

@ -37,7 +37,6 @@ std::set<std::string> val_to_topic_set(zeek::Val* val)
for ( const auto& te : *tbl ) for ( const auto& te : *tbl )
{ {
auto k = te.GetHashKey(); auto k = te.GetHashKey();
auto* v = te.GetValue<zeek::TableEntryVal*>();
auto index = val->AsTableVal()->RecreateIndex(*k); auto index = val->AsTableVal()->RecreateIndex(*k);
rval.emplace(index->Idx(0)->AsString()->CheckString()); rval.emplace(index->Idx(0)->AsString()->CheckString());

View file

@ -96,7 +96,8 @@ public:
void DrainModifications(); void DrainModifications();
// Iterator support // Iterator support
using iterator = zeek::DictIterator; using iterator = zeek::DictIterator<file_analysis::Analyzer>;
;
using const_iterator = const iterator; using const_iterator = const iterator;
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>;

View file

@ -377,7 +377,7 @@ void File::DeliverStream(const u_char* data, uint64_t len)
for ( const auto& entry : analyzers ) for ( const auto& entry : analyzers )
{ {
auto* a = entry.GetValue<file_analysis::Analyzer*>(); auto* a = entry.value;
DBG_LOG(DBG_FILE_ANALYSIS, "stream delivery to analyzer %s", DBG_LOG(DBG_FILE_ANALYSIS, "stream delivery to analyzer %s",
file_mgr->GetComponentName(a->Tag()).c_str()); file_mgr->GetComponentName(a->Tag()).c_str());
@ -475,7 +475,7 @@ void File::DeliverChunk(const u_char* data, uint64_t len, uint64_t offset)
for ( const auto& entry : analyzers ) for ( const auto& entry : analyzers )
{ {
auto* a = entry.GetValue<file_analysis::Analyzer*>(); auto* a = entry.value;
DBG_LOG(DBG_FILE_ANALYSIS, "chunk delivery to analyzer %s", DBG_LOG(DBG_FILE_ANALYSIS, "chunk delivery to analyzer %s",
file_mgr->GetComponentName(a->Tag()).c_str()); file_mgr->GetComponentName(a->Tag()).c_str());
@ -539,7 +539,7 @@ void File::EndOfFile()
for ( const auto& entry : analyzers ) for ( const auto& entry : analyzers )
{ {
auto* a = entry.GetValue<file_analysis::Analyzer*>(); auto* a = entry.value;
if ( ! a->EndOfFile() ) if ( ! a->EndOfFile() )
analyzers.QueueRemove(a->Tag(), a->GetArgs()); analyzers.QueueRemove(a->Tag(), a->GetArgs());
@ -574,7 +574,7 @@ void File::Gap(uint64_t offset, uint64_t len)
for ( const auto& entry : analyzers ) for ( const auto& entry : analyzers )
{ {
auto* a = entry.GetValue<file_analysis::Analyzer*>(); auto* a = entry.value;
if ( ! a->Undelivered(offset, len) ) if ( ! a->Undelivered(offset, len) )
analyzers.QueueRemove(a->Tag(), a->GetArgs()); analyzers.QueueRemove(a->Tag(), a->GetArgs());

View file

@ -272,7 +272,7 @@ bool Manager::CreateStream(Stream* info, RecordVal* description)
for ( const auto& icte : *info_config_table ) for ( const auto& icte : *info_config_table )
{ {
auto k = icte.GetHashKey(); auto k = icte.GetHashKey();
auto* v = icte.GetValue<TableEntryVal*>(); auto* v = icte.value;
auto index = info->config->RecreateIndex(*k); auto index = info->config->RecreateIndex(*k);
string key = index->Idx(0)->AsString()->CheckString(); string key = index->Idx(0)->AsString()->CheckString();
@ -1402,7 +1402,7 @@ void Manager::EndCurrentSend(ReaderFrontend* reader)
for ( auto it = stream->lastDict->begin_robust(); it != stream->lastDict->end_robust(); ++it ) for ( auto it = stream->lastDict->begin_robust(); it != stream->lastDict->end_robust(); ++it )
{ {
auto lastDictIdxKey = it->GetHashKey(); auto lastDictIdxKey = it->GetHashKey();
InputHash* ih = it->GetValue<InputHash*>(); InputHash* ih = it->value;
ValPtr val; ValPtr val;
ValPtr predidx; ValPtr predidx;

View file

@ -889,7 +889,7 @@ bool Manager::Write(EnumVal* id, RecordVal* columns_arg)
for ( const auto& fcte : *filter_config_table ) for ( const auto& fcte : *filter_config_table )
{ {
auto k = fcte.GetHashKey(); auto k = fcte.GetHashKey();
auto* v = fcte.GetValue<TableEntryVal*>(); auto* v = fcte.value;
auto index = filter->config->RecreateIndex(*k); auto index = filter->config->RecreateIndex(*k);
string key = index->Idx(0)->AsString()->CheckString(); string key = index->Idx(0)->AsString()->CheckString();

View file

@ -186,7 +186,6 @@ function Reporter::set_weird_sampling_whitelist%(weird_sampling_whitelist: strin
for ( const auto& tble : *wl_table ) for ( const auto& tble : *wl_table )
{ {
auto k = tble.GetHashKey(); auto k = tble.GetHashKey();
auto* v = tble.GetValue<TableEntryVal*>();
auto index = wl_val->RecreateIndex(*k); auto index = wl_val->RecreateIndex(*k);
string key = index->Idx(0)->AsString()->CheckString(); string key = index->Idx(0)->AsString()->CheckString();

View file

@ -65,7 +65,7 @@ public:
// For the current iteration, returns the corresponding value. // For the current iteration, returns the corresponding value.
ZVal IterValue() ZVal IterValue()
{ {
auto tev = (*tbl_iter)->GetValue<TableEntryVal*>(); auto tev = (*tbl_iter)->value;
return ZVal(tev->GetVal(), aux->value_var_type); return ZVal(tev->GetVal(), aux->value_var_type);
} }
@ -88,8 +88,8 @@ private:
// Associated auxiliary information. // Associated auxiliary information.
ZInstAux* aux = nullptr; ZInstAux* aux = nullptr;
std::optional<DictIterator> tbl_iter; std::optional<DictIterator<TableEntryVal>> tbl_iter;
std::optional<DictIterator> tbl_end; std::optional<DictIterator<TableEntryVal>> tbl_end;
}; };
// Class for simple step-wise iteration across an integer range. // Class for simple step-wise iteration across an integer range.

View file

@ -1271,7 +1271,7 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
for ( const auto& ee : *env_table ) for ( const auto& ee : *env_table )
{ {
auto k = ee.GetHashKey(); auto k = ee.GetHashKey();
auto* v = ee.GetValue<TableEntryVal*>(); auto* v = ee.value;
auto key = env_table_val->RecreateIndex(*k); auto key = env_table_val->RecreateIndex(*k);
auto name = key->Idx(0)->AsStringVal()->ToStdString(); auto name = key->Idx(0)->AsStringVal()->ToStdString();
@ -1286,7 +1286,7 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
for ( const auto& cte : *cluster_table ) for ( const auto& cte : *cluster_table )
{ {
auto k = cte.GetHashKey(); auto k = cte.GetHashKey();
auto* v = cte.GetValue<TableEntryVal*>(); auto* v = cte.value;
auto key = cluster_table_val->RecreateIndex(*k); auto key = cluster_table_val->RecreateIndex(*k);
auto name = key->Idx(0)->AsStringVal()->ToStdString(); auto name = key->Idx(0)->AsStringVal()->ToStdString();

View file

@ -55,7 +55,7 @@ std::vector<zeek::telemetry::LabelView> sv_tbl(zeek::TableVal* xs)
{ {
for ( auto& val : *xs->Get() ) for ( auto& val : *xs->Get() )
{ {
auto val_ptr = val.GetValue<zeek::TableEntryVal*>()->GetVal(); auto val_ptr = val.value->GetVal();
result.emplace_back(std::string_view{val.GetKey(), val.key_size}, result.emplace_back(std::string_view{val.GetKey(), val.key_size},
sv(val_ptr->AsStringVal())); sv(val_ptr->AsStringVal()));
} }