/* * Copyright (c) 2015, Cryptonomex, Inc. * All rights reserved. * * This source code is provided for evaluation in private test networks only, until September 8, 2015. After this date, this license expires and * the code may not be used, modified or distributed for any purpose. Redistribution and use in source and binary forms, with or without modification, * are permitted until September 8, 2015, provided that the following conditions are met: * * 1. The code and/or derivative works are used only for private test networks consisting of no more than 10 P2P nodes. * * 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()) ); assert( _index[space_id].size() > type_id ); //, "", ("space_id",space_id)("type_id",type_id)("index[space_id].size",_index[space_id].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"); assert(!fc::exists(data_dir / "object_database")); } void object_database::open( const fc::path& data_dir ) { try { ilog("Open object_database in ${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) ); } 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