Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/timw/2208-ordered-tables'
Browse files Browse the repository at this point in the history
* origin/topic/timw/2208-ordered-tables:
  Add &ordered attribute for tables/sets
  • Loading branch information
timwoj committed Oct 17, 2022
2 parents e3682a0 + bd7df9e commit 5dbe982
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
5.2.0-dev.101 | 2022-10-16 17:48:26 -0700

* Add &ordered attribute for tables/sets (Tim Wojtulewicz, Corelight)

5.2.0-dev.99 | 2022-10-16 15:30:25 -0700

* Update zeek-3rdparty submodule for doctest/sqlite updates (Tim Wojtulewicz, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.2.0-dev.99
5.2.0-dev.101
6 changes: 6 additions & 0 deletions src/Attr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const char* attr_name(AttrTag t)
"&deprecated",
"&is_assigned",
"&is_used",
"&ordered",
};

return attr_names[int(t)];
Expand Down Expand Up @@ -624,6 +625,11 @@ void Attributes::CheckAttr(Attr* a)
break;
}

case ATTR_ORDERED:
if ( type->Tag() != TYPE_TABLE )
Error("&ordered only applicable to tables");
break;

default:
BadTag("Attributes::CheckAttr", attr_name(a->Tag()));
}
Expand Down
1 change: 1 addition & 0 deletions src/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum AttrTag
ATTR_DEPRECATED,
ATTR_IS_ASSIGNED, // to suppress usage warnings
ATTR_IS_USED, // to suppress usage warnings
ATTR_ORDERED, // used to store tables in ordered mode
NUM_ATTRS // this item should always be last
};

Expand Down
11 changes: 8 additions & 3 deletions src/Val.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,8 @@ static void find_nested_record_types(const TypePtr& t, std::set<RecordType*>* fo

TableVal::TableVal(TableTypePtr t, detail::AttributesPtr a) : Val(t)
{
Init(std::move(t));
bool ordered = (a != nullptr && a->Find(detail::ATTR_ORDERED) != nullptr);
Init(std::move(t), ordered);
SetAttrs(std::move(a));

if ( ! run_state::is_parsing )
Expand All @@ -1408,7 +1409,7 @@ TableVal::TableVal(TableTypePtr t, detail::AttributesPtr a) : Val(t)
}
}

void TableVal::Init(TableTypePtr t)
void TableVal::Init(TableTypePtr t, bool ordered)
{
table_type = std::move(t);
expire_func = nullptr;
Expand All @@ -1423,7 +1424,11 @@ void TableVal::Init(TableTypePtr t)
subnets = nullptr;

table_hash = new detail::CompositeHash(table_type->GetIndices());
table_val = new PDict<TableEntryVal>;
if ( ordered )
table_val = new PDict<TableEntryVal>(DictOrder::ORDERED);
else
table_val = new PDict<TableEntryVal>(DictOrder::UNORDERED);

table_val->SetDeleteFunc(table_entry_val_delete_func);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Val.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ class TableVal final : public Val, public notifier::detail::Modifiable
void EnableChangeNotifications() { in_change_func = false; }

protected:
void Init(TableTypePtr t);
void Init(TableTypePtr t, bool ordered = false);

using TableRecordDependencies = std::unordered_map<RecordType*, std::vector<TableValPtr>>;

Expand Down
6 changes: 4 additions & 2 deletions src/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Switching parser table type fixes ambiguity problems.
%define lr.type ielr

%expect 199
%expect 205

%token TOK_ADD TOK_ADD_TO TOK_ADDR TOK_ANY
%token TOK_ATENDIF TOK_ATELSE TOK_ATIF TOK_ATIFDEF TOK_ATIFNDEF
Expand All @@ -29,7 +29,7 @@
%token TOK_ATTR_BROKER_STORE_ALLOW_COMPLEX TOK_ATTR_BACKEND
%token TOK_ATTR_PRIORITY TOK_ATTR_LOG TOK_ATTR_ERROR_HANDLER
%token TOK_ATTR_TYPE_COLUMN TOK_ATTR_DEPRECATED
%token TOK_ATTR_IS_ASSIGNED TOK_ATTR_IS_USED
%token TOK_ATTR_IS_ASSIGNED TOK_ATTR_IS_USED TOK_ATTR_ORDERED

%token TOK_DEBUG

Expand Down Expand Up @@ -1730,6 +1730,8 @@ attr:
$$ = new Attr(ATTR_DEPRECATED);
}
}
| TOK_ATTR_ORDERED
{ $$ = new Attr(ATTR_ORDERED); }
;

stmt:
Expand Down
1 change: 1 addition & 0 deletions src/scan.l
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ when return TOK_WHEN;
&broker_store return TOK_ATTR_BROKER_STORE;
&broker_allow_complex_type return TOK_ATTR_BROKER_STORE_ALLOW_COMPLEX;
&backend return TOK_ATTR_BACKEND;
&ordered return TOK_ATTR_ORDERED;

@deprecated.* {
auto num_files = file_stack.length();
Expand Down
1 change: 1 addition & 0 deletions testing/btest/Baseline/language.table-iteration/.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
7 changes: 7 additions & 0 deletions testing/btest/Baseline/language.table-iteration/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
2
1
3
1
2
3
27 changes: 27 additions & 0 deletions testing/btest/language/table-iteration.zeek
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr

global tbl: table[count] of count;
global tbl2: table[count] of count &ordered;

event zeek_init()
{
local i = 0;
while ( i < 3 )
{
++i;
tbl[i] = i;
tbl2[i] = i;
}

for ( [k], v in tbl )
{
print(v);
}

for ( [k], v in tbl2 )
{
print(v);
}
}

0 comments on commit 5dbe982

Please sign in to comment.