format.h 3.0 KB

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