作者: 韩晨旭 10225101440 李畅 10225102463
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 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. // Read the block identified by "handle" from "file". On success,
  68. // store a pointer to the heap-allocated result in *block and return
  69. // OK. On failure store NULL in *block and return non-OK.
  70. extern Status ReadBlock(RandomAccessFile* file,
  71. const ReadOptions& options,
  72. const BlockHandle& handle,
  73. Block** block);
  74. // Implementation details follow. Clients should ignore,
  75. inline BlockHandle::BlockHandle()
  76. : offset_(~static_cast<uint64_t>(0)),
  77. size_(~static_cast<uint64_t>(0)) {
  78. }
  79. }
  80. #endif // STORAGE_LEVELDB_TABLE_FORMAT_H_