d4eec30ed0
Summary: This patch makes Table and TableBuilder a abstract class and make all the implementation of the current table into BlockedBasedTable and BlockedBasedTable Builder. Test Plan: Make db_test.cc to work with block based table. Add a new test simple_table_db_test.cc where a different simple table format is implemented. Reviewers: dhruba, haobo, kailiu, emayanke, vamsi Reviewed By: dhruba CC: leveldb Differential Revision: https://reviews.facebook.net/D13521
190 lines
7.5 KiB
C++
190 lines
7.5 KiB
C++
// 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.
|
|
//
|
|
// 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
|
|
#include <memory>
|
|
#include <stdint.h>
|
|
#include "rocksdb/env.h"
|
|
#include "rocksdb/iterator.h"
|
|
#include "rocksdb/table_stats.h"
|
|
|
|
namespace rocksdb {
|
|
|
|
struct Options;
|
|
class RandomAccessFile;
|
|
struct ReadOptions;
|
|
class TableCache;
|
|
class WritableFile;
|
|
|
|
using std::unique_ptr;
|
|
|
|
// TableBuilder provides the interface used to build a Table
|
|
// (an immutable and sorted map from keys to values).
|
|
//
|
|
// Multiple threads can invoke const methods on a TableBuilder without
|
|
// external synchronization, but if any of the threads may call a
|
|
// non-const method, all threads accessing the same TableBuilder must use
|
|
// external synchronization.
|
|
|
|
class TableBuilder {
|
|
public:
|
|
// Create a builder that will store the contents of the table it is
|
|
// building in *file. Does not close the file. It is up to the
|
|
// caller to close the file after calling Finish(). The output file
|
|
// will be part of level specified by 'level'. A value of -1 means
|
|
// that the caller does not know which level the output file will reside.
|
|
//
|
|
// If enable_compression=true, this table will follow the compression
|
|
// setting given in parameter options. If enable_compression=false, the
|
|
// table will not be compressed.
|
|
explicit TableBuilder(int level = -1, const bool enable_compression = true) :
|
|
level_(level) {
|
|
}
|
|
|
|
// REQUIRES: Either Finish() or Abandon() has been called.
|
|
virtual ~TableBuilder() {}
|
|
|
|
// Add key,value to the table being constructed.
|
|
// REQUIRES: key is after any previously added key according to comparator.
|
|
// REQUIRES: Finish(), Abandon() have not been called
|
|
virtual void Add(const Slice& key, const Slice& value) = 0;
|
|
|
|
// Return non-ok iff some error has been detected.
|
|
virtual Status status() const = 0;
|
|
|
|
// Finish building the table.
|
|
// REQUIRES: Finish(), Abandon() have not been called
|
|
virtual Status Finish() = 0;
|
|
|
|
// Indicate that the contents of this builder should be abandoned.
|
|
// If the caller is not going to call Finish(), it must call Abandon()
|
|
// before destroying this builder.
|
|
// REQUIRES: Finish(), Abandon() have not been called
|
|
virtual void Abandon() = 0;
|
|
|
|
// Number of calls to Add() so far.
|
|
virtual uint64_t NumEntries() const = 0;
|
|
|
|
// Size of the file generated so far. If invoked after a successful
|
|
// Finish() call, returns the size of the final generated file.
|
|
virtual uint64_t FileSize() const = 0;
|
|
|
|
protected:
|
|
int level_;
|
|
};
|
|
|
|
// A Table is a sorted map from strings to strings. Tables are
|
|
// immutable and persistent. A Table may be safely accessed from
|
|
// multiple threads without external synchronization.
|
|
class Table {
|
|
public:
|
|
virtual ~Table() {}
|
|
|
|
// Determine whether there is a chance that the current table file
|
|
// contains the key a key starting with iternal_prefix. The specific
|
|
// table implementation can use bloom filter and/or other heuristic
|
|
// to filter out this table as a whole.
|
|
virtual bool PrefixMayMatch(const Slice& internal_prefix) = 0;
|
|
|
|
// 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).
|
|
virtual Iterator* NewIterator(const ReadOptions&) = 0;
|
|
|
|
// 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.
|
|
virtual uint64_t ApproximateOffsetOf(const Slice& key) = 0;
|
|
|
|
// Returns true if the block for the specified key is in cache.
|
|
// REQUIRES: key is in this table.
|
|
virtual bool TEST_KeyInCache(const ReadOptions& options,
|
|
const Slice& key) = 0;
|
|
|
|
// Set up the table for Compaction. Might change some parameters with
|
|
// posix_fadvise
|
|
virtual void SetupForCompaction() = 0;
|
|
|
|
virtual TableStats& GetTableStats() = 0;
|
|
|
|
// Get function issued to look for specific key.
|
|
// The table will search the first entry in the table whose user key
|
|
// matches key, and pass it to the call back function handle_result,
|
|
// with the first argument to be parameter arg, and the last bool
|
|
// parameter to be whether an I/O is issued.
|
|
// mark_key_may_exist call back is called when it is configured to be
|
|
// memory only and the key is not found in the block cache, with
|
|
// the parameter to be arg.
|
|
virtual Status Get(
|
|
const ReadOptions&, const Slice& key,
|
|
void* arg,
|
|
bool (*handle_result)(void* arg, const Slice& k, const Slice& v, bool),
|
|
void (*mark_key_may_exist)(void*) = nullptr) = 0;
|
|
};
|
|
|
|
struct TableStatsNames {
|
|
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;
|
|
};
|
|
|
|
// A base class for table factories
|
|
class TableFactory {
|
|
public:
|
|
virtual ~TableFactory() {}
|
|
|
|
// The name of the comparator.
|
|
//
|
|
// The client of this package should switch to a new name whenever
|
|
// the table format implementation changes.
|
|
//
|
|
// Names starting with "rocksdb." are reserved and should not be used
|
|
// by any clients of this package.
|
|
virtual const char* Name() const = 0;
|
|
|
|
// Returns a Table object table that can fetch data from file specified
|
|
// in parameter file. It's the caller's responsibility to make sure
|
|
// file is in the correct format.
|
|
//
|
|
// OpenTable() is called in two places:
|
|
// (1) TableCache::FindTable() calls the function when table cache miss
|
|
// and cache the table object returned.
|
|
// (1) SstFileReader (for SST Dump) opens the table and dump the table
|
|
// contents using the interator of the table.
|
|
virtual Status OpenTable(const Options& options,
|
|
const EnvOptions& soptions,
|
|
unique_ptr<RandomAccessFile>&& file,
|
|
uint64_t file_size,
|
|
unique_ptr<Table>* table) const = 0;
|
|
|
|
// Return a table builder to write to a file for this table type.
|
|
//
|
|
// It is called in several places:
|
|
// (1) When flushing memtable to a level-0 output file, it creates a table
|
|
// builder (In DBImpl::WriteLevel0Table(), by calling BuildTable())
|
|
// (2) During compaction, it gets the builder for writing compaction output
|
|
// files in DBImpl::OpenCompactionOutputFile().
|
|
// (3) When recovering from transaction logs, it creates a table builder to
|
|
// write to a level-0 output file (In DBImpl::WriteLevel0TableForRecovery,
|
|
// by calling BuildTable())
|
|
// (4) When running Repairer, it creates a table builder to convert logs to
|
|
// SST files (In Repairer::ConvertLogToTable() by calling BuildTable())
|
|
virtual TableBuilder* GetTableBuilder(
|
|
const Options& options, WritableFile* file, int level,
|
|
const bool enable_compression) const = 0;
|
|
};
|
|
} // namespace rocksdb
|