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.
|
|
|
|
|
|
|
|
#include "db/table_cache.h"
|
|
|
|
|
|
|
|
#include "db/filename.h"
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/table.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
struct TableAndFile {
|
|
|
|
RandomAccessFile* file;
|
|
|
|
Table* table;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void DeleteEntry(const Slice& key, void* value) {
|
|
|
|
TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
|
|
|
|
delete tf->table;
|
|
|
|
delete tf->file;
|
|
|
|
delete tf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UnrefEntry(void* arg1, void* arg2) {
|
|
|
|
Cache* cache = reinterpret_cast<Cache*>(arg1);
|
|
|
|
Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);
|
|
|
|
cache->Release(h);
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
TableCache::TableCache(const std::string& dbname, const Options& options,
|
2011-03-18 22:37:00 +00:00
|
|
|
int entries)
|
2018-03-16 10:06:35 -07:00
|
|
|
: env_(options.env),
|
2011-03-18 22:37:00 +00:00
|
|
|
dbname_(dbname),
|
|
|
|
options_(options),
|
2019-05-02 11:01:00 -07:00
|
|
|
cache_(NewLRUCache(entries)) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
TableCache::~TableCache() { delete cache_; }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2012-04-17 08:36:46 -07:00
|
|
|
Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
|
|
|
|
Cache::Handle** handle) {
|
|
|
|
Status s;
|
2011-03-18 22:37:00 +00:00
|
|
|
char buf[sizeof(file_number)];
|
|
|
|
EncodeFixed64(buf, file_number);
|
|
|
|
Slice key(buf, sizeof(buf));
|
2012-04-17 08:36:46 -07:00
|
|
|
*handle = cache_->Lookup(key);
|
2018-04-10 16:18:06 -07:00
|
|
|
if (*handle == nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
std::string fname = TableFileName(dbname_, file_number);
|
2018-04-10 16:18:06 -07:00
|
|
|
RandomAccessFile* file = nullptr;
|
|
|
|
Table* table = nullptr;
|
2012-04-17 08:36:46 -07:00
|
|
|
s = env_->NewRandomAccessFile(fname, &file);
|
2013-09-19 13:42:22 -07:00
|
|
|
if (!s.ok()) {
|
|
|
|
std::string old_fname = SSTTableFileName(dbname_, file_number);
|
|
|
|
if (env_->NewRandomAccessFile(old_fname, &file).ok()) {
|
|
|
|
s = Status::OK();
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
if (s.ok()) {
|
2018-03-16 10:06:35 -07:00
|
|
|
s = Table::Open(options_, file, file_size, &table);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
2018-04-10 16:18:06 -07:00
|
|
|
assert(table == nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
delete file;
|
|
|
|
// We do not cache error results so that if the error is transient,
|
|
|
|
// or somebody repairs the file, we recover automatically.
|
2012-04-17 08:36:46 -07:00
|
|
|
} else {
|
|
|
|
TableAndFile* tf = new TableAndFile;
|
|
|
|
tf->file = file;
|
|
|
|
tf->table = table;
|
|
|
|
*handle = cache_->Insert(key, tf, 1, &DeleteEntry);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2012-04-17 08:36:46 -07:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2012-04-17 08:36:46 -07:00
|
|
|
Iterator* TableCache::NewIterator(const ReadOptions& options,
|
2019-05-02 11:01:00 -07:00
|
|
|
uint64_t file_number, uint64_t file_size,
|
2012-04-17 08:36:46 -07:00
|
|
|
Table** tableptr) {
|
2018-04-10 16:18:06 -07:00
|
|
|
if (tableptr != nullptr) {
|
|
|
|
*tableptr = nullptr;
|
2012-04-17 08:36:46 -07:00
|
|
|
}
|
|
|
|
|
2018-04-10 16:18:06 -07:00
|
|
|
Cache::Handle* handle = nullptr;
|
2012-04-17 08:36:46 -07:00
|
|
|
Status s = FindTable(file_number, file_size, &handle);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return NewErrorIterator(s);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
|
|
|
|
Iterator* result = table->NewIterator(options);
|
|
|
|
result->RegisterCleanup(&UnrefEntry, cache_, handle);
|
2018-04-10 16:18:06 -07:00
|
|
|
if (tableptr != nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
*tableptr = table;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Status TableCache::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&)) {
|
2018-04-10 16:18:06 -07:00
|
|
|
Cache::Handle* handle = nullptr;
|
2012-04-17 08:36:46 -07:00
|
|
|
Status s = FindTable(file_number, file_size, &handle);
|
|
|
|
if (s.ok()) {
|
|
|
|
Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
|
2019-05-02 11:01:00 -07:00
|
|
|
s = t->InternalGet(options, k, arg, handle_result);
|
2012-04-17 08:36:46 -07:00
|
|
|
cache_->Release(handle);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
void TableCache::Evict(uint64_t file_number) {
|
|
|
|
char buf[sizeof(file_number)];
|
|
|
|
EncodeFixed64(buf, file_number);
|
|
|
|
cache_->Erase(Slice(buf, sizeof(buf)));
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|