rippled
Loading...
Searching...
No Matches
EncodedBlob.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/nodestore/NodeObject.h>
5
6#include <boost/align/align_up.hpp>
7
8#include <algorithm>
9#include <array>
10#include <cstdint>
11
12namespace xrpl {
13namespace NodeStore {
14
36{
39
46 std::array<std::uint8_t, boost::alignment::align_up(9 + 1024, alignof(std::uint32_t))>
48
51
58
59public:
61 : size_([&obj]() {
62 XRPL_ASSERT(obj, "xrpl::NodeStore::EncodedBlob::EncodedBlob : non-null input");
63
64 if (!obj)
65 throw std::runtime_error("EncodedBlob: unseated std::shared_ptr used.");
66
67 return obj->getData().size() + 9;
68 }())
69 , ptr_((size_ <= payload_.size()) ? payload_.data() : new std::uint8_t[size_])
70 {
72 ptr_[8] = static_cast<std::uint8_t>(obj->getType());
73 std::copy_n(obj->getData().data(), obj->getData().size(), ptr_ + 9);
74 std::copy_n(obj->getHash().data(), obj->getHash().size(), key_.data());
75 }
76
78 {
79 XRPL_ASSERT(
80 ((ptr_ == payload_.data()) && (size_ <= payload_.size())) ||
81 ((ptr_ != payload_.data()) && (size_ > payload_.size())),
82 "xrpl::NodeStore::EncodedBlob::~EncodedBlob : valid payload "
83 "pointer");
84
85 if (ptr_ != payload_.data())
86 delete[] ptr_;
87 }
88
89 [[nodiscard]] void const*
90 getKey() const noexcept
91 {
92 return static_cast<void const*>(key_.data());
93 }
94
95 [[nodiscard]] std::size_t
96 getSize() const noexcept
97 {
98 return size_;
99 }
100
101 [[nodiscard]] void const*
102 getData() const noexcept
103 {
104 return static_cast<void const*>(ptr_);
105 }
106};
107
108} // namespace NodeStore
109} // namespace xrpl
Convert a NodeObject from in-memory to database format.
Definition EncodedBlob.h:36
void const * getKey() const noexcept
Definition EncodedBlob.h:90
std::array< std::uint8_t, 32 > key_
The 32-byte key of the serialized object.
Definition EncodedBlob.h:38
std::size_t getSize() const noexcept
Definition EncodedBlob.h:96
std::uint8_t *const ptr_
A pointer to the serialized data.
Definition EncodedBlob.h:57
EncodedBlob(std::shared_ptr< NodeObject > const &obj)
Definition EncodedBlob.h:60
std::uint32_t size_
The size of the serialized data.
Definition EncodedBlob.h:50
std::array< std::uint8_t, boost::alignment::align_up(9+1024, alignof(std::uint32_t))> payload_
A pre-allocated buffer for the serialized object.
Definition EncodedBlob.h:47
void const * getData() const noexcept
T copy_n(T... args)
T data(T... args)
T fill_n(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T size(T... args)