]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/table/persistent_cache_helper.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / table / persistent_cache_helper.cc
1 // Copyright (c) 2011-present, 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).
5
6 #include "table/persistent_cache_helper.h"
7
8 #include "table/block_based/block_based_table_reader.h"
9 #include "table/format.h"
10
11 namespace ROCKSDB_NAMESPACE {
12
13 const PersistentCacheOptions PersistentCacheOptions::kEmpty;
14
15 void PersistentCacheHelper::InsertSerialized(
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
21 CacheKey key =
22 BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
23
24 cache_options.persistent_cache->Insert(key.AsSlice(), data, size)
25 .PermitUncheckedError();
26 }
27
28 void PersistentCacheHelper::InsertUncompressed(
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());
33 // Precondition:
34 // (1) content is cacheable
35 // (2) content is not compressed
36
37 CacheKey key =
38 BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
39
40 cache_options.persistent_cache
41 ->Insert(key.AsSlice(), contents.data.data(), contents.data.size())
42 .PermitUncheckedError();
43 ;
44 }
45
46 Status PersistentCacheHelper::LookupSerialized(
47 const PersistentCacheOptions& cache_options, const BlockHandle& handle,
48 std::unique_ptr<char[]>* out_data, const size_t expected_data_size) {
49 #ifdef NDEBUG
50 (void)expected_data_size;
51 #endif
52 assert(cache_options.persistent_cache);
53 assert(cache_options.persistent_cache->IsCompressed());
54
55 CacheKey key =
56 BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
57
58 size_t size;
59 Status s =
60 cache_options.persistent_cache->Lookup(key.AsSlice(), out_data, &size);
61 if (!s.ok()) {
62 // cache miss
63 RecordTick(cache_options.statistics, PERSISTENT_CACHE_MISS);
64 return s;
65 }
66
67 // cache hit
68 // Block-based table is assumed
69 assert(expected_data_size ==
70 handle.size() + BlockBasedTable::kBlockTrailerSize);
71 assert(size == expected_data_size);
72 RecordTick(cache_options.statistics, PERSISTENT_CACHE_HIT);
73 return Status::OK();
74 }
75
76 Status PersistentCacheHelper::LookupUncompressed(
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
87 CacheKey key =
88 BlockBasedTable::GetCacheKey(cache_options.base_cache_key, handle);
89
90 std::unique_ptr<char[]> data;
91 size_t size;
92 Status s =
93 cache_options.persistent_cache->Lookup(key.AsSlice(), &data, &size);
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
107 *contents = BlockContents(std::move(data), size);
108 return Status::OK();
109 }
110
111 } // namespace ROCKSDB_NAMESPACE