/src/rocksdb/db/blob/blob_contents.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
6 | | #pragma once |
7 | | |
8 | | #include <memory> |
9 | | |
10 | | #include "memory/memory_allocator_impl.h" |
11 | | #include "rocksdb/advanced_cache.h" |
12 | | #include "rocksdb/rocksdb_namespace.h" |
13 | | #include "rocksdb/slice.h" |
14 | | #include "rocksdb/status.h" |
15 | | |
16 | | namespace ROCKSDB_NAMESPACE { |
17 | | |
18 | | // A class representing a single uncompressed value read from a blob file. |
19 | | class BlobContents { |
20 | | public: |
21 | | BlobContents(CacheAllocationPtr&& allocation, size_t size) |
22 | 0 | : allocation_(std::move(allocation)), data_(allocation_.get(), size) {} |
23 | | |
24 | | BlobContents(const BlobContents&) = delete; |
25 | | BlobContents& operator=(const BlobContents&) = delete; |
26 | | |
27 | | BlobContents(BlobContents&&) = default; |
28 | | BlobContents& operator=(BlobContents&&) = default; |
29 | | |
30 | 0 | ~BlobContents() = default; |
31 | | |
32 | 0 | const Slice& data() const { return data_; } |
33 | 0 | size_t size() const { return data_.size(); } |
34 | | |
35 | | size_t ApproximateMemoryUsage() const; |
36 | | |
37 | | // For TypedCacheInterface |
38 | 0 | const Slice& ContentSlice() const { return data_; } |
39 | | static constexpr CacheEntryRole kCacheEntryRole = CacheEntryRole::kBlobValue; |
40 | | |
41 | | private: |
42 | | CacheAllocationPtr allocation_; |
43 | | Slice data_; |
44 | | }; |
45 | | |
46 | | class BlobContentsCreator : public Cache::CreateContext { |
47 | | public: |
48 | | static void Create(std::unique_ptr<BlobContents>* out, size_t* out_charge, |
49 | | const Slice& contents, CompressionType /*type*/, |
50 | 0 | MemoryAllocator* alloc) { |
51 | 0 | auto raw = new BlobContents(AllocateAndCopyBlock(contents, alloc), |
52 | 0 | contents.size()); |
53 | 0 | out->reset(raw); |
54 | 0 | if (out_charge) { |
55 | 0 | *out_charge = raw->ApproximateMemoryUsage(); |
56 | 0 | } |
57 | 0 | } |
58 | | }; |
59 | | |
60 | | } // namespace ROCKSDB_NAMESPACE |