2013-10-29 03:34:02 +00: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.
|
|
|
|
|
|
|
|
#pragma once
|
2014-04-25 19:21:34 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2013-10-29 03:34:02 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <memory>
|
2014-01-25 05:10:19 +00:00
|
|
|
#include <vector>
|
2014-01-27 21:53:22 +00:00
|
|
|
#include <string>
|
2013-10-29 03:34:02 +00:00
|
|
|
#include <stdint.h>
|
2014-01-28 05:58:46 +00:00
|
|
|
|
2014-01-27 21:53:22 +00:00
|
|
|
#include "db/dbformat.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
2014-01-28 18:35:48 +00:00
|
|
|
#include "rocksdb/slice_transform.h"
|
2014-01-28 05:58:46 +00:00
|
|
|
#include "rocksdb/table.h"
|
|
|
|
#include "rocksdb/table_properties.h"
|
|
|
|
#include "table/table_reader.h"
|
|
|
|
#include "table/plain_table_factory.h"
|
2014-07-18 23:58:13 +00:00
|
|
|
#include "table/plain_table_index.h"
|
2014-05-04 20:55:53 +00:00
|
|
|
#include "util/arena.h"
|
2014-06-09 19:30:19 +00:00
|
|
|
#include "util/dynamic_bloom.h"
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class Block;
|
2014-07-20 14:56:40 +00:00
|
|
|
struct BlockContents;
|
2013-10-29 03:34:02 +00:00
|
|
|
class BlockHandle;
|
|
|
|
class Footer;
|
|
|
|
struct Options;
|
|
|
|
class RandomAccessFile;
|
|
|
|
struct ReadOptions;
|
|
|
|
class TableCache;
|
|
|
|
class TableReader;
|
2014-01-27 21:53:22 +00:00
|
|
|
class InternalKeyComparator;
|
2014-06-18 23:36:48 +00:00
|
|
|
class PlainTableKeyDecoder;
|
2014-09-29 18:09:09 +00:00
|
|
|
class GetContext;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::unordered_map;
|
2014-07-18 23:58:13 +00:00
|
|
|
using std::vector;
|
2014-01-28 05:58:46 +00:00
|
|
|
extern const uint32_t kPlainTableVariableLength;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
// Based on following output file format shown in plain_table_factory.h
|
2013-10-29 03:34:02 +00:00
|
|
|
// When opening the output file, IndexedTableReader creates a hash table
|
|
|
|
// from key prefixes to offset of the output file. IndexedTable will decide
|
|
|
|
// whether it points to the data offset of the first key with the key prefix
|
|
|
|
// or the offset of it. If there are too many keys share this prefix, it will
|
|
|
|
// create a binary search-able index from the suffix to offset on disk.
|
|
|
|
//
|
|
|
|
// The implementation of IndexedTableReader requires output file is mmaped
|
|
|
|
class PlainTableReader: public TableReader {
|
2014-01-25 05:10:19 +00:00
|
|
|
public:
|
2014-09-04 23:18:36 +00:00
|
|
|
static Status Open(const ImmutableCFOptions& ioptions,
|
|
|
|
const EnvOptions& env_options,
|
2014-01-27 21:53:22 +00:00
|
|
|
const InternalKeyComparator& internal_comparator,
|
2014-01-28 18:35:48 +00:00
|
|
|
unique_ptr<RandomAccessFile>&& file, uint64_t file_size,
|
2014-01-28 05:58:46 +00:00
|
|
|
unique_ptr<TableReader>* table,
|
2014-02-08 00:25:38 +00:00
|
|
|
const int bloom_bits_per_key, double hash_table_ratio,
|
2014-06-18 23:36:48 +00:00
|
|
|
size_t index_sparseness, size_t huge_page_tlb_size,
|
|
|
|
bool full_scan_mode);
|
2013-10-29 03:34:02 +00:00
|
|
|
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
Iterator* NewIterator(const ReadOptions&, Arena* arena = nullptr) override;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2014-06-12 17:06:18 +00:00
|
|
|
void Prepare(const Slice& target);
|
|
|
|
|
2014-09-29 18:09:09 +00:00
|
|
|
Status Get(const ReadOptions&, const Slice& key,
|
|
|
|
GetContext* get_context) override;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
uint64_t ApproximateOffsetOf(const Slice& key);
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
uint32_t GetIndexSize() const { return index_.GetIndexSize(); }
|
2013-10-29 03:34:02 +00:00
|
|
|
void SetupForCompaction();
|
|
|
|
|
2014-02-08 03:26:49 +00:00
|
|
|
std::shared_ptr<const TableProperties> GetTableProperties() const {
|
|
|
|
return table_properties_;
|
|
|
|
}
|
2013-10-29 03:34:02 +00:00
|
|
|
|
2014-08-05 18:27:34 +00:00
|
|
|
virtual size_t ApproximateMemoryUsage() const override {
|
|
|
|
return arena_.MemoryAllocatedBytes();
|
|
|
|
}
|
|
|
|
|
2014-09-04 23:18:36 +00:00
|
|
|
PlainTableReader(const ImmutableCFOptions& ioptions,
|
|
|
|
unique_ptr<RandomAccessFile>&& file,
|
|
|
|
const EnvOptions& env_options,
|
2014-01-27 21:53:22 +00:00
|
|
|
const InternalKeyComparator& internal_comparator,
|
2014-06-18 23:36:48 +00:00
|
|
|
EncodingType encoding_type, uint64_t file_size,
|
|
|
|
const TableProperties* table_properties);
|
2014-02-08 00:25:38 +00:00
|
|
|
virtual ~PlainTableReader();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Check bloom filter to see whether it might contain this prefix.
|
|
|
|
// The hash of the prefix is given, since it can be reused for index lookup
|
|
|
|
// too.
|
|
|
|
virtual bool MatchBloom(uint32_t hash) const;
|
|
|
|
|
|
|
|
// PopulateIndex() builds index of keys. It must be called before any query
|
|
|
|
// to the table.
|
|
|
|
//
|
2014-04-23 01:31:55 +00:00
|
|
|
// props: the table properties object that need to be stored. Ownership of
|
|
|
|
// the object will be passed.
|
|
|
|
//
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
Status PopulateIndex(TableProperties* props, int bloom_bits_per_key,
|
|
|
|
double hash_table_ratio, size_t index_sparseness,
|
|
|
|
size_t huge_page_tlb_size);
|
2014-07-18 23:58:13 +00:00
|
|
|
|
2014-06-18 23:36:48 +00:00
|
|
|
Status MmapDataFile();
|
2014-02-08 00:25:38 +00:00
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
private:
|
2014-01-27 21:53:22 +00:00
|
|
|
const InternalKeyComparator internal_comparator_;
|
2014-06-18 23:36:48 +00:00
|
|
|
EncodingType encoding_type_;
|
2014-02-13 23:27:59 +00:00
|
|
|
// represents plain table's current status.
|
2013-10-29 03:34:02 +00:00
|
|
|
Status status_;
|
2013-11-21 19:11:02 +00:00
|
|
|
Slice file_data_;
|
2013-12-20 17:35:24 +00:00
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
PlainTableIndex index_;
|
|
|
|
bool full_scan_mode_;
|
|
|
|
|
2014-02-13 23:27:59 +00:00
|
|
|
// data_start_offset_ and data_end_offset_ defines the range of the
|
|
|
|
// sst file that stores data.
|
2014-01-25 05:10:19 +00:00
|
|
|
const uint32_t data_start_offset_ = 0;
|
2013-12-20 17:35:24 +00:00
|
|
|
const uint32_t data_end_offset_;
|
|
|
|
const size_t user_key_len_;
|
2014-06-09 19:30:19 +00:00
|
|
|
const SliceTransform* prefix_extractor_;
|
2013-10-29 03:34:02 +00:00
|
|
|
|
|
|
|
static const size_t kNumInternalBytes = 8;
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
// Bloom filter is used to rule out non-existent key
|
|
|
|
bool enable_bloom_;
|
|
|
|
DynamicBloom bloom_;
|
|
|
|
Arena arena_;
|
|
|
|
|
2014-09-04 23:18:36 +00:00
|
|
|
const ImmutableCFOptions& ioptions_;
|
2014-06-09 19:30:19 +00:00
|
|
|
unique_ptr<RandomAccessFile> file_;
|
|
|
|
uint32_t file_size_;
|
|
|
|
std::shared_ptr<const TableProperties> table_properties_;
|
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
bool IsFixedLength() const {
|
2014-01-28 05:58:46 +00:00
|
|
|
return user_key_len_ != kPlainTableVariableLength;
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-25 05:10:19 +00:00
|
|
|
size_t GetFixedInternalKeyLength() const {
|
2013-12-20 17:35:24 +00:00
|
|
|
return user_key_len_ + kNumInternalBytes;
|
2013-10-29 03:34:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
Slice GetPrefix(const Slice& target) const {
|
|
|
|
assert(target.size() >= 8); // target is internal key
|
|
|
|
return GetPrefixFromUserKey(GetUserKey(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice GetPrefix(const ParsedInternalKey& target) const {
|
|
|
|
return GetPrefixFromUserKey(target.user_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice GetUserKey(const Slice& key) const {
|
|
|
|
return Slice(key.data(), key.size() - 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice GetPrefixFromUserKey(const Slice& user_key) const {
|
|
|
|
if (!IsTotalOrderMode()) {
|
|
|
|
return prefix_extractor_->Transform(user_key);
|
|
|
|
} else {
|
|
|
|
// Use empty slice as prefix if prefix_extractor is not set.
|
|
|
|
// In that case,
|
|
|
|
// it falls back to pure binary search and
|
|
|
|
// total iterator seek is supported.
|
|
|
|
return Slice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
friend class TableCache;
|
|
|
|
friend class PlainTableIterator;
|
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
// Internal helper function to generate an IndexRecordList object from all
|
|
|
|
// the rows, which contains index records as a list.
|
2014-02-08 00:25:38 +00:00
|
|
|
// If bloom_ is not null, all the keys' full-key hash will be added to the
|
|
|
|
// bloom filter.
|
2014-07-18 23:58:13 +00:00
|
|
|
Status PopulateIndexRecordList(PlainTableIndexBuilder* index_builder,
|
|
|
|
vector<uint32_t>* prefix_hashes);
|
|
|
|
|
|
|
|
// Internal helper function to allocate memory for bloom filter and fill it
|
|
|
|
void AllocateAndFillBloom(int bloom_bits_per_key, int num_prefixes,
|
|
|
|
size_t huge_page_tlb_size,
|
|
|
|
vector<uint32_t>* prefix_hashes);
|
|
|
|
|
|
|
|
void FillBloom(vector<uint32_t>* prefix_hashes);
|
2014-01-25 05:10:19 +00:00
|
|
|
|
2014-06-18 23:36:48 +00:00
|
|
|
// Read the key and value at `offset` to parameters for keys, the and
|
|
|
|
// `seekable`.
|
2014-02-13 23:27:59 +00:00
|
|
|
// On success, `offset` will be updated as the offset for the next key.
|
2014-06-18 23:36:48 +00:00
|
|
|
// `parsed_key` will be key in parsed format.
|
|
|
|
// if `internal_key` is not empty, it will be filled with key with slice
|
|
|
|
// format.
|
|
|
|
// if `seekable` is not null, it will return whether we can directly read
|
|
|
|
// data using this offset.
|
|
|
|
Status Next(PlainTableKeyDecoder* decoder, uint32_t* offset,
|
|
|
|
ParsedInternalKey* parsed_key, Slice* internal_key, Slice* value,
|
|
|
|
bool* seekable = nullptr) const;
|
2013-11-21 19:11:02 +00:00
|
|
|
// Get file offset for key target.
|
|
|
|
// return value prefix_matched is set to true if the offset is confirmed
|
|
|
|
// for a key with the same prefix as target.
|
2013-12-20 17:35:24 +00:00
|
|
|
Status GetOffset(const Slice& target, const Slice& prefix,
|
|
|
|
uint32_t prefix_hash, bool& prefix_matched,
|
2014-02-13 23:27:59 +00:00
|
|
|
uint32_t* offset) const;
|
2014-02-08 00:25:38 +00:00
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
bool IsTotalOrderMode() const { return (prefix_extractor_ == nullptr); }
|
2014-01-27 21:53:22 +00:00
|
|
|
|
2013-10-29 03:34:02 +00:00
|
|
|
// No copying allowed
|
|
|
|
explicit PlainTableReader(const TableReader&) = delete;
|
|
|
|
void operator=(const TableReader&) = delete;
|
|
|
|
};
|
|
|
|
} // namespace rocksdb
|
2014-04-15 20:39:26 +00:00
|
|
|
#endif // ROCKSDB_LITE
|