leveldb
Public Member Functions | Private Member Functions | Private Attributes | List of all members
leveldb::TableCache Class Reference

#include <table_cache.h>

Collaboration diagram for leveldb::TableCache:
Collaboration graph
[legend]

Public Member Functions

 TableCache (const std::string &dbname, const Options *options, int entries)
 
 ~TableCache ()
 
IteratorNewIterator (const ReadOptions &options, uint64_t file_number, uint64_t file_size, Table **tableptr=NULL)
 
Status Get (const ReadOptions &options, uint64_t file_number, uint64_t file_size, const Slice &k, void *arg, void(*handle_result)(void *, const Slice &, const Slice &))
 
void Evict (uint64_t file_number)
 

Private Member Functions

Status FindTable (uint64_t file_number, uint64_t file_size, Cache::Handle **)
 

Private Attributes

Env *const env_
 
const std::string dbname_
 
const Optionsoptions_
 
Cachecache_
 

Detailed Description

Definition at line 21 of file table_cache.h.

Constructor & Destructor Documentation

§ TableCache()

leveldb::TableCache::TableCache ( const std::string &  dbname,
const Options options,
int  entries 
)

Definition at line 32 of file table_cache.cc.

35  : env_(options->env),
36  dbname_(dbname),
37  options_(options),
38  cache_(NewLRUCache(entries)) {
39 }
const Options * options_
Definition: table_cache.h:53
static char dbname[200]
Definition: c_test.c:15
Cache * NewLRUCache(size_t capacity)
Definition: cache.cc:401
const std::string dbname_
Definition: table_cache.h:52

§ ~TableCache()

leveldb::TableCache::~TableCache ( )

Definition at line 41 of file table_cache.cc.

41  {
42  delete cache_;
43 }

Member Function Documentation

§ Evict()

void leveldb::TableCache::Evict ( uint64_t  file_number)

Definition at line 121 of file table_cache.cc.

121  {
122  char buf[sizeof(file_number)];
123  EncodeFixed64(buf, file_number);
124  cache_->Erase(Slice(buf, sizeof(buf)));
125 }
void EncodeFixed64(char *buf, uint64_t value)
Definition: coding.cc:20
virtual void Erase(const Slice &key)=0
Here is the call graph for this function:
Here is the caller graph for this function:

§ FindTable()

Status leveldb::TableCache::FindTable ( uint64_t  file_number,
uint64_t  file_size,
Cache::Handle **  handle 
)
private

Definition at line 45 of file table_cache.cc.

46  {
47  Status s;
48  char buf[sizeof(file_number)];
49  EncodeFixed64(buf, file_number);
50  Slice key(buf, sizeof(buf));
51  *handle = cache_->Lookup(key);
52  if (*handle == NULL) {
53  std::string fname = TableFileName(dbname_, file_number);
54  RandomAccessFile* file = NULL;
55  Table* table = NULL;
56  s = env_->NewRandomAccessFile(fname, &file);
57  if (!s.ok()) {
58  std::string old_fname = SSTTableFileName(dbname_, file_number);
59  if (env_->NewRandomAccessFile(old_fname, &file).ok()) {
60  s = Status::OK();
61  }
62  }
63  if (s.ok()) {
64  s = Table::Open(*options_, file, file_size, &table);
65  }
66 
67  if (!s.ok()) {
68  assert(table == NULL);
69  delete file;
70  // We do not cache error results so that if the error is transient,
71  // or somebody repairs the file, we recover automatically.
72  } else {
73  TableAndFile* tf = new TableAndFile;
74  tf->file = file;
75  tf->table = table;
76  *handle = cache_->Insert(key, tf, 1, &DeleteEntry);
77  }
78  }
79  return s;
80 }
virtual Handle * Insert(const Slice &key, void *value, size_t charge, void(*deleter)(const Slice &key, void *value))=0
static void DeleteEntry(const Slice &key, void *value)
Definition: table_cache.cc:19
static Status Open(const Options &options, RandomAccessFile *file, uint64_t file_size, Table **table)
Definition: table.cc:38
const Options * options_
Definition: table_cache.h:53
virtual Handle * Lookup(const Slice &key)=0
std::string TableFileName(const std::string &name, uint64_t number)
Definition: filename.cc:32
virtual Status NewRandomAccessFile(const std::string &fname, RandomAccessFile **result)=0
void EncodeFixed64(char *buf, uint64_t value)
Definition: coding.cc:20
static Status OK()
Definition: status.h:32
std::string SSTTableFileName(const std::string &name, uint64_t number)
Definition: filename.cc:37
bool ok() const
Definition: status.h:52
const std::string dbname_
Definition: table_cache.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

§ Get()

Status leveldb::TableCache::Get ( const ReadOptions options,
uint64_t  file_number,
uint64_t  file_size,
const Slice k,
void *  arg,
void(*)(void *, const Slice &, const Slice &)  handle_result 
)

Definition at line 105 of file table_cache.cc.

110  {
111  Cache::Handle* handle = NULL;
112  Status s = FindTable(file_number, file_size, &handle);
113  if (s.ok()) {
114  Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
115  s = t->InternalGet(options, k, arg, saver);
116  cache_->Release(handle);
117  }
118  return s;
119 }
virtual void Release(Handle *handle)=0
virtual void * Value(Handle *handle)=0
Status FindTable(uint64_t file_number, uint64_t file_size, Cache::Handle **)
Definition: table_cache.cc:45
Here is the call graph for this function:
Here is the caller graph for this function:

§ NewIterator()

Iterator * leveldb::TableCache::NewIterator ( const ReadOptions options,
uint64_t  file_number,
uint64_t  file_size,
Table **  tableptr = NULL 
)

Definition at line 82 of file table_cache.cc.

85  {
86  if (tableptr != NULL) {
87  *tableptr = NULL;
88  }
89 
90  Cache::Handle* handle = NULL;
91  Status s = FindTable(file_number, file_size, &handle);
92  if (!s.ok()) {
93  return NewErrorIterator(s);
94  }
95 
96  Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
97  Iterator* result = table->NewIterator(options);
98  result->RegisterCleanup(&UnrefEntry, cache_, handle);
99  if (tableptr != NULL) {
100  *tableptr = table;
101  }
102  return result;
103 }
virtual void * Value(Handle *handle)=0
Status FindTable(uint64_t file_number, uint64_t file_size, Cache::Handle **)
Definition: table_cache.cc:45
Iterator * NewErrorIterator(const Status &status)
Definition: iterator.cc:63
static void UnrefEntry(void *arg1, void *arg2)
Definition: table_cache.cc:26
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

§ cache_

Cache* leveldb::TableCache::cache_
private

Definition at line 54 of file table_cache.h.

§ dbname_

const std::string leveldb::TableCache::dbname_
private

Definition at line 52 of file table_cache.h.

§ env_

Env* const leveldb::TableCache::env_
private

Definition at line 51 of file table_cache.h.

§ options_

const Options* leveldb::TableCache::options_
private

Definition at line 53 of file table_cache.h.


The documentation for this class was generated from the following files: