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