diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-12-14 16:47:40 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-12-14 16:47:40 +0000 |
commit | 385ea22741ed5bad794fb6b1dff2b46481f241c4 (patch) | |
tree | 32ff87191e49ddb80670ec35ed90a9f760ff9b2e | |
parent | edaa28771fca801dd77527fd7cb39128a8c3b613 (diff) | |
parent | 26eba2879d7d7c9f51d14eba2484e25df15b8dd3 (diff) | |
download | system_core-385ea22741ed5bad794fb6b1dff2b46481f241c4.tar.gz system_core-385ea22741ed5bad794fb6b1dff2b46481f241c4.tar.bz2 system_core-385ea22741ed5bad794fb6b1dff2b46481f241c4.zip |
Merge "Always check prefix matches array at each node"
3 files changed, 105 insertions, 12 deletions
diff --git a/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h b/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h index c2114ccff..8c3507e4f 100644 --- a/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h +++ b/property_service/libpropertyinfoparser/include/property_info_parser/property_info_parser.h @@ -172,6 +172,9 @@ class PropertyInfoArea : private SerializedData { TrieNode root_node() const { return trie(header()->root_offset); } private: + void CheckPrefixMatch(const char* remaining_name, const TrieNode& trie_node, + uint32_t* context_index, uint32_t* schema_index) const; + const PropertyInfoAreaHeader* header() const { return reinterpret_cast<const PropertyInfoAreaHeader*>(data_base()); } diff --git a/property_service/libpropertyinfoparser/property_info_parser.cpp b/property_service/libpropertyinfoparser/property_info_parser.cpp index 84f8c29f8..89864dd78 100644 --- a/property_service/libpropertyinfoparser/property_info_parser.cpp +++ b/property_service/libpropertyinfoparser/property_info_parser.cpp @@ -87,6 +87,21 @@ bool TrieNode::FindChildForString(const char* name, uint32_t namelen, TrieNode* return true; } +void PropertyInfoArea::CheckPrefixMatch(const char* remaining_name, const TrieNode& trie_node, + uint32_t* context_index, uint32_t* schema_index) const { + const uint32_t remaining_name_size = strlen(remaining_name); + for (uint32_t i = 0; i < trie_node.num_prefixes(); ++i) { + auto prefix_len = trie_node.prefix(i)->namelen; + if (prefix_len > remaining_name_size) continue; + + if (!strncmp(c_string(trie_node.prefix(i)->name_offset), remaining_name, prefix_len)) { + *context_index = trie_node.prefix(i)->context_index; + *schema_index = trie_node.prefix(i)->schema_index; + return; + } + } +} + void PropertyInfoArea::GetPropertyInfoIndexes(const char* name, uint32_t* context_index, uint32_t* schema_index) const { uint32_t return_context_index = ~0u; @@ -104,6 +119,10 @@ void PropertyInfoArea::GetPropertyInfoIndexes(const char* name, uint32_t* contex return_schema_index = trie_node.schema_index(); } + // Check prefixes at this node. This comes after the node check since these prefixes are by + // definition longer than the node itself. + CheckPrefixMatch(remaining_name, trie_node, &return_context_index, &return_schema_index); + if (sep == nullptr) { break; } @@ -128,18 +147,8 @@ void PropertyInfoArea::GetPropertyInfoIndexes(const char* name, uint32_t* contex } } // Check prefix matches for prefixes not deliminated with '.' - const uint32_t remaining_name_size = strlen(remaining_name); - for (uint32_t i = 0; i < trie_node.num_prefixes(); ++i) { - auto prefix_len = trie_node.prefix(i)->namelen; - if (prefix_len > remaining_name_size) continue; - - if (!strncmp(c_string(trie_node.prefix(i)->name_offset), remaining_name, prefix_len)) { - if (context_index != nullptr) *context_index = trie_node.prefix(i)->context_index; - if (schema_index != nullptr) *schema_index = trie_node.prefix(i)->schema_index; - return; - } - } - // Return previously found '.' deliminated prefix match. + CheckPrefixMatch(remaining_name, trie_node, &return_context_index, &return_schema_index); + // Return previously found prefix match. if (context_index != nullptr) *context_index = return_context_index; if (schema_index != nullptr) *schema_index = return_schema_index; return; diff --git a/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp b/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp index 5fa3463e1..b3fae268e 100644 --- a/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp +++ b/property_service/libpropertyinfoserializer/property_info_serializer_test.cpp @@ -763,5 +763,86 @@ TEST(propertyinfoserializer, RealProperties) { } } +TEST(propertyinfoserializer, GetPropertyInfo_prefix_without_dot) { + auto property_info = std::vector<PropertyInfoEntry>{ + {"persist.radio", "1st", "1st", false}, + {"persist.radio.something.else.here", "2nd", "2nd", false}, + }; + + auto serialized_trie = std::string(); + auto build_trie_error = std::string(); + ASSERT_TRUE(BuildTrie(property_info, "default", "default", &serialized_trie, &build_trie_error)) + << build_trie_error; + + auto property_info_area = reinterpret_cast<const PropertyInfoArea*>(serialized_trie.data()); + + const char* context; + const char* schema; + property_info_area->GetPropertyInfo("persist.radio", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.subproperty", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radiowords", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.long.long.long.sub.property", &context, + &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.here", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.here2", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.here.after", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else.nothere", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio.something.else", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); +} + +TEST(propertyinfoserializer, GetPropertyInfo_prefix_with_dot_vs_without) { + auto property_info = std::vector<PropertyInfoEntry>{ + {"persist.", "1st", "1st", false}, + {"persist.radio", "2nd", "2nd", false}, + {"persist.radio.long.property.exact.match", "3rd", "3rd", true}, + }; + + auto serialized_trie = std::string(); + auto build_trie_error = std::string(); + ASSERT_TRUE(BuildTrie(property_info, "default", "default", &serialized_trie, &build_trie_error)) + << build_trie_error; + + auto property_info_area = reinterpret_cast<const PropertyInfoArea*>(serialized_trie.data()); + + const char* context; + const char* schema; + property_info_area->GetPropertyInfo("persist.notradio", &context, &schema); + EXPECT_STREQ("1st", context); + EXPECT_STREQ("1st", schema); + property_info_area->GetPropertyInfo("persist.radio", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.subproperty", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radiowords", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.long.property.prefix.match", &context, &schema); + EXPECT_STREQ("2nd", context); + EXPECT_STREQ("2nd", schema); + property_info_area->GetPropertyInfo("persist.radio.long.property.exact.match", &context, &schema); + EXPECT_STREQ("3rd", context); + EXPECT_STREQ("3rd", schema); +} + } // namespace properties } // namespace android |