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 #ifndef ICING_SCORING_SCORED_DOCUMENT_HITS_RANKER_H_ 16 #define ICING_SCORING_SCORED_DOCUMENT_HITS_RANKER_H_ 17 18 #include "icing/scoring/scored-document-hit.h" 19 20 namespace icing { 21 namespace lib { 22 23 // TODO(sungyc): re-evaluate other similar implementations (e.g. std::sort + 24 // std::queue/std::vector). Also revisit the capacity shrinking 25 // issue for PopNext(). 26 27 // ScoredDocumentHitsRanker is an interface class for ranking 28 // ScoredDocumentHits. 29 class ScoredDocumentHitsRanker { 30 public: 31 virtual ~ScoredDocumentHitsRanker() = default; 32 33 // Pop the next top JoinedScoredDocumentHit and return. It is undefined to 34 // call PopNext on an empty ranker, so the caller should check if it is not 35 // empty before calling. 36 // 37 // Note: ranker may store ScoredDocumentHit or JoinedScoredDocumentHit. We can 38 // add template for this interface, but since JoinedScoredDocumentHit is a 39 // superset of ScoredDocumentHit, we unify the return type of PopNext to use 40 // the superset type JoinedScoredDocumentHit in order to make it simple, and 41 // rankers storing ScoredDocumentHit should convert it to 42 // JoinedScoredDocumentHit before returning. It makes the implementation 43 // simpler, especially for ResultRetriever, which now only needs to deal with 44 // one single return format. 45 virtual JoinedScoredDocumentHit PopNext() = 0; 46 47 // Truncates the remaining ScoredDocumentHits to the given size. The best 48 // ScoredDocumentHits (according to the ranking policy) should be kept. 49 // If new_size is invalid (< 0), or greater or equal to # of remaining 50 // ScoredDocumentHits, then no action will be taken. Otherwise truncates the 51 // the remaining ScoredDocumentHits to the given size. 52 virtual void TruncateHitsTo(int new_size) = 0; 53 54 virtual int size() const = 0; 55 56 virtual bool empty() const = 0; 57 }; 58 59 } // namespace lib 60 } // namespace icing 61 62 #endif // ICING_SCORING_SCORED_DOCUMENT_HITS_RANKER_H_ 63