void for_each (std::function <void(NodeObject::Ptr)> f) { rocksdb::ReadOptions const options; std::unique_ptr <rocksdb::Iterator> it (m_db->NewIterator (options)); for (it->SeekToFirst (); it->Valid (); it->Next ()) { if (it->key ().size () == m_keyBytes) { DecodedBlob decoded (it->key ().data (), it->value ().data (), it->value ().size ()); if (decoded.wasOk ()) { f (decoded.createObject ()); } else { // Uh oh, corrupted data! if (m_journal.fatal) m_journal.fatal << "Corrupt NodeObject #" << uint256 (it->key ().data ()); } } else { // VFALCO NOTE What does it mean to find an // incorrectly sized key? Corruption? if (m_journal.fatal) m_journal.fatal << "Bad key size = " << it->key ().size (); } } }
Status fetch (void const* key, NodeObject::Ptr* pObject) { pObject->reset (); Status status (ok); MDB_txn* txn = nullptr; int error = 0; error = mdb_txn_begin (m_env, NULL, MDB_RDONLY, &txn); if (error == 0) { MDB_val dbkey; MDB_val data; dbkey.mv_size = m_keyBytes; dbkey.mv_data = mdb_cast (key); error = mdb_get (txn, m_dbi, &dbkey, &data); if (error == 0) { DecodedBlob decoded (key, data.mv_data, data.mv_size); if (decoded.wasOk ()) { *pObject = decoded.createObject (); } else { status = dataCorrupt; } } else if (error == MDB_NOTFOUND) { status = notFound; } else { status = unknown; WriteLog (lsWARNING, NodeObject) << "MDB txn failed, code=" << error; } mdb_txn_abort (txn); } else { status = unknown; WriteLog (lsWARNING, NodeObject) << "MDB txn failed, code=" << error; } return status; }
Status fetch (void const* key, NodeObject::Ptr* pObject) { pObject->reset (); Status status (ok); rocksdb::ReadOptions const options; rocksdb::Slice const slice (static_cast <char const*> (key), m_keyBytes); std::string string; rocksdb::Status getStatus = m_db->Get (options, slice, &string); if (getStatus.ok ()) { DecodedBlob decoded (key, string.data (), string.size ()); if (decoded.wasOk ()) { *pObject = decoded.createObject (); } else { // Decoding failed, probably corrupted! // status = dataCorrupt; } } else { if (getStatus.IsCorruption ()) { status = dataCorrupt; } else if (getStatus.IsNotFound ()) { status = notFound; } else { status = Status (customCode + getStatus.code()); m_journal.error << getStatus.ToString (); } } return status; }
// Checks encoding/decoding blobs void testBlobs (int64 const seedValue) { beginTestCase ("encoding"); Batch batch; createPredictableBatch (batch, 0, numObjectsToTest, seedValue); EncodedBlob encoded; for (int i = 0; i < batch.size (); ++i) { encoded.prepare (batch [i]); DecodedBlob decoded (encoded.getKey (), encoded.getData (), encoded.getSize ()); expect (decoded.wasOk (), "Should be ok"); if (decoded.wasOk ()) { NodeObject::Ptr const object (decoded.createObject ()); expect (batch [i]->isCloneOf (object), "Should be clones"); } } }
Status fetch (void const* key, NodeObject::Ptr* pObject) { pObject->reset (); Status status (unknown); void* v (nullptr); std::size_t vsize; int rc (sp_get (m_db, key, m_keyBytes, &v, &vsize)); if (rc == 1) { DecodedBlob decoded (key, v, vsize); if (decoded.wasOk ()) { *pObject = decoded.createObject (); status = ok; } else { status = dataCorrupt; } ::free (v); } else if (rc == 0) { status = notFound; } else { String s; s << "Sophia failed with error code " << rc; Throw (std::runtime_error (s.toStdString()), __FILE__, __LINE__); status = notFound; } return status; }
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; }