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 <xrpl/beast/utility/instrumentation.h>
24#include <xrpl/nodestore/NodeObject.h>
25
26#include <boost/align/align_up.hpp>
27
28#include <algorithm>
29#include <array>
30#include <cstdint>
31
32namespace ripple {
33namespace NodeStore {
34
56{
59
68 boost::alignment::align_up(9 + 1024, alignof(std::uint32_t))>
70
73
80
81public:
83 : size_([&obj]() {
84 XRPL_ASSERT(
85 obj,
86 "ripple::NodeStore::EncodedBlob::EncodedBlob : non-null input");
87
88 if (!obj)
90 "EncodedBlob: unseated std::shared_ptr used.");
91
92 return obj->getData().size() + 9;
93 }())
94 , ptr_(
96 : new std::uint8_t[size_])
97 {
99 ptr_[8] = static_cast<std::uint8_t>(obj->getType());
100 std::copy_n(obj->getData().data(), obj->getData().size(), ptr_ + 9);
101 std::copy_n(obj->getHash().data(), obj->getHash().size(), key_.data());
102 }
103
105 {
106 XRPL_ASSERT(
107 ((ptr_ == payload_.data()) && (size_ <= payload_.size())) ||
108 ((ptr_ != payload_.data()) && (size_ > payload_.size())),
109 "ripple::NodeStore::EncodedBlob::~EncodedBlob : valid payload "
110 "pointer");
111
112 if (ptr_ != payload_.data())
113 delete[] ptr_;
114 }
115
116 [[nodiscard]] void const*
117 getKey() const noexcept
118 {
119 return static_cast<void const*>(key_.data());
120 }
121
122 [[nodiscard]] std::size_t
123 getSize() const noexcept
124 {
125 return size_;
126 }
127
128 [[nodiscard]] void const*
129 getData() const noexcept
130 {
131 return static_cast<void const*>(ptr_);
132 }
133};
134
135} // namespace NodeStore
136} // namespace ripple
137
138#endif
Convert a NodeObject from in-memory to database format.
Definition EncodedBlob.h:56
std::uint8_t *const ptr_
A pointer to the serialized data.
Definition EncodedBlob.h:79
EncodedBlob(std::shared_ptr< NodeObject > const &obj)
Definition EncodedBlob.h:82
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:69
std::uint32_t size_
The size of the serialized data.
Definition EncodedBlob.h:72
void const * getKey() const noexcept
std::array< std::uint8_t, 32 > key_
The 32-byte key of the serialized object.
Definition EncodedBlob.h:58
std::size_t getSize() const noexcept
void const * getData() const noexcept
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:25
STL namespace.
T size(T... args)