block_cache_tier_metadata.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #pragma once
  6. #ifndef ROCKSDB_LITE
  7. #include <functional>
  8. #include <string>
  9. #include <unordered_map>
  10. #include "rocksdb/slice.h"
  11. #include "utilities/persistent_cache/block_cache_tier_file.h"
  12. #include "utilities/persistent_cache/hash_table.h"
  13. #include "utilities/persistent_cache/hash_table_evictable.h"
  14. #include "utilities/persistent_cache/lrulist.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. //
  17. // Block Cache Tier Metadata
  18. //
  19. // The BlockCacheTierMetadata holds all the metadata associated with block
  20. // cache. It
  21. // fundamentally contains 2 indexes and an LRU.
  22. //
  23. // Block Cache Index
  24. //
  25. // This is a forward index that maps a given key to a LBA (Logical Block
  26. // Address). LBA is a disk pointer that points to a record on the cache.
  27. //
  28. // LBA = { cache-id, offset, size }
  29. //
  30. // Cache File Index
  31. //
  32. // This is a forward index that maps a given cache-id to a cache file object.
  33. // Typically you would lookup using LBA and use the object to read or write
  34. struct BlockInfo {
  35. explicit BlockInfo(const Slice& key, const LBA& lba = LBA())
  36. : key_(key.ToString()), lba_(lba) {}
  37. std::string key_;
  38. LBA lba_;
  39. };
  40. class BlockCacheTierMetadata {
  41. public:
  42. explicit BlockCacheTierMetadata(const uint32_t blocks_capacity = 1024 * 1024,
  43. const uint32_t cachefile_capacity = 10 * 1024)
  44. : cache_file_index_(cachefile_capacity), block_index_(blocks_capacity) {}
  45. virtual ~BlockCacheTierMetadata() {}
  46. // Insert a given cache file
  47. bool Insert(BlockCacheFile* file);
  48. // Lookup cache file based on cache_id
  49. BlockCacheFile* Lookup(const uint32_t cache_id);
  50. // Insert block information to block index
  51. BlockInfo* Insert(const Slice& key, const LBA& lba);
  52. // bool Insert(BlockInfo* binfo);
  53. // Lookup block information from block index
  54. bool Lookup(const Slice& key, LBA* lba);
  55. // Remove a given from the block index
  56. BlockInfo* Remove(const Slice& key);
  57. // Find and evict a cache file using LRU policy
  58. BlockCacheFile* Evict();
  59. // Clear the metadata contents
  60. virtual void Clear();
  61. protected:
  62. // Remove all block information from a given file
  63. virtual void RemoveAllKeys(BlockCacheFile* file);
  64. private:
  65. // Cache file index definition
  66. //
  67. // cache-id => BlockCacheFile
  68. struct BlockCacheFileHash {
  69. uint64_t operator()(const BlockCacheFile* rec) {
  70. return std::hash<uint32_t>()(rec->cacheid());
  71. }
  72. };
  73. struct BlockCacheFileEqual {
  74. uint64_t operator()(const BlockCacheFile* lhs, const BlockCacheFile* rhs) {
  75. return lhs->cacheid() == rhs->cacheid();
  76. }
  77. };
  78. typedef EvictableHashTable<BlockCacheFile, BlockCacheFileHash,
  79. BlockCacheFileEqual>
  80. CacheFileIndexType;
  81. // Block Lookup Index
  82. //
  83. // key => LBA
  84. struct Hash {
  85. size_t operator()(BlockInfo* node) const {
  86. return std::hash<std::string>()(node->key_);
  87. }
  88. };
  89. struct Equal {
  90. size_t operator()(BlockInfo* lhs, BlockInfo* rhs) const {
  91. return lhs->key_ == rhs->key_;
  92. }
  93. };
  94. typedef HashTable<BlockInfo*, Hash, Equal> BlockIndexType;
  95. CacheFileIndexType cache_file_index_;
  96. BlockIndexType block_index_;
  97. };
  98. } // namespace ROCKSDB_NAMESPACE
  99. #endif