NuDBBackend (int keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) : journal_ (journal) , keyBytes_ (keyBytes) , name_ (get<std::string>(keyValues, "path")) , deletePath_(false) , scheduler_ (scheduler) { if (name_.empty()) Throw<std::runtime_error> ( "nodestore: Missing path in NuDB backend"); auto const folder = boost::filesystem::path (name_); boost::filesystem::create_directories (folder); auto const dp = (folder / "nudb.dat").string(); auto const kp = (folder / "nudb.key").string (); auto const lp = (folder / "nudb.log").string (); using beast::nudb::make_salt; api::create (dp, kp, lp, currentType, make_salt(), keyBytes, beast::nudb::block_size(kp), 0.50); try { if (! db_.open (dp, kp, lp, arena_alloc_size)) Throw<std::runtime_error> ("nodestore: open failed"); if (db_.appnum() != currentType) Throw<std::runtime_error> ("nodestore: unknown appnum"); } catch (std::exception const& e) { // log and terminate? std::cerr << e.what(); std::terminate(); } }
void verify() override { auto const dp = db_.dat_path(); auto const kp = db_.key_path(); auto const lp = db_.log_path(); db_.close(); api::verify (dp, kp); db_.open (dp, kp, lp, arena_alloc_size); }
void close() override { if (db_.is_open()) { db_.close(); if (deletePath_) { boost::filesystem::remove_all (name_); } } }
void do_insert (std::shared_ptr <NodeObject> const& no) { EncodedBlob e; e.prepare (no); db_.insert (e.getKey(), e.getData(), e.getSize()); }
void for_each (std::function <void(std::shared_ptr<NodeObject>)> f) override { auto const dp = db_.dat_path(); auto const kp = db_.key_path(); auto const lp = db_.log_path(); //auto const appnum = db_.appnum(); db_.close(); api::visit (dp, [&]( void const* key, std::size_t key_bytes, void const* data, std::size_t size) { DecodedBlob decoded (key, data, size); if (! decoded.wasOk ()) return false; f (decoded.createObject()); return true; }); db_.open (dp, kp, lp, arena_alloc_size); }
Status fetch (void const* key, std::shared_ptr<NodeObject>* pno) override { Status status; pno->reset(); if (! db_.fetch (key, [key, pno, &status](void const* data, std::size_t size) { DecodedBlob decoded (key, data, size); if (! decoded.wasOk ()) { status = dataCorrupt; return; } *pno = decoded.createObject(); status = ok; })) { return notFound; } return status; }