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
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 \
--- /dev/null
+/* 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;
+ }
+}
#include "policy.hpp"
#include "own_tree.hpp"
#include <vector>
+#include <boost/utility/string_ref.hpp>
namespace ldp_xml_parser {
/****************** PolicyBase: a base for policies ************************/
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>;
-> 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>
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 ************************/
template <typename T>
void addItemGroup(gid_t, T &) { assert(false); }
+
+ void createIndexes() {}
};
/****************** PolicySet ************************/
const P &getRefPolicyContextMandatory() const { return contextMandatory; }
const P &getRefPolicyContextDefault() const { return contextDefault; }
+ void createIndexes() {
+ contextMandatory.createIndexes();
+ contextDefault.createIndexes();
+ UG::createIndexes();
+ }
};
/****************** StorageBackendXML ************************/
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>