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