block_cache_tier_metadata.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef ROCKSDB_LITE
  6. #include "utilities/persistent_cache/block_cache_tier_metadata.h"
  7. #include <functional>
  8. namespace ROCKSDB_NAMESPACE {
  9. bool BlockCacheTierMetadata::Insert(BlockCacheFile* file) {
  10. return cache_file_index_.Insert(file);
  11. }
  12. BlockCacheFile* BlockCacheTierMetadata::Lookup(const uint32_t cache_id) {
  13. BlockCacheFile* ret = nullptr;
  14. BlockCacheFile lookup_key(cache_id);
  15. bool ok = cache_file_index_.Find(&lookup_key, &ret);
  16. if (ok) {
  17. assert(ret->refs_);
  18. return ret;
  19. }
  20. return nullptr;
  21. }
  22. BlockCacheFile* BlockCacheTierMetadata::Evict() {
  23. using std::placeholders::_1;
  24. auto fn = std::bind(&BlockCacheTierMetadata::RemoveAllKeys, this, _1);
  25. return cache_file_index_.Evict(fn);
  26. }
  27. void BlockCacheTierMetadata::Clear() {
  28. cache_file_index_.Clear([](BlockCacheFile* arg){ delete arg; });
  29. block_index_.Clear([](BlockInfo* arg){ delete arg; });
  30. }
  31. BlockInfo* BlockCacheTierMetadata::Insert(const Slice& key, const LBA& lba) {
  32. std::unique_ptr<BlockInfo> binfo(new BlockInfo(key, lba));
  33. if (!block_index_.Insert(binfo.get())) {
  34. return nullptr;
  35. }
  36. return binfo.release();
  37. }
  38. bool BlockCacheTierMetadata::Lookup(const Slice& key, LBA* lba) {
  39. BlockInfo lookup_key(key);
  40. BlockInfo* block;
  41. port::RWMutex* rlock = nullptr;
  42. if (!block_index_.Find(&lookup_key, &block, &rlock)) {
  43. return false;
  44. }
  45. ReadUnlock _(rlock);
  46. assert(block->key_ == key.ToString());
  47. if (lba) {
  48. *lba = block->lba_;
  49. }
  50. return true;
  51. }
  52. BlockInfo* BlockCacheTierMetadata::Remove(const Slice& key) {
  53. BlockInfo lookup_key(key);
  54. BlockInfo* binfo = nullptr;
  55. bool ok __attribute__((__unused__));
  56. ok = block_index_.Erase(&lookup_key, &binfo);
  57. assert(ok);
  58. return binfo;
  59. }
  60. void BlockCacheTierMetadata::RemoveAllKeys(BlockCacheFile* f) {
  61. for (BlockInfo* binfo : f->block_infos()) {
  62. BlockInfo* tmp = nullptr;
  63. bool status = block_index_.Erase(binfo, &tmp);
  64. (void)status;
  65. assert(status);
  66. assert(tmp == binfo);
  67. delete binfo;
  68. }
  69. f->block_infos().clear();
  70. }
  71. } // namespace ROCKSDB_NAMESPACE
  72. #endif