/* * Copyright (c) 2015 Cryptonomex, Inc., and contributors. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Any modified source or binaries are used only with the BitShares network. * * 2. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * * 3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include #include #include #include namespace graphene { namespace db { object_database::object_database() :_undo_db(*this) { _index.resize(255); _undo_db.enable(); } object_database::~object_database(){} void object_database::close() { } const object* object_database::find_object( object_id_type id )const { return get_index(id.space(),id.type()).find( id ); } const object& object_database::get_object( object_id_type id )const { return get_index(id.space(),id.type()).get( id ); } const index& object_database::get_index(uint8_t space_id, uint8_t type_id)const { FC_ASSERT( _index.size() > space_id, "", ("space_id",space_id)("type_id",type_id)("index.size",_index.size()) ); FC_ASSERT( _index[space_id].size() > type_id, "", ("space_id",space_id)("type_id",type_id)("index[space_id].size",_index[space_id].size()) ); const auto& tmp = _index[space_id][type_id]; FC_ASSERT( tmp ); return *tmp; } index& object_database::get_mutable_index(uint8_t space_id, uint8_t type_id) { FC_ASSERT( _index.size() > space_id, "", ("space_id",space_id)("type_id",type_id)("index.size",_index.size()) ); FC_ASSERT( _index[space_id].size() > type_id , "", ("space_id",space_id)("type_id",type_id)("index[space_id].size",_index[space_id].size()) ); const auto& idx = _index[space_id][type_id]; FC_ASSERT( idx, "", ("space",space_id)("type",type_id) ); return *idx; } void object_database::flush() { // ilog("Save object_database in ${d}", ("d", _data_dir)); for( uint32_t space = 0; space < _index.size(); ++space ) { fc::create_directories( _data_dir / "object_database" / fc::to_string(space) ); const auto types = _index[space].size(); for( uint32_t type = 0; type < types; ++type ) if( _index[space][type] ) _index[space][type]->save( _data_dir / "object_database" / fc::to_string(space)/fc::to_string(type) ); } } void object_database::wipe(const fc::path& data_dir) { close(); ilog("Wiping object database..."); fc::remove_all(data_dir / "object_database"); ilog("Done wiping object databse."); } void object_database::open(const fc::path& data_dir) { try { ilog("Opening object database from ${d} ...", ("d", data_dir)); _data_dir = data_dir; for( uint32_t space = 0; space < _index.size(); ++space ) for( uint32_t type = 0; type < _index[space].size(); ++type ) if( _index[space][type] ) _index[space][type]->open( _data_dir / "object_database" / fc::to_string(space)/fc::to_string(type) ); ilog( "Done opening object database." ); } FC_CAPTURE_AND_RETHROW( (data_dir) ) } void object_database::pop_undo() { try { _undo_db.pop_commit(); } FC_CAPTURE_AND_RETHROW() } void object_database::save_undo( const object& obj ) { _undo_db.on_modify( obj ); } void object_database::save_undo_add( const object& obj ) { _undo_db.on_create( obj ); } void object_database::save_undo_remove(const object& obj) { _undo_db.on_remove( obj ); } } } // namespace graphene::db