Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/leveldb/table/table.cc
Line
Count
Source
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 "leveldb/table.h"
6
7
#include "leveldb/cache.h"
8
#include "leveldb/comparator.h"
9
#include "leveldb/env.h"
10
#include "leveldb/filter_policy.h"
11
#include "leveldb/options.h"
12
#include "table/block.h"
13
#include "table/filter_block.h"
14
#include "table/format.h"
15
#include "table/two_level_iterator.h"
16
#include "util/coding.h"
17
18
namespace leveldb {
19
20
struct Table::Rep {
21
24
  ~Rep() {
22
24
    delete filter;
23
24
    delete[] filter_data;
24
24
    delete index_block;
25
24
  }
26
27
  Options options;
28
  Status status;
29
  RandomAccessFile* file;
30
  uint64_t cache_id;
31
  FilterBlockReader* filter;
32
  const char* filter_data;
33
34
  BlockHandle metaindex_handle;  // Handle to metaindex_block: saved from footer
35
  Block* index_block;
36
};
37
38
Status Table::Open(const Options& options, RandomAccessFile* file,
39
24
                   uint64_t size, Table** table) {
40
24
  *table = nullptr;
41
24
  if (size < Footer::kEncodedLength) {
  Branch (41:7): [True: 0, False: 24]
42
0
    return Status::Corruption("file is too short to be an sstable");
43
0
  }
44
45
24
  char footer_space[Footer::kEncodedLength];
46
24
  Slice footer_input;
47
24
  Status s = file->Read(size - Footer::kEncodedLength, Footer::kEncodedLength,
48
24
                        &footer_input, footer_space);
49
24
  if (!s.ok()) return s;
  Branch (49:7): [True: 0, False: 24]
50
51
24
  Footer footer;
52
24
  s = footer.DecodeFrom(&footer_input);
53
24
  if (!s.ok()) return s;
  Branch (53:7): [True: 0, False: 24]
54
55
  // Read the index block
56
24
  BlockContents index_block_contents;
57
24
  if (s.ok()) {
  Branch (57:7): [True: 24, False: 0]
58
24
    ReadOptions opt;
59
24
    if (options.paranoid_checks) {
  Branch (59:9): [True: 24, False: 0]
60
24
      opt.verify_checksums = true;
61
24
    }
62
24
    s = ReadBlock(file, opt, footer.index_handle(), &index_block_contents);
63
24
  }
64
65
24
  if (s.ok()) {
  Branch (65:7): [True: 24, False: 0]
66
    // We've successfully read the footer and the index block: we're
67
    // ready to serve requests.
68
24
    Block* index_block = new Block(index_block_contents);
69
24
    Rep* rep = new Table::Rep;
70
24
    rep->options = options;
71
24
    rep->file = file;
72
24
    rep->metaindex_handle = footer.metaindex_handle();
73
24
    rep->index_block = index_block;
74
24
    rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0);
  Branch (74:22): [True: 24, False: 0]
75
24
    rep->filter_data = nullptr;
76
24
    rep->filter = nullptr;
77
24
    *table = new Table(rep);
78
24
    (*table)->ReadMeta(footer);
79
24
  }
80
81
24
  return s;
82
24
}
83
84
24
void Table::ReadMeta(const Footer& footer) {
85
24
  if (rep_->options.filter_policy == nullptr) {
  Branch (85:7): [True: 0, False: 24]
86
0
    return;  // Do not need any metadata
87
0
  }
88
89
  // TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
90
  // it is an empty block.
91
24
  ReadOptions opt;
92
24
  if (rep_->options.paranoid_checks) {
  Branch (92:7): [True: 24, False: 0]
93
24
    opt.verify_checksums = true;
94
24
  }
95
24
  BlockContents contents;
96
24
  if (!ReadBlock(rep_->file, opt, footer.metaindex_handle(), &contents).ok()) {
  Branch (96:7): [True: 0, False: 24]
97
    // Do not propagate errors since meta info is not needed for operation
98
0
    return;
99
0
  }
100
24
  Block* meta = new Block(contents);
101
102
24
  Iterator* iter = meta->NewIterator(BytewiseComparator());
103
24
  std::string key = "filter.";
104
24
  key.append(rep_->options.filter_policy->Name());
105
24
  iter->Seek(key);
106
24
  if (iter->Valid() && iter->key() == Slice(key)) {
  Branch (106:7): [True: 24, False: 0]
  Branch (106:7): [True: 24, False: 0]
  Branch (106:24): [True: 24, False: 0]
107
24
    ReadFilter(iter->value());
108
24
  }
109
24
  delete iter;
110
24
  delete meta;
111
24
}
112
113
24
void Table::ReadFilter(const Slice& filter_handle_value) {
114
24
  Slice v = filter_handle_value;
115
24
  BlockHandle filter_handle;
116
24
  if (!filter_handle.DecodeFrom(&v).ok()) {
  Branch (116:7): [True: 0, False: 24]
117
0
    return;
118
0
  }
119
120
  // We might want to unify with ReadBlock() if we start
121
  // requiring checksum verification in Table::Open.
122
24
  ReadOptions opt;
123
24
  if (rep_->options.paranoid_checks) {
  Branch (123:7): [True: 24, False: 0]
124
24
    opt.verify_checksums = true;
125
24
  }
126
24
  BlockContents block;
127
24
  if (!ReadBlock(rep_->file, opt, filter_handle, &block).ok()) {
  Branch (127:7): [True: 0, False: 24]
128
0
    return;
129
0
  }
130
24
  if (block.heap_allocated) {
  Branch (130:7): [True: 0, False: 24]
131
0
    rep_->filter_data = block.data.data();  // Will need to delete later
132
0
  }
133
24
  rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
134
24
}
135
136
24
Table::~Table() { delete rep_; }
137
138
12
static void DeleteBlock(void* arg, void* ignored) {
139
12
  delete reinterpret_cast<Block*>(arg);
140
12
}
141
142
0
static void DeleteCachedBlock(const Slice& key, void* value) {
143
0
  Block* block = reinterpret_cast<Block*>(value);
144
0
  delete block;
145
0
}
146
147
0
static void ReleaseBlock(void* arg, void* h) {
148
0
  Cache* cache = reinterpret_cast<Cache*>(arg);
149
0
  Cache::Handle* handle = reinterpret_cast<Cache::Handle*>(h);
150
0
  cache->Release(handle);
151
0
}
152
153
// Convert an index iterator value (i.e., an encoded BlockHandle)
154
// into an iterator over the contents of the corresponding block.
155
Iterator* Table::BlockReader(void* arg, const ReadOptions& options,
156
12
                             const Slice& index_value) {
157
12
  Table* table = reinterpret_cast<Table*>(arg);
158
12
  Cache* block_cache = table->rep_->options.block_cache;
159
12
  Block* block = nullptr;
160
12
  Cache::Handle* cache_handle = nullptr;
161
162
12
  BlockHandle handle;
163
12
  Slice input = index_value;
164
12
  Status s = handle.DecodeFrom(&input);
165
  // We intentionally allow extra stuff in index_value so that we
166
  // can add more features in the future.
167
168
12
  if (s.ok()) {
  Branch (168:7): [True: 12, False: 0]
169
12
    BlockContents contents;
170
12
    if (block_cache != nullptr) {
  Branch (170:9): [True: 12, False: 0]
171
12
      char cache_key_buffer[16];
172
12
      EncodeFixed64(cache_key_buffer, table->rep_->cache_id);
173
12
      EncodeFixed64(cache_key_buffer + 8, handle.offset());
174
12
      Slice key(cache_key_buffer, sizeof(cache_key_buffer));
175
12
      cache_handle = block_cache->Lookup(key);
176
12
      if (cache_handle != nullptr) {
  Branch (176:11): [True: 0, False: 12]
177
0
        block = reinterpret_cast<Block*>(block_cache->Value(cache_handle));
178
12
      } else {
179
12
        s = ReadBlock(table->rep_->file, options, handle, &contents);
180
12
        if (s.ok()) {
  Branch (180:13): [True: 12, False: 0]
181
12
          block = new Block(contents);
182
12
          if (contents.cachable && options.fill_cache) {
  Branch (182:15): [True: 0, False: 12]
  Branch (182:36): [True: 0, False: 0]
183
0
            cache_handle = block_cache->Insert(key, block, block->size(),
184
0
                                               &DeleteCachedBlock);
185
0
          }
186
12
        }
187
12
      }
188
12
    } else {
189
0
      s = ReadBlock(table->rep_->file, options, handle, &contents);
190
0
      if (s.ok()) {
  Branch (190:11): [True: 0, False: 0]
191
0
        block = new Block(contents);
192
0
      }
193
0
    }
194
12
  }
195
196
12
  Iterator* iter;
197
12
  if (block != nullptr) {
  Branch (197:7): [True: 12, False: 0]
198
12
    iter = block->NewIterator(table->rep_->options.comparator);
199
12
    if (cache_handle == nullptr) {
  Branch (199:9): [True: 12, False: 0]
200
12
      iter->RegisterCleanup(&DeleteBlock, block, nullptr);
201
12
    } else {
202
0
      iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle);
203
0
    }
204
12
  } else {
205
0
    iter = NewErrorIterator(s);
206
0
  }
207
12
  return iter;
208
12
}
209
210
31
Iterator* Table::NewIterator(const ReadOptions& options) const {
211
31
  return NewTwoLevelIterator(
212
31
      rep_->index_block->NewIterator(rep_->options.comparator),
213
31
      &Table::BlockReader, const_cast<Table*>(this), options);
214
31
}
215
216
Status Table::InternalGet(const ReadOptions& options, const Slice& k, void* arg,
217
                          void (*handle_result)(void*, const Slice&,
218
5
                                                const Slice&)) {
219
5
  Status s;
220
5
  Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator);
221
5
  iiter->Seek(k);
222
5
  if (iiter->Valid()) {
  Branch (222:7): [True: 5, False: 0]
223
5
    Slice handle_value = iiter->value();
224
5
    FilterBlockReader* filter = rep_->filter;
225
5
    BlockHandle handle;
226
5
    if (filter != nullptr && handle.DecodeFrom(&handle_value).ok() &&
  Branch (226:9): [True: 5, False: 0]
  Branch (226:9): [True: 0, False: 5]
  Branch (226:30): [True: 5, False: 0]
227
5
        !filter->KeyMayMatch(handle.offset(), k)) {
  Branch (227:9): [True: 0, False: 5]
228
      // Not found
229
5
    } else {
230
5
      Iterator* block_iter = BlockReader(this, options, iiter->value());
231
5
      block_iter->Seek(k);
232
5
      if (block_iter->Valid()) {
  Branch (232:11): [True: 5, False: 0]
233
5
        (*handle_result)(arg, block_iter->key(), block_iter->value());
234
5
      }
235
5
      s = block_iter->status();
236
5
      delete block_iter;
237
5
    }
238
5
  }
239
5
  if (s.ok()) {
  Branch (239:7): [True: 5, False: 0]
240
5
    s = iiter->status();
241
5
  }
242
5
  delete iiter;
243
5
  return s;
244
5
}
245
246
0
uint64_t Table::ApproximateOffsetOf(const Slice& key) const {
247
0
  Iterator* index_iter =
248
0
      rep_->index_block->NewIterator(rep_->options.comparator);
249
0
  index_iter->Seek(key);
250
0
  uint64_t result;
251
0
  if (index_iter->Valid()) {
  Branch (251:7): [True: 0, False: 0]
252
0
    BlockHandle handle;
253
0
    Slice input = index_iter->value();
254
0
    Status s = handle.DecodeFrom(&input);
255
0
    if (s.ok()) {
  Branch (255:9): [True: 0, False: 0]
256
0
      result = handle.offset();
257
0
    } else {
258
      // Strange: we can't decode the block handle in the index block.
259
      // We'll just return the offset of the metaindex block, which is
260
      // close to the whole file size for this case.
261
0
      result = rep_->metaindex_handle.offset();
262
0
    }
263
0
  } else {
264
    // key is past the last key in the file.  Approximate the offset
265
    // by returning the offset of the metaindex block (which is
266
    // right near the end of the file).
267
0
    result = rep_->metaindex_handle.offset();
268
0
  }
269
0
  delete index_iter;
270
0
  return result;
271
0
}
272
273
}  // namespace leveldb