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