1 // Copyright (C) 2019 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/index/iterator/doc-hit-info-iterator-property-in-schema.h"
16
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <string_view>
21 #include <utility>
22
23 #include "icing/text_classifier/lib3/utils/base/status.h"
24 #include "icing/text_classifier/lib3/utils/base/statusor.h"
25 #include "icing/absl_ports/canonical_errors.h"
26 #include "icing/absl_ports/str_cat.h"
27 #include "icing/index/hit/doc-hit-info.h"
28 #include "icing/index/iterator/doc-hit-info-iterator.h"
29 #include "icing/schema/schema-store.h"
30 #include "icing/store/document-id.h"
31 #include "icing/store/document-store.h"
32
33 namespace icing {
34 namespace lib {
35
DocHitInfoIteratorPropertyInSchema(std::unique_ptr<DocHitInfoIterator> delegate,const DocumentStore * document_store,const SchemaStore * schema_store,std::set<std::string> target_sections,int64_t current_time_ms)36 DocHitInfoIteratorPropertyInSchema::DocHitInfoIteratorPropertyInSchema(
37 std::unique_ptr<DocHitInfoIterator> delegate,
38 const DocumentStore* document_store, const SchemaStore* schema_store,
39 std::set<std::string> target_sections, int64_t current_time_ms)
40 : delegate_(std::move(delegate)),
41 document_store_(*document_store),
42 schema_store_(*schema_store),
43 target_properties_(std::move(target_sections)),
44 current_time_ms_(current_time_ms) {}
45
Advance()46 libtextclassifier3::Status DocHitInfoIteratorPropertyInSchema::Advance() {
47 doc_hit_info_ = DocHitInfo(kInvalidDocumentId);
48
49 // Maps from SchemaTypeId to a bool indicating whether or not the type has
50 // the requested property.
51 std::unordered_map<SchemaTypeId, bool> property_defined_types;
52 while (delegate_->Advance().ok()) {
53 DocumentId document_id = delegate_->doc_hit_info().document_id();
54 auto data_optional = document_store_.GetAliveDocumentFilterData(
55 document_id, current_time_ms_);
56 if (!data_optional) {
57 // Ran into some error retrieving information on this hit, skip
58 continue;
59 }
60
61 // Guaranteed that the DocumentFilterData exists at this point
62 SchemaTypeId schema_type_id = data_optional.value().schema_type_id();
63 bool valid_match = false;
64 auto itr = property_defined_types.find(schema_type_id);
65 if (itr != property_defined_types.end()) {
66 valid_match = itr->second;
67 } else {
68 for (const auto& property : target_properties_) {
69 if (schema_store_.IsPropertyDefinedInSchema(schema_type_id, property)) {
70 valid_match = true;
71 break;
72 }
73 }
74 property_defined_types[schema_type_id] = valid_match;
75 }
76
77 if (valid_match) {
78 doc_hit_info_ = delegate_->doc_hit_info();
79 return libtextclassifier3::Status::OK;
80 }
81
82 // The document's schema does not define any properties listed in
83 // target_properties_. Continue.
84 }
85
86 // Didn't find anything on the delegate iterator.
87 return absl_ports::ResourceExhaustedError("No more DocHitInfos in iterator");
88 }
89
90 libtextclassifier3::StatusOr<DocHitInfoIterator::TrimmedNode>
TrimRightMostNode()91 DocHitInfoIteratorPropertyInSchema::TrimRightMostNode() && {
92 // Don't generate suggestion if the last operator is this custom function.
93 return absl_ports::InvalidArgumentError(
94 "Cannot generate suggestion if the last term is hasPropertyDefined().");
95 }
96
ToString() const97 std::string DocHitInfoIteratorPropertyInSchema::ToString() const {
98 return absl_ports::StrCat("(", absl_ports::StrJoin(target_properties_, ","),
99 "): ", delegate_->ToString());
100 }
101
102 } // namespace lib
103 } // namespace icing
104