rippled
Loading...
Searching...
No Matches
EncodedBlob.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED
21#define RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED
22
23#include <xrpld/nodestore/NodeObject.h>
24
25#include <xrpl/beast/utility/instrumentation.h>
26
27#include <boost/align/align_up.hpp>
28
29#include <algorithm>
30#include <array>
31#include <cstdint>
32
33namespace ripple {
34namespace NodeStore {
35
57{
60
69 boost::alignment::align_up(9 + 1024, alignof(std::uint32_t))>
71
74
81
82public:
84 : size_([&obj]() {
85 XRPL_ASSERT(
86 obj,
87 "ripple::NodeStore::EncodedBlob::EncodedBlob : non-null input");
88
89 if (!obj)
91 "EncodedBlob: unseated std::shared_ptr used.");
92
93 return obj->getData().size() + 9;
94 }())
95 , ptr_(
97 : new std::uint8_t[size_])
98 {
100 ptr_[8] = static_cast<std::uint8_t>(obj->getType());
101 std::copy_n(obj->getData().data(), obj->getData().size(), ptr_ + 9);
102 std::copy_n(obj->getHash().data(), obj->getHash().size(), key_.data());
103 }
104
106 {
107 XRPL_ASSERT(
108 ((ptr_ == payload_.data()) && (size_ <= payload_.size())) ||
109 ((ptr_ != payload_.data()) && (size_ > payload_.size())),
110 "ripple::NodeStore::EncodedBlob::~EncodedBlob : valid payload "
111 "pointer");
112
113 if (ptr_ != payload_.data())
114 delete[] ptr_;
115 }
116
117 [[nodiscard]] void const*
118 getKey() const noexcept
119 {
120 return static_cast<void const*>(key_.data());
121 }
122
123 [[nodiscard]] std::size_t
124 getSize() const noexcept
125 {
126 return size_;
127 }
128
129 [[nodiscard]] void const*
130 getData() const noexcept
131 {
132 return static_cast<void const*>(ptr_);
133 }
134};
135
136} // namespace NodeStore
137} // namespace ripple
138
139#endif
Convert a NodeObject from in-memory to database format.
Definition: EncodedBlob.h:57
std::uint8_t *const ptr_
A pointer to the serialized data.
Definition: EncodedBlob.h:80
EncodedBlob(std::shared_ptr< NodeObject > const &obj)
Definition: EncodedBlob.h:83
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:70
std::uint32_t size_
The size of the serialized data.
Definition: EncodedBlob.h:73
void const * getKey() const noexcept
Definition: EncodedBlob.h:118
std::array< std::uint8_t, 32 > key_
The 32-byte key of the serialized object.
Definition: EncodedBlob.h:59
std::size_t getSize() const noexcept
Definition: EncodedBlob.h:124
void const * getData() const noexcept
Definition: EncodedBlob.h:130
T copy_n(T... args)
T data(T... args)
T fill_n(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T size(T... args)