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_STORE_DOCUMENT_ASSOCIATED_SCORE_DATA_H_ 16 #define ICING_STORE_DOCUMENT_ASSOCIATED_SCORE_DATA_H_ 17 18 #include <cstdint> 19 #include <type_traits> 20 21 #include "icing/legacy/core/icing-packed-pod.h" 22 #include "icing/store/corpus-id.h" 23 24 namespace icing { 25 namespace lib { 26 27 // This is the cache entity of document-associated scores. It contains scores 28 // that are related to the document itself. The ground-truth data is stored 29 // somewhere else. The cache includes: 30 // 1. Corpus Id. 31 // 2. Document score. It's defined in and passed from DocumentProto.score. 32 // Positive values are required. 33 // 3. Document creation timestamp. Unix timestamp of when the document is 34 // created and inserted into Icing. 35 // 4. Document length in number of tokens. 36 // 5. Index of the ScorablePropertySetProto data of a document in the scorable 37 // property cache, which is owned by the document-store. 38 class DocumentAssociatedScoreData { 39 public: 40 explicit DocumentAssociatedScoreData(CorpusId corpus_id, int document_score, 41 int64_t creation_timestamp_ms, 42 int32_t scorable_property_cache_index, 43 int length_in_tokens = 0) creation_timestamp_ms_(creation_timestamp_ms)44 : creation_timestamp_ms_(creation_timestamp_ms), 45 corpus_id_(corpus_id), 46 document_score_(document_score), 47 length_in_tokens_(length_in_tokens), 48 scorable_property_cache_index_(scorable_property_cache_index) {} 49 50 bool operator==(const DocumentAssociatedScoreData& other) const { 51 return document_score_ == other.document_score() && 52 creation_timestamp_ms_ == other.creation_timestamp_ms() && 53 length_in_tokens_ == other.length_in_tokens() && 54 corpus_id_ == other.corpus_id() && 55 scorable_property_cache_index_ == 56 other.scorable_property_cache_index(); 57 } 58 corpus_id()59 CorpusId corpus_id() const { return corpus_id_; } 60 document_score()61 int document_score() const { return document_score_; } 62 creation_timestamp_ms()63 int64_t creation_timestamp_ms() const { return creation_timestamp_ms_; } 64 length_in_tokens()65 int length_in_tokens() const { return length_in_tokens_; } 66 scorable_property_cache_index()67 int32_t scorable_property_cache_index() const { 68 return scorable_property_cache_index_; 69 } 70 set_scorable_property_cache_index(int32_t scorable_property_cache_index)71 void set_scorable_property_cache_index( 72 int32_t scorable_property_cache_index) { 73 scorable_property_cache_index_ = scorable_property_cache_index; 74 } 75 76 private: 77 int64_t creation_timestamp_ms_; 78 CorpusId corpus_id_; 79 int document_score_; 80 int length_in_tokens_; 81 int32_t scorable_property_cache_index_; 82 } __attribute__((packed)); 83 84 static_assert(sizeof(DocumentAssociatedScoreData) == 24, 85 "Size of DocumentAssociatedScoreData should be 24"); 86 static_assert(icing_is_packed_pod<DocumentAssociatedScoreData>::value, 87 "go/icing-ubsan"); 88 89 } // namespace lib 90 } // namespace icing 91 92 #endif // ICING_STORE_DOCUMENT_ASSOCIATED_SCORE_DATA_H_ 93