From: sanghyeok oh Date: Fri, 1 Nov 2019 08:08:01 +0000 (+0000) Subject: Merge "serialization: serialize send index" into tizen_5.5 X-Git-Tag: accepted/tizen/5.5/unified/20191104.111320~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=222e8a9eb182f0213d371f9733de9f086403b6e9;p=platform%2Fcore%2Fsystem%2Flibdbuspolicy.git Merge "serialization: serialize send index" into tizen_5.5 --- 222e8a9eb182f0213d371f9733de9f086403b6e9 diff --cc src/internal/storage_backend_serialized.cpp index cf37c6b,8900779..5271adf --- a/src/internal/storage_backend_serialized.cpp +++ b/src/internal/storage_backend_serialized.cpp @@@ -161,16 -69,57 +148,42 @@@ public const M *getPolicySet(); }; -inline boost::string_ref s(const flatbuffers::String *str) { - return boost::string_ref(str->c_str(), str->size()); -} - -template -bool match(const T &match, const I *i) { - return match.match(makeMessageType(i->type()), - s(i->interface()), - s(i->path()), - s(i->member()), - s(i->name()), - i->is_name_prefix(), - makeDecision(i->decision()->decision())); -} - ldp_xml_parser::DecisionItem StorageBackendSerialized::StorageBackendSerializedImpl::getDecisionFromSendIndex(const MatchItemSend &item, - const MapIndex &index, const SendPrefixIndex &prefixIndex) { - ldp_xml_parser::DecisionItem decision(ldp_xml_parser::Decision::ANY); + const FB::PolicySend *policy) { + + const auto *index = policy->index(); + + if (!index || index->size() == 0) // this indicates version earlier than LDP2 + return getDecisionItem(item, policy); // make it old way for old databases + + std::pair currentBest(nullptr, 0); + + auto updateCurrentBest = [¤tBest, &item, &policy](const flatbuffers::Vector *vec) { + // look from the back, the rule is the same as for the full database + // we now only check among less elements, because the database is indexed to small lists + // item_scores are in increasing order in the index, and they serve also as ids of the policy rules + for (auto item_score_it = vec->rbegin(); item_score_it != vec->rend(); item_score_it++) { + const auto *fb_item = policy->items()->Get(*item_score_it - 1); // rules are indexed/scored from 1 + if (*item_score_it > currentBest.second && match(item, fb_item)) { + currentBest.first = fb_item; + currentBest.second = *item_score_it; + return; + } else if (*item_score_it <= currentBest.second) { + // there is no need to look further as we can't improve the score anymore + return; + } + } + }; - ItemWithScore currentBest(nullptr, 0); + auto searchAndUpdateCurrentBest = [¤tBest, &index, &updateCurrentBest](boost::string_ref name_ref) { + // we need to create C-string for flatbuffers lookups + // boost::string_ref gives us correct start, but possibly NUL-terminated in a wrong place, as it does not modify + // input string and keeps only the length + std::string name(name_ref.data(), name_ref.size()); - auto updateCurrentBest = [¤tBest, &item, &index](boost::string_ref name) { // check if there are any rules for the name - auto fit = index.find(name); - if (fit == index.end()) + const auto *fit = index->LookupByKey(name.c_str()); + if (!fit) return; // check if there's any chance to get better score