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.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_INCLUDE_TABLE_H_
|
|
|
|
#define STORAGE_LEVELDB_INCLUDE_TABLE_H_
|
|
|
|
|
2013-01-20 11:07:13 +01:00
|
|
|
#include <memory>
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <stdint.h>
|
2011-03-30 20:49:03 +02:00
|
|
|
#include "leveldb/iterator.h"
|
2013-03-15 01:00:04 +01:00
|
|
|
#include "leveldb/env.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
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;
|
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.
|
2011-03-18 23:37:00 +01:00
|
|
|
class Table {
|
|
|
|
public:
|
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
|
|
|
//
|
|
|
|
// 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 03:04:58 +01:00
|
|
|
// to nullptr and returns a non-ok status. Does not take ownership of
|
2011-03-18 23:37:00 +01: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 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-01-20 11:07:13 +01:00
|
|
|
unique_ptr<Table>* table);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
~Table();
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
Iterator* NewIterator(const ReadOptions&) const;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
uint64_t ApproximateOffsetOf(const Slice& key) const;
|
|
|
|
|
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.
|
|
|
|
bool TEST_KeyInCache(const ReadOptions& options, const Slice& key);
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
private:
|
|
|
|
struct Rep;
|
|
|
|
Rep* rep_;
|
|
|
|
|
|
|
|
explicit Table(Rep* rep) { rep_ = rep; }
|
2013-03-15 01:00:04 +01:00
|
|
|
static Iterator* BlockReader(void*, const ReadOptions&,
|
|
|
|
const EnvOptions& soptions, const Slice&);
|
2012-09-27 10:05:38 +02:00
|
|
|
static Iterator* BlockReader(void*, const ReadOptions&, const Slice&,
|
|
|
|
bool* didIO);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2012-04-17 17:36:46 +02:00
|
|
|
// Calls (*handle_result)(arg, ...) with the entry found after a call
|
|
|
|
// to Seek(key). May not make such a call if filter policy says
|
|
|
|
// that key is not present.
|
|
|
|
friend class TableCache;
|
|
|
|
Status InternalGet(
|
|
|
|
const ReadOptions&, const Slice& key,
|
|
|
|
void* arg,
|
2012-09-27 10:05:38 +02:00
|
|
|
void (*handle_result)(void* arg, const Slice& k, const Slice& v, bool));
|
2012-04-17 17:36:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
void ReadMeta(const Footer& footer);
|
|
|
|
void ReadFilter(const Slice& filter_handle_value);
|
|
|
|
|
2013-02-01 00:20:24 +01:00
|
|
|
static void SetupCacheKeyPrefix(Rep* rep);
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// No copying allowed
|
|
|
|
Table(const Table&);
|
|
|
|
void operator=(const Table&);
|
|
|
|
};
|
|
|
|
|
2011-10-31 18:22:06 +01:00
|
|
|
} // namespace leveldb
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_INCLUDE_TABLE_H_
|