2016-04-23 01:18:33 +02: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.
|
2017-04-28 02:50:56 +02:00
|
|
|
// This source code is also licensed under the GPLv2 license found in the
|
|
|
|
// COPYING file in the root directory of this source tree.
|
2016-04-23 01:18:33 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#include "utilities/persistent_cache/block_cache_tier_metadata.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
bool BlockCacheTierMetadata::Insert(BlockCacheFile* file) {
|
|
|
|
return cache_file_index_.Insert(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockCacheFile* BlockCacheTierMetadata::Lookup(const uint32_t cache_id) {
|
|
|
|
BlockCacheFile* ret = nullptr;
|
|
|
|
BlockCacheFile lookup_key(cache_id);
|
|
|
|
bool ok = cache_file_index_.Find(&lookup_key, &ret);
|
|
|
|
if (ok) {
|
|
|
|
assert(ret->refs_);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockCacheFile* BlockCacheTierMetadata::Evict() {
|
|
|
|
using std::placeholders::_1;
|
|
|
|
auto fn = std::bind(&BlockCacheTierMetadata::RemoveAllKeys, this, _1);
|
|
|
|
return cache_file_index_.Evict(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockCacheTierMetadata::Clear() {
|
|
|
|
cache_file_index_.Clear([](BlockCacheFile* arg){ delete arg; });
|
|
|
|
block_index_.Clear([](BlockInfo* arg){ delete arg; });
|
|
|
|
}
|
|
|
|
|
2016-05-03 20:05:42 +02:00
|
|
|
BlockInfo* BlockCacheTierMetadata::Insert(const Slice& key, const LBA& lba) {
|
|
|
|
std::unique_ptr<BlockInfo> binfo(new BlockInfo(key, lba));
|
|
|
|
if (!block_index_.Insert(binfo.get())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return binfo.release();
|
2016-04-23 01:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockCacheTierMetadata::Lookup(const Slice& key, LBA* lba) {
|
|
|
|
BlockInfo lookup_key(key);
|
|
|
|
BlockInfo* block;
|
|
|
|
port::RWMutex* rlock = nullptr;
|
|
|
|
if (!block_index_.Find(&lookup_key, &block, &rlock)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadUnlock _(rlock);
|
|
|
|
assert(block->key_ == key.ToString());
|
|
|
|
if (lba) {
|
|
|
|
*lba = block->lba_;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInfo* BlockCacheTierMetadata::Remove(const Slice& key) {
|
|
|
|
BlockInfo lookup_key(key);
|
|
|
|
BlockInfo* binfo = nullptr;
|
2016-05-03 20:05:42 +02:00
|
|
|
bool ok __attribute__((__unused__)) = block_index_.Erase(&lookup_key, &binfo);
|
|
|
|
assert(ok);
|
2016-04-23 01:18:33 +02:00
|
|
|
return binfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockCacheTierMetadata::RemoveAllKeys(BlockCacheFile* f) {
|
|
|
|
for (BlockInfo* binfo : f->block_infos()) {
|
|
|
|
BlockInfo* tmp = nullptr;
|
|
|
|
bool status = block_index_.Erase(binfo, &tmp);
|
|
|
|
(void)status;
|
|
|
|
assert(status);
|
|
|
|
assert(tmp == binfo);
|
|
|
|
delete binfo;
|
|
|
|
}
|
|
|
|
f->block_infos().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
#endif
|