github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/fs/bc_storage_session.cpp (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #include "fs/bc_storage_session.h" 21 22 namespace neb { 23 namespace fs { 24 bc_storage_session::bc_storage_session() : m_storage(), m_mutex() { 25 m_storage = std::make_unique<rocksdb_storage>(); 26 m_init_already = false; 27 } 28 29 bc_storage_session::~bc_storage_session() { 30 boost::unique_lock<boost::shared_mutex> _l(m_mutex); 31 m_storage->close_database(); 32 } 33 34 void bc_storage_session::init(const std::string &path, 35 enum storage_open_flag flag) { 36 boost::unique_lock<boost::shared_mutex> _l(m_mutex); 37 if (m_init_already) 38 return; 39 m_init_already = true; 40 m_open_flag = flag; 41 m_path = path; 42 m_storage->open_database(m_path, m_open_flag); 43 } 44 45 bytes bc_storage_session::get_bytes(const bytes &key) { 46 boost::shared_lock<boost::shared_mutex> _l(m_mutex); 47 bool no_exception = true; 48 bool tried_already = false; 49 while (no_exception) { 50 if (tried_already) { 51 return m_storage->get_bytes(key); 52 } else { 53 try { 54 return m_storage->get_bytes(key); 55 } catch (...) { 56 tried_already = true; 57 _l.unlock(); 58 m_mutex.lock(); 59 m_storage->close_database(); 60 m_storage->open_database(m_path, m_open_flag); 61 m_mutex.unlock(); 62 _l.lock(); 63 } 64 } 65 } 66 return bytes(); 67 } 68 69 void bc_storage_session::put_bytes(const bytes &key, const bytes &val) { 70 boost::shared_lock<boost::shared_mutex> _l(m_mutex); 71 m_storage->put_bytes(key, val); 72 } 73 } // namespace fs 74 } // namespace neb