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