aboutsummaryrefslogtreecommitdiff
path: root/icing/index/hit/doc-hit-info.cc
blob: 8e418c8c90a4ed965723cc803e751fe6f9140b07 (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
// 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.

#include "icing/index/hit/doc-hit-info.h"

#include "icing/legacy/core/icing-string-util.h"

namespace icing {
namespace lib {

bool DocHitInfo::operator<(const DocHitInfo& other) const {
  if (document_id() != other.document_id()) {
    // Sort by document_id descending. This mirrors how the individual hits that
    // are collapsed into this DocHitInfo would sort with other hits -
    // document_ids are inverted when encoded in hits. Hits are encoded this way
    // because they are appended to posting lists and the most recent value
    // appended to a posting list must have the smallest encoded value of any
    // hit on the posting list.
    return document_id() > other.document_id();
  }
  if (hit_section_ids_mask() != other.hit_section_ids_mask()) {
    return hit_section_ids_mask() < other.hit_section_ids_mask();
  }
  // Doesn't matter which way we compare this array, as long as
  // DocHitInfo is unequal when it is unequal.
  return memcmp(hit_term_frequency_, other.hit_term_frequency_,
                sizeof(hit_term_frequency_)) < 0;
}

void DocHitInfo::UpdateSection(SectionId section_id,
                               Hit::TermFrequency hit_term_frequency) {
  SectionIdMask section_id_mask = (1u << section_id);
  if ((hit_section_ids_mask() & section_id_mask)) {
    // If the sectionId is already embedded in the hit_section_ids_mask,
    // then the term frequencies should always match. So there is no
    // need to update anything.
    return;
  }
  hit_term_frequency_[section_id] = hit_term_frequency;
  hit_section_ids_mask_ |= section_id_mask;
}

void DocHitInfo::MergeSectionsFrom(const DocHitInfo& other) {
  SectionIdMask other_mask = other.hit_section_ids_mask();
  while (other_mask) {
    SectionId section_id = __builtin_ctz(other_mask);
    UpdateSection(section_id, other.hit_term_frequency(section_id));
    other_mask &= ~(1u << section_id);
  }
}

}  // namespace lib
}  // namespace icing