rippled
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 <ripple/basics/Buffer.h>
24 #include <ripple/nodestore/NodeObject.h>
25 #include <boost/align/align_up.hpp>
26 #include <algorithm>
27 #include <array>
28 #include <cassert>
29 #include <cstdint>
30 
31 namespace ripple {
32 namespace NodeStore {
33 
55 {
58 
65  std::array<
67  boost::alignment::align_up(9 + 1024, alignof(std::uint32_t))>
69 
72 
79 
80 public:
82  : size_([&obj]() {
83  assert(obj);
84 
85  if (!obj)
86  throw std::runtime_error(
87  "EncodedBlob: unseated std::shared_ptr used.");
88 
89  return obj->getData().size() + 9;
90  }())
91  , ptr_(
92  (size_ <= payload_.size()) ? payload_.data()
93  : new std::uint8_t[size_])
94  {
96  ptr_[8] = static_cast<std::uint8_t>(obj->getType());
97  std::copy_n(obj->getData().data(), obj->getData().size(), ptr_ + 9);
98  std::copy_n(obj->getHash().data(), obj->getHash().size(), key_.data());
99  }
100 
102  {
103  assert(
104  ((ptr_ == payload_.data()) && (size_ <= payload_.size())) ||
105  ((ptr_ != payload_.data()) && (size_ > payload_.size())));
106 
107  if (ptr_ != payload_.data())
108  delete[] ptr_;
109  }
110 
111  [[nodiscard]] void const*
112  getKey() const noexcept
113  {
114  return static_cast<void const*>(key_.data());
115  }
116 
117  [[nodiscard]] std::size_t
118  getSize() const noexcept
119  {
120  return size_;
121  }
122 
123  [[nodiscard]] void const*
124  getData() const noexcept
125  {
126  return static_cast<void const*>(ptr_);
127  }
128 };
129 
130 } // namespace NodeStore
131 } // namespace ripple
132 
133 #endif
std::shared_ptr< NodeObject >
std::array::size
T size(T... args)
ripple::NodeStore::EncodedBlob::~EncodedBlob
~EncodedBlob()
Definition: EncodedBlob.h:101
algorithm
std::copy_n
T copy_n(T... args)
ripple::NodeStore::EncodedBlob::EncodedBlob
EncodedBlob(std::shared_ptr< NodeObject > const &obj)
Definition: EncodedBlob.h:81
ripple::NodeStore::EncodedBlob::getData
void const * getData() const noexcept
Definition: EncodedBlob.h:124
array
cstdint
std::runtime_error
STL class.
std::uint8_t
ripple::NodeStore::EncodedBlob::size_
std::uint32_t size_
The size of the serialized data.
Definition: EncodedBlob.h:71
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::EncodedBlob::getSize
std::size_t getSize() const noexcept
Definition: EncodedBlob.h:118
ripple::NodeStore::EncodedBlob
Convert a NodeObject from in-memory to database format.
Definition: EncodedBlob.h:54
cassert
ripple::NodeStore::EncodedBlob::key_
std::array< std::uint8_t, 32 > key_
The 32-byte key of the serialized object.
Definition: EncodedBlob.h:57
std::size_t
ripple::NodeStore::EncodedBlob::payload_
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
ripple::NodeStore::EncodedBlob::ptr_
std::uint8_t *const ptr_
A pointer to the serialized data.
Definition: EncodedBlob.h:78
std::fill_n
T fill_n(T... args)
std::array::data
T data(T... args)
ripple::NodeStore::EncodedBlob::getKey
void const * getKey() const noexcept
Definition: EncodedBlob.h:112