Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

145 linhas
4.5 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. BlockContents* result) {
  61. result->data = Slice();
  62. result->cachable = false;
  63. result->heap_allocated = 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. result->data = Slice(data, n);
  97. result->heap_allocated = false;
  98. result->cachable = false; // Do not double-cache
  99. } else {
  100. result->data = Slice(buf, n);
  101. result->heap_allocated = true;
  102. result->cachable = true;
  103. }
  104. // Ok
  105. break;
  106. case kSnappyCompression: {
  107. size_t ulength = 0;
  108. if (!port::Snappy_GetUncompressedLength(data, n, &ulength)) {
  109. delete[] buf;
  110. return Status::Corruption("corrupted compressed block contents");
  111. }
  112. char* ubuf = new char[ulength];
  113. if (!port::Snappy_Uncompress(data, n, ubuf)) {
  114. delete[] buf;
  115. delete[] ubuf;
  116. return Status::Corruption("corrupted compressed block contents");
  117. }
  118. delete[] buf;
  119. result->data = Slice(ubuf, ulength);
  120. result->heap_allocated = true;
  121. result->cachable = true;
  122. break;
  123. }
  124. default:
  125. delete[] buf;
  126. return Status::Corruption("bad block type");
  127. }
  128. return Status::OK();
  129. }
  130. } // namespace leveldb