-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathobject_database.cpp
More file actions
148 lines (131 loc) · 5.15 KB
/
object_database.cpp
File metadata and controls
148 lines (131 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <graphene/db/object_database.hpp>
#include <fc/io/raw.hpp>
#include <fc/container/flat.hpp>
#include <fc/thread/parallel.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()
{
// ilog("Save object_database in ${d}", ("d", _data_dir));
fc::create_directories( _data_dir / "object_database.tmp" / "lock" );
std::vector<boost::fibers::future<void>> tasks;
tasks.reserve(200);
for( uint32_t space = 0; space < _index.size(); ++space )
{
fc::create_directories( _data_dir / "object_database.tmp" / fc::to_string(space) );
const auto types = _index[space].size();
for( uint32_t type = 0; type < types; ++type )
if( _index[space][type] )
tasks.push_back( fc::do_parallel( [this,space,type] () {
_index[space][type]->save( _data_dir / "object_database.tmp" / fc::to_string(space)/fc::to_string(type) );
} ) );
}
for( auto& task : tasks )
task.wait();
fc::remove_all( _data_dir / "object_database.tmp" / "lock" );
if( fc::exists( _data_dir / "object_database" ) )
fc::rename( _data_dir / "object_database", _data_dir / "object_database.old" );
fc::rename( _data_dir / "object_database.tmp", _data_dir / "object_database" );
fc::remove_all( _data_dir / "object_database.old" );
}
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 {
_data_dir = data_dir;
if( fc::exists( _data_dir / "object_database" / "lock" ) )
{
wlog("Ignoring locked object_database");
return;
}
std::vector<boost::fibers::future<void>> tasks;
tasks.reserve(200);
ilog("Opening object database from ${d} ...", ("d", 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] )
tasks.push_back( fc::do_parallel( [this,space,type] () {
_index[space][type]->open( _data_dir / "object_database" / fc::to_string(space)/fc::to_string(type) );
} ) );
for( auto& task : tasks )
task.wait();
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