2015-06-08 15:50:35 +00:00
|
|
|
/*
|
2015-10-12 17:02:59 +00:00
|
|
|
* Copyright (c) 2015 Cryptonomex, Inc., and contributors. All rights reserved.
|
2015-06-08 15:50:35 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
2015-10-12 17:02:59 +00:00
|
|
|
*
|
2015-06-08 15:50:35 +00:00
|
|
|
*/
|
|
|
|
|
#include <graphene/db/object_database.hpp>
|
|
|
|
|
|
|
|
|
|
#include <fc/io/raw.hpp>
|
|
|
|
|
#include <fc/container/flat.hpp>
|
|
|
|
|
#include <fc/uint128.hpp>
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2015-06-18 19:17:48 +00:00
|
|
|
// ilog("Save object_database in ${d}", ("d", _data_dir));
|
2015-06-16 19:56:13 +00:00
|
|
|
for( uint32_t space = 0; space < _index.size(); ++space )
|
|
|
|
|
{
|
2015-06-16 22:45:33 +00:00
|
|
|
fc::create_directories( _data_dir / "object_database" / fc::to_string(space) );
|
2015-06-16 19:56:13 +00:00
|
|
|
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) );
|
|
|
|
|
}
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void object_database::wipe(const fc::path& data_dir)
|
|
|
|
|
{
|
|
|
|
|
close();
|
2015-10-09 20:31:40 +00:00
|
|
|
ilog("Wiping object database...");
|
2015-06-08 15:50:35 +00:00
|
|
|
fc::remove_all(data_dir / "object_database");
|
2015-10-09 20:31:40 +00:00
|
|
|
ilog("Done wiping object databse.");
|
2015-06-08 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-18 13:19:14 +00:00
|
|
|
void object_database::open(const fc::path& data_dir)
|
2015-06-08 15:50:35 +00:00
|
|
|
{ try {
|
2015-10-09 20:31:40 +00:00
|
|
|
ilog("Opening object database from ${d} ...", ("d", data_dir));
|
2015-06-08 15:50:35 +00:00
|
|
|
_data_dir = data_dir;
|
2015-06-16 19:56:13 +00:00
|
|
|
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) );
|
2015-10-09 20:31:40 +00:00
|
|
|
ilog( "Done opening object database." );
|
2015-06-16 19:56:13 +00:00
|
|
|
|
2015-06-08 15:50:35 +00:00
|
|
|
} 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
|