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-filter.h"
16
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <string_view>
21 #include <unordered_set>
22 #include <utility>
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/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-filter-data.h"
31 #include "icing/store/document-id.h"
32 #include "icing/store/document-store.h"
33 #include "icing/util/status-macros.h"
34
35 namespace icing {
36 namespace lib {
37
DocHitInfoIteratorFilter(std::unique_ptr<DocHitInfoIterator> delegate,const DocumentStore * document_store,const SchemaStore * schema_store,const Options & options,int64_t current_time_ms)38 DocHitInfoIteratorFilter::DocHitInfoIteratorFilter(
39 std::unique_ptr<DocHitInfoIterator> delegate,
40 const DocumentStore* document_store, const SchemaStore* schema_store,
41 const Options& options, int64_t current_time_ms)
42 : delegate_(std::move(delegate)),
43 document_store_(*document_store),
44 schema_store_(*schema_store),
45 options_(options),
46 current_time_ms_(current_time_ms) {}
47
Advance()48 libtextclassifier3::Status DocHitInfoIteratorFilter::Advance() {
49 while (delegate_->Advance().ok()) {
50 // Try to get the DocumentFilterData
51 auto document_filter_data_optional =
52 document_store_.GetAliveDocumentFilterData(
53 delegate_->doc_hit_info().document_id(), current_time_ms_);
54 if (!document_filter_data_optional) {
55 // Didn't find the DocumentFilterData in the filter cache. This could be
56 // because the Document doesn't exist or the DocumentId isn't valid or the
57 // filter cache is in some invalid state. This is bad, but not the query's
58 // responsibility to fix, so just skip this result for now.
59 continue;
60 }
61 // We should be guaranteed that this exists now.
62 DocumentFilterData data = document_filter_data_optional.value();
63
64 if (options_.filter_by_namespace_id_enabled &&
65 options_.target_namespace_ids.count(data.namespace_id()) == 0) {
66 // Doesn't match one of the specified namespaces. Keep searching
67 continue;
68 }
69
70 if (options_.filter_by_schema_type_id_enabled &&
71 options_.target_schema_type_ids.count(data.schema_type_id()) == 0) {
72 // Doesn't match one of the specified schema types. Keep searching
73 continue;
74 }
75
76 // Satisfied all our specified filters
77 doc_hit_info_ = delegate_->doc_hit_info();
78 return libtextclassifier3::Status::OK;
79 }
80
81 // Didn't find anything on the delegate iterator.
82 doc_hit_info_ = DocHitInfo(kInvalidDocumentId);
83 return absl_ports::ResourceExhaustedError("No more DocHitInfos in iterator");
84 }
85
86 libtextclassifier3::StatusOr<DocHitInfoIterator::TrimmedNode>
TrimRightMostNode()87 DocHitInfoIteratorFilter::TrimRightMostNode() && {
88 ICING_ASSIGN_OR_RETURN(TrimmedNode trimmed_delegate,
89 std::move(*delegate_).TrimRightMostNode());
90 if (trimmed_delegate.iterator_ != nullptr) {
91 trimmed_delegate.iterator_ = std::make_unique<DocHitInfoIteratorFilter>(
92 std::move(trimmed_delegate.iterator_), &document_store_, &schema_store_,
93 options_, current_time_ms_);
94 }
95 return trimmed_delegate;
96 }
97
ToString() const98 std::string DocHitInfoIteratorFilter::ToString() const {
99 return delegate_->ToString();
100 }
101
102 } // namespace lib
103 } // namespace icing
104