Add filter to VirtualTable
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 23 Dec 2019 02:39:20 +0000 (11:39 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Thu, 26 Dec 2019 04:53:48 +0000 (13:53 +0900)
- usage -
VirtualTable<Policy<int>> table;
auto row = table.filter(&Policy<int>::name, "sample-int-policy");
auto value = row[&Policy<int>::value];

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/client/tests/virtual-table.cpp
src/vist/client/virtual-table.cpp
src/vist/client/virtual-table.hpp
src/vist/policy/policy-storage.cpp

index 6ba3920..61dbdcf 100644 (file)
@@ -29,7 +29,8 @@ using namespace vist::schema;
 
 class VirtualTableTests : public testing::Test {};
 
-TEST(VirtualTableTests, time_row_at) {
+TEST(VirtualTableTests, time_row_at)
+{
        Time result = { -1, -1, -1 };
 
        VirtualRow<Time> time;
@@ -54,7 +55,8 @@ TEST(VirtualTableTests, time_row_at) {
        EXPECT_NE(result.seconds, -1);
 }
 
-TEST(VirtualTableTests, time_row_arry_op) {
+TEST(VirtualTableTests, time_row_arry_op)
+{
        Time result = { -1, -1, -1 };
 
        VirtualRow<Time> time;
@@ -73,7 +75,8 @@ TEST(VirtualTableTests, time_row_arry_op) {
        EXPECT_NE(result.seconds, -1);
 }
 
-TEST(VirtualTableTests, processes_table) {
+TEST(VirtualTableTests, processes_table)
+{
        Processes result;
        VirtualTable<Processes> processes;
        EXPECT_TRUE(processes.size() > 0);
@@ -105,7 +108,8 @@ TEST(VirtualTableTests, processes_table) {
        }
 }
 
-TEST(VirtualTableTests, policy_int_table) {
+TEST(VirtualTableTests, policy_int_table)
+{
        VirtualTable<Policy<int>> table;
        EXPECT_TRUE(table.size() > 0);
 
@@ -118,7 +122,19 @@ TEST(VirtualTableTests, policy_int_table) {
        }
 }
 
-TEST(VirtualTableTests, policy_str_table) {
+TEST(VirtualTableTests, policy_int_filter)
+{
+       VirtualTable<Policy<int>> table;
+       EXPECT_TRUE(table.size() > 0);
+
+       auto row = table.filter(&Policy<int>::name, "sample-int-policy");
+       auto value = row[&Policy<int>::value];
+       EXPECT_TRUE(value > 0);
+       EXPECT_EQ(row[&Policy<int>::name], "sample-int-policy");
+}
+
+TEST(VirtualTableTests, policy_str_table)
+{
        VirtualTable<Policy<std::string>> table;
        EXPECT_TRUE(table.size() > 0);
 
@@ -133,3 +149,14 @@ TEST(VirtualTableTests, policy_str_table) {
                INFO(VIST_CLIENT) << "\t value: " << policy.value;
        }
 }
+
+TEST(VirtualTableTests, policy_str_filter)
+{
+       VirtualTable<Policy<std::string>> table;
+       EXPECT_TRUE(table.size() > 0);
+
+       auto row = table.filter(&Policy<std::string>::name, "sample-str-policy");
+       auto value = row[&Policy<std::string>::value];
+       EXPECT_TRUE(!value.empty());
+       EXPECT_EQ(row[&Policy<std::string>::name], "sample-str-policy");
+}
index 714e17d..19df7c6 100644 (file)
@@ -129,7 +129,7 @@ VirtualTable<T>::VirtualTable()
                                 row["value"].find("S/") == std::string::npos)
                        continue;
 
-               this->dataset.emplace_back(VirtualRow<T>(std::move(row)));
+               this->rows.emplace_back(VirtualRow<T>(std::move(row)));
        }
 }
 
index 85de131..a64077a 100644 (file)
@@ -51,15 +51,23 @@ public:
        using Iter = typename std::vector<VirtualRow<T>>::iterator;
        using CIter = typename std::vector<VirtualRow<T>>::const_iterator;
 
-       inline Iter begin() { return dataset.begin(); }
-       inline CIter begin() const { return dataset.cbegin(); }
-       inline Iter end() { return dataset.end(); }
-       inline CIter end() const { return dataset.end(); }
+       inline Iter begin() { return rows.begin(); }
+       inline CIter begin() const { return rows.cbegin(); }
+       inline Iter end() { return rows.end(); }
+       inline CIter end() const { return rows.end(); }
 
-       inline std::size_t size() const { return dataset.size(); }
+       inline std::size_t size() const { return rows.size(); }
+
+       template<typename Struct, typename Member>
+       VirtualRow<T>& filter(Member Struct::*field, const std::string& value)
+       {
+               for (auto& row : this->rows)
+                       if (row[field] == value)
+                               return row;
+       }
 
 private:
-       std::vector<VirtualRow<T>> dataset;
+       std::vector<VirtualRow<T>> rows;
 };
 
 } // namespace vist
index d5b2b28..c4748f4 100644 (file)
@@ -145,7 +145,7 @@ void PolicyStorage::define(const std::string& policy, const PolicyValue& ivalue)
        if (!stmt.exec())
                THROW(ErrCode::RuntimeError) << "Failed to define policy: " << pd.name;
 
-       INFO(VIST) << "Policy defined >> name: " << pd.name << ", ivalue" << pd.ivalue;
+       INFO(VIST) << "Policy defined >> name: " << pd.name << ", ivalue" << pd.ivalue;
        this->definitions.emplace(pd.name, std::move(pd));
 }