1 // Copyright (C) 2024 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 #include "icing/schema/scorable_property_manager.h"
16
17 #include <optional>
18 #include <string>
19 #include <string_view>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23
24 #include "icing/text_classifier/lib3/utils/base/status.h"
25 #include "icing/text_classifier/lib3/utils/base/statusor.h"
26 #include "icing/absl_ports/canonical_errors.h"
27 #include "icing/legacy/core/icing-string-util.h"
28 #include "icing/schema/schema-property-iterator.h"
29 #include "icing/schema/schema-util.h"
30 #include "icing/store/document-filter-data.h"
31 #include "icing/util/status-macros.h"
32
33 namespace icing {
34 namespace lib {
35
36 libtextclassifier3::StatusOr<std::optional<int>>
GetScorablePropertyIndex(SchemaTypeId schema_type_id,std::string_view property_path,const SchemaUtil::TypeConfigMap & type_config_map,const std::unordered_map<SchemaTypeId,std::string> & schema_id_to_type_map)37 ScorablePropertyManager::GetScorablePropertyIndex(
38 SchemaTypeId schema_type_id, std::string_view property_path,
39 const SchemaUtil::TypeConfigMap& type_config_map,
40 const std::unordered_map<SchemaTypeId, std::string>&
41 schema_id_to_type_map) {
42 ICING_ASSIGN_OR_RETURN(auto cache_iter, LookupAndMaybeUpdateCache(
43 schema_type_id, type_config_map,
44 schema_id_to_type_map));
45 auto iter =
46 cache_iter->second.property_path_to_index_map.find(property_path.data());
47 if (iter == cache_iter->second.property_path_to_index_map.end()) {
48 return std::nullopt;
49 }
50 return iter->second;
51 }
52
53 libtextclassifier3::StatusOr<
54 const std::vector<ScorablePropertyManager::ScorablePropertyInfo>*>
GetOrderedScorablePropertyInfo(SchemaTypeId schema_type_id,const SchemaUtil::TypeConfigMap & type_config_map,const std::unordered_map<SchemaTypeId,std::string> & schema_id_to_type_map)55 ScorablePropertyManager::GetOrderedScorablePropertyInfo(
56 SchemaTypeId schema_type_id,
57 const SchemaUtil::TypeConfigMap& type_config_map,
58 const std::unordered_map<SchemaTypeId, std::string>&
59 schema_id_to_type_map) {
60 ICING_ASSIGN_OR_RETURN(auto cache_iter, LookupAndMaybeUpdateCache(
61 schema_type_id, type_config_map,
62 schema_id_to_type_map));
63 return &cache_iter->second.ordered_scorable_property_info;
64 }
65
66 libtextclassifier3::StatusOr<std::unordered_map<
67 SchemaTypeId,
68 ScorablePropertyManager::DerivedScorablePropertySchema>::iterator>
LookupAndMaybeUpdateCache(SchemaTypeId schema_type_id,const SchemaUtil::TypeConfigMap & type_config_map,const std::unordered_map<SchemaTypeId,std::string> & schema_id_to_type_map)69 ScorablePropertyManager::LookupAndMaybeUpdateCache(
70 SchemaTypeId schema_type_id,
71 const SchemaUtil::TypeConfigMap& type_config_map,
72 const std::unordered_map<SchemaTypeId, std::string>&
73 schema_id_to_type_map) {
74 auto cache_iter = scorable_property_schema_cache_.find(schema_type_id);
75 if (cache_iter == scorable_property_schema_cache_.end()) {
76 if (UpdateCache(schema_type_id, type_config_map, schema_id_to_type_map)) {
77 cache_iter = scorable_property_schema_cache_.find(schema_type_id);
78 } else {
79 // schema type id not found neither in the cache nor in the schema
80 // config.
81 return absl_ports::InvalidArgumentError(IcingStringUtil::StringPrintf(
82 "LookupAndMaybeUpdateCache failed: Schema type id %d not found",
83 schema_type_id));
84 }
85 }
86 return cache_iter;
87 }
88
UpdateCache(SchemaTypeId schema_type_id,const SchemaUtil::TypeConfigMap & type_config_map,const std::unordered_map<SchemaTypeId,std::string> & schema_id_to_type_map)89 bool ScorablePropertyManager::UpdateCache(
90 SchemaTypeId schema_type_id,
91 const SchemaUtil::TypeConfigMap& type_config_map,
92 const std::unordered_map<SchemaTypeId, std::string>&
93 schema_id_to_type_map) {
94 auto schema_id_iter = schema_id_to_type_map.find(schema_type_id);
95 if (schema_id_iter == schema_id_to_type_map.end()) {
96 return false;
97 }
98 auto schema_config_iter = type_config_map.find(schema_id_iter->second);
99 if (schema_config_iter == type_config_map.end()) {
100 return false;
101 }
102 std::vector<ScorablePropertyInfo> property_info_vector;
103 std::unordered_map<std::string, int> index_map;
104
105 SchemaPropertyIterator iterator(schema_config_iter->second, type_config_map);
106 while (true) {
107 libtextclassifier3::Status status = iterator.Advance();
108 if (!status.ok()) {
109 if (absl_ports::IsOutOfRange(status)) {
110 break;
111 }
112 // Swallow other type of errors which should not happen if the schema has
113 // been validated already.
114 return false;
115 }
116 if (iterator.GetCurrentPropertyConfig().scorable_type() ==
117 PropertyConfigProto::ScorableType::ENABLED) {
118 index_map[iterator.GetCurrentPropertyPath()] =
119 property_info_vector.size();
120 ScorablePropertyInfo scorable_property_info;
121 scorable_property_info.property_path = iterator.GetCurrentPropertyPath();
122 scorable_property_info.data_type =
123 iterator.GetCurrentPropertyConfig().data_type();
124 property_info_vector.push_back(std::move(scorable_property_info));
125 }
126 }
127 scorable_property_schema_cache_.insert(
128 {schema_type_id,
129 DerivedScorablePropertySchema(std::move(property_info_vector),
130 std::move(index_map))});
131 return true;
132 }
133
134 } // namespace lib
135 } // namespace icing
136