]>
git.proxmox.com Git - ceph.git/blob - 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).
6 #include "table/persistent_cache_helper.h"
7 #include "table/block_based_table_reader.h"
8 #include "table/format.h"
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());
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(),
23 // insert content to cache
24 cache_options
.persistent_cache
->Insert(key
, data
, size
);
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
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(),
44 // insert block contents to page cache
45 cache_options
.persistent_cache
->Insert(key
, contents
.data
.data(),
46 contents
.data
.size());
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
) {
55 assert(cache_options
.persistent_cache
);
56 assert(cache_options
.persistent_cache
->IsCompressed());
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(),
65 Status s
= cache_options
.persistent_cache
->Lookup(key
, raw_data
, &size
);
68 RecordTick(cache_options
.statistics
, PERSISTENT_CACHE_MISS
);
73 assert(raw_data_size
== handle
.size() + kBlockTrailerSize
);
74 assert(size
== raw_data_size
);
75 RecordTick(cache_options
.statistics
, PERSISTENT_CACHE_HIT
);
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());
85 // We shouldn't lookup in the cache. Either
86 // (1) Nowhere to store
87 return Status::NotFound();
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(),
96 std::unique_ptr
<char[]> data
;
98 Status s
= cache_options
.persistent_cache
->Lookup(key
, &data
, &size
);
101 RecordTick(cache_options
.statistics
, PERSISTENT_CACHE_MISS
);
105 // please note we are potentially comparing compressed data size with
106 // uncompressed data size
107 assert(handle
.size() <= size
);
110 RecordTick(cache_options
.statistics
, PERSISTENT_CACHE_HIT
);
111 // construct result and return
113 BlockContents(std::move(data
), size
, false /*cacheable*/, kNoCompression
);
117 } // namespace rocksdb