aboutsummaryrefslogtreecommitdiff
path: root/icing/index/lite/lite-index-header.h
blob: 75de8faca2b52f1e5de4a6fc3f8f132ac8d3b3ae (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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_LEGACY_INDEX_ICING_LITE_INDEX_HEADER_H_
#define ICING_LEGACY_INDEX_ICING_LITE_INDEX_HEADER_H_

#include <cstddef>
#include <cstdint>

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

namespace icing {
namespace lib {

// A wrapper around the actual mmapped header data.
class LiteIndex_Header {
 public:
  virtual ~LiteIndex_Header() = default;

  // Returns true if the magic of the header matches the hard-coded magic
  // value associated with this header format.
  virtual bool check_magic() const = 0;

  virtual uint32_t lite_index_crc() const = 0;
  virtual void set_lite_index_crc(uint32_t crc) = 0;

  virtual uint32_t last_added_docid() const = 0;
  virtual void set_last_added_docid(uint32_t last_added_docid) = 0;

  virtual uint32_t cur_size() const = 0;
  virtual void set_cur_size(uint32_t cur_size) = 0;

  virtual uint32_t searchable_end() const = 0;
  virtual void set_searchable_end(uint32_t searchable_end) = 0;

  virtual uint32_t CalculateHeaderCrc() const = 0;

  virtual void Reset() = 0;
};

class LiteIndex_HeaderImpl : public LiteIndex_Header {
 public:
  struct HeaderData {
    static uint32_t GetCurrentMagic(
        bool include_property_existence_metadata_hits) {
      if (!include_property_existence_metadata_hits) {
        return 0x01c61418;
      } else {
        return 0x56e07d5b;
      }
    }

    uint32_t lite_index_crc;
    uint32_t magic;
    // This field is available to be reclaimed for another purpose without
    // forcing a change in header size. NOTE: claiming this fields doesn't
    // guarantee that the newly claimed field will have the proper value. If you
    // are depending on the value of this field then you will have to have a
    // migration - either a one-time event during Upgrade() or Init() or
    // determined by a flag change in Init().
    uint32_t padding;
    uint32_t last_added_docid;
    uint32_t cur_size;
    uint32_t searchable_end;
  };

  explicit LiteIndex_HeaderImpl(HeaderData *hdr,
                                bool include_property_existence_metadata_hits)
      : hdr_(hdr),
        include_property_existence_metadata_hits_(
            include_property_existence_metadata_hits) {}

  bool check_magic() const override {
    return hdr_->magic == HeaderData::GetCurrentMagic(
                              include_property_existence_metadata_hits_);
  }

  uint32_t lite_index_crc() const override { return hdr_->lite_index_crc; }
  void set_lite_index_crc(uint32_t crc) override { hdr_->lite_index_crc = crc; }

  uint32_t last_added_docid() const override { return hdr_->last_added_docid; }
  void set_last_added_docid(uint32_t last_added_docid) override {
    hdr_->last_added_docid = last_added_docid;
  }

  uint32_t cur_size() const override { return hdr_->cur_size; }
  void set_cur_size(uint32_t cur_size) override { hdr_->cur_size = cur_size; }

  uint32_t searchable_end() const override { return hdr_->searchable_end; }
  void set_searchable_end(uint32_t searchable_end) override {
    hdr_->searchable_end = searchable_end;
  }

  uint32_t CalculateHeaderCrc() const override {
    return IcingStringUtil::UpdateCrc32(
        0, reinterpret_cast<const char *>(hdr_) + offsetof(HeaderData, magic),
        sizeof(HeaderData) - offsetof(HeaderData, magic));
  }

  void Reset() override {
    hdr_->lite_index_crc = 0;
    hdr_->magic =
        HeaderData::GetCurrentMagic(include_property_existence_metadata_hits_);
    hdr_->last_added_docid = kInvalidDocumentId;
    hdr_->cur_size = 0;
    hdr_->searchable_end = 0;
  }

 private:
  HeaderData *hdr_;
  bool include_property_existence_metadata_hits_;
};
static_assert(24 == sizeof(LiteIndex_HeaderImpl::HeaderData),
              "sizeof(HeaderData) != 24");

}  // namespace lib
}  // namespace icing

#endif  // ICING_LEGACY_INDEX_ICING_LITE_INDEX_HEADER_H_