Change TableVal::RecoverIndex() to return IntrusivePtr

This commit is contained in:
Jon Siwek 2020-03-23 12:48:21 -07:00
parent 0b5a18495d
commit b045ce4bb3
11 changed files with 45 additions and 90 deletions

View file

@ -207,7 +207,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0,
auto tbl = tv->AsTable(); auto tbl = tv->AsTable();
auto it = tbl->InitForIteration(); auto it = tbl->InitForIteration();
ListVal* lv = new ListVal(TYPE_ANY); auto lv = make_intrusive<ListVal>(TYPE_ANY);
struct HashKeyComparer { struct HashKeyComparer {
bool operator()(const HashKey* a, const HashKey* b) const bool operator()(const HashKey* a, const HashKey* b) const
@ -229,7 +229,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0,
while ( tbl->NextEntry(k, it) ) while ( tbl->NextEntry(k, it) )
{ {
hashkeys[k] = idx++; hashkeys[k] = idx++;
lv->Append(tv->RecoverIndex(k)); lv->Append(tv->RecoverIndex(k).release());
} }
for ( auto& kv : hashkeys ) for ( auto& kv : hashkeys )
@ -242,10 +242,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0,
if ( ! (kp1 = SingleValHash(type_check, kp1, key->Type(), key, if ( ! (kp1 = SingleValHash(type_check, kp1, key->Type(), key,
false)) ) false)) )
{
Unref(lv);
return 0; return 0;
}
if ( ! v->Type()->IsSet() ) if ( ! v->Type()->IsSet() )
{ {
@ -253,14 +250,10 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0,
if ( ! (kp1 = SingleValHash(type_check, kp1, val->Type(), if ( ! (kp1 = SingleValHash(type_check, kp1, val->Type(),
val.get(), false)) ) val.get(), false)) )
{
Unref(lv);
return 0; return 0;
} }
} }
}
Unref(lv);
} }
break; break;

View file

@ -80,7 +80,6 @@ void Reporter::InitOptions()
auto index = wl_val->RecoverIndex(k); auto index = wl_val->RecoverIndex(k);
string key = index->Index(0)->AsString()->CheckString(); string key = index->Index(0)->AsString()->CheckString();
weird_sampling_whitelist.emplace(move(key)); weird_sampling_whitelist.emplace(move(key));
Unref(index);
delete k; delete k;
} }
} }

View file

@ -1198,7 +1198,7 @@ IntrusivePtr<Val> ForStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const
IterCookie* c = loop_vals->InitForIteration(); IterCookie* c = loop_vals->InitForIteration();
while ( (current_tev = loop_vals->NextEntry(k, c)) ) while ( (current_tev = loop_vals->NextEntry(k, c)) )
{ {
IntrusivePtr<ListVal> ind_lv{AdoptRef{}, tv->RecoverIndex(k)}; auto ind_lv = tv->RecoverIndex(k);
delete k; delete k;
if ( value_var ) if ( value_var )

View file

@ -532,12 +532,7 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val*
{ {
auto lv = tval->RecoverIndex(k); auto lv = tval->RecoverIndex(k);
delete k; delete k;
Val* entry_key = lv->Length() == 1 ? lv->Index(0) : lv.get();
Val* entry_key;
if ( lv->Length() == 1 )
entry_key = lv->Index(0)->Ref();
else
entry_key = lv->Ref();
if ( tval->Type()->IsSet() ) if ( tval->Type()->IsSet() )
BuildJSON(writer, entry_key, only_loggable, re); BuildJSON(writer, entry_key, only_loggable, re);
@ -556,9 +551,6 @@ static void BuildJSON(threading::formatter::JSON::NullDoubleWriter& writer, Val*
BuildJSON(writer, entry->Value(), only_loggable, re, key_str); BuildJSON(writer, entry->Value(), only_loggable, re, key_str);
} }
Unref(entry_key);
Unref(lv);
} }
if ( tval->Type()->IsSet() ) if ( tval->Type()->IsSet() )
@ -1537,9 +1529,8 @@ int TableVal::Assign(Val* index, HashKey* k, IntrusivePtr<Val> new_val)
{ {
if ( ! index ) if ( ! index )
{ {
Val* v = RecoverIndex(&k_copy); auto v = RecoverIndex(&k_copy);
subnets->Insert(v, new_entry_val); subnets->Insert(v.get(), new_entry_val);
Unref(v);
} }
else else
subnets->Insert(index, new_entry_val); subnets->Insert(index, new_entry_val);
@ -1553,10 +1544,10 @@ int TableVal::Assign(Val* index, HashKey* k, IntrusivePtr<Val> new_val)
if ( change_func ) if ( change_func )
{ {
Val* change_index = index ? index->Ref() : RecoverIndex(&k_copy); auto change_index = index ? IntrusivePtr<Val>{NewRef{}, index}
: RecoverIndex(&k_copy);
Val* v = old_entry_val ? old_entry_val->Value() : new_val.get(); Val* v = old_entry_val ? old_entry_val->Value() : new_val.get();
CallChangeFunc(change_index, v, old_entry_val ? ELEMENT_CHANGED : ELEMENT_NEW); CallChangeFunc(change_index.get(), v, old_entry_val ? ELEMENT_CHANGED : ELEMENT_NEW);
Unref(change_index);
} }
delete old_entry_val; delete old_entry_val;
@ -1991,9 +1982,9 @@ bool TableVal::UpdateTimestamp(Val* index)
return true; return true;
} }
ListVal* TableVal::RecoverIndex(const HashKey* k) const IntrusivePtr<ListVal> TableVal::RecoverIndex(const HashKey* k) const
{ {
return table_hash->RecoverVals(k).release(); return table_hash->RecoverVals(k);
} }
void TableVal::CallChangeFunc(const Val* index, Val* old_value, OnChangeType tpe) void TableVal::CallChangeFunc(const Val* index, Val* old_value, OnChangeType tpe)
@ -2344,11 +2335,12 @@ void TableVal::DoExpire(double t)
else if ( v->ExpireAccessTime() + timeout < t ) else if ( v->ExpireAccessTime() + timeout < t )
{ {
Val* idx = nullptr; IntrusivePtr<ListVal> idx = nullptr;
if ( expire_func ) if ( expire_func )
{ {
idx = RecoverIndex(k); idx = RecoverIndex(k);
double secs = CallExpireFunc({NewRef{}, idx}); double secs = CallExpireFunc(idx);
// It's possible that the user-provided // It's possible that the user-provided
// function modified or deleted the table // function modified or deleted the table
@ -2359,7 +2351,6 @@ void TableVal::DoExpire(double t)
if ( ! v ) if ( ! v )
{ // user-provided function deleted it { // user-provided function deleted it
v = v_saved; v = v_saved;
Unref(idx);
delete k; delete k;
continue; continue;
} }
@ -2369,7 +2360,6 @@ void TableVal::DoExpire(double t)
// User doesn't want us to expire // User doesn't want us to expire
// this now. // this now.
v->SetExpireAccess(network_time - timeout + secs); v->SetExpireAccess(network_time - timeout + secs);
Unref(idx);
delete k; delete k;
continue; continue;
} }
@ -2380,7 +2370,7 @@ void TableVal::DoExpire(double t)
{ {
if ( ! idx ) if ( ! idx )
idx = RecoverIndex(k); idx = RecoverIndex(k);
if ( ! subnets->Remove(idx) ) if ( ! subnets->Remove(idx.get()) )
reporter->InternalWarning("index not in prefix table"); reporter->InternalWarning("index not in prefix table");
} }
@ -2389,9 +2379,9 @@ void TableVal::DoExpire(double t)
{ {
if ( ! idx ) if ( ! idx )
idx = RecoverIndex(k); idx = RecoverIndex(k);
CallChangeFunc(idx, v->Value(), ELEMENT_EXPIRED); CallChangeFunc(idx.get(), v->Value(), ELEMENT_EXPIRED);
} }
Unref(idx);
delete v; delete v;
modified = true; modified = true;
} }
@ -2439,7 +2429,7 @@ double TableVal::GetExpireTime()
return -1; return -1;
} }
double TableVal::CallExpireFunc(IntrusivePtr<Val> idx) double TableVal::CallExpireFunc(IntrusivePtr<ListVal> idx)
{ {
if ( ! expire_func ) if ( ! expire_func )
return 0; return 0;
@ -2468,8 +2458,6 @@ double TableVal::CallExpireFunc(IntrusivePtr<Val> idx)
// backwards compatibility with idx: any idiom // backwards compatibility with idx: any idiom
bool any_idiom = func_args->length() == 2 && func_args->back()->Tag() == TYPE_ANY; bool any_idiom = func_args->length() == 2 && func_args->back()->Tag() == TYPE_ANY;
if ( idx->Type()->Tag() == TYPE_LIST )
{
if ( ! any_idiom ) if ( ! any_idiom )
{ {
for ( const auto& v : *idx->AsListVal()->Vals() ) for ( const auto& v : *idx->AsListVal()->Vals() )
@ -2480,13 +2468,10 @@ double TableVal::CallExpireFunc(IntrusivePtr<Val> idx)
ListVal* idx_list = idx->AsListVal(); ListVal* idx_list = idx->AsListVal();
// Flatten if only one element // Flatten if only one element
if ( idx_list->Length() == 1 ) if ( idx_list->Length() == 1 )
idx = {NewRef{}, idx_list->Index(0)}; vl.append(idx_list->Index(0)->Ref());
vl.append(idx.release());
}
}
else else
vl.append(idx.release()); vl.append(idx.release());
}
auto result = f->Call(&vl); auto result = f->Call(&vl);
@ -2518,9 +2503,8 @@ IntrusivePtr<Val> TableVal::DoClone(CloneState* state)
if ( subnets ) if ( subnets )
{ {
Val* idx = RecoverIndex(key); auto idx = RecoverIndex(key);
tv->subnets->Insert(idx, nval); tv->subnets->Insert(idx.get(), nval);
Unref(idx);
} }
delete key; delete key;
@ -2609,7 +2593,7 @@ TableVal::ParseTimeTableState TableVal::DumpTableState()
while ( (val = tbl->NextEntry(key, cookie)) ) while ( (val = tbl->NextEntry(key, cookie)) )
{ {
rval.emplace_back(IntrusivePtr<Val>{AdoptRef{}, RecoverIndex(key)}, rval.emplace_back(RecoverIndex(key),
IntrusivePtr<Val>{NewRef{}, val->Value()}); IntrusivePtr<Val>{NewRef{}, val->Value()});
delete key; delete key;

View file

@ -766,7 +766,7 @@ public:
bool UpdateTimestamp(Val* index); bool UpdateTimestamp(Val* index);
// Returns the index corresponding to the given HashKey. // Returns the index corresponding to the given HashKey.
ListVal* RecoverIndex(const HashKey* k) const; IntrusivePtr<ListVal> RecoverIndex(const HashKey* k) const;
// Returns the element if it was in the table, false otherwise. // Returns the element if it was in the table, false otherwise.
IntrusivePtr<Val> Delete(const Val* index); IntrusivePtr<Val> Delete(const Val* index);
@ -851,7 +851,7 @@ protected:
double GetExpireTime(); double GetExpireTime();
// Calls &expire_func and returns its return interval; // Calls &expire_func and returns its return interval;
double CallExpireFunc(IntrusivePtr<Val> idx); double CallExpireFunc(IntrusivePtr<ListVal> idx);
// Enum for the different kinds of changes an &on_change handler can see // Enum for the different kinds of changes an &on_change handler can see
enum OnChangeType { ELEMENT_NEW, ELEMENT_CHANGED, ELEMENT_REMOVED, ELEMENT_EXPIRED }; enum OnChangeType { ELEMENT_NEW, ELEMENT_CHANGED, ELEMENT_REMOVED, ELEMENT_EXPIRED };

View file

@ -898,29 +898,14 @@ broker::expected<broker::data> bro_broker::val_to_data(const Val* v)
else else
rval = broker::table(); rval = broker::table();
struct iter_guard { HashKey* hk;
iter_guard(HashKey* arg_k, ListVal* arg_lv)
: k(arg_k), lv(arg_lv)
{}
~iter_guard()
{
delete k;
Unref(lv);
}
HashKey* k;
ListVal* lv;
};
HashKey* k;
TableEntryVal* entry; TableEntryVal* entry;
auto c = table->InitForIteration(); auto c = table->InitForIteration();
while ( (entry = table->NextEntry(k, c)) ) while ( (entry = table->NextEntry(hk, c)) )
{ {
auto vl = table_val->RecoverIndex(k); auto vl = table_val->RecoverIndex(hk);
iter_guard ig(k, vl); delete hk;
broker::vector composite_key; broker::vector composite_key;
composite_key.reserve(vl->Length()); composite_key.reserve(vl->Length());

View file

@ -40,7 +40,6 @@ std::set<std::string> val_to_topic_set(Val* val)
{ {
auto index = val->AsTableVal()->RecoverIndex(k); auto index = val->AsTableVal()->RecoverIndex(k);
rval.emplace(index->Index(0)->AsString()->CheckString()); rval.emplace(index->Index(0)->AsString()->CheckString());
Unref(index);
delete k; delete k;
} }
} }

View file

@ -283,11 +283,10 @@ bool Manager::CreateStream(Stream* info, RecordVal* description)
TableEntryVal* v; TableEntryVal* v;
while ( (v = info->config->AsTable()->NextEntry(k, c)) ) while ( (v = info->config->AsTable()->NextEntry(k, c)) )
{ {
ListVal* index = info->config->RecoverIndex(k); auto index = info->config->RecoverIndex(k);
string key = index->Index(0)->AsString()->CheckString(); string key = index->Index(0)->AsString()->CheckString();
string value = v->Value()->AsString()->CheckString(); string value = v->Value()->AsString()->CheckString();
rinfo.config.insert(std::make_pair(copy_string(key.c_str()), copy_string(value.c_str()))); rinfo.config.insert(std::make_pair(copy_string(key.c_str()), copy_string(value.c_str())));
Unref(index);
delete k; delete k;
} }
} }
@ -1335,7 +1334,6 @@ void Manager::EndCurrentSend(ReaderFrontend* reader)
while ( ( ih = stream->lastDict->NextEntry(lastDictIdxKey, c) ) ) while ( ( ih = stream->lastDict->NextEntry(lastDictIdxKey, c) ) )
{ {
ListVal * idx = 0;
IntrusivePtr<Val> val; IntrusivePtr<Val> val;
Val* predidx = 0; Val* predidx = 0;
@ -1344,12 +1342,11 @@ void Manager::EndCurrentSend(ReaderFrontend* reader)
if ( stream->pred || stream->event ) if ( stream->pred || stream->event )
{ {
idx = stream->tab->RecoverIndex(ih->idxkey); auto idx = stream->tab->RecoverIndex(ih->idxkey);
assert(idx != 0); assert(idx != nullptr);
val = stream->tab->Lookup(idx); val = stream->tab->Lookup(idx.get());
assert(val != 0); assert(val != 0);
predidx = ListValToRecordVal(idx, stream->itype, &startpos); predidx = ListValToRecordVal(idx.get(), stream->itype, &startpos);
Unref(idx);
ev = BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED).release(); ev = BifType::Enum::Input::Event->GetVal(BifEnum::Input::EVENT_REMOVED).release();
} }

View file

@ -868,11 +868,10 @@ bool Manager::Write(EnumVal* id, RecordVal* columns_arg)
TableEntryVal* v; TableEntryVal* v;
while ( (v = filter->config->AsTable()->NextEntry(k, c)) ) while ( (v = filter->config->AsTable()->NextEntry(k, c)) )
{ {
ListVal* index = filter->config->RecoverIndex(k); auto index = filter->config->RecoverIndex(k);
string key = index->Index(0)->AsString()->CheckString(); string key = index->Index(0)->AsString()->CheckString();
string value = v->Value()->AsString()->CheckString(); string value = v->Value()->AsString()->CheckString();
info->config.insert(std::make_pair(copy_string(key.c_str()), copy_string(value.c_str()))); info->config.insert(std::make_pair(copy_string(key.c_str()), copy_string(value.c_str())));
Unref(index);
delete k; delete k;
} }

View file

@ -182,7 +182,6 @@ function Reporter::set_weird_sampling_whitelist%(weird_sampling_whitelist: strin
auto index = wl_val->RecoverIndex(k); auto index = wl_val->RecoverIndex(k);
string key = index->Index(0)->AsString()->CheckString(); string key = index->Index(0)->AsString()->CheckString();
whitelist_set.emplace(move(key)); whitelist_set.emplace(move(key));
Unref(index);
delete k; delete k;
} }
reporter->SetWeirdSamplingWhitelist(whitelist_set); reporter->SetWeirdSamplingWhitelist(whitelist_set);

View file

@ -1024,7 +1024,7 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
while ( (v = cluster_table->NextEntry(k, c)) ) while ( (v = cluster_table->NextEntry(k, c)) )
{ {
IntrusivePtr<ListVal> key{AdoptRef{}, cluster_table_val->RecoverIndex(k)}; auto key = cluster_table_val->RecoverIndex(k);
delete k; delete k;
auto name = key->Index(0)->AsStringVal()->ToStdString(); auto name = key->Index(0)->AsStringVal()->ToStdString();
auto rv = v->Value()->AsRecordVal(); auto rv = v->Value()->AsRecordVal();