20 #include <ripple/basics/contract.h>
21 #include <ripple/beast/clock/basic_seconds_clock.h>
22 #include <ripple/beast/core/LexicalCast.h>
23 #include <ripple/beast/rfc2616.h>
24 #include <ripple/beast/unit_test.h>
25 #include <ripple/nodestore/impl/codec.h>
26 #include <boost/beast/core/string.hpp>
27 #include <boost/regex.hpp>
32 #include <nudb/create.hpp>
33 #include <nudb/detail/format.hpp>
34 #include <nudb/xxhasher.hpp>
37 #include <ripple/unity/rocksdb.h>
91 template <
class Rep,
class Period>
109 os << date::round<nanoseconds>(d).count();
125 os << date::round<microseconds>(d).count();
141 os << date::round<milliseconds>(d).count();
157 os << date::round<seconds>(d).count();
173 os << date::round<minutes>(d).count();
180 template <
class Period,
class Rep>
203 bool estimate_ =
false;
215 auto const now = clock_type::now();
219 auto const elapsed = now - start_;
230 auto const rate = elapsed.count() / double(work);
233 log <<
"Remaining: " <<
detail::fmtdur(remain) <<
" (" << work <<
" of "
235 << (work - prev_) <<
" in " <<
detail::fmtdur(now - report_) <<
")";
244 log <<
"Total time: " <<
detail::fmtdur(clock_type::now() - start_);
252 static boost::regex
const re1(
255 "([a-zA-Z][_a-zA-Z0-9]*)"
262 boost::regex_constants::optimize);
265 for (
auto const& kv : v)
268 if (!boost::regex_match(kv, m, re1))
269 Throw<std::runtime_error>(
"invalid parameter " + kv);
270 auto const result = map.
emplace(m[1], m[2]);
272 Throw<std::runtime_error>(
"duplicate parameter " + m[1]);
279 #if RIPPLE_ROCKSDB_AVAILABLE
281 class import_test :
public beast::unit_test::suite
287 testcase(beast::unit_test::abort_on_fail) << arg();
289 using namespace nudb;
290 using namespace nudb::detail;
294 bool usage = args.empty();
296 if (!usage && args.find(
"from") == args.end())
298 log <<
"Missing parameter: from";
301 if (!usage && args.find(
"to") == args.end())
303 log <<
"Missing parameter: to";
306 if (!usage && args.find(
"buffer") == args.end())
308 log <<
"Missing parameter: buffer";
315 <<
"--unittest-arg=from=<from>,to=<to>,buffer=<buffer>\n"
316 <<
"from: RocksDB database to import from\n"
317 <<
"to: NuDB database to import to\n"
318 <<
"buffer: Buffer size (bigger is faster)\n"
319 <<
"NuDB database must not already exist.";
328 auto const from_path = args.at(
"from");
329 auto const to_path = args.at(
"to");
331 using hash_type = nudb::xxhasher;
332 auto const bulk_size = 64 * 1024 * 1024;
333 float const load_factor = 0.5;
335 auto const dp = to_path +
".dat";
336 auto const kp = to_path +
".key";
340 log <<
"from: " << from_path
350 rocksdb::Options options;
351 options.create_if_missing =
false;
352 options.max_open_files = 2000;
353 rocksdb::DB* pdb =
nullptr;
355 rocksdb::DB::OpenForReadOnly(options, from_path, &pdb);
357 Throw<std::runtime_error>(
358 "Can't open '" + from_path +
"': " +
status.ToString());
365 dh.version = currentVersion;
372 df.create(file_mode::append, dp, ec);
374 Throw<nudb::system_error>(ec);
375 bulk_writer<native_file> dw(df, 0, bulk_size);
378 auto os = dw.prepare(dat_file_header::size, ec);
380 Throw<nudb::system_error>(ec);
383 rocksdb::ReadOptions options;
384 options.verify_checksums =
false;
385 options.fill_cache =
false;
389 for (it->SeekToFirst(); it->Valid(); it->Next())
391 if (it->key().size() != 32)
392 Throw<std::runtime_error>(
393 "Unexpected key size " +
395 void const*
const key = it->key().data();
396 void const*
const data = it->value().data();
397 auto const size = it->value().size();
407 BEAST_EXPECT(
check.second == size);
412 auto os = dw.prepare(
413 field<uint48_t>::size +
418 Throw<nudb::system_error>(ec);
419 write<uint48_t>(os,
out.second);
427 Throw<nudb::system_error>(ec);
430 log <<
"Import data: "
432 auto const df_size = df.size(ec);
434 Throw<nudb::system_error>(ec);
437 kh.version = currentVersion;
439 kh.appnum = dh.appnum;
441 kh.salt = make_salt();
442 kh.pepper = pepper<hash_type>(kh.salt);
443 kh.block_size = block_size(kp);
444 kh.load_factor = std::min<std::size_t>(65536.0 * load_factor, 65535);
446 std::ceil(nitems / (bucket_capacity(kh.block_size) * load_factor));
447 kh.modulus = ceil_pow2(kh.buckets);
449 kf.create(file_mode::append, kp, ec);
451 Throw<nudb::system_error>(ec);
452 buffer buf(kh.block_size);
455 ostream os(buf.get(), kh.block_size);
457 kf.write(0, buf.get(), kh.block_size, ec);
459 Throw<nudb::system_error>(ec);
465 std::max<std::size_t>(1, buffer_size / kh.block_size);
466 buf.reserve(buckets * kh.block_size);
467 auto const passes = (kh.buckets + buckets - 1) / buckets;
468 log <<
"items: " << nitems
478 progress p(df_size * passes);
480 for (
std::size_t b0 = 0; b0 < kh.buckets; b0 += buckets)
482 auto const b1 =
std::min(b0 + buckets, kh.buckets);
484 auto const bn = b1 - b0;
488 bucket b(kh.block_size, buf.get() + i * kh.block_size, empty);
492 bulk_reader<native_file> r(
493 df, dat_file_header::size, df_size, bulk_size);
496 auto const offset = r.offset();
499 auto is = r.prepare(field<uint48_t>::size, ec);
501 Throw<nudb::system_error>(ec);
502 read<uint48_t>(is, size);
511 Throw<nudb::system_error>(ec);
513 auto const h = hash<hash_type>(key, kh.key_size, kh.salt);
514 auto const n = bucket_index(h, kh.buckets, kh.modulus);
515 p(log, npass * df_size + r.offset());
516 if (n < b0 || n >= b1)
519 kh.block_size, buf.get() + (n - b0) * kh.block_size);
520 maybe_spill(b, dw, ec);
522 Throw<nudb::system_error>(ec);
523 b.insert(offset, size, h);
529 is = r.prepare(field<std::uint16_t>::size, ec);
531 Throw<nudb::system_error>(ec);
532 read<std::uint16_t>(is, size);
535 Throw<nudb::system_error>(ec);
539 (b0 + 1) * kh.block_size, buf.get(), bn * kh.block_size, ec);
541 Throw<nudb::system_error>(ec);
546 Throw<nudb::system_error>(ec);