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 "db/blob/blob_file_cache.h"
11 #include "db/blob/blob_file_reader.h"
12 #include "options/cf_options.h"
13 #include "rocksdb/cache.h"
14 #include "rocksdb/slice.h"
15 #include "test_util/sync_point.h"
16 #include "trace_replay/io_tracer.h"
17 #include "util/hash.h"
19 namespace ROCKSDB_NAMESPACE
{
21 BlobFileCache::BlobFileCache(Cache
* cache
,
22 const ImmutableOptions
* immutable_options
,
23 const FileOptions
* file_options
,
24 uint32_t column_family_id
,
25 HistogramImpl
* blob_file_read_hist
,
26 const std::shared_ptr
<IOTracer
>& io_tracer
)
28 mutex_(kNumberOfMutexStripes
, kGetSliceNPHash64UnseededFnPtr
),
29 immutable_options_(immutable_options
),
30 file_options_(file_options
),
31 column_family_id_(column_family_id
),
32 blob_file_read_hist_(blob_file_read_hist
),
33 io_tracer_(io_tracer
) {
35 assert(immutable_options_
);
36 assert(file_options_
);
39 Status
BlobFileCache::GetBlobFileReader(
40 uint64_t blob_file_number
,
41 CacheHandleGuard
<BlobFileReader
>* blob_file_reader
) {
42 assert(blob_file_reader
);
43 assert(blob_file_reader
->IsEmpty());
45 const Slice key
= GetSlice(&blob_file_number
);
49 Cache::Handle
* handle
= cache_
->Lookup(key
);
51 *blob_file_reader
= CacheHandleGuard
<BlobFileReader
>(cache_
, handle
);
55 TEST_SYNC_POINT("BlobFileCache::GetBlobFileReader:DoubleCheck");
57 // Check again while holding mutex
58 MutexLock
lock(mutex_
.get(key
));
60 handle
= cache_
->Lookup(key
);
62 *blob_file_reader
= CacheHandleGuard
<BlobFileReader
>(cache_
, handle
);
66 assert(immutable_options_
);
67 Statistics
* const statistics
= immutable_options_
->stats
;
69 RecordTick(statistics
, NO_FILE_OPENS
);
71 std::unique_ptr
<BlobFileReader
> reader
;
74 assert(file_options_
);
75 const Status s
= BlobFileReader::Create(
76 *immutable_options_
, *file_options_
, column_family_id_
,
77 blob_file_read_hist_
, blob_file_number
, io_tracer_
, &reader
);
79 RecordTick(statistics
, NO_FILE_ERRORS
);
85 constexpr size_t charge
= 1;
87 const Status s
= cache_
->Insert(key
, reader
.get(), charge
,
88 &DeleteCacheEntry
<BlobFileReader
>, &handle
);
90 RecordTick(statistics
, NO_FILE_ERRORS
);
97 *blob_file_reader
= CacheHandleGuard
<BlobFileReader
>(cache_
, handle
);
102 } // namespace ROCKSDB_NAMESPACE