Commit e0f347a1 authored by Teng Qin's avatar Teng Qin

Use bpf_get_first_key in C++ API

parent db7fab56
......@@ -70,6 +70,11 @@ class BPFTableBase {
static_cast<void*>(value)) >= 0;
}
bool first(KeyType* key) {
return bpf_get_first_key(desc.fd, static_cast<void*>(key),
desc.key_size) >= 0;
}
bool next(KeyType* key, KeyType* next_key) {
return bpf_get_next_key(desc.fd, static_cast<void*>(key),
static_cast<void*>(next_key)) >= 0;
......@@ -174,17 +179,18 @@ class BPFHashTable : public BPFTableBase<KeyType, ValueType> {
std::vector<std::pair<KeyType, ValueType>> get_table_offline() {
std::vector<std::pair<KeyType, ValueType>> res;
KeyType cur, nxt;
KeyType cur;
ValueType value;
if (!this->first(&cur))
return res;
while (true) {
if (!this->next(&cur, &nxt))
if (!this->lookup(&cur, &value))
break;
if (!this->lookup(&nxt, &value))
res.emplace_back(cur, value);
if (!this->next(&cur, &cur))
break;
res.emplace_back(nxt, value);
std::swap(cur, nxt);
}
return res;
......
......@@ -71,4 +71,17 @@ TEST_CASE("test hash table", "[hash_table]") {
res = t.get_value(k, v2);
REQUIRE(res.code() != 0);
}
SECTION("walk table") {
for (int i = 1; i <= 10; i++) {
res = t.update_value(i * 3, i);
REQUIRE(res.code() == 0);
}
auto offline = t.get_table_offline();
REQUIRE(offline.size() == 10);
for (const auto &pair : offline) {
REQUIRE(pair.first % 3 == 0);
REQUIRE(pair.first / 3 == pair.second);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment