fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
721 lines
30 KiB
721 lines
30 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
7 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
11 years ago
|
//
|
||
14 years ago
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||
|
|
||
11 years ago
|
#pragma once
|
||
11 years ago
|
|
||
14 years ago
|
#include <stdint.h>
|
||
11 years ago
|
#include <memory>
|
||
8 years ago
|
#include <set>
|
||
11 years ago
|
#include <string>
|
||
8 years ago
|
#include <utility>
|
||
|
#include <vector>
|
||
11 years ago
|
|
||
6 years ago
|
#include "db/range_tombstone_fragmenter.h"
|
||
8 years ago
|
#include "options/cf_options.h"
|
||
10 years ago
|
#include "rocksdb/options.h"
|
||
9 years ago
|
#include "rocksdb/persistent_cache.h"
|
||
11 years ago
|
#include "rocksdb/statistics.h"
|
||
11 years ago
|
#include "rocksdb/status.h"
|
||
|
#include "rocksdb/table.h"
|
||
6 years ago
|
#include "table/block_based/block.h"
|
||
|
#include "table/block_based/block_based_table_factory.h"
|
||
|
#include "table/block_based/cachable_entry.h"
|
||
|
#include "table/block_based/filter_block.h"
|
||
8 years ago
|
#include "table/format.h"
|
||
6 years ago
|
#include "table/get_context.h"
|
||
|
#include "table/multiget_context.h"
|
||
8 years ago
|
#include "table/persistent_cache_helper.h"
|
||
10 years ago
|
#include "table/table_properties_internal.h"
|
||
9 years ago
|
#include "table/table_reader.h"
|
||
8 years ago
|
#include "table/two_level_iterator.h"
|
||
11 years ago
|
#include "util/coding.h"
|
||
9 years ago
|
#include "util/file_reader_writer.h"
|
||
6 years ago
|
#include "util/user_comparator_wrapper.h"
|
||
14 years ago
|
|
||
11 years ago
|
namespace rocksdb {
|
||
14 years ago
|
|
||
|
class BlockHandle;
|
||
11 years ago
|
class Cache;
|
||
|
class FilterBlockReader;
|
||
10 years ago
|
class BlockBasedFilterBlockReader;
|
||
|
class FullFilterBlockReader;
|
||
13 years ago
|
class Footer;
|
||
11 years ago
|
class InternalKeyComparator;
|
||
|
class Iterator;
|
||
14 years ago
|
class RandomAccessFile;
|
||
13 years ago
|
class TableCache;
|
||
11 years ago
|
class TableReader;
|
||
11 years ago
|
class WritableFile;
|
||
11 years ago
|
struct BlockBasedTableOptions;
|
||
11 years ago
|
struct EnvOptions;
|
||
|
struct ReadOptions;
|
||
10 years ago
|
class GetContext;
|
||
14 years ago
|
|
||
8 years ago
|
typedef std::vector<std::pair<std::string, std::string>> KVPairBlock;
|
||
|
|
||
6 years ago
|
// Reader class for BlockBasedTable format.
|
||
|
// For the format of BlockBasedTable refer to
|
||
|
// https://github.com/facebook/rocksdb/wiki/Rocksdb-BlockBasedTable-Format.
|
||
|
// This is the default table type. Data is chucked into fixed size blocks and
|
||
|
// each block in-turn stores entries. When storing data, we can compress and/or
|
||
|
// encode data efficiently within a block, which often results in a much smaller
|
||
|
// data size compared with the raw data size. As for the record retrieval, we'll
|
||
|
// first locate the block where target record may reside, then read the block to
|
||
|
// memory, and finally search that record within the block. Of course, to avoid
|
||
|
// frequent reads of the same block, we introduced the block cache to keep the
|
||
|
// loaded blocks in the memory.
|
||
11 years ago
|
class BlockBasedTable : public TableReader {
|
||
14 years ago
|
public:
|
||
11 years ago
|
static const std::string kFilterBlockPrefix;
|
||
10 years ago
|
static const std::string kFullFilterBlockPrefix;
|
||
8 years ago
|
static const std::string kPartitionedFilterBlockPrefix;
|
||
9 years ago
|
// The longest prefix of the cache key used to identify blocks.
|
||
|
// For Posix files the unique ID is three varints.
|
||
|
static const size_t kMaxCacheKeyPrefixSize = kMaxVarint64Length * 3 + 1;
|
||
11 years ago
|
|
||
14 years ago
|
// Attempt to open the table that is stored in bytes [0..file_size)
|
||
|
// of "file", and read the metadata entries necessary to allow
|
||
|
// retrieving data from the table.
|
||
14 years ago
|
//
|
||
11 years ago
|
// If successful, returns ok and sets "*table_reader" to the newly opened
|
||
|
// table. The client should delete "*table_reader" when no longer needed.
|
||
|
// If there was an error while initializing the table, sets "*table_reader"
|
||
|
// to nullptr and returns a non-ok status.
|
||
14 years ago
|
//
|
||
9 years ago
|
// @param file must remain live while this Table is in use.
|
||
8 years ago
|
// @param prefetch_index_and_filter_in_cache can be used to disable
|
||
|
// prefetching of
|
||
|
// index and filter blocks into block cache at startup
|
||
9 years ago
|
// @param skip_filters Disables loading/accessing the filter block. Overrides
|
||
8 years ago
|
// prefetch_index_and_filter_in_cache, so filter will be skipped if both
|
||
|
// are set.
|
||
10 years ago
|
static Status Open(const ImmutableCFOptions& ioptions,
|
||
|
const EnvOptions& env_options,
|
||
11 years ago
|
const BlockBasedTableOptions& table_options,
|
||
11 years ago
|
const InternalKeyComparator& internal_key_comparator,
|
||
6 years ago
|
std::unique_ptr<RandomAccessFileReader>&& file,
|
||
|
uint64_t file_size,
|
||
|
std::unique_ptr<TableReader>* table_reader,
|
||
7 years ago
|
const SliceTransform* prefix_extractor = nullptr,
|
||
8 years ago
|
bool prefetch_index_and_filter_in_cache = true,
|
||
6 years ago
|
bool skip_filters = false, int level = -1,
|
||
6 years ago
|
const bool immortal_table = false,
|
||
6 years ago
|
const SequenceNumber largest_seqno = 0,
|
||
6 years ago
|
TailPrefetchStats* tail_prefetch_stats = nullptr);
|
||
14 years ago
|
|
||
7 years ago
|
bool PrefixMayMatch(const Slice& internal_key,
|
||
6 years ago
|
const ReadOptions& read_options,
|
||
|
const SliceTransform* options_prefix_extractor,
|
||
|
const bool need_upper_bound_check);
|
||
11 years ago
|
|
||
14 years ago
|
// Returns a new iterator over the table contents.
|
||
|
// The result of NewIterator() is initially invalid (caller must
|
||
|
// call one of the Seek methods on the iterator before using it).
|
||
9 years ago
|
// @param skip_filters Disables loading/accessing the filter block
|
||
7 years ago
|
InternalIterator* NewIterator(const ReadOptions&,
|
||
|
const SliceTransform* prefix_extractor,
|
||
|
Arena* arena = nullptr,
|
||
6 years ago
|
bool skip_filters = false,
|
||
|
bool for_compaction = false) override;
|
||
11 years ago
|
|
||
6 years ago
|
FragmentedRangeTombstoneIterator* NewRangeTombstoneIterator(
|
||
8 years ago
|
const ReadOptions& read_options) override;
|
||
|
|
||
9 years ago
|
// @param skip_filters Disables loading/accessing the filter block
|
||
11 years ago
|
Status Get(const ReadOptions& readOptions, const Slice& key,
|
||
7 years ago
|
GetContext* get_context, const SliceTransform* prefix_extractor,
|
||
|
bool skip_filters = false) override;
|
||
14 years ago
|
|
||
6 years ago
|
void MultiGet(const ReadOptions& readOptions,
|
||
|
const MultiGetContext::Range* mget_range,
|
||
|
const SliceTransform* prefix_extractor,
|
||
|
bool skip_filters = false) override;
|
||
|
|
||
10 years ago
|
// Pre-fetch the disk blocks that correspond to the key range specified by
|
||
9 years ago
|
// (kbegin, kend). The call will return error status in the event of
|
||
10 years ago
|
// IO or iteration error.
|
||
|
Status Prefetch(const Slice* begin, const Slice* end) override;
|
||
|
|
||
14 years ago
|
// Given a key, return an approximate byte offset in the file where
|
||
|
// the data for that key begins (or would begin if the key were
|
||
|
// present in the file). The returned value is in terms of file
|
||
|
// bytes, and so includes effects like compression of the underlying data.
|
||
|
// E.g., the approximate offset of the last key in the table will
|
||
|
// be close to the file length.
|
||
11 years ago
|
uint64_t ApproximateOffsetOf(const Slice& key) override;
|
||
14 years ago
|
|
||
6 years ago
|
bool TEST_BlockInCache(const BlockHandle& handle) const;
|
||
|
|
||
12 years ago
|
// Returns true if the block for the specified key is in cache.
|
||
11 years ago
|
// REQUIRES: key is in this table && block cache enabled
|
||
11 years ago
|
bool TEST_KeyInCache(const ReadOptions& options, const Slice& key);
|
||
12 years ago
|
|
||
12 years ago
|
// Set up the table for Compaction. Might change some parameters with
|
||
|
// posix_fadvise
|
||
11 years ago
|
void SetupForCompaction() override;
|
||
|
|
||
11 years ago
|
std::shared_ptr<const TableProperties> GetTableProperties() const override;
|
||
12 years ago
|
|
||
10 years ago
|
size_t ApproximateMemoryUsage() const override;
|
||
|
|
||
10 years ago
|
// convert SST file to a human readable form
|
||
7 years ago
|
Status DumpTable(WritableFile* out_file,
|
||
|
const SliceTransform* prefix_extractor = nullptr) override;
|
||
10 years ago
|
|
||
7 years ago
|
Status VerifyChecksum() override;
|
||
|
|
||
9 years ago
|
void Close() override;
|
||
|
|
||
11 years ago
|
~BlockBasedTable();
|
||
11 years ago
|
|
||
11 years ago
|
bool TEST_filter_block_preloaded() const;
|
||
6 years ago
|
bool TEST_IndexBlockInCache() const;
|
||
8 years ago
|
|
||
6 years ago
|
// IndexReader is the interface that provides the functionality for index
|
||
8 years ago
|
// access.
|
||
|
class IndexReader {
|
||
|
public:
|
||
6 years ago
|
virtual ~IndexReader() = default;
|
||
|
|
||
|
// Create an iterator for index access. If iter is null, then a new object
|
||
|
// is created on the heap, and the callee will have the ownership.
|
||
|
// If a non-null iter is passed in, it will be used, and the returned value
|
||
|
// is either the same as iter or a new on-heap object that
|
||
|
// wraps the passed iter. In the latter case the return value points
|
||
|
// to a different object then iter, and the callee has the ownership of the
|
||
8 years ago
|
// returned object.
|
||
6 years ago
|
virtual InternalIteratorBase<BlockHandle>* NewIterator(
|
||
6 years ago
|
const ReadOptions& read_options, bool disable_prefix_seek,
|
||
|
IndexBlockIter* iter, GetContext* get_context) = 0;
|
||
|
|
||
8 years ago
|
// Report an approximation of how much memory has been used other than
|
||
6 years ago
|
// memory that was allocated in block cache.
|
||
8 years ago
|
virtual size_t ApproximateMemoryUsage() const = 0;
|
||
6 years ago
|
// Cache the dependencies of the index reader (e.g. the partitions
|
||
|
// of a partitioned index).
|
||
|
virtual void CacheDependencies(bool /* pin */) {}
|
||
8 years ago
|
};
|
||
11 years ago
|
|
||
6 years ago
|
class IndexReaderCommon;
|
||
|
|
||
9 years ago
|
static Slice GetCacheKey(const char* cache_key_prefix,
|
||
|
size_t cache_key_prefix_size,
|
||
|
const BlockHandle& handle, char* cache_key);
|
||
|
|
||
8 years ago
|
// Retrieve all key value pairs from data blocks in the table.
|
||
|
// The key retrieved are internal keys.
|
||
|
Status GetKVPairsFromDataBlocks(std::vector<KVPairBlock>* kv_pair_blocks);
|
||
|
|
||
7 years ago
|
struct Rep;
|
||
|
|
||
|
Rep* get_rep() { return rep_; }
|
||
|
|
||
|
// input_iter: if it is not null, update this one and return it as Iterator
|
||
6 years ago
|
template <typename TBlockIter>
|
||
|
static TBlockIter* NewDataBlockIterator(
|
||
6 years ago
|
Rep* rep, const ReadOptions& ro, const Slice& index_value,
|
||
6 years ago
|
TBlockIter* input_iter = nullptr, bool is_index = false,
|
||
6 years ago
|
bool key_includes_seq = true, bool index_key_is_full = true,
|
||
|
GetContext* get_context = nullptr,
|
||
6 years ago
|
FilePrefetchBuffer* prefetch_buffer = nullptr);
|
||
6 years ago
|
template <typename TBlockIter>
|
||
|
static TBlockIter* NewDataBlockIterator(
|
||
6 years ago
|
Rep* rep, const ReadOptions& ro, const BlockHandle& block_hanlde,
|
||
6 years ago
|
TBlockIter* input_iter = nullptr, bool is_index = false,
|
||
6 years ago
|
bool key_includes_seq = true, bool index_key_is_full = true,
|
||
|
GetContext* get_context = nullptr, Status s = Status(),
|
||
|
FilePrefetchBuffer* prefetch_buffer = nullptr);
|
||
7 years ago
|
|
||
|
class PartitionedIndexIteratorState;
|
||
8 years ago
|
|
||
8 years ago
|
friend class PartitionIndexReader;
|
||
|
|
||
|
protected:
|
||
14 years ago
|
Rep* rep_;
|
||
8 years ago
|
explicit BlockBasedTable(Rep* rep) : rep_(rep) {}
|
||
8 years ago
|
|
||
|
private:
|
||
7 years ago
|
friend class MockedBlockBasedTable;
|
||
7 years ago
|
static std::atomic<uint64_t> next_cache_key_id_;
|
||
7 years ago
|
|
||
8 years ago
|
// If block cache enabled (compressed or uncompressed), looks for the block
|
||
|
// identified by handle in (1) uncompressed cache, (2) compressed cache, and
|
||
|
// then (3) file. If found, inserts into the cache(s) that were searched
|
||
|
// unsuccessfully (e.g., if found in file, will add to both uncompressed and
|
||
|
// compressed caches if they're enabled).
|
||
|
//
|
||
|
// @param block_entry value is set to the uncompressed block if found. If
|
||
|
// in uncompressed block cache, also sets cache_handle to reference that
|
||
|
// block.
|
||
6 years ago
|
static Status MaybeReadBlockAndLoadToCache(
|
||
6 years ago
|
FilePrefetchBuffer* prefetch_buffer, const Rep* rep,
|
||
|
const ReadOptions& ro, const BlockHandle& handle,
|
||
|
const UncompressionDict& uncompression_dict,
|
||
6 years ago
|
CachableEntry<Block>* block_entry, bool is_index = false,
|
||
|
GetContext* get_context = nullptr);
|
||
14 years ago
|
|
||
6 years ago
|
// Similar to the above, with one crucial difference: it will retrieve the
|
||
|
// block from the file even if there are no caches configured (assuming the
|
||
|
// read options allow I/O).
|
||
|
static Status RetrieveBlock(
|
||
|
FilePrefetchBuffer* prefetch_buffer, const Rep* rep,
|
||
|
const ReadOptions& ro, const BlockHandle& handle,
|
||
|
const UncompressionDict& uncompression_dict,
|
||
|
CachableEntry<Block>* block_entry, bool is_index,
|
||
|
GetContext* get_context);
|
||
|
|
||
11 years ago
|
// For the following two functions:
|
||
|
// if `no_io == true`, we will not try to read filter/index from sst file
|
||
|
// were they not present in cache yet.
|
||
7 years ago
|
CachableEntry<FilterBlockReader> GetFilter(
|
||
7 years ago
|
const SliceTransform* prefix_extractor = nullptr,
|
||
7 years ago
|
FilePrefetchBuffer* prefetch_buffer = nullptr, bool no_io = false,
|
||
|
GetContext* get_context = nullptr) const;
|
||
8 years ago
|
virtual CachableEntry<FilterBlockReader> GetFilter(
|
||
7 years ago
|
FilePrefetchBuffer* prefetch_buffer, const BlockHandle& filter_blk_handle,
|
||
7 years ago
|
const bool is_a_filter_partition, bool no_io, GetContext* get_context,
|
||
|
const SliceTransform* prefix_extractor = nullptr) const;
|
||
11 years ago
|
|
||
6 years ago
|
static CachableEntry<UncompressionDict> GetUncompressionDict(
|
||
|
Rep* rep, FilePrefetchBuffer* prefetch_buffer, bool no_io,
|
||
|
GetContext* get_context);
|
||
|
|
||
11 years ago
|
// Get the iterator from the index reader.
|
||
10 years ago
|
// If input_iter is not set, return new Iterator
|
||
|
// If input_iter is set, update it and return it as Iterator
|
||
11 years ago
|
//
|
||
11 years ago
|
// Note: ErrorIterator with Status::Incomplete shall be returned if all the
|
||
|
// following conditions are met:
|
||
|
// 1. We enabled table_options.cache_index_and_filter_blocks.
|
||
|
// 2. index is not present in block cache.
|
||
|
// 3. We disallowed any io to be performed, that is, read_options ==
|
||
|
// kBlockCacheTier
|
||
6 years ago
|
InternalIteratorBase<BlockHandle>* NewIndexIterator(
|
||
6 years ago
|
const ReadOptions& read_options, bool need_upper_bound_check = false,
|
||
6 years ago
|
IndexBlockIter* input_iter = nullptr,
|
||
7 years ago
|
GetContext* get_context = nullptr);
|
||
11 years ago
|
|
||
|
// Read block cache from block caches (if set): block_cache and
|
||
|
// block_cache_compressed.
|
||
|
// On success, Status::OK with be returned and @block will be populated with
|
||
|
// pointer to the block as well as its block handle.
|
||
6 years ago
|
// @param uncompression_dict Data for presetting the compression library's
|
||
9 years ago
|
// dictionary.
|
||
11 years ago
|
static Status GetDataBlockFromCache(
|
||
|
const Slice& block_cache_key, const Slice& compressed_block_cache_key,
|
||
6 years ago
|
Cache* block_cache, Cache* block_cache_compressed, const Rep* rep,
|
||
6 years ago
|
const ReadOptions& read_options,
|
||
6 years ago
|
CachableEntry<Block>* block, const UncompressionDict& uncompression_dict,
|
||
6 years ago
|
size_t read_amp_bytes_per_bit, bool is_index = false,
|
||
|
GetContext* get_context = nullptr);
|
||
9 years ago
|
|
||
11 years ago
|
// Put a raw block (maybe compressed) to the corresponding block caches.
|
||
|
// This method will perform decompression against raw_block if needed and then
|
||
|
// populate the block caches.
|
||
|
// On success, Status::OK will be returned; also @block will be populated with
|
||
|
// uncompressed block and its cache handle.
|
||
11 years ago
|
//
|
||
6 years ago
|
// Allocated memory managed by raw_block_contents will be transferred to
|
||
|
// PutDataBlockToCache(). After the call, the object will be invalid.
|
||
6 years ago
|
// @param uncompression_dict Data for presetting the compression library's
|
||
9 years ago
|
// dictionary.
|
||
11 years ago
|
static Status PutDataBlockToCache(
|
||
|
const Slice& block_cache_key, const Slice& compressed_block_cache_key,
|
||
|
Cache* block_cache, Cache* block_cache_compressed,
|
||
8 years ago
|
const ReadOptions& read_options, const ImmutableCFOptions& ioptions,
|
||
6 years ago
|
CachableEntry<Block>* block, BlockContents* raw_block_contents,
|
||
|
CompressionType raw_block_comp_type, uint32_t format_version,
|
||
6 years ago
|
const UncompressionDict& uncompression_dict, SequenceNumber seq_no,
|
||
6 years ago
|
size_t read_amp_bytes_per_bit, MemoryAllocator* memory_allocator,
|
||
|
bool is_index = false, Cache::Priority pri = Cache::Priority::LOW,
|
||
|
GetContext* get_context = nullptr);
|
||
11 years ago
|
|
||
12 years ago
|
// Calls (*handle_result)(arg, ...) repeatedly, starting with the entry found
|
||
|
// after a call to Seek(key), until handle_result returns false.
|
||
|
// May not make such a call if filter policy says that key is not present.
|
||
13 years ago
|
friend class TableCache;
|
||
11 years ago
|
friend class BlockBasedTableBuilder;
|
||
13 years ago
|
|
||
|
void ReadMeta(const Footer& footer);
|
||
11 years ago
|
|
||
6 years ago
|
// Figure the index type, update it in rep_, and also return it.
|
||
|
BlockBasedTableOptions::IndexType UpdateIndexType();
|
||
|
|
||
11 years ago
|
// Create a index reader based on the index type stored in the table.
|
||
|
// Optionally, user can pass a preloaded meta_index_iter for the index that
|
||
|
// need to access extra meta blocks for index construction. This parameter
|
||
|
// helps avoid re-reading meta index block if caller already created one.
|
||
9 years ago
|
Status CreateIndexReader(
|
||
6 years ago
|
FilePrefetchBuffer* prefetch_buffer,
|
||
|
InternalIterator* preloaded_meta_index_iter, bool use_cache,
|
||
|
bool prefetch, bool pin, IndexReader** index_reader);
|
||
13 years ago
|
|
||
7 years ago
|
bool FullFilterKeyMayMatch(
|
||
|
const ReadOptions& read_options, FilterBlockReader* filter,
|
||
|
const Slice& user_key, const bool no_io,
|
||
|
const SliceTransform* prefix_extractor = nullptr) const;
|
||
10 years ago
|
|
||
6 years ago
|
void FullFilterKeysMayMatch(
|
||
|
const ReadOptions& read_options, FilterBlockReader* filter,
|
||
|
MultiGetRange* range, const bool no_io,
|
||
|
const SliceTransform* prefix_extractor = nullptr) const;
|
||
|
|
||
6 years ago
|
static Status PrefetchTail(
|
||
|
RandomAccessFileReader* file, uint64_t file_size,
|
||
|
TailPrefetchStats* tail_prefetch_stats, const bool prefetch_all,
|
||
|
const bool preload_all,
|
||
|
std::unique_ptr<FilePrefetchBuffer>* prefetch_buffer);
|
||
7 years ago
|
static Status ReadMetaBlock(Rep* rep, FilePrefetchBuffer* prefetch_buffer,
|
||
|
std::unique_ptr<Block>* meta_block,
|
||
9 years ago
|
std::unique_ptr<InternalIterator>* iter);
|
||
6 years ago
|
static Status TryReadPropertiesWithGlobalSeqno(
|
||
|
Rep* rep, FilePrefetchBuffer* prefetch_buffer, const Slice& handle_value,
|
||
|
TableProperties** table_properties);
|
||
6 years ago
|
static Status ReadPropertiesBlock(Rep* rep,
|
||
|
FilePrefetchBuffer* prefetch_buffer,
|
||
|
InternalIterator* meta_iter,
|
||
|
const SequenceNumber largest_seqno);
|
||
|
static Status ReadRangeDelBlock(
|
||
|
Rep* rep, FilePrefetchBuffer* prefetch_buffer,
|
||
6 years ago
|
InternalIterator* meta_iter,
|
||
6 years ago
|
const InternalKeyComparator& internal_comparator);
|
||
6 years ago
|
static Status ReadCompressionDictBlock(
|
||
|
Rep* rep, FilePrefetchBuffer* prefetch_buffer,
|
||
|
std::unique_ptr<const BlockContents>* compression_dict_block);
|
||
6 years ago
|
static Status PrefetchIndexAndFilterBlocks(
|
||
|
Rep* rep, FilePrefetchBuffer* prefetch_buffer,
|
||
|
InternalIterator* meta_iter, BlockBasedTable* new_table,
|
||
6 years ago
|
bool prefetch_all, const BlockBasedTableOptions& table_options,
|
||
|
const int level);
|
||
11 years ago
|
|
||
6 years ago
|
Status VerifyChecksumInMetaBlocks(InternalIteratorBase<Slice>* index_iter);
|
||
6 years ago
|
Status VerifyChecksumInBlocks(InternalIteratorBase<BlockHandle>* index_iter);
|
||
7 years ago
|
|
||
11 years ago
|
// Create the filter from the filter block.
|
||
6 years ago
|
virtual FilterBlockReader* ReadFilter(
|
||
7 years ago
|
FilePrefetchBuffer* prefetch_buffer, const BlockHandle& filter_handle,
|
||
|
const bool is_a_filter_partition,
|
||
|
const SliceTransform* prefix_extractor = nullptr) const;
|
||
11 years ago
|
|
||
6 years ago
|
static void SetupCacheKeyPrefix(Rep* rep);
|
||
12 years ago
|
|
||
11 years ago
|
// Generate a cache key prefix from the file
|
||
6 years ago
|
static void GenerateCachePrefix(Cache* cc, RandomAccessFile* file,
|
||
|
char* buffer, size_t* size);
|
||
|
static void GenerateCachePrefix(Cache* cc, WritableFile* file, char* buffer,
|
||
|
size_t* size);
|
||
11 years ago
|
|
||
10 years ago
|
// Helper functions for DumpTable()
|
||
|
Status DumpIndexBlock(WritableFile* out_file);
|
||
|
Status DumpDataBlocks(WritableFile* out_file);
|
||
8 years ago
|
void DumpKeyValue(const Slice& key, const Slice& value,
|
||
|
WritableFile* out_file);
|
||
10 years ago
|
|
||
11 years ago
|
// No copying allowed
|
||
11 years ago
|
explicit BlockBasedTable(const TableReader&) = delete;
|
||
|
void operator=(const TableReader&) = delete;
|
||
8 years ago
|
|
||
|
friend class PartitionedFilterBlockReader;
|
||
|
friend class PartitionedFilterBlockTest;
|
||
11 years ago
|
};
|
||
|
|
||
6 years ago
|
// Maitaning state of a two-level iteration on a partitioned index structure.
|
||
7 years ago
|
class BlockBasedTable::PartitionedIndexIteratorState
|
||
|
: public TwoLevelIteratorState {
|
||
8 years ago
|
public:
|
||
7 years ago
|
PartitionedIndexIteratorState(
|
||
|
BlockBasedTable* table,
|
||
7 years ago
|
std::unordered_map<uint64_t, CachableEntry<Block>>* block_map,
|
||
6 years ago
|
const bool index_key_includes_seq, const bool index_key_is_full);
|
||
|
InternalIteratorBase<BlockHandle>* NewSecondaryIterator(
|
||
|
const BlockHandle& index_value) override;
|
||
8 years ago
|
|
||
|
private:
|
||
|
// Don't own table_
|
||
|
BlockBasedTable* table_;
|
||
7 years ago
|
std::unordered_map<uint64_t, CachableEntry<Block>>* block_map_;
|
||
7 years ago
|
bool index_key_includes_seq_;
|
||
6 years ago
|
bool index_key_is_full_;
|
||
8 years ago
|
};
|
||
|
|
||
6 years ago
|
// Stores all the properties associated with a BlockBasedTable.
|
||
|
// These are immutable.
|
||
8 years ago
|
struct BlockBasedTable::Rep {
|
||
|
Rep(const ImmutableCFOptions& _ioptions, const EnvOptions& _env_options,
|
||
|
const BlockBasedTableOptions& _table_opt,
|
||
6 years ago
|
const InternalKeyComparator& _internal_comparator, bool skip_filters,
|
||
6 years ago
|
int _level, const bool _immortal_table)
|
||
8 years ago
|
: ioptions(_ioptions),
|
||
|
env_options(_env_options),
|
||
|
table_options(_table_opt),
|
||
|
filter_policy(skip_filters ? nullptr : _table_opt.filter_policy.get()),
|
||
|
internal_comparator(_internal_comparator),
|
||
|
filter_type(FilterType::kNoFilter),
|
||
7 years ago
|
index_type(BlockBasedTableOptions::IndexType::kBinarySearch),
|
||
|
hash_index_allow_collision(false),
|
||
8 years ago
|
whole_key_filtering(_table_opt.whole_key_filtering),
|
||
|
prefix_filtering(true),
|
||
6 years ago
|
global_seqno(kDisableGlobalSequenceNumber),
|
||
6 years ago
|
level(_level),
|
||
6 years ago
|
immortal_table(_immortal_table) {}
|
||
8 years ago
|
|
||
|
const ImmutableCFOptions& ioptions;
|
||
|
const EnvOptions& env_options;
|
||
7 years ago
|
const BlockBasedTableOptions table_options;
|
||
8 years ago
|
const FilterPolicy* const filter_policy;
|
||
|
const InternalKeyComparator& internal_comparator;
|
||
|
Status status;
|
||
6 years ago
|
std::unique_ptr<RandomAccessFileReader> file;
|
||
8 years ago
|
char cache_key_prefix[kMaxCacheKeyPrefixSize];
|
||
|
size_t cache_key_prefix_size = 0;
|
||
|
char persistent_cache_key_prefix[kMaxCacheKeyPrefixSize];
|
||
|
size_t persistent_cache_key_prefix_size = 0;
|
||
|
char compressed_cache_key_prefix[kMaxCacheKeyPrefixSize];
|
||
|
size_t compressed_cache_key_prefix_size = 0;
|
||
|
PersistentCacheOptions persistent_cache_options;
|
||
|
|
||
|
// Footer contains the fixed table information
|
||
|
Footer footer;
|
||
6 years ago
|
// `filter` and `uncompression_dict` will be populated (i.e., non-nullptr)
|
||
|
// and used only when options.block_cache is nullptr or when
|
||
|
// `cache_index_and_filter_blocks == false`. Otherwise, we will get the
|
||
|
// filter and compression dictionary blocks via the block cache. In that case,
|
||
|
// `filter_handle`, and `compression_dict_handle` are used to lookup these
|
||
|
// meta-blocks in block cache.
|
||
|
//
|
||
|
// Note: the IndexReader object is always stored in this member variable;
|
||
|
// the index block itself, however, may or may not be in the block cache
|
||
|
// based on the settings above. We plan to change the handling of the
|
||
|
// filter and compression dictionary similarly.
|
||
6 years ago
|
std::unique_ptr<IndexReader> index_reader;
|
||
|
std::unique_ptr<FilterBlockReader> filter;
|
||
6 years ago
|
std::unique_ptr<UncompressionDict> uncompression_dict;
|
||
8 years ago
|
|
||
|
enum class FilterType {
|
||
|
kNoFilter,
|
||
|
kFullFilter,
|
||
|
kBlockFilter,
|
||
|
kPartitionedFilter,
|
||
|
};
|
||
|
FilterType filter_type;
|
||
|
BlockHandle filter_handle;
|
||
6 years ago
|
BlockHandle compression_dict_handle;
|
||
8 years ago
|
|
||
|
std::shared_ptr<const TableProperties> table_properties;
|
||
|
BlockBasedTableOptions::IndexType index_type;
|
||
|
bool hash_index_allow_collision;
|
||
|
bool whole_key_filtering;
|
||
|
bool prefix_filtering;
|
||
|
// TODO(kailiu) It is very ugly to use internal key in table, since table
|
||
|
// module should not be relying on db module. However to make things easier
|
||
|
// and compatible with existing code, we introduce a wrapper that allows
|
||
|
// block to extract prefix without knowing if a key is internal or not.
|
||
6 years ago
|
std::unique_ptr<SliceTransform> internal_prefix_transform;
|
||
6 years ago
|
std::shared_ptr<const SliceTransform> table_prefix_extractor;
|
||
8 years ago
|
|
||
6 years ago
|
// only used in level 0 files when pin_l0_filter_and_index_blocks_in_cache is
|
||
|
// true or in all levels when pin_top_level_index_and_filter is set in
|
||
6 years ago
|
// combination with partitioned filters: then we do use the LRU cache,
|
||
|
// but we always keep the filter block's handle checked out here (=we
|
||
6 years ago
|
// don't call Release()), plus the parsed out objects the LRU cache will never
|
||
|
// push flush them out, hence they're pinned
|
||
8 years ago
|
CachableEntry<FilterBlockReader> filter_entry;
|
||
6 years ago
|
std::shared_ptr<const FragmentedRangeTombstoneList> fragmented_range_dels;
|
||
8 years ago
|
|
||
|
// If global_seqno is used, all Keys in this file will have the same
|
||
|
// seqno with value `global_seqno`.
|
||
|
//
|
||
|
// A value of kDisableGlobalSequenceNumber means that this feature is disabled
|
||
|
// and every key have it's own seqno.
|
||
|
SequenceNumber global_seqno;
|
||
7 years ago
|
|
||
6 years ago
|
// the level when the table is opened, could potentially change when trivial
|
||
|
// move is involved
|
||
|
int level;
|
||
|
|
||
7 years ago
|
// If false, blocks in this file are definitely all uncompressed. Knowing this
|
||
|
// before reading individual blocks enables certain optimizations.
|
||
|
bool blocks_maybe_compressed = true;
|
||
|
|
||
6 years ago
|
// If true, data blocks in this file are definitely ZSTD compressed. If false
|
||
|
// they might not be. When false we skip creating a ZSTD digested
|
||
|
// uncompression dictionary. Even if we get a false negative, things should
|
||
|
// still work, just not as quickly.
|
||
|
bool blocks_definitely_zstd_compressed = false;
|
||
|
|
||
7 years ago
|
bool closed = false;
|
||
6 years ago
|
const bool immortal_table;
|
||
6 years ago
|
|
||
|
SequenceNumber get_global_seqno(bool is_index) const {
|
||
|
return is_index ? kDisableGlobalSequenceNumber : global_seqno;
|
||
|
}
|
||
8 years ago
|
};
|
||
|
|
||
6 years ago
|
// Iterates over the contents of BlockBasedTable.
|
||
6 years ago
|
template <class TBlockIter, typename TValue = Slice>
|
||
|
class BlockBasedTableIterator : public InternalIteratorBase<TValue> {
|
||
7 years ago
|
public:
|
||
|
BlockBasedTableIterator(BlockBasedTable* table,
|
||
|
const ReadOptions& read_options,
|
||
|
const InternalKeyComparator& icomp,
|
||
6 years ago
|
InternalIteratorBase<BlockHandle>* index_iter,
|
||
|
bool check_filter, bool need_upper_bound_check,
|
||
7 years ago
|
const SliceTransform* prefix_extractor, bool is_index,
|
||
6 years ago
|
bool key_includes_seq = true,
|
||
6 years ago
|
bool index_key_is_full = true,
|
||
6 years ago
|
bool for_compaction = false)
|
||
6 years ago
|
: InternalIteratorBase<TValue>(false),
|
||
|
table_(table),
|
||
7 years ago
|
read_options_(read_options),
|
||
|
icomp_(icomp),
|
||
6 years ago
|
user_comparator_(icomp.user_comparator()),
|
||
7 years ago
|
index_iter_(index_iter),
|
||
|
pinned_iters_mgr_(nullptr),
|
||
|
block_iter_points_to_real_block_(false),
|
||
7 years ago
|
check_filter_(check_filter),
|
||
6 years ago
|
need_upper_bound_check_(need_upper_bound_check),
|
||
|
prefix_extractor_(prefix_extractor),
|
||
7 years ago
|
is_index_(is_index),
|
||
|
key_includes_seq_(key_includes_seq),
|
||
6 years ago
|
index_key_is_full_(index_key_is_full),
|
||
6 years ago
|
for_compaction_(for_compaction) {}
|
||
7 years ago
|
|
||
|
~BlockBasedTableIterator() { delete index_iter_; }
|
||
|
|
||
|
void Seek(const Slice& target) override;
|
||
|
void SeekForPrev(const Slice& target) override;
|
||
|
void SeekToFirst() override;
|
||
|
void SeekToLast() override;
|
||
6 years ago
|
void Next() final override;
|
||
6 years ago
|
bool NextAndGetResult(IterateResult* result) override;
|
||
7 years ago
|
void Prev() override;
|
||
|
bool Valid() const override {
|
||
6 years ago
|
return !is_out_of_bound_ && block_iter_points_to_real_block_ &&
|
||
6 years ago
|
block_iter_.Valid();
|
||
7 years ago
|
}
|
||
|
Slice key() const override {
|
||
|
assert(Valid());
|
||
6 years ago
|
return block_iter_.key();
|
||
7 years ago
|
}
|
||
6 years ago
|
Slice user_key() const override {
|
||
|
assert(Valid());
|
||
|
return block_iter_.user_key();
|
||
|
}
|
||
6 years ago
|
TValue value() const override {
|
||
7 years ago
|
assert(Valid());
|
||
6 years ago
|
return block_iter_.value();
|
||
7 years ago
|
}
|
||
|
Status status() const override {
|
||
|
if (!index_iter_->status().ok()) {
|
||
|
return index_iter_->status();
|
||
7 years ago
|
} else if (block_iter_points_to_real_block_) {
|
||
6 years ago
|
return block_iter_.status();
|
||
7 years ago
|
} else {
|
||
|
return Status::OK();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// Whether iterator invalidated for being out of bound.
|
||
7 years ago
|
bool IsOutOfBound() override { return is_out_of_bound_; }
|
||
|
|
||
6 years ago
|
inline bool MayBeOutOfUpperBound() override {
|
||
|
assert(Valid());
|
||
|
return !data_block_within_upper_bound_;
|
||
|
}
|
||
|
|
||
7 years ago
|
void SetPinnedItersMgr(PinnedIteratorsManager* pinned_iters_mgr) override {
|
||
|
pinned_iters_mgr_ = pinned_iters_mgr;
|
||
|
}
|
||
|
bool IsKeyPinned() const override {
|
||
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
||
6 years ago
|
block_iter_points_to_real_block_ && block_iter_.IsKeyPinned();
|
||
7 years ago
|
}
|
||
|
bool IsValuePinned() const override {
|
||
7 years ago
|
// BlockIter::IsValuePinned() is always true. No need to check
|
||
7 years ago
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
||
|
block_iter_points_to_real_block_;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool CheckPrefixMayMatch(const Slice& ikey) {
|
||
|
if (check_filter_ &&
|
||
|
!table_->PrefixMayMatch(ikey, read_options_, prefix_extractor_,
|
||
|
need_upper_bound_check_)) {
|
||
7 years ago
|
// TODO remember the iterator is invalidated because of prefix
|
||
|
// match. This can avoid the upper level file iterator to falsely
|
||
|
// believe the position is the end of the SST file and move to
|
||
|
// the first key of the next file.
|
||
|
ResetDataIter();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void ResetDataIter() {
|
||
|
if (block_iter_points_to_real_block_) {
|
||
7 years ago
|
if (pinned_iters_mgr_ != nullptr && pinned_iters_mgr_->PinningEnabled()) {
|
||
6 years ago
|
block_iter_.DelegateCleanupsTo(pinned_iters_mgr_);
|
||
7 years ago
|
}
|
||
6 years ago
|
block_iter_.Invalidate(Status::OK());
|
||
7 years ago
|
block_iter_points_to_real_block_ = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SavePrevIndexValue() {
|
||
|
if (block_iter_points_to_real_block_) {
|
||
|
// Reseek. If they end up with the same data block, we shouldn't re-fetch
|
||
|
// the same data block.
|
||
6 years ago
|
prev_index_value_ = index_iter_->value();
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void InitDataBlock();
|
||
6 years ago
|
inline void FindKeyForward();
|
||
|
void FindBlockForward();
|
||
7 years ago
|
void FindKeyBackward();
|
||
6 years ago
|
void CheckOutOfBound();
|
||
7 years ago
|
|
||
|
private:
|
||
|
BlockBasedTable* table_;
|
||
|
const ReadOptions read_options_;
|
||
|
const InternalKeyComparator& icomp_;
|
||
6 years ago
|
UserComparatorWrapper user_comparator_;
|
||
6 years ago
|
InternalIteratorBase<BlockHandle>* index_iter_;
|
||
7 years ago
|
PinnedIteratorsManager* pinned_iters_mgr_;
|
||
6 years ago
|
TBlockIter block_iter_;
|
||
7 years ago
|
bool block_iter_points_to_real_block_;
|
||
|
bool is_out_of_bound_ = false;
|
||
6 years ago
|
// Whether current data block being fully within iterate upper bound.
|
||
|
bool data_block_within_upper_bound_ = false;
|
||
7 years ago
|
bool check_filter_;
|
||
6 years ago
|
// TODO(Zhongyi): pick a better name
|
||
|
bool need_upper_bound_check_;
|
||
|
const SliceTransform* prefix_extractor_;
|
||
7 years ago
|
// If the blocks over which we iterate are index blocks
|
||
|
bool is_index_;
|
||
|
// If the keys in the blocks over which we iterate include 8 byte sequence
|
||
|
bool key_includes_seq_;
|
||
6 years ago
|
bool index_key_is_full_;
|
||
6 years ago
|
// If this iterator is created for compaction
|
||
|
bool for_compaction_;
|
||
6 years ago
|
BlockHandle prev_index_value_;
|
||
7 years ago
|
|
||
6 years ago
|
// All the below fields control iterator readahead
|
||
|
static const size_t kInitAutoReadaheadSize = 8 * 1024;
|
||
7 years ago
|
// Found that 256 KB readahead size provides the best performance, based on
|
||
6 years ago
|
// experiments, for auto readahead. Experiment data is in PR #3282.
|
||
|
static const size_t kMaxAutoReadaheadSize;
|
||
|
static const int kMinNumFileReadsToStartAutoReadahead = 2;
|
||
|
size_t readahead_size_ = kInitAutoReadaheadSize;
|
||
7 years ago
|
size_t readahead_limit_ = 0;
|
||
6 years ago
|
int64_t num_file_reads_ = 0;
|
||
6 years ago
|
std::unique_ptr<FilePrefetchBuffer> prefetch_buffer_;
|
||
7 years ago
|
};
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|