#pragma once #include #include namespace MWEB { class DBBatch : public mw::DBBatch { public: DBBatch(CDBWrapper* pDB, const std::shared_ptr& pBatch) : m_pDB(pDB), m_pBatch(pBatch) {} void Write(const std::string& key, const std::vector& value) final { m_pBatch->Write(key, value); } void Erase(const std::string& key) final { m_pBatch->Erase(key); } void Commit() final { m_pDB->WriteBatch(*m_pBatch); } private: CDBWrapper* m_pDB; std::shared_ptr m_pBatch; }; class DBIterator : public mw::DBIterator { public: explicit DBIterator(CDBIterator* pIterator) : m_pIterator(std::unique_ptr(pIterator)) {} void Seek(const std::string& key) final { m_pIterator->Seek(key); } void Next() final { m_pIterator->Next(); } bool GetKey(std::string& key) const final { return m_pIterator->GetKey(key); } bool Valid() const final { return m_pIterator->Valid(); } private: std::unique_ptr m_pIterator; }; class DBWrapper : public mw::DBWrapper { public: explicit DBWrapper(CDBWrapper* pDB) : m_pDB(pDB) {} bool Read(const std::string& key, std::vector& value) const final { return m_pDB->Read(key, value); } std::unique_ptr NewIterator() final { return std::unique_ptr(new MWEB::DBIterator(m_pDB->NewIterator())); } std::unique_ptr CreateBatch() final { return std::unique_ptr(new MWEB::DBBatch(m_pDB, std::make_shared(*m_pDB))); } private: CDBWrapper* m_pDB; }; } // namespace MWEB