1 // Copyright (C) 2022 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/result/result-state-v2.h"
16
17 #include <atomic>
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <vector>
22
23 #include "icing/proto/search.pb.h"
24 #include "icing/result/result-adjustment-info.h"
25 #include "icing/scoring/scored-document-hits-ranker.h"
26 #include "icing/store/document-store.h"
27
28 namespace icing {
29 namespace lib {
30
ResultStateV2(std::unique_ptr<ScoredDocumentHitsRanker> scored_document_hits_ranker_in,std::unique_ptr<ResultAdjustmentInfo> parent_adjustment_info,std::unique_ptr<ResultAdjustmentInfo> child_adjustment_info,const ResultSpecProto & result_spec,const DocumentStore & document_store)31 ResultStateV2::ResultStateV2(
32 std::unique_ptr<ScoredDocumentHitsRanker> scored_document_hits_ranker_in,
33 std::unique_ptr<ResultAdjustmentInfo> parent_adjustment_info,
34 std::unique_ptr<ResultAdjustmentInfo> child_adjustment_info,
35 const ResultSpecProto& result_spec, const DocumentStore& document_store)
36 : scored_document_hits_ranker(std::move(scored_document_hits_ranker_in)),
37 num_returned(0),
38 parent_adjustment_info_(std::move(parent_adjustment_info)),
39 child_adjustment_info_(std::move(child_adjustment_info)),
40 num_per_page_(result_spec.num_per_page()),
41 num_total_bytes_per_page_threshold_(
42 result_spec.num_total_bytes_per_page_threshold()),
43 max_joined_children_per_parent_to_return_(
44 result_spec.max_joined_children_per_parent_to_return()),
45 num_total_hits_(nullptr),
46 result_group_type_(result_spec.result_group_type()) {
47 for (const ResultSpecProto::ResultGrouping& result_grouping :
48 result_spec.result_groupings()) {
49 int group_id = group_result_limits.size();
50 group_result_limits.push_back(result_grouping.max_results());
51 for (const ResultSpecProto::ResultGrouping::Entry& entry :
52 result_grouping.entry_groupings()) {
53 const std::string& name_space = entry.namespace_();
54 const std::string& schema = entry.schema();
55 auto entry_id_or = document_store.GetResultGroupingEntryId(
56 result_group_type_, name_space, schema);
57 if (!entry_id_or.ok()) {
58 continue;
59 }
60 int32_t entry_id = entry_id_or.ValueOrDie();
61 entry_id_group_id_map_.insert({entry_id, group_id});
62 }
63 }
64 }
65
~ResultStateV2()66 ResultStateV2::~ResultStateV2() {
67 IncrementNumTotalHits(-1 * scored_document_hits_ranker->size());
68 }
69
RegisterNumTotalHits(std::atomic<int> * num_total_hits)70 void ResultStateV2::RegisterNumTotalHits(std::atomic<int>* num_total_hits) {
71 // Decrement the original num_total_hits_ before registering a new one.
72 IncrementNumTotalHits(-1 * scored_document_hits_ranker->size());
73 num_total_hits_ = num_total_hits;
74 IncrementNumTotalHits(scored_document_hits_ranker->size());
75 }
76
IncrementNumTotalHits(int increment_by)77 void ResultStateV2::IncrementNumTotalHits(int increment_by) {
78 if (num_total_hits_ != nullptr) {
79 *num_total_hits_ += increment_by;
80 }
81 }
82
83 } // namespace lib
84 } // namespace icing
85