blob_contents.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  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 <memory>
  7. #include "memory/memory_allocator_impl.h"
  8. #include "rocksdb/advanced_cache.h"
  9. #include "rocksdb/rocksdb_namespace.h"
  10. #include "rocksdb/slice.h"
  11. #include "rocksdb/status.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. // A class representing a single uncompressed value read from a blob file.
  14. class BlobContents {
  15. public:
  16. BlobContents(CacheAllocationPtr&& allocation, size_t size)
  17. : allocation_(std::move(allocation)), data_(allocation_.get(), size) {}
  18. BlobContents(const BlobContents&) = delete;
  19. BlobContents& operator=(const BlobContents&) = delete;
  20. BlobContents(BlobContents&&) = default;
  21. BlobContents& operator=(BlobContents&&) = default;
  22. ~BlobContents() = default;
  23. const Slice& data() const { return data_; }
  24. size_t size() const { return data_.size(); }
  25. size_t ApproximateMemoryUsage() const;
  26. // For TypedCacheInterface
  27. const Slice& ContentSlice() const { return data_; }
  28. static constexpr CacheEntryRole kCacheEntryRole = CacheEntryRole::kBlobValue;
  29. private:
  30. CacheAllocationPtr allocation_;
  31. Slice data_;
  32. };
  33. class BlobContentsCreator : public Cache::CreateContext {
  34. public:
  35. static void Create(std::unique_ptr<BlobContents>* out, size_t* out_charge,
  36. const Slice& contents, CompressionType /*type*/,
  37. MemoryAllocator* alloc) {
  38. auto raw = new BlobContents(AllocateAndCopyBlock(contents, alloc),
  39. contents.size());
  40. out->reset(raw);
  41. if (out_charge) {
  42. *out_charge = raw->ApproximateMemoryUsage();
  43. }
  44. }
  45. };
  46. } // namespace ROCKSDB_NAMESPACE