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