]>
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" | |
1e59de90 | 7 | |
f67539c2 | 8 | #include "table/block_based/block_based_table_reader.h" |
7c673cae FG |
9 | #include "table/format.h" |
10 | ||
f67539c2 | 11 | namespace ROCKSDB_NAMESPACE { |
7c673cae | 12 | |
1e59de90 TL |
13 | const PersistentCacheOptions PersistentCacheOptions::kEmpty; |
14 | ||
15 | void PersistentCacheHelper::InsertSerialized( | |
7c673cae FG |
16 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, |
17 | const char* data, const size_t size) { | |
18 | assert(cache_options.persistent_cache); | |
19 | assert(cache_options.persistent_cache->IsCompressed()); | |
20 | ||
1e59de90 TL |
21 | CacheKey key = |
22 | BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle); | |
23 | ||
24 | cache_options.persistent_cache->Insert(key.AsSlice(), data, size) | |
20effc67 | 25 | .PermitUncheckedError(); |
7c673cae FG |
26 | } |
27 | ||
1e59de90 | 28 | void PersistentCacheHelper::InsertUncompressed( |
7c673cae FG |
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 | 36 | |
1e59de90 TL |
37 | CacheKey key = |
38 | BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle); | |
39 | ||
20effc67 | 40 | cache_options.persistent_cache |
1e59de90 | 41 | ->Insert(key.AsSlice(), contents.data.data(), contents.data.size()) |
20effc67 TL |
42 | .PermitUncheckedError(); |
43 | ; | |
7c673cae FG |
44 | } |
45 | ||
1e59de90 | 46 | Status PersistentCacheHelper::LookupSerialized( |
7c673cae | 47 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, |
1e59de90 | 48 | std::unique_ptr<char[]>* out_data, const size_t expected_data_size) { |
11fdf7f2 | 49 | #ifdef NDEBUG |
1e59de90 | 50 | (void)expected_data_size; |
11fdf7f2 | 51 | #endif |
7c673cae FG |
52 | assert(cache_options.persistent_cache); |
53 | assert(cache_options.persistent_cache->IsCompressed()); | |
54 | ||
1e59de90 TL |
55 | CacheKey key = |
56 | BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle); | |
57 | ||
7c673cae | 58 | size_t size; |
1e59de90 TL |
59 | Status s = |
60 | cache_options.persistent_cache->Lookup(key.AsSlice(), out_data, &size); | |
7c673cae FG |
61 | if (!s.ok()) { |
62 | // cache miss | |
63 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS); | |
64 | return s; | |
65 | } | |
66 | ||
67 | // cache hit | |
1e59de90 TL |
68 | // Block-based table is assumed |
69 | assert(expected_data_size == | |
70 | handle.size() + BlockBasedTable::kBlockTrailerSize); | |
71 | assert(size == expected_data_size); | |
7c673cae FG |
72 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT); |
73 | return Status::OK(); | |
74 | } | |
75 | ||
1e59de90 | 76 | Status PersistentCacheHelper::LookupUncompressed( |
7c673cae FG |
77 | const PersistentCacheOptions& cache_options, const BlockHandle& handle, |
78 | BlockContents* contents) { | |
79 | assert(cache_options.persistent_cache); | |
80 | assert(!cache_options.persistent_cache->IsCompressed()); | |
81 | if (!contents) { | |
82 | // We shouldn't lookup in the cache. Either | |
83 | // (1) Nowhere to store | |
84 | return Status::NotFound(); | |
85 | } | |
86 | ||
1e59de90 TL |
87 | CacheKey key = |
88 | BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle); | |
89 | ||
7c673cae FG |
90 | std::unique_ptr<char[]> data; |
91 | size_t size; | |
1e59de90 TL |
92 | Status s = |
93 | cache_options.persistent_cache->Lookup(key.AsSlice(), &data, &size); | |
7c673cae FG |
94 | if (!s.ok()) { |
95 | // cache miss | |
96 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS); | |
97 | return s; | |
98 | } | |
99 | ||
100 | // please note we are potentially comparing compressed data size with | |
101 | // uncompressed data size | |
102 | assert(handle.size() <= size); | |
103 | ||
104 | // update stats | |
105 | RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT); | |
106 | // construct result and return | |
494da23a | 107 | *contents = BlockContents(std::move(data), size); |
7c673cae FG |
108 | return Status::OK(); |
109 | } | |
110 | ||
f67539c2 | 111 | } // namespace ROCKSDB_NAMESPACE |