20 #ifndef RIPPLE_NODESTORE_CODEC_H_INCLUDED
21 #define RIPPLE_NODESTORE_CODEC_H_INCLUDED
24 #define LZ4_DISABLE_DEPRECATE_WARNINGS
26 #include <ripple/basics/contract.h>
27 #include <ripple/basics/safe_cast.h>
28 #include <ripple/nodestore/NodeObject.h>
29 #include <ripple/nodestore/impl/varint.h>
30 #include <ripple/protocol/HashPrefix.h>
34 #include <nudb/detail/field.hpp>
41 template <
class BufferFactory>
46 using namespace nudb::detail;
51 Throw<std::runtime_error>(
"lz4 decompress: n == 0");
54 if (LZ4_decompress_safe(
55 reinterpret_cast<char const*
>(
in) + n,
56 reinterpret_cast<char*
>(
out),
59 Throw<std::runtime_error>(
"lz4 decompress: LZ4_decompress_safe");
63 template <
class BufferFactory>
68 using namespace nudb::detail;
72 auto const out_max = LZ4_compressBound(in_size);
76 auto const out_size = LZ4_compress_default(
77 reinterpret_cast<char const*
>(
in),
78 reinterpret_cast<char*
>(
out + n),
82 Throw<std::runtime_error>(
"lz4 compress");
83 result.
second = n + out_size;
98 template <
class BufferFactory>
102 using namespace nudb::detail;
108 Throw<std::runtime_error>(
"nodeobject decompress");
128 auto const hs = field<std::uint16_t>::size;
129 if (in_size < hs + 32)
130 Throw<std::runtime_error>(
131 "nodeobject codec v1: short inner node size: " +
134 istream is(p, in_size);
136 read<std::uint16_t>(is, mask);
142 write<std::uint32_t>(os, 0);
143 write<std::uint32_t>(os, 0);
145 write<std::uint32_t>(
148 Throw<std::runtime_error>(
149 "nodeobject codec v1: empty inner node");
151 for (
int i = 16; i--; bit >>= 1)
156 Throw<std::runtime_error>(
157 "nodeobject codec v1: short inner node subsize: " +
170 Throw<std::runtime_error>(
171 "nodeobject codec v1: long inner node, in_size = " +
177 if (in_size != 16 * 32)
178 Throw<std::runtime_error>(
179 "nodeobject codec v1: short full inner node, in_size = " +
181 istream is(p, in_size);
186 write<std::uint32_t>(os, 0);
187 write<std::uint32_t>(os, 0);
189 write<std::uint32_t>(
191 write(os, is(512), 512);
195 Throw<std::runtime_error>(
201 template <
class =
void>
209 template <
class BufferFactory>
214 using namespace nudb::detail;
219 istream is(
in, in_size);
224 read<std::uint32_t>(is, index);
225 read<std::uint32_t>(is, unused);
226 read<std::uint8_t>(is, kind);
227 read<std::uint32_t>(is, prefix);
233 for (
unsigned bit = 0x8000; bit; bit >>= 1)
235 void const*
const h = is(32);
246 auto const type = 2U;
248 result.
second = vs + field<std::uint16_t>::size +
254 write<varint>(os, type);
255 write<std::uint16_t>(os, mask);
260 auto const type = 3U;
262 result.
second = vs + n * 32;
267 write<varint>(os, type);
291 result.
second = vn + lzr.second;
295 Throw<std::logic_error>(
305 template <
class =
void>
309 using namespace nudb::detail;
314 istream is(
in, in_size);
319 read<std::uint32_t>(is, index);
320 read<std::uint32_t>(is, unused);
321 read<std::uint8_t>(is, kind);
322 read<std::uint32_t>(is, prefix);
326 write<std::uint32_t>(os, 0);
327 write<std::uint32_t>(os, 0);