2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// 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.
|
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2013-01-20 11:07:13 +01:00
|
|
|
#include <memory>
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <stdint.h>
|
2013-09-02 08:23:40 +02:00
|
|
|
#include "rocksdb/cache.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/env.h"
|
2013-10-10 20:43:24 +02:00
|
|
|
#include "rocksdb/iterator.h"
|
2013-11-13 07:46:51 +01:00
|
|
|
#include "rocksdb/statistics.h"
|
2013-10-10 20:43:24 +02:00
|
|
|
#include "rocksdb/table_stats.h"
|
2013-10-29 01:54:09 +01:00
|
|
|
#include "rocksdb/table.h"
|
2013-09-02 08:23:40 +02:00
|
|
|
#include "util/coding.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
class Block;
|
|
|
|
class BlockHandle;
|
2012-04-17 17:36:46 +02:00
|
|
|
class Footer;
|
2011-03-18 23:37:00 +01:00
|
|
|
struct Options;
|
|
|
|
class RandomAccessFile;
|
|
|
|
struct ReadOptions;
|
2012-04-17 17:36:46 +02:00
|
|
|
class TableCache;
|
2013-10-30 18:52:33 +01:00
|
|
|
class TableReader;
|
2013-11-13 07:46:51 +01:00
|
|
|
class FilterBlockReader;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-01-20 11:07:13 +01:00
|
|
|
using std::unique_ptr;
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// A Table is a sorted map from strings to strings. Tables are
|
2011-05-21 04:17:43 +02:00
|
|
|
// immutable and persistent. A Table may be safely accessed from
|
|
|
|
// multiple threads without external synchronization.
|
2013-10-30 18:52:33 +01:00
|
|
|
class BlockBasedTable : public TableReader {
|
2011-03-18 23:37:00 +01:00
|
|
|
public:
|
2013-10-10 20:43:24 +02:00
|
|
|
static const std::string kFilterBlockPrefix;
|
|
|
|
static const std::string kStatsBlock;
|
|
|
|
|
2011-03-28 22:43:44 +02:00
|
|
|
// 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.
|
2011-03-18 23:37:00 +01:00
|
|
|
//
|
2013-10-30 18:52:33 +01:00
|
|
|
// 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.
|
2011-03-18 23:37:00 +01:00
|
|
|
//
|
|
|
|
// *file must remain live while this Table is in use.
|
|
|
|
static Status Open(const Options& options,
|
2013-03-15 01:00:04 +01:00
|
|
|
const EnvOptions& soptions,
|
2013-01-20 11:07:13 +01:00
|
|
|
unique_ptr<RandomAccessFile>&& file,
|
2011-03-28 22:43:44 +02:00
|
|
|
uint64_t file_size,
|
2013-10-30 18:52:33 +01:00
|
|
|
unique_ptr<TableReader>* table_reader);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-29 01:54:09 +01:00
|
|
|
bool PrefixMayMatch(const Slice& internal_prefix) override;
|
2013-08-13 23:04:56 +02:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// 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).
|
2013-10-29 01:54:09 +01:00
|
|
|
Iterator* NewIterator(const ReadOptions&) override;
|
|
|
|
|
|
|
|
Status Get(
|
2013-10-30 18:52:33 +01:00
|
|
|
const ReadOptions& readOptions,
|
|
|
|
const Slice& key,
|
|
|
|
void* handle_context,
|
|
|
|
bool (*result_handler)(void* handle_context, const Slice& k,
|
|
|
|
const Slice& v, bool didIO),
|
|
|
|
void (*mark_key_may_exist_handler)(void* handle_context) = nullptr)
|
|
|
|
override;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// 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.
|
2013-10-29 01:54:09 +01:00
|
|
|
uint64_t ApproximateOffsetOf(const Slice& key) override;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-02-01 00:20:24 +01:00
|
|
|
// Returns true if the block for the specified key is in cache.
|
|
|
|
// REQUIRES: key is in this table.
|
2013-10-29 01:54:09 +01:00
|
|
|
bool TEST_KeyInCache(const ReadOptions& options, const Slice& key) override;
|
2013-02-01 00:20:24 +01:00
|
|
|
|
2013-06-14 02:25:09 +02:00
|
|
|
// Set up the table for Compaction. Might change some parameters with
|
|
|
|
// posix_fadvise
|
2013-10-29 01:54:09 +01:00
|
|
|
void SetupForCompaction() override;
|
|
|
|
|
|
|
|
TableStats& GetTableStats() override;
|
2013-05-18 00:53:01 +02:00
|
|
|
|
2013-10-29 01:54:09 +01:00
|
|
|
~BlockBasedTable();
|
2013-10-10 20:43:24 +02:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
private:
|
2013-11-13 07:46:51 +01:00
|
|
|
template <class TValue>
|
|
|
|
struct CachableEntry;
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
struct Rep;
|
|
|
|
Rep* rep_;
|
2013-06-14 02:25:09 +02:00
|
|
|
bool compaction_optimized_;
|
2013-10-10 20:43:24 +02:00
|
|
|
|
2013-03-15 01:00:04 +01:00
|
|
|
static Iterator* BlockReader(void*, const ReadOptions&,
|
2013-05-18 00:53:01 +02:00
|
|
|
const EnvOptions& soptions, const Slice&,
|
|
|
|
bool for_compaction);
|
2013-11-13 07:46:51 +01:00
|
|
|
|
2012-09-27 10:05:38 +02:00
|
|
|
static Iterator* BlockReader(void*, const ReadOptions&, const Slice&,
|
2013-08-25 07:48:51 +02:00
|
|
|
bool* didIO, bool for_compaction = false);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-11-13 07:46:51 +01:00
|
|
|
// if `no_io == true`, we will not try to read filter from sst file
|
|
|
|
// if it is not cached yet.
|
|
|
|
CachableEntry<FilterBlockReader> GetFilter(bool no_io = false) const;
|
|
|
|
|
|
|
|
Iterator* IndexBlockReader(const ReadOptions& options) const;
|
|
|
|
|
|
|
|
// Read the block, either from sst file or from cache. This method will try
|
|
|
|
// to read from cache only when block_cache is set or ReadOption doesn't
|
|
|
|
// explicitly prohibit storage IO.
|
|
|
|
//
|
|
|
|
// If the block is read from cache, the statistics for cache miss/hit of the
|
|
|
|
// the given type of block will be updated. User can specify
|
|
|
|
// `block_cache_miss_ticker` and `block_cache_hit_ticker` for the statistics
|
|
|
|
// update.
|
|
|
|
//
|
|
|
|
// On success, the `result` parameter will be populated, which contains a
|
|
|
|
// pointer to the block and its cache handle, which will be nullptr if it's
|
|
|
|
// not read from the cache.
|
|
|
|
static Status GetBlock(const BlockBasedTable* table,
|
|
|
|
const BlockHandle& handle,
|
|
|
|
const ReadOptions& options,
|
|
|
|
bool for_compaction,
|
|
|
|
Tickers block_cache_miss_ticker,
|
|
|
|
Tickers block_cache_hit_ticker,
|
|
|
|
bool* didIO,
|
|
|
|
CachableEntry<Block>* result);
|
|
|
|
|
2013-03-21 23:59:47 +01:00
|
|
|
// 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.
|
2012-04-17 17:36:46 +02:00
|
|
|
friend class TableCache;
|
2013-09-02 08:23:40 +02:00
|
|
|
friend class BlockBasedTableBuilder;
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
void ReadMeta(const Footer& footer);
|
|
|
|
void ReadFilter(const Slice& filter_handle_value);
|
2013-10-10 20:43:24 +02:00
|
|
|
static Status ReadStats(const Slice& handle_value, Rep* rep);
|
2012-04-17 17:36:46 +02:00
|
|
|
|
2013-11-13 07:46:51 +01:00
|
|
|
// Read the meta block from sst.
|
|
|
|
static Status ReadMetaBlock(
|
|
|
|
Rep* rep,
|
|
|
|
std::unique_ptr<Block>* meta_block,
|
|
|
|
std::unique_ptr<Iterator>* iter);
|
|
|
|
|
|
|
|
// Create the filter from the filter block.
|
|
|
|
static FilterBlockReader* ReadFilter(
|
|
|
|
const Slice& filter_handle_value,
|
|
|
|
Rep* rep,
|
|
|
|
size_t* filter_size = nullptr);
|
|
|
|
|
|
|
|
// Read the table stats from stats block.
|
|
|
|
static Status ReadStats(
|
|
|
|
const Slice& handle_value, Rep* rep, TableStats* stats);
|
|
|
|
|
2013-02-01 00:20:24 +01:00
|
|
|
static void SetupCacheKeyPrefix(Rep* rep);
|
|
|
|
|
2013-10-29 01:54:09 +01:00
|
|
|
explicit BlockBasedTable(Rep* rep) :
|
|
|
|
compaction_optimized_(false) {
|
|
|
|
rep_ = rep;
|
|
|
|
}
|
2013-09-02 08:23:40 +02:00
|
|
|
// Generate a cache key prefix from the file
|
|
|
|
static void GenerateCachePrefix(shared_ptr<Cache> cc,
|
|
|
|
RandomAccessFile* file, char* buffer, size_t* size);
|
|
|
|
static void GenerateCachePrefix(shared_ptr<Cache> cc,
|
|
|
|
WritableFile* file, char* buffer, size_t* size);
|
|
|
|
|
|
|
|
// 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;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-29 01:54:09 +01:00
|
|
|
// No copying allowed
|
2013-10-30 18:52:33 +01:00
|
|
|
explicit BlockBasedTable(const TableReader&) = delete;
|
|
|
|
void operator=(const TableReader&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BlockBasedTableStatsNames {
|
|
|
|
static const std::string kDataSize;
|
|
|
|
static const std::string kIndexSize;
|
|
|
|
static const std::string kRawKeySize;
|
|
|
|
static const std::string kRawValueSize;
|
|
|
|
static const std::string kNumDataBlocks;
|
|
|
|
static const std::string kNumEntries;
|
|
|
|
static const std::string kFilterPolicy;
|
2013-10-10 20:43:24 +02:00
|
|
|
};
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|