Coverage Report

Created: 2025-07-11 07:01

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