• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class DocumentAssociatedScoreData {
37  public:
38   explicit DocumentAssociatedScoreData(CorpusId corpus_id, int document_score,
39                                        int64_t creation_timestamp_ms,
40                                        int length_in_tokens = 0)
creation_timestamp_ms_(creation_timestamp_ms)41       : creation_timestamp_ms_(creation_timestamp_ms),
42         corpus_id_(corpus_id),
43         document_score_(document_score),
44         length_in_tokens_(length_in_tokens) {}
45 
46   bool operator==(const DocumentAssociatedScoreData& other) const {
47     return document_score_ == other.document_score() &&
48            creation_timestamp_ms_ == other.creation_timestamp_ms() &&
49            length_in_tokens_ == other.length_in_tokens() &&
50            corpus_id_ == other.corpus_id();
51   }
52 
corpus_id()53   CorpusId corpus_id() const { return corpus_id_; }
54 
document_score()55   int document_score() const { return document_score_; }
56 
creation_timestamp_ms()57   int64_t creation_timestamp_ms() const { return creation_timestamp_ms_; }
58 
length_in_tokens()59   int length_in_tokens() const { return length_in_tokens_; }
60 
61  private:
62   int64_t creation_timestamp_ms_;
63   CorpusId corpus_id_;
64   int document_score_;
65   int length_in_tokens_;
66 } __attribute__((packed));
67 
68 static_assert(sizeof(DocumentAssociatedScoreData) == 20,
69               "Size of DocumentAssociatedScoreData should be 20");
70 static_assert(icing_is_packed_pod<DocumentAssociatedScoreData>::value,
71               "go/icing-ubsan");
72 
73 }  // namespace lib
74 }  // namespace icing
75 
76 #endif  // ICING_STORE_DOCUMENT_ASSOCIATED_SCORE_DATA_H_
77