作者: 韩晨旭 10225101440 李畅 10225102463
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

142 рядки
4.4 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. #include "table/format.h"
  5. #include "leveldb/env.h"
  6. #include "port/port.h"
  7. #include "table/block.h"
  8. #include "util/coding.h"
  9. #include "util/crc32c.h"
  10. namespace leveldb {
  11. void BlockHandle::EncodeTo(std::string* dst) const {
  12. // Sanity check that all fields have been set
  13. assert(offset_ != ~static_cast<uint64_t>(0));
  14. assert(size_ != ~static_cast<uint64_t>(0));
  15. PutVarint64(dst, offset_);
  16. PutVarint64(dst, size_);
  17. }
  18. Status BlockHandle::DecodeFrom(Slice* input) {
  19. if (GetVarint64(input, &offset_) &&
  20. GetVarint64(input, &size_)) {
  21. return Status::OK();
  22. } else {
  23. return Status::Corruption("bad block handle");
  24. }
  25. }
  26. void Footer::EncodeTo(std::string* dst) const {
  27. #ifndef NDEBUG
  28. const size_t original_size = dst->size();
  29. #endif
  30. metaindex_handle_.EncodeTo(dst);
  31. index_handle_.EncodeTo(dst);
  32. dst->resize(2 * BlockHandle::kMaxEncodedLength); // Padding
  33. PutFixed32(dst, static_cast<uint32_t>(kTableMagicNumber & 0xffffffffu));
  34. PutFixed32(dst, static_cast<uint32_t>(kTableMagicNumber >> 32));
  35. assert(dst->size() == original_size + kEncodedLength);
  36. }
  37. Status Footer::DecodeFrom(Slice* input) {
  38. const char* magic_ptr = input->data() + kEncodedLength - 8;
  39. const uint32_t magic_lo = DecodeFixed32(magic_ptr);
  40. const uint32_t magic_hi = DecodeFixed32(magic_ptr + 4);
  41. const uint64_t magic = ((static_cast<uint64_t>(magic_hi) << 32) |
  42. (static_cast<uint64_t>(magic_lo)));
  43. if (magic != kTableMagicNumber) {
  44. return Status::InvalidArgument("not an sstable (bad magic number)");
  45. }
  46. Status result = metaindex_handle_.DecodeFrom(input);
  47. if (result.ok()) {
  48. result = index_handle_.DecodeFrom(input);
  49. }
  50. if (result.ok()) {
  51. // We skip over any leftover data (just padding for now) in "input"
  52. const char* end = magic_ptr + 8;
  53. *input = Slice(end, input->data() + input->size() - end);
  54. }
  55. return result;
  56. }
  57. Status ReadBlock(RandomAccessFile* file,
  58. const ReadOptions& options,
  59. const BlockHandle& handle,
  60. Block** block,
  61. bool* may_cache) {
  62. *block = NULL;
  63. *may_cache = false;
  64. // Read the block contents as well as the type/crc footer.
  65. // See table_builder.cc for the code that built this structure.
  66. size_t n = static_cast<size_t>(handle.size());
  67. char* buf = new char[n + kBlockTrailerSize];
  68. Slice contents;
  69. Status s = file->Read(handle.offset(), n + kBlockTrailerSize, &contents, buf);
  70. if (!s.ok()) {
  71. delete[] buf;
  72. return s;
  73. }
  74. if (contents.size() != n + kBlockTrailerSize) {
  75. delete[] buf;
  76. return Status::Corruption("truncated block read");
  77. }
  78. // Check the crc of the type and the block contents
  79. const char* data = contents.data(); // Pointer to where Read put the data
  80. if (options.verify_checksums) {
  81. const uint32_t crc = crc32c::Unmask(DecodeFixed32(data + n + 1));
  82. const uint32_t actual = crc32c::Value(data, n + 1);
  83. if (actual != crc) {
  84. delete[] buf;
  85. s = Status::Corruption("block checksum mismatch");
  86. return s;
  87. }
  88. }
  89. switch (data[n]) {
  90. case kNoCompression:
  91. if (data != buf) {
  92. // File implementation gave us pointer to some other data.
  93. // Use it directly under the assumption that it will be live
  94. // while the file is open.
  95. delete[] buf;
  96. *block = new Block(data, n, false /* do not take ownership */);
  97. *may_cache = false; // Do not double-cache
  98. } else {
  99. *block = new Block(buf, n, true /* take ownership */);
  100. *may_cache = true;
  101. }
  102. // Ok
  103. break;
  104. case kSnappyCompression: {
  105. size_t ulength = 0;
  106. if (!port::Snappy_GetUncompressedLength(data, n, &ulength)) {
  107. delete[] buf;
  108. return Status::Corruption("corrupted compressed block contents");
  109. }
  110. char* ubuf = new char[ulength];
  111. if (!port::Snappy_Uncompress(data, n, ubuf)) {
  112. delete[] buf;
  113. delete[] ubuf;
  114. return Status::Corruption("corrupted compressed block contents");
  115. }
  116. delete[] buf;
  117. *block = new Block(ubuf, ulength, true /* take ownership */);
  118. *may_cache = true;
  119. break;
  120. }
  121. default:
  122. delete[] buf;
  123. return Status::Corruption("bad block type");
  124. }
  125. return Status::OK();
  126. }
  127. } // namespace leveldb