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))> payload_;
47
50
57
58public:
60 : size_([&obj]() {
61 XRPL_ASSERT(obj, "xrpl::NodeStore::EncodedBlob::EncodedBlob : non-null input");
62
63 if (!obj)
64 throw std::runtime_error("EncodedBlob: unseated std::shared_ptr used.");
65
66 return obj->getData().size() + 9;
67 }())
68 , ptr_((size_ <= payload_.size()) ? payload_.data() : new std::uint8_t[size_])
69 {
71 ptr_[8] = static_cast<std::uint8_t>(obj->getType());
72 std::copy_n(obj->getData().data(), obj->getData().size(), ptr_ + 9);
73 std::copy_n(obj->getHash().data(), obj->getHash().size(), key_.data());
74 }
75
77 {
78 XRPL_ASSERT(
79 ((ptr_ == payload_.data()) && (size_ <= payload_.size())) ||
80 ((ptr_ != payload_.data()) && (size_ > payload_.size())),
81 "xrpl::NodeStore::EncodedBlob::~EncodedBlob : valid payload "
82 "pointer");
83
84 if (ptr_ != payload_.data())
85 delete[] ptr_;
86 }
87
88 [[nodiscard]] void const*
89 getKey() const noexcept
90 {
91 return static_cast<void const*>(key_.data());
92 }
93
94 [[nodiscard]] std::size_t
95 getSize() const noexcept
96 {
97 return size_;
98 }
99
100 [[nodiscard]] void const*
101 getData() const noexcept
102 {
103 return static_cast<void const*>(ptr_);
104 }
105};
106
107} // namespace NodeStore
108} // namespace xrpl
Convert a NodeObject from in-memory to database format.
Definition EncodedBlob.h:36
void const * getKey() const noexcept
Definition EncodedBlob.h:89
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:95
std::uint8_t *const ptr_
A pointer to the serialized data.
Definition EncodedBlob.h:56
EncodedBlob(std::shared_ptr< NodeObject > const &obj)
Definition EncodedBlob.h:59
std::uint32_t size_
The size of the serialized data.
Definition EncodedBlob.h:49
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:46
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)