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