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 #ifndef ICING_SCORING_SCORED_DOCUMENT_HIT_H_ 16 #define ICING_SCORING_SCORED_DOCUMENT_HIT_H_ 17 18 #include <type_traits> 19 20 #include "icing/legacy/core/icing-packed-pod.h" 21 #include "icing/schema/section.h" 22 #include "icing/store/document-id.h" 23 24 namespace icing { 25 namespace lib { 26 27 // A data class containing information about the document, hit sections, and a 28 // score. The score is calculated against both the document and the hit 29 // sections. 30 class ScoredDocumentHit { 31 public: ScoredDocumentHit(DocumentId document_id,SectionIdMask hit_section_id_mask,double score)32 ScoredDocumentHit(DocumentId document_id, SectionIdMask hit_section_id_mask, 33 double score) 34 : document_id_(document_id), 35 hit_section_id_mask_(hit_section_id_mask), 36 score_(score) {} 37 38 bool operator<(const ScoredDocumentHit& other) const { 39 if (score() < other.score()) return true; 40 if (score() > other.score()) return false; 41 return document_id() < other.document_id(); 42 } 43 document_id()44 DocumentId document_id() const { return document_id_; } 45 hit_section_id_mask()46 SectionIdMask hit_section_id_mask() const { return hit_section_id_mask_; } 47 score()48 double score() const { return score_; } 49 50 private: 51 DocumentId document_id_; 52 SectionIdMask hit_section_id_mask_; 53 double score_; 54 } __attribute__((packed)); 55 56 static_assert(sizeof(ScoredDocumentHit) == 14, 57 "Size of ScoredDocHit should be 14"); 58 static_assert(icing_is_packed_pod<ScoredDocumentHit>::value, "go/icing-ubsan"); 59 60 // A custom comparator for ScoredDocumentHit that determines which 61 // ScoredDocumentHit is better (should come first) based off of 62 // ScoredDocumentHit itself and the order of its score. 63 // 64 // Returns true if left is better than right according to score and order. 65 // Comparison is based off of score with ties broken by 66 // ScoredDocumentHit.document_id(). 67 class ScoredDocumentHitComparator { 68 public: 69 explicit ScoredDocumentHitComparator(bool is_descending = true) is_descending_(is_descending)70 : is_descending_(is_descending) {} 71 operator()72 bool operator()(const ScoredDocumentHit& lhs, 73 const ScoredDocumentHit& rhs) const { 74 return is_descending_ == !(lhs < rhs); 75 } 76 77 private: 78 bool is_descending_; 79 }; 80 81 } // namespace lib 82 } // namespace icing 83 84 #endif // ICING_SCORING_SCORED_DOCUMENT_HIT_H_ 85