blob_file_cache.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #pragma once
  6. #include <cinttypes>
  7. #include "cache/typed_cache.h"
  8. #include "db/blob/blob_file_reader.h"
  9. #include "rocksdb/rocksdb_namespace.h"
  10. #include "util/mutexlock.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class Cache;
  13. struct ImmutableOptions;
  14. struct FileOptions;
  15. class HistogramImpl;
  16. class Status;
  17. class Slice;
  18. class IOTracer;
  19. class BlobFileCache {
  20. public:
  21. BlobFileCache(Cache* cache, const ImmutableOptions* immutable_options,
  22. const FileOptions* file_options, uint32_t column_family_id,
  23. HistogramImpl* blob_file_read_hist,
  24. const std::shared_ptr<IOTracer>& io_tracer);
  25. BlobFileCache(const BlobFileCache&) = delete;
  26. BlobFileCache& operator=(const BlobFileCache&) = delete;
  27. Status GetBlobFileReader(const ReadOptions& read_options,
  28. uint64_t blob_file_number,
  29. CacheHandleGuard<BlobFileReader>* blob_file_reader);
  30. // Called when a blob file is obsolete to ensure it is removed from the cache
  31. // to avoid effectively leaking the open file and assicated memory
  32. void Evict(uint64_t blob_file_number);
  33. // Used to identify cache entries for blob files (not normally useful)
  34. static const Cache::CacheItemHelper* GetHelper() {
  35. return CacheInterface::GetBasicHelper();
  36. }
  37. private:
  38. using CacheInterface =
  39. BasicTypedCacheInterface<BlobFileReader, CacheEntryRole::kMisc>;
  40. using TypedHandle = CacheInterface::TypedHandle;
  41. CacheInterface cache_;
  42. // Note: mutex_ below is used to guard against multiple threads racing to open
  43. // the same file.
  44. Striped<CacheAlignedWrapper<port::Mutex>> mutex_;
  45. const ImmutableOptions* immutable_options_;
  46. const FileOptions* file_options_;
  47. uint32_t column_family_id_;
  48. HistogramImpl* blob_file_read_hist_;
  49. std::shared_ptr<IOTracer> io_tracer_;
  50. static constexpr size_t kNumberOfMutexStripes = 1 << 7;
  51. };
  52. } // namespace ROCKSDB_NAMESPACE