format.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "leveldb/options.h"
  7. #include "port/port.h"
  8. #include "table/block.h"
  9. #include "util/coding.h"
  10. #include "util/crc32c.h"
  11. namespace leveldb {
  12. void BlockHandle::EncodeTo(std::string* dst) const {
  13. // Sanity check that all fields have been set
  14. assert(offset_ != ~static_cast<uint64_t>(0));
  15. assert(size_ != ~static_cast<uint64_t>(0));
  16. PutVarint64(dst, offset_);
  17. PutVarint64(dst, size_);
  18. }
  19. Status BlockHandle::DecodeFrom(Slice* input) {
  20. if (GetVarint64(input, &offset_) && 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. const size_t original_size = dst->size();
  28. metaindex_handle_.EncodeTo(dst);
  29. index_handle_.EncodeTo(dst);
  30. dst->resize(2 * BlockHandle::kMaxEncodedLength); // Padding
  31. PutFixed32(dst, static_cast<uint32_t>(kTableMagicNumber & 0xffffffffu));
  32. PutFixed32(dst, static_cast<uint32_t>(kTableMagicNumber >> 32));
  33. assert(dst->size() == original_size + kEncodedLength);
  34. (void)original_size; // Disable unused variable warning.
  35. }
  36. Status Footer::DecodeFrom(Slice* input) {
  37. if (input->size() < kEncodedLength) {
  38. return Status::Corruption("not an sstable (footer too short)");
  39. }
  40. const char* magic_ptr = input->data() + kEncodedLength - 8;
  41. const uint32_t magic_lo = DecodeFixed32(magic_ptr);
  42. const uint32_t magic_hi = DecodeFixed32(magic_ptr + 4);
  43. const uint64_t magic = ((static_cast<uint64_t>(magic_hi) << 32) |
  44. (static_cast<uint64_t>(magic_lo)));
  45. if (magic != kTableMagicNumber) {
  46. return Status::Corruption("not an sstable (bad magic number)");
  47. }
  48. Status result = metaindex_handle_.DecodeFrom(input);
  49. if (result.ok()) {
  50. result = index_handle_.DecodeFrom(input);
  51. }
  52. if (result.ok()) {
  53. // We skip over any leftover data (just padding for now) in "input"
  54. const char* end = magic_ptr + 8;
  55. *input = Slice(end, input->data() + input->size() - end);
  56. }
  57. return result;
  58. }
  59. Status ReadBlock(RandomAccessFile* file, const ReadOptions& options,
  60. const BlockHandle& handle, 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 snappy compressed block length");
  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 snappy 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. case kZstdCompression: {
  125. size_t ulength = 0;
  126. if (!port::Zstd_GetUncompressedLength(data, n, &ulength)) {
  127. delete[] buf;
  128. return Status::Corruption("corrupted zstd compressed block length");
  129. }
  130. char* ubuf = new char[ulength];
  131. if (!port::Zstd_Uncompress(data, n, ubuf)) {
  132. delete[] buf;
  133. delete[] ubuf;
  134. return Status::Corruption("corrupted zstd compressed block contents");
  135. }
  136. delete[] buf;
  137. result->data = Slice(ubuf, ulength);
  138. result->heap_allocated = true;
  139. result->cachable = true;
  140. break;
  141. }
  142. default:
  143. delete[] buf;
  144. return Status::Corruption("bad block type");
  145. }
  146. return Status::OK();
  147. }
  148. } // namespace leveldb