1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors. 4*9507f98cSAndroid Build Coastguard Worker 5*9507f98cSAndroid Build Coastguard Worker #ifndef STORAGE_LEVELDB_TABLE_FORMAT_H_ 6*9507f98cSAndroid Build Coastguard Worker #define STORAGE_LEVELDB_TABLE_FORMAT_H_ 7*9507f98cSAndroid Build Coastguard Worker 8*9507f98cSAndroid Build Coastguard Worker #include <cstdint> 9*9507f98cSAndroid Build Coastguard Worker #include <string> 10*9507f98cSAndroid Build Coastguard Worker 11*9507f98cSAndroid Build Coastguard Worker #include "leveldb/slice.h" 12*9507f98cSAndroid Build Coastguard Worker #include "leveldb/status.h" 13*9507f98cSAndroid Build Coastguard Worker #include "leveldb/table_builder.h" 14*9507f98cSAndroid Build Coastguard Worker 15*9507f98cSAndroid Build Coastguard Worker namespace leveldb { 16*9507f98cSAndroid Build Coastguard Worker 17*9507f98cSAndroid Build Coastguard Worker class Block; 18*9507f98cSAndroid Build Coastguard Worker class RandomAccessFile; 19*9507f98cSAndroid Build Coastguard Worker struct ReadOptions; 20*9507f98cSAndroid Build Coastguard Worker 21*9507f98cSAndroid Build Coastguard Worker // BlockHandle is a pointer to the extent of a file that stores a data 22*9507f98cSAndroid Build Coastguard Worker // block or a meta block. 23*9507f98cSAndroid Build Coastguard Worker class BlockHandle { 24*9507f98cSAndroid Build Coastguard Worker public: 25*9507f98cSAndroid Build Coastguard Worker // Maximum encoding length of a BlockHandle 26*9507f98cSAndroid Build Coastguard Worker enum { kMaxEncodedLength = 10 + 10 }; 27*9507f98cSAndroid Build Coastguard Worker 28*9507f98cSAndroid Build Coastguard Worker BlockHandle(); 29*9507f98cSAndroid Build Coastguard Worker 30*9507f98cSAndroid Build Coastguard Worker // The offset of the block in the file. offset()31*9507f98cSAndroid Build Coastguard Worker uint64_t offset() const { return offset_; } set_offset(uint64_t offset)32*9507f98cSAndroid Build Coastguard Worker void set_offset(uint64_t offset) { offset_ = offset; } 33*9507f98cSAndroid Build Coastguard Worker 34*9507f98cSAndroid Build Coastguard Worker // The size of the stored block size()35*9507f98cSAndroid Build Coastguard Worker uint64_t size() const { return size_; } set_size(uint64_t size)36*9507f98cSAndroid Build Coastguard Worker void set_size(uint64_t size) { size_ = size; } 37*9507f98cSAndroid Build Coastguard Worker 38*9507f98cSAndroid Build Coastguard Worker void EncodeTo(std::string* dst) const; 39*9507f98cSAndroid Build Coastguard Worker Status DecodeFrom(Slice* input); 40*9507f98cSAndroid Build Coastguard Worker 41*9507f98cSAndroid Build Coastguard Worker private: 42*9507f98cSAndroid Build Coastguard Worker uint64_t offset_; 43*9507f98cSAndroid Build Coastguard Worker uint64_t size_; 44*9507f98cSAndroid Build Coastguard Worker }; 45*9507f98cSAndroid Build Coastguard Worker 46*9507f98cSAndroid Build Coastguard Worker // Footer encapsulates the fixed information stored at the tail 47*9507f98cSAndroid Build Coastguard Worker // end of every table file. 48*9507f98cSAndroid Build Coastguard Worker class Footer { 49*9507f98cSAndroid Build Coastguard Worker public: 50*9507f98cSAndroid Build Coastguard Worker // Encoded length of a Footer. Note that the serialization of a 51*9507f98cSAndroid Build Coastguard Worker // Footer will always occupy exactly this many bytes. It consists 52*9507f98cSAndroid Build Coastguard Worker // of two block handles and a magic number. 53*9507f98cSAndroid Build Coastguard Worker enum { kEncodedLength = 2 * BlockHandle::kMaxEncodedLength + 8 }; 54*9507f98cSAndroid Build Coastguard Worker 55*9507f98cSAndroid Build Coastguard Worker Footer() = default; 56*9507f98cSAndroid Build Coastguard Worker 57*9507f98cSAndroid Build Coastguard Worker // The block handle for the metaindex block of the table metaindex_handle()58*9507f98cSAndroid Build Coastguard Worker const BlockHandle& metaindex_handle() const { return metaindex_handle_; } set_metaindex_handle(const BlockHandle & h)59*9507f98cSAndroid Build Coastguard Worker void set_metaindex_handle(const BlockHandle& h) { metaindex_handle_ = h; } 60*9507f98cSAndroid Build Coastguard Worker 61*9507f98cSAndroid Build Coastguard Worker // The block handle for the index block of the table index_handle()62*9507f98cSAndroid Build Coastguard Worker const BlockHandle& index_handle() const { return index_handle_; } set_index_handle(const BlockHandle & h)63*9507f98cSAndroid Build Coastguard Worker void set_index_handle(const BlockHandle& h) { index_handle_ = h; } 64*9507f98cSAndroid Build Coastguard Worker 65*9507f98cSAndroid Build Coastguard Worker void EncodeTo(std::string* dst) const; 66*9507f98cSAndroid Build Coastguard Worker Status DecodeFrom(Slice* input); 67*9507f98cSAndroid Build Coastguard Worker 68*9507f98cSAndroid Build Coastguard Worker private: 69*9507f98cSAndroid Build Coastguard Worker BlockHandle metaindex_handle_; 70*9507f98cSAndroid Build Coastguard Worker BlockHandle index_handle_; 71*9507f98cSAndroid Build Coastguard Worker }; 72*9507f98cSAndroid Build Coastguard Worker 73*9507f98cSAndroid Build Coastguard Worker // kTableMagicNumber was picked by running 74*9507f98cSAndroid Build Coastguard Worker // echo http://code.google.com/p/leveldb/ | sha1sum 75*9507f98cSAndroid Build Coastguard Worker // and taking the leading 64 bits. 76*9507f98cSAndroid Build Coastguard Worker static const uint64_t kTableMagicNumber = 0xdb4775248b80fb57ull; 77*9507f98cSAndroid Build Coastguard Worker 78*9507f98cSAndroid Build Coastguard Worker // 1-byte type + 32-bit crc 79*9507f98cSAndroid Build Coastguard Worker static const size_t kBlockTrailerSize = 5; 80*9507f98cSAndroid Build Coastguard Worker 81*9507f98cSAndroid Build Coastguard Worker struct BlockContents { 82*9507f98cSAndroid Build Coastguard Worker Slice data; // Actual contents of data 83*9507f98cSAndroid Build Coastguard Worker bool cachable; // True iff data can be cached 84*9507f98cSAndroid Build Coastguard Worker bool heap_allocated; // True iff caller should delete[] data.data() 85*9507f98cSAndroid Build Coastguard Worker }; 86*9507f98cSAndroid Build Coastguard Worker 87*9507f98cSAndroid Build Coastguard Worker // Read the block identified by "handle" from "file". On failure 88*9507f98cSAndroid Build Coastguard Worker // return non-OK. On success fill *result and return OK. 89*9507f98cSAndroid Build Coastguard Worker Status ReadBlock(RandomAccessFile* file, const ReadOptions& options, 90*9507f98cSAndroid Build Coastguard Worker const BlockHandle& handle, BlockContents* result); 91*9507f98cSAndroid Build Coastguard Worker 92*9507f98cSAndroid Build Coastguard Worker // Implementation details follow. Clients should ignore, 93*9507f98cSAndroid Build Coastguard Worker BlockHandle()94*9507f98cSAndroid Build Coastguard Workerinline BlockHandle::BlockHandle() 95*9507f98cSAndroid Build Coastguard Worker : offset_(~static_cast<uint64_t>(0)), size_(~static_cast<uint64_t>(0)) {} 96*9507f98cSAndroid Build Coastguard Worker 97*9507f98cSAndroid Build Coastguard Worker } // namespace leveldb 98*9507f98cSAndroid Build Coastguard Worker 99*9507f98cSAndroid Build Coastguard Worker #endif // STORAGE_LEVELDB_TABLE_FORMAT_H_ 100