diff options
author | Adrian Szyndela <adrian.s@samsung.com> | 2020-10-19 10:33:00 +0200 |
---|---|---|
committer | Adrian Szyndela <adrian.s@samsung.com> | 2020-10-30 12:47:35 +0100 |
commit | 2d5b17ac874f01acb92b522a109537e0607f68b2 (patch) | |
tree | fe0b2ded4abc53733bf9e01e26264ff816eadd21 | |
parent | c6d04cb74ad879df4731acf9c6fc7b17d25ab865 (diff) | |
download | libdbuspolicy-2d5b17ac874f01acb92b522a109537e0607f68b2.tar.gz libdbuspolicy-2d5b17ac874f01acb92b522a109537e0607f68b2.tar.bz2 libdbuspolicy-2d5b17ac874f01acb92b522a109537e0607f68b2.zip |
refactoring: create indexes in StorageBackendXML
This moves creating of indexes from serializers to StorageBackendXML.
This way it's standardized, in a single place.
Serializers now serialize the indexes from the StorageBackendXML instead
of creating their own.
Change-Id: Ie2aec8a8cbace425f2e74c402cb99c25f21a27d1
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | src/internal/policy_containers.cpp | 43 | ||||
-rw-r--r-- | src/internal/policy_containers.hpp | 18 | ||||
-rw-r--r-- | src/internal/serializer_flatbuffers.cpp | 31 | ||||
-rw-r--r-- | src/internal/storage_backend_xml.cpp | 18 |
5 files changed, 84 insertions, 27 deletions
diff --git a/Makefile.am b/Makefile.am index 1848ecd..f17af0b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -64,6 +64,7 @@ COMMON_SRC =\ src/internal/tslog.cpp \ src/internal/serializer.cpp \ src/internal/serializer_flatbuffers.cpp \ + src/internal/policy_containers.cpp \ src/internal/print_content.cpp \ src/internal/storage_backend_flatbuffers.cpp \ src/internal/storage_backend_serialized.cpp \ diff --git a/src/internal/policy_containers.cpp b/src/internal/policy_containers.cpp new file mode 100644 index 0000000..37f3183 --- /dev/null +++ b/src/internal/policy_containers.cpp @@ -0,0 +1,43 @@ +/* MIT License + * + * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ + +#include "policy_containers.hpp" + +using namespace ldp_xml_parser; + +void PolicySend::createIndexes() { + uint32_t cnt = 1; + + for (const auto &item : getItems()) { + // create indexes + if (!item.isNamePrefix()) { + auto &elem = m_index[item.getName()]; // create or get an entry + elem.m_itemRefs.push_back(cnt); // add score/id to the list + // we insert items in increasing score/id order, so we just update the highest score on each add + elem.m_bestScore = cnt; + } else { + // just collect the prefix rules + m_prefixIndex.push_back(cnt); + } + ++cnt; + } +} diff --git a/src/internal/policy_containers.hpp b/src/internal/policy_containers.hpp index 0516d73..f61473a 100644 --- a/src/internal/policy_containers.hpp +++ b/src/internal/policy_containers.hpp @@ -18,6 +18,7 @@ #include "policy.hpp" #include "own_tree.hpp" #include <vector> +#include <boost/utility/string_ref.hpp> namespace ldp_xml_parser { /****************** PolicyBase: a base for policies ************************/ @@ -37,7 +38,22 @@ public: const std::vector<TI> &getItems() const { return m_items; } }; -using PolicySend = PolicyBase<ItemSend>; +class PolicySend : public PolicyBase<ItemSend> { +protected: + struct IndexEntry { + uint32_t m_bestScore; + std::vector<uint32_t> m_itemRefs; + }; + // this maps a name to a pair (highest score + list of scores/ids for this name) + std::map<boost::string_ref, IndexEntry> m_index; + // prefix index is just a list of ids + std::vector<uint32_t> m_prefixIndex; +public: + void createIndexes(); + const auto &getIndex() const { return m_index; } + const auto &getPrefixIndex() const { return m_prefixIndex; } +}; + using PolicyReceive = PolicyBase<ItemReceive>; using PolicyAccess = PolicyBase<ItemAccess>; diff --git a/src/internal/serializer_flatbuffers.cpp b/src/internal/serializer_flatbuffers.cpp index dc38275..a253a48 100644 --- a/src/internal/serializer_flatbuffers.cpp +++ b/src/internal/serializer_flatbuffers.cpp @@ -209,41 +209,22 @@ auto SerializerFlatbuffers::serialize_policy(const PolicySend &policy) -> FbOff<FB::PolicySend> { std::vector<FbOff<FB::ItemSend>> items; - // this maps a name to a pair (highest score + list of scores/ids for this name) - std::map<boost::string_ref, std::pair<uint32_t, std::vector<uint32_t>>> policyIndex; - // prefix index is just a list of ids - std::vector<uint32_t> prefixIndex; - uint32_t cnt = 1; - - for (const auto &item : policy.getItems()) { + for (const auto &item : policy.getItems()) items.push_back(serialize_item<PolicySend>(item)); - // create indexes - if (!item.isNamePrefix()) { - auto &elem = policyIndex[item.getName()]; // create or get an entry - elem.second.push_back(cnt); // add score/id to the list - // we insert items in increasing score/id order, so we just update the highest score on each add - elem.first = cnt; - } else { - // just collect the prefix rules - prefixIndex.push_back(cnt); - } - ++cnt; - } - // serialize main index std::vector<FbOff<FB::NameScoresPair>> index; - for (auto &it: policyIndex) + for (auto &it: policy.getIndex()) index.push_back(FB::CreateNameScoresPairDirect(m_builder, - it.first.data(), // name - it.second.first, // best_score - &it.second.second)); // vector of scores/ids + it.first.data(), // name + it.second.m_bestScore, // best_score + &it.second.m_itemRefs)); // vector of scores/ids return FB::CreatePolicySend(m_builder, m_builder.CreateVector(items), m_builder.CreateVector(index), - m_builder.CreateVector(prefixIndex)); + m_builder.CreateVector(policy.getPrefixIndex())); } template <typename T> diff --git a/src/internal/storage_backend_xml.cpp b/src/internal/storage_backend_xml.cpp index ec1b494..69ecf21 100644 --- a/src/internal/storage_backend_xml.cpp +++ b/src/internal/storage_backend_xml.cpp @@ -46,6 +46,12 @@ protected: public: const std::map<uid_t, P> &getPoliciesUser() const { return user; } const std::map<uid_t, P> &getPoliciesGroup() const { return group; } + void createIndexes() { + for (auto &elem: user) + elem.second.createIndexes(); + for (auto &elem: group) + elem.second.createIndexes(); + } }; /****************** NoUserAndGroupContext ************************/ @@ -56,6 +62,8 @@ protected: template <typename T> void addItemGroup(gid_t, T &) { assert(false); } + + void createIndexes() {} }; /****************** PolicySet ************************/ @@ -102,6 +110,11 @@ public: const P &getRefPolicyContextMandatory() const { return contextMandatory; } const P &getRefPolicyContextDefault() const { return contextDefault; } + void createIndexes() { + contextMandatory.createIndexes(); + contextDefault.createIndexes(); + UG::createIndexes(); + } }; /****************** StorageBackendXML ************************/ @@ -145,7 +158,10 @@ void StorageBackendXML::addItem(const ldp_xml_parser::PolicyType policy_type, bool StorageBackendXML::init(const char *filename) { pimpl.reset(new StorageBackendXMLImpl); XmlParser parser(*this, filename); - return parser.parsePolicyConfigFile() == 0; + bool result = parser.parsePolicyConfigFile() == 0; + if (result) + pimpl->getPolicySetFromMatchItem<MatchItemSend>().createIndexes(); + return result; } template <typename T> |