2013-10-16 21:59:46 +00: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 22:37:00 +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.
|
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2013-01-20 10:07:13 +00:00
|
|
|
#include <memory>
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <stdint.h>
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/env.h"
|
2013-10-10 18:43:24 +00:00
|
|
|
#include "rocksdb/iterator.h"
|
|
|
|
#include "rocksdb/table_stats.h"
|
2013-10-29 00:54:09 +00:00
|
|
|
#include "rocksdb/table.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
class Block;
|
|
|
|
class BlockHandle;
|
2012-04-17 15:36:46 +00:00
|
|
|
class Footer;
|
2011-03-18 22:37:00 +00:00
|
|
|
struct Options;
|
|
|
|
class RandomAccessFile;
|
|
|
|
struct ReadOptions;
|
2012-04-17 15:36:46 +00:00
|
|
|
class TableCache;
|
2013-10-29 00:54:09 +00:00
|
|
|
class Table;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-01-20 10:07:13 +00:00
|
|
|
using std::unique_ptr;
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
// A Table is a sorted map from strings to strings. Tables are
|
2011-05-21 02:17:43 +00:00
|
|
|
// immutable and persistent. A Table may be safely accessed from
|
|
|
|
// multiple threads without external synchronization.
|
2013-10-29 00:54:09 +00:00
|
|
|
class BlockBasedTable : public Table {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2013-10-10 18:43:24 +00:00
|
|
|
static const std::string kFilterBlockPrefix;
|
|
|
|
static const std::string kStatsBlock;
|
|
|
|
|
2011-03-28 20:43:44 +00: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 22:37:00 +00:00
|
|
|
//
|
|
|
|
// If successful, returns ok and sets "*table" to the newly opened
|
|
|
|
// table. The client should delete "*table" when no longer needed.
|
|
|
|
// If there was an error while initializing the table, sets "*table"
|
2013-03-01 02:04:58 +00:00
|
|
|
// to nullptr and returns a non-ok status. Does not take ownership of
|
2011-03-18 22:37:00 +00:00
|
|
|
// "*source", but the client must ensure that "source" remains live
|
|
|
|
// for the duration of the returned table's lifetime.
|
|
|
|
//
|
|
|
|
// *file must remain live while this Table is in use.
|
|
|
|
static Status Open(const Options& options,
|
2013-03-15 00:00:04 +00:00
|
|
|
const EnvOptions& soptions,
|
2013-01-20 10:07:13 +00:00
|
|
|
unique_ptr<RandomAccessFile>&& file,
|
2011-03-28 20:43:44 +00:00
|
|
|
uint64_t file_size,
|
2013-01-20 10:07:13 +00:00
|
|
|
unique_ptr<Table>* table);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-29 00:54:09 +00:00
|
|
|
bool PrefixMayMatch(const Slice& internal_prefix) override;
|
2013-08-13 21:04:56 +00:00
|
|
|
|
2011-03-18 22:37:00 +00: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 00:54:09 +00:00
|
|
|
Iterator* NewIterator(const ReadOptions&) override;
|
|
|
|
|
|
|
|
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) override;
|
2011-03-18 22:37:00 +00: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 00:54:09 +00:00
|
|
|
uint64_t ApproximateOffsetOf(const Slice& key) override;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-01-31 23:20:24 +00:00
|
|
|
// Returns true if the block for the specified key is in cache.
|
|
|
|
// REQUIRES: key is in this table.
|
2013-10-29 00:54:09 +00:00
|
|
|
bool TEST_KeyInCache(const ReadOptions& options, const Slice& key) override;
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2013-06-14 00:25:09 +00:00
|
|
|
// Set up the table for Compaction. Might change some parameters with
|
|
|
|
// posix_fadvise
|
2013-10-29 00:54:09 +00:00
|
|
|
void SetupForCompaction() override;
|
|
|
|
|
|
|
|
TableStats& GetTableStats() override;
|
2013-05-17 22:53:01 +00:00
|
|
|
|
2013-10-29 00:54:09 +00:00
|
|
|
~BlockBasedTable();
|
2013-10-10 18:43:24 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
|
|
|
struct Rep;
|
|
|
|
Rep* rep_;
|
2013-06-14 00:25:09 +00:00
|
|
|
bool compaction_optimized_;
|
2013-10-10 18:43:24 +00:00
|
|
|
|
2013-03-15 00:00:04 +00:00
|
|
|
static Iterator* BlockReader(void*, const ReadOptions&,
|
2013-05-17 22:53:01 +00:00
|
|
|
const EnvOptions& soptions, const Slice&,
|
|
|
|
bool for_compaction);
|
2012-09-27 08:05:38 +00:00
|
|
|
static Iterator* BlockReader(void*, const ReadOptions&, const Slice&,
|
2013-08-25 05:48:51 +00:00
|
|
|
bool* didIO, bool for_compaction = false);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-03-21 22:59:47 +00: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 15:36:46 +00:00
|
|
|
friend class TableCache;
|
|
|
|
|
|
|
|
void ReadMeta(const Footer& footer);
|
|
|
|
void ReadFilter(const Slice& filter_handle_value);
|
2013-10-10 18:43:24 +00:00
|
|
|
static Status ReadStats(const Slice& handle_value, Rep* rep);
|
2012-04-17 15:36:46 +00:00
|
|
|
|
2013-01-31 23:20:24 +00:00
|
|
|
static void SetupCacheKeyPrefix(Rep* rep);
|
|
|
|
|
2013-10-29 00:54:09 +00:00
|
|
|
explicit BlockBasedTable(Rep* rep) :
|
|
|
|
compaction_optimized_(false) {
|
|
|
|
rep_ = rep;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-29 00:54:09 +00:00
|
|
|
// No copying allowed
|
|
|
|
explicit BlockBasedTable(const Table&) = delete;
|
|
|
|
void operator=(const Table&) = delete;
|
2013-10-10 18:43:24 +00:00
|
|
|
};
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|