• 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_LEGACY_INDEX_ICING_LITE_INDEX_HEADER_H_
16 #define ICING_LEGACY_INDEX_ICING_LITE_INDEX_HEADER_H_
17 
18 #include "icing/legacy/core/icing-string-util.h"
19 #include "icing/store/document-id.h"
20 
21 namespace icing {
22 namespace lib {
23 
24 // A wrapper around the actual mmapped header data.
25 class LiteIndex_Header {
26  public:
27   virtual ~LiteIndex_Header() = default;
28 
29   // Returns true if the magic of the header matches the hard-coded magic
30   // value associated with this header format.
31   virtual bool check_magic() const = 0;
32 
33   virtual uint32_t lite_index_crc() const = 0;
34   virtual void set_lite_index_crc(uint32_t crc) = 0;
35 
36   virtual uint32_t last_added_docid() const = 0;
37   virtual void set_last_added_docid(uint32_t last_added_docid) = 0;
38 
39   virtual uint32_t cur_size() const = 0;
40   virtual void set_cur_size(uint32_t cur_size) = 0;
41 
42   virtual uint32_t searchable_end() const = 0;
43   virtual void set_searchable_end(uint32_t searchable_end) = 0;
44 
45   virtual uint32_t CalculateHeaderCrc() const = 0;
46 
47   virtual void Reset() = 0;
48 };
49 
50 class LiteIndex_HeaderImpl : public LiteIndex_Header {
51  public:
52   struct HeaderData {
53     static const uint32_t kMagic = 0xb4fb8792;
54 
55     uint32_t lite_index_crc;
56     uint32_t magic;
57     // This field is available to be reclaimed for another purpose without
58     // forcing a change in header size. NOTE: claiming this fields doesn't
59     // guarantee that the newly claimed field will have the proper value. If you
60     // are depending on the value of this field then you will have to have a
61     // migration - either a one-time event during Upgrade() or Init() or
62     // determined by a flag change in Init().
63     uint32_t padding;
64     uint32_t last_added_docid;
65     uint32_t cur_size;
66     uint32_t searchable_end;
67   };
68 
LiteIndex_HeaderImpl(HeaderData * hdr)69   explicit LiteIndex_HeaderImpl(HeaderData *hdr) : hdr_(hdr) {}
70 
check_magic()71   bool check_magic() const override {
72     return hdr_->magic == HeaderData::kMagic;
73   }
74 
lite_index_crc()75   uint32_t lite_index_crc() const override { return hdr_->lite_index_crc; }
set_lite_index_crc(uint32_t crc)76   void set_lite_index_crc(uint32_t crc) override { hdr_->lite_index_crc = crc; }
77 
last_added_docid()78   uint32_t last_added_docid() const override { return hdr_->last_added_docid; }
set_last_added_docid(uint32_t last_added_docid)79   void set_last_added_docid(uint32_t last_added_docid) override {
80     hdr_->last_added_docid = last_added_docid;
81   }
82 
cur_size()83   uint32_t cur_size() const override { return hdr_->cur_size; }
set_cur_size(uint32_t cur_size)84   void set_cur_size(uint32_t cur_size) override { hdr_->cur_size = cur_size; }
85 
searchable_end()86   uint32_t searchable_end() const override { return hdr_->searchable_end; }
set_searchable_end(uint32_t searchable_end)87   void set_searchable_end(uint32_t searchable_end) override {
88     hdr_->searchable_end = searchable_end;
89   }
90 
CalculateHeaderCrc()91   uint32_t CalculateHeaderCrc() const override {
92     return IcingStringUtil::UpdateCrc32(
93         0, reinterpret_cast<const char *>(hdr_) + offsetof(HeaderData, magic),
94         sizeof(HeaderData) - offsetof(HeaderData, magic));
95   }
96 
Reset()97   void Reset() override {
98     hdr_->lite_index_crc = 0;
99     hdr_->magic = HeaderData::kMagic;
100     hdr_->last_added_docid = kInvalidDocumentId;
101     hdr_->cur_size = 0;
102     hdr_->searchable_end = 0;
103   }
104 
105  private:
106   HeaderData *hdr_;
107 };
108 static_assert(24 == sizeof(LiteIndex_HeaderImpl::HeaderData),
109               "sizeof(HeaderData) != 24");
110 
111 }  // namespace lib
112 }  // namespace icing
113 
114 #endif  // ICING_LEGACY_INDEX_ICING_LITE_INDEX_HEADER_H_
115