小组成员: 曹可心-10223903406 朴祉燕-10224602413
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1 KiB

  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 <string>
  7. #include <stdint.h>
  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. BlockHandle();
  20. // The offset of the block in the file.
  21. uint64_t offset() const { return offset_; }
  22. void set_offset(uint64_t offset) { offset_ = offset; }
  23. // The size of the stored block
  24. uint64_t size() const { return size_; }
  25. void set_size(uint64_t size) { size_ = size; }
  26. void EncodeTo(std::string* dst) const;
  27. Status DecodeFrom(Slice* input);
  28. // Maximum encoding length of a BlockHandle
  29. enum { kMaxEncodedLength = 10 + 10 };
  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. Footer() { }
  39. // The block handle for the metaindex block of the table
  40. const BlockHandle& metaindex_handle() const { return metaindex_handle_; }
  41. void set_metaindex_handle(const BlockHandle& h) { metaindex_handle_ = h; }
  42. // The block handle for the index block of the table
  43. const BlockHandle& index_handle() const {
  44. return index_handle_;
  45. }
  46. void set_index_handle(const BlockHandle& h) {
  47. index_handle_ = h;
  48. }
  49. void EncodeTo(std::string* dst) const;
  50. Status DecodeFrom(Slice* input);
  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 {
  55. kEncodedLength = 2*BlockHandle::kMaxEncodedLength + 8
  56. };
  57. private:
  58. BlockHandle metaindex_handle_;
  59. BlockHandle index_handle_;
  60. };
  61. // kTableMagicNumber was picked by running
  62. // echo http://code.google.com/p/leveldb/ | sha1sum
  63. // and taking the leading 64 bits.
  64. static const uint64_t kTableMagicNumber = 0xdb4775248b80fb57ull;
  65. // 1-byte type + 32-bit crc
  66. static const size_t kBlockTrailerSize = 5;
  67. struct BlockContents {
  68. Slice data; // Actual contents of data
  69. bool cachable; // True iff data can be cached
  70. bool heap_allocated; // True iff caller should delete[] data.data()
  71. };
  72. // Read the block identified by "handle" from "file". On failure
  73. // return non-OK. On success fill *result and return OK.
  74. extern Status ReadBlock(RandomAccessFile* file,
  75. const ReadOptions& options,
  76. const BlockHandle& handle,
  77. BlockContents* result);
  78. // Implementation details follow. Clients should ignore,
  79. inline BlockHandle::BlockHandle()
  80. : offset_(~static_cast<uint64_t>(0)),
  81. size_(~static_cast<uint64_t>(0)) {
  82. }
  83. } // namespace leveldb
  84. #endif // STORAGE_LEVELDB_TABLE_FORMAT_H_