]>
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" | |
f67539c2 | 7 | #include "table/block_based/block_based_table_reader.h" |
7c673cae FG |
8 | #include "table/format.h" |
9 | ||
f67539c2 | 10 | namespace ROCKSDB_NAMESPACE { |
7c673cae FG |
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 | |
20effc67 TL |
24 | cache_options.persistent_cache->Insert(key, data, size) |
25 | .PermitUncheckedError(); | |
7c673cae FG |
26 | } |
27 | ||
28 | void PersistentCacheHelper::InsertUncompressedPage( | |
29 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, | |
30 | const BlockContents& contents) { | |
31 | assert(cache_options.persistent_cache); | |
32 | assert(!cache_options.persistent_cache->IsCompressed()); | |
494da23a TL |
33 | // Precondition: |
34 | // (1) content is cacheable | |
35 | // (2) content is not compressed | |
7c673cae FG |
36 | |
37 | // construct the page key | |
38 | char cache_key[BlockBasedTable::kMaxCacheKeyPrefixSize + kMaxVarint64Length]; | |
39 | auto key = BlockBasedTable::GetCacheKey(cache_options.key_prefix.c_str(), | |
40 | cache_options.key_prefix.size(), | |
41 | handle, cache_key); | |
42 | // insert block contents to page cache | |
20effc67 TL |
43 | cache_options.persistent_cache |
44 | ->Insert(key, contents.data.data(), contents.data.size()) | |
45 | .PermitUncheckedError(); | |
46 | ; | |
7c673cae FG |
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 | |
494da23a | 112 | *contents = BlockContents(std::move(data), size); |
7c673cae FG |
113 | return Status::OK(); |
114 | } | |
115 | ||
f67539c2 | 116 | } // namespace ROCKSDB_NAMESPACE |