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_fast(
55 reinterpret_cast<char const*
>(
in) + n,
56 reinterpret_cast<char*
>(
out),
60 Throw<std::runtime_error>(
"lz4 decompress: LZ4_decompress_fast");
64 template <
class BufferFactory>
69 using namespace nudb::detail;
73 auto const out_max = LZ4_compressBound(in_size);
77 auto const out_size = LZ4_compress_default(
78 reinterpret_cast<char const*
>(
in),
79 reinterpret_cast<char*
>(
out + n),
83 Throw<std::runtime_error>(
"lz4 compress");
84 result.
second = n + out_size;
99 template <
class BufferFactory>
103 using namespace nudb::detail;
109 Throw<std::runtime_error>(
"nodeobject decompress");
129 auto const hs = field<std::uint16_t>::size;
130 if (in_size < hs + 32)
131 Throw<std::runtime_error>(
132 "nodeobject codec v1: short inner node size: " +
135 istream is(p, in_size);
137 read<std::uint16_t>(is, mask);
143 write<std::uint32_t>(os, 0);
144 write<std::uint32_t>(os, 0);
146 write<std::uint32_t>(
149 Throw<std::runtime_error>(
150 "nodeobject codec v1: empty inner node");
152 for (
int i = 16; i--; bit >>= 1)
157 Throw<std::runtime_error>(
158 "nodeobject codec v1: short inner node subsize: " +
171 Throw<std::runtime_error>(
172 "nodeobject codec v1: long inner node, in_size = " +
178 if (in_size != 16 * 32)
179 Throw<std::runtime_error>(
180 "nodeobject codec v1: short full inner node, in_size = " +
182 istream is(p, in_size);
187 write<std::uint32_t>(os, 0);
188 write<std::uint32_t>(os, 0);
190 write<std::uint32_t>(
192 write(os, is(512), 512);
196 Throw<std::runtime_error>(
202 template <
class =
void>
210 template <
class BufferFactory>
215 using namespace nudb::detail;
220 istream is(
in, in_size);
225 read<std::uint32_t>(is, index);
226 read<std::uint32_t>(is, unused);
227 read<std::uint8_t>(is, kind);
228 read<std::uint32_t>(is, prefix);
234 for (
unsigned bit = 0x8000; bit; bit >>= 1)
236 void const*
const h = is(32);
247 auto const type = 2U;
249 result.
second = vs + field<std::uint16_t>::size +
255 write<varint>(os, type);
256 write<std::uint16_t>(os, mask);
261 auto const type = 3U;
263 result.
second = vs + n * 32;
268 write<varint>(os, type);
292 result.
second = vn + lzr.second;
296 Throw<std::logic_error>(
306 template <
class =
void>
310 using namespace nudb::detail;
315 istream is(
in, in_size);
320 read<std::uint32_t>(is, index);
321 read<std::uint32_t>(is, unused);
322 read<std::uint8_t>(is, kind);
323 read<std::uint32_t>(is, prefix);
327 write<std::uint32_t>(os, 0);
328 write<std::uint32_t>(os, 0);