]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, 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 | |
6 | #include "table/persistent_cache_helper.h" | |
7 | #include "table/block_based_table_reader.h" | |
8 | #include "table/format.h" | |
9 | ||
10 | namespace rocksdb { | |
11 | ||
12 | void PersistentCacheHelper::InsertRawPage( | |
13 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, | |
14 | const char* data, const size_t size) { | |
15 | assert(cache_options.persistent_cache); | |
16 | assert(cache_options.persistent_cache->IsCompressed()); | |
17 | ||
18 | // construct the page key | |
19 | char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length]; | |
20 | auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(), | |
21 | cache_options.key_prefix.size(), | |
22 | handle, cache_key); | |
23 | // insert content to cache | |
24 | cache_options.persistent_cache->Insert(key, data, size); | |
25 | } | |
26 | ||
27 | void PersistentCacheHelper::InsertUncompressedPage( | |
28 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, | |
29 | const BlockContents& contents) { | |
30 | assert(cache_options.persistent_cache); | |
31 | assert(!cache_options.persistent_cache->IsCompressed()); | |
32 | if (!contents.cachable || contents.compression_type != kNoCompression) { | |
33 | // We shouldn't cache this. Either | |
34 | // (1) content is not cacheable | |
35 | // (2) content is compressed | |
36 | return; | |
37 | } | |
38 | ||
39 | // construct the page key | |
40 | char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length]; | |
41 | auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(), | |
42 | cache_options.key_prefix.size(), | |
43 | handle, cache_key); | |
44 | // insert block contents to page cache | |
45 | cache_options.persistent_cache->Insert(key, contents.data.data(), | |
46 | contents.data.size()); | |
47 | } | |
48 | ||
49 | Status PersistentCacheHelper::LookupRawPage( | |
50 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, | |
51 | std::unique_ptr<char[]>* raw_data, const size_t raw_data_size) { | |
11fdf7f2 TL |
52 | #ifdef NDEBUG |
53 | (void)raw_data_size; | |
54 | #endif | |
7c673cae FG |
55 | assert(cache_options.persistent_cache); |
56 | assert(cache_options.persistent_cache->IsCompressed()); | |
57 | ||
58 | // construct the page key | |
59 | char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length]; | |
60 | auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(), | |
61 | cache_options.key_prefix.size(), | |
62 | handle, cache_key); | |
63 | // Lookup page | |
64 | size_t size; | |
65 | Status s = cache_options.persistent_cache->Lookup(key, raw_data, &size); | |
66 | if (!s.ok()) { | |
67 | // cache miss | |
68 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS); | |
69 | return s; | |
70 | } | |
71 | ||
72 | // cache hit | |
73 | assert(raw_data_size == handle.size() + kBlockTrailerSize); | |
74 | assert(size == raw_data_size); | |
75 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT); | |
76 | return Status::OK(); | |
77 | } | |
78 | ||
79 | Status PersistentCacheHelper::LookupUncompressedPage( | |
80 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, | |
81 | BlockContents* contents) { | |
82 | assert(cache_options.persistent_cache); | |
83 | assert(!cache_options.persistent_cache->IsCompressed()); | |
84 | if (!contents) { | |
85 | // We shouldn't lookup in the cache. Either | |
86 | // (1) Nowhere to store | |
87 | return Status::NotFound(); | |
88 | } | |
89 | ||
90 | // construct the page key | |
91 | char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length]; | |
92 | auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(), | |
93 | cache_options.key_prefix.size(), | |
94 | handle, cache_key); | |
95 | // Lookup page | |
96 | std::unique_ptr<char[]> data; | |
97 | size_t size; | |
98 | Status s = cache_options.persistent_cache->Lookup(key, &data, &size); | |
99 | if (!s.ok()) { | |
100 | // cache miss | |
101 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS); | |
102 | return s; | |
103 | } | |
104 | ||
105 | // please note we are potentially comparing compressed data size with | |
106 | // uncompressed data size | |
107 | assert(handle.size() <= size); | |
108 | ||
109 | // update stats | |
110 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT); | |
111 | // construct result and return | |
112 | *contents = | |
113 | BlockContents(std::move(data), size, false /*cacheable*/, kNoCompression); | |
114 | return Status::OK(); | |
115 | } | |
116 | ||
117 | } // namespace rocksdb |