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