1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 #ifndef STORAGE_LEVELDB_TABLE_FORMAT_H_
6 #define STORAGE_LEVELDB_TABLE_FORMAT_H_
10 #include "leveldb/slice.h"
11 #include "leveldb/status.h"
12 #include "leveldb/table_builder.h"
17 class RandomAccessFile;
20 // BlockHandle is a pointer to the extent of a file that stores a data
21 // block or a meta block.
26 // The offset of the block in the file.
27 uint64_t offset() const { return offset_; }
28 void set_offset(uint64_t offset) { offset_ = offset; }
30 // The size of the stored block
31 uint64_t size() const { return size_; }
32 void set_size(uint64_t size) { size_ = size; }
34 void EncodeTo(std::string* dst) const;
35 Status DecodeFrom(Slice* input);
37 // Maximum encoding length of a BlockHandle
38 enum { kMaxEncodedLength = 10 + 10 };
45 // Footer encapsulates the fixed information stored at the tail
46 // end of every table file.
51 // The block handle for the metaindex block of the table
52 const BlockHandle& metaindex_handle() const { return metaindex_handle_; }
53 void set_metaindex_handle(const BlockHandle& h) { metaindex_handle_ = h; }
55 // The block handle for the index block of the table
56 const BlockHandle& index_handle() const {
59 void set_index_handle(const BlockHandle& h) {
63 void EncodeTo(std::string* dst) const;
64 Status DecodeFrom(Slice* input);
66 // Encoded length of a Footer. Note that the serialization of a
67 // Footer will always occupy exactly this many bytes. It consists
68 // of two block handles and a magic number.
70 kEncodedLength = 2*BlockHandle::kMaxEncodedLength + 8
74 BlockHandle metaindex_handle_;
75 BlockHandle index_handle_;
78 // kTableMagicNumber was picked by running
79 // echo http://code.google.com/p/leveldb/ | sha1sum
80 // and taking the leading 64 bits.
81 static const uint64_t kTableMagicNumber = 0xdb4775248b80fb57ull;
83 // 1-byte type + 32-bit crc
84 static const size_t kBlockTrailerSize = 5;
86 // Read the block identified by "handle" from "file". On success,
87 // store a pointer to the heap-allocated result in *block and return
88 // OK. On failure store NULL in *block and return non-OK.
89 extern Status ReadBlock(RandomAccessFile* file,
90 const ReadOptions& options,
91 const BlockHandle& handle,
94 // Implementation details follow. Clients should ignore,
96 inline BlockHandle::BlockHandle()
97 : offset_(~static_cast<uint64_t>(0)),
98 size_(~static_cast<uint64_t>(0)) {
101 } // namespace leveldb
103 #endif // STORAGE_LEVELDB_TABLE_FORMAT_H_