1 // Copyright (C) 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_INDEX_ITERATOR_SECTION_RESTRICT_DATA_H_ 16 #define ICING_INDEX_ITERATOR_SECTION_RESTRICT_DATA_H_ 17 18 #include <cstdint> 19 #include <set> 20 #include <string> 21 #include <unordered_map> 22 #include <utility> 23 24 #include "icing/schema/schema-store.h" 25 #include "icing/schema/section.h" 26 #include "icing/store/document-id.h" 27 #include "icing/store/document-store.h" 28 29 namespace icing { 30 namespace lib { 31 32 class SectionRestrictData { 33 public: 34 // Does not take any ownership, and all pointers must refer to valid objects 35 // that outlive the one constructed. SectionRestrictData(const DocumentStore * document_store,const SchemaStore * schema_store,int64_t current_time_ms,std::unordered_map<std::string,std::set<std::string>> type_property_filters)36 SectionRestrictData(const DocumentStore* document_store, 37 const SchemaStore* schema_store, int64_t current_time_ms, 38 std::unordered_map<std::string, std::set<std::string>> 39 type_property_filters) 40 : document_store_(*document_store), 41 schema_store_(*schema_store), 42 current_time_ms_(current_time_ms), 43 type_property_filters_(std::move(type_property_filters)) {} 44 45 // Calculates the section mask of allowed sections(determined by the 46 // property filters map) for the given schema type and caches the same for any 47 // future calls. 48 // 49 // Returns: 50 // - If type_property_filters_ has an entry for the given schema type or 51 // wildcard(*), return a bitwise or of section IDs in the schema type 52 // that are also present in the relevant filter list. 53 // - Otherwise, return kSectionIdMaskAll. 54 SectionIdMask ComputeAllowedSectionsMask(const std::string& schema_type); 55 56 // Calculates the section mask of allowed sections(determined by the 57 // property filters map) for the given document id, by retrieving its schema 58 // type name and calling the above method. 59 // 60 // Returns: 61 // - If type_property_filters_ has an entry for the given document's schema 62 // type or wildcard(*), return a bitwise or of section IDs in the schema 63 // type that are also present in the relevant filter list. 64 // - Otherwise, return kSectionIdMaskAll. 65 SectionIdMask ComputeAllowedSectionsMask(DocumentId document_id); 66 document_store()67 const DocumentStore& document_store() const { return document_store_; } 68 schema_store()69 const SchemaStore& schema_store() const { return schema_store_; } 70 current_time_ms()71 int64_t current_time_ms() const { return current_time_ms_; } 72 73 const std::unordered_map<std::string, std::set<std::string>>& type_property_filters()74 type_property_filters() const { 75 return type_property_filters_; 76 } 77 78 private: 79 const DocumentStore& document_store_; 80 const SchemaStore& schema_store_; 81 int64_t current_time_ms_; 82 83 // Map of property filters per schema type. Supports wildcard(*) for schema 84 // type that will apply to all schema types that are not specifically 85 // specified in the mapping otherwise. 86 std::unordered_map<std::string, std::set<std::string>> type_property_filters_; 87 // Mapping of schema type to the section mask of allowed sections for that 88 // schema type. This section mask is lazily calculated based on the 89 // specified property filters and cached for any future use. 90 std::unordered_map<std::string, SectionIdMask> type_property_masks_; 91 92 // Generates a section mask for the given schema type and the target 93 // sections. 94 // 95 // Returns: 96 // - A bitwise or of section IDs in the schema_type that that are also 97 // present in the target_sections list. 98 // - If none of the sections in the schema_type are present in the 99 // target_sections list, return kSectionIdMaskNone. 100 // This is done by doing a bitwise or of the target section ids for the 101 // given schema type. 102 SectionIdMask GenerateSectionMask( 103 const std::string& schema_type, 104 const std::set<std::string>& target_sections) const; 105 }; 106 107 } // namespace lib 108 } // namespace icing 109 110 #endif // ICING_INDEX_ITERATOR_SECTION_RESTRICT_DATA_H_ 111