]>
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"
8 #include "table/block_based/block_based_table_reader.h"
9 #include "table/format.h"
11 namespace ROCKSDB_NAMESPACE
{
13 const PersistentCacheOptions
PersistentCacheOptions::kEmpty
;
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());
22 BlockBasedTable::GetCacheKey(cache_options
.base_cache_key
, handle
);
24 cache_options
.persistent_cache
->Insert(key
.AsSlice(), data
, size
)
25 .PermitUncheckedError();
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());
34 // (1) content is cacheable
35 // (2) content is not compressed
38 BlockBasedTable::GetCacheKey(cache_options
.base_cache_key
, handle
);
40 cache_options
.persistent_cache
41 ->Insert(key
.AsSlice(), contents
.data
.data(), contents
.data
.size())
42 .PermitUncheckedError();
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
) {
50 (void)expected_data_size
;
52 assert(cache_options
.persistent_cache
);
53 assert(cache_options
.persistent_cache
->IsCompressed());
56 BlockBasedTable::GetCacheKey(cache_options
.base_cache_key
, handle
);
60 cache_options
.persistent_cache
->Lookup(key
.AsSlice(), out_data
, &size
);
63 RecordTick(cache_options
.statistics
, PERSISTENT_CACHE_MISS
);
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
);
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());
82 // We shouldn't lookup in the cache. Either
83 // (1) Nowhere to store
84 return Status::NotFound();
88 BlockBasedTable::GetCacheKey(cache_options
.base_cache_key
, handle
);
90 std::unique_ptr
<char[]> data
;
93 cache_options
.persistent_cache
->Lookup(key
.AsSlice(), &data
, &size
);
96 RecordTick(cache_options
.statistics
, PERSISTENT_CACHE_MISS
);
100 // please note we are potentially comparing compressed data size with
101 // uncompressed data size
102 assert(handle
.size() <= size
);
105 RecordTick(cache_options
.statistics
, PERSISTENT_CACHE_HIT
);
106 // construct result and return
107 *contents
= BlockContents(std::move(data
), size
);
111 } // namespace ROCKSDB_NAMESPACE