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).
11 #include <unordered_map>
13 #include "rocksdb/slice.h"
15 #include "utilities/persistent_cache/block_cache_tier_file.h"
16 #include "utilities/persistent_cache/hash_table.h"
17 #include "utilities/persistent_cache/hash_table_evictable.h"
18 #include "utilities/persistent_cache/lrulist.h"
23 // Block Cache Tier Metadata
25 // The BlockCacheTierMetadata holds all the metadata associated with block
27 // fundamentally contains 2 indexes and an LRU.
31 // This is a forward index that maps a given key to a LBA (Logical Block
32 // Address). LBA is a disk pointer that points to a record on the cache.
34 // LBA = { cache-id, offset, size }
38 // This is a forward index that maps a given cache-id to a cache file object.
39 // Typically you would lookup using LBA and use the object to read or write
41 explicit BlockInfo(const Slice
& key
, const LBA
& lba
= LBA())
42 : key_(key
.ToString()), lba_(lba
) {}
48 class BlockCacheTierMetadata
{
50 explicit BlockCacheTierMetadata(const uint32_t blocks_capacity
= 1024 * 1024,
51 const uint32_t cachefile_capacity
= 10 * 1024)
52 : cache_file_index_(cachefile_capacity
), block_index_(blocks_capacity
) {}
54 virtual ~BlockCacheTierMetadata() {}
56 // Insert a given cache file
57 bool Insert(BlockCacheFile
* file
);
59 // Lookup cache file based on cache_id
60 BlockCacheFile
* Lookup(const uint32_t cache_id
);
62 // Insert block information to block index
63 BlockInfo
* Insert(const Slice
& key
, const LBA
& lba
);
64 // bool Insert(BlockInfo* binfo);
66 // Lookup block information from block index
67 bool Lookup(const Slice
& key
, LBA
* lba
);
69 // Remove a given from the block index
70 BlockInfo
* Remove(const Slice
& key
);
72 // Find and evict a cache file using LRU policy
73 BlockCacheFile
* Evict();
75 // Clear the metadata contents
79 // Remove all block information from a given file
80 virtual void RemoveAllKeys(BlockCacheFile
* file
);
83 // Cache file index definition
85 // cache-id => BlockCacheFile
86 struct BlockCacheFileHash
{
87 uint64_t operator()(const BlockCacheFile
* rec
) {
88 return std::hash
<uint32_t>()(rec
->cacheid());
92 struct BlockCacheFileEqual
{
93 uint64_t operator()(const BlockCacheFile
* lhs
, const BlockCacheFile
* rhs
) {
94 return lhs
->cacheid() == rhs
->cacheid();
98 typedef EvictableHashTable
<BlockCacheFile
, BlockCacheFileHash
,
102 // Block Lookup Index
106 size_t operator()(BlockInfo
* node
) const {
107 return std::hash
<std::string
>()(node
->key_
);
112 size_t operator()(BlockInfo
* lhs
, BlockInfo
* rhs
) const {
113 return lhs
->key_
== rhs
->key_
;
117 typedef HashTable
<BlockInfo
*, Hash
, Equal
> BlockIndexType
;
119 CacheFileIndexType cache_file_index_
;
120 BlockIndexType block_index_
;
123 } // namespace rocksdb