block_cache_tier_metadata.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2013, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "utilities/persistent_cache/block_cache_tier_metadata.h"
  6. #include <functional>
  7. namespace ROCKSDB_NAMESPACE {
  8. bool BlockCacheTierMetadata::Insert(BlockCacheFile* file) {
  9. return cache_file_index_.Insert(file);
  10. }
  11. BlockCacheFile* BlockCacheTierMetadata::Lookup(const uint32_t cache_id) {
  12. BlockCacheFile* ret = nullptr;
  13. BlockCacheFile lookup_key(cache_id);
  14. bool ok = cache_file_index_.Find(&lookup_key, &ret);
  15. if (ok) {
  16. assert(ret->refs_);
  17. return ret;
  18. }
  19. return nullptr;
  20. }
  21. BlockCacheFile* BlockCacheTierMetadata::Evict() {
  22. using std::placeholders::_1;
  23. auto fn = std::bind(&BlockCacheTierMetadata::RemoveAllKeys, this, _1);
  24. return cache_file_index_.Evict(fn);
  25. }
  26. void BlockCacheTierMetadata::Clear() {
  27. cache_file_index_.Clear([](BlockCacheFile* arg) { delete arg; });
  28. block_index_.Clear([](BlockInfo* arg) { delete arg; });
  29. }
  30. BlockInfo* BlockCacheTierMetadata::Insert(const Slice& key, const LBA& lba) {
  31. std::unique_ptr<BlockInfo> binfo(new BlockInfo(key, lba));
  32. if (!block_index_.Insert(binfo.get())) {
  33. return nullptr;
  34. }
  35. return binfo.release();
  36. }
  37. bool BlockCacheTierMetadata::Lookup(const Slice& key, LBA* lba) {
  38. BlockInfo lookup_key(key);
  39. BlockInfo* block;
  40. port::RWMutex* rlock = nullptr;
  41. if (!block_index_.Find(&lookup_key, &block, &rlock)) {
  42. return false;
  43. }
  44. ReadUnlock _(rlock);
  45. assert(block->key_ == key.ToString());
  46. if (lba) {
  47. *lba = block->lba_;
  48. }
  49. return true;
  50. }
  51. BlockInfo* BlockCacheTierMetadata::Remove(const Slice& key) {
  52. BlockInfo lookup_key(key);
  53. BlockInfo* binfo = nullptr;
  54. bool ok __attribute__((__unused__));
  55. ok = block_index_.Erase(&lookup_key, &binfo);
  56. assert(ok);
  57. return binfo;
  58. }
  59. void BlockCacheTierMetadata::RemoveAllKeys(BlockCacheFile* f) {
  60. for (BlockInfo* binfo : f->block_infos()) {
  61. BlockInfo* tmp = nullptr;
  62. bool status = block_index_.Erase(binfo, &tmp);
  63. (void)status;
  64. assert(status);
  65. assert(tmp == binfo);
  66. delete binfo;
  67. }
  68. f->block_infos().clear();
  69. }
  70. } // namespace ROCKSDB_NAMESPACE