aboutsummaryrefslogtreecommitdiff
path: root/icing/store/document-associated-score-data.h
blob: 9a711c83822a12472e9ac90295d7b39b046f2394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (C) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ICING_STORE_DOCUMENT_ASSOCIATED_SCORE_DATA_H_
#define ICING_STORE_DOCUMENT_ASSOCIATED_SCORE_DATA_H_

#include <cstdint>
#include <type_traits>

#include "icing/legacy/core/icing-packed-pod.h"
#include "icing/store/corpus-id.h"

namespace icing {
namespace lib {

// This is the cache entity of document-associated scores. It contains scores
// that are related to the document itself. The ground-truth data is stored
// somewhere else. The cache includes:
// 1. Corpus Id.
// 2. Document score. It's defined in and passed from DocumentProto.score.
//    Positive values are required.
// 3. Document creation timestamp. Unix timestamp of when the document is
//    created and inserted into Icing.
// 4. Document length in number of tokens.
class DocumentAssociatedScoreData {
 public:
  explicit DocumentAssociatedScoreData(CorpusId corpus_id, int document_score,
                                       int64_t creation_timestamp_ms,
                                       int length_in_tokens = 0)
      : creation_timestamp_ms_(creation_timestamp_ms),
        corpus_id_(corpus_id),
        document_score_(document_score),
        length_in_tokens_(length_in_tokens) {}

  bool operator==(const DocumentAssociatedScoreData& other) const {
    return document_score_ == other.document_score() &&
           creation_timestamp_ms_ == other.creation_timestamp_ms() &&
           length_in_tokens_ == other.length_in_tokens() &&
           corpus_id_ == other.corpus_id();
  }

  CorpusId corpus_id() const { return corpus_id_; }

  int document_score() const { return document_score_; }

  int64_t creation_timestamp_ms() const { return creation_timestamp_ms_; }

  int length_in_tokens() const { return length_in_tokens_; }

 private:
  int64_t creation_timestamp_ms_;
  CorpusId corpus_id_;
  int document_score_;
  int length_in_tokens_;
} __attribute__((packed));

static_assert(sizeof(DocumentAssociatedScoreData) == 20,
              "Size of DocumentAssociatedScoreData should be 20");
static_assert(icing_is_packed_pod<DocumentAssociatedScoreData>::value,
              "go/icing-ubsan");

}  // namespace lib
}  // namespace icing

#endif  // ICING_STORE_DOCUMENT_ASSOCIATED_SCORE_DATA_H_