Skip to content

Commit 0cabea5

Browse files
committed
done
1 parent 723463f commit 0cabea5

File tree

3 files changed

+68
-93
lines changed

3 files changed

+68
-93
lines changed

include/geode/basic/database.h

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,45 @@ namespace geode
8787
index_t nb_data() const;
8888

8989
template < typename DataType >
90-
const uuid& register_data( DataType&& data )
90+
uuid register_new_data( DataType&& data )
91+
{
92+
static_assert( std::is_base_of< Identifier, DataType >::value,
93+
"[Database::register_data] Data is not a subclass of "
94+
"Identifier" );
95+
uuid new_id;
96+
register_unique_data(
97+
new_id, absl::make_unique< DataType >( std::move( data ) ) );
98+
return new_id;
99+
}
100+
101+
template < typename DataType >
102+
uuid register_new_data( std::unique_ptr< DataType >&& data )
103+
{
104+
static_assert( std::is_base_of< Identifier, DataType >::value,
105+
"[Database::register_data] Data is not a subclass of "
106+
"Identifier" );
107+
uuid new_id;
108+
register_unique_data( new_id, std::move( data ) );
109+
return new_id;
110+
}
111+
112+
template < typename DataType >
113+
void update_data( const uuid& id, DataType&& data )
91114
{
92115
static_assert( std::is_base_of< Identifier, DataType >::value,
93116
"[Database::register_data] Data is not a subclass of "
94117
"Identifier" );
95118
return register_unique_data(
96-
absl::make_unique< DataType >( std::move( data ) ) );
119+
id, absl::make_unique< DataType >( std::move( data ) ) );
97120
}
98121

99122
template < typename DataType >
100-
const uuid& register_data( std::unique_ptr< DataType >&& data )
123+
void update_data( const uuid& id, std::unique_ptr< DataType >&& data )
101124
{
102125
static_assert( std::is_base_of< Identifier, DataType >::value,
103126
"[Database::register_data] Data is not a subclass of "
104127
"Identifier" );
105-
return register_unique_data( std::move( data ) );
128+
return register_unique_data( id, std::move( data ) );
106129
}
107130

108131
void delete_data( const uuid& id );
@@ -117,10 +140,8 @@ namespace geode
117140
std::unique_ptr< DataType > take_data( const uuid& id )
118141
{
119142
get_data( id ).get< DataType >();
120-
DEBUG( "get" );
121143
auto* data =
122144
dynamic_cast< DataType* >( steal_data( id ).release() );
123-
DEBUG( "steal" );
124145
return std::unique_ptr< DataType >{ data };
125146
}
126147

@@ -132,8 +153,8 @@ namespace geode
132153
serializer_function serializer, serializer_function deserializer );
133154

134155
private:
135-
const uuid& register_unique_data(
136-
std::unique_ptr< Identifier >&& data );
156+
void register_unique_data(
157+
const uuid& id, std::unique_ptr< Identifier >&& data );
137158

138159
std::unique_ptr< Identifier > steal_data( const uuid& id );
139160

src/geode/basic/database.cpp

Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,20 @@
2626
#include <condition_variable>
2727
#include <mutex>
2828
#include <queue>
29-
#include <sstream>
30-
#include <thread>
3129

3230
#include <async++.h>
3331

3432
#include <ghc/filesystem.hpp>
3533

3634
#include <absl/container/flat_hash_map.h>
37-
#include <absl/time/time.h>
3835

3936
#include <geode/basic/identifier.h>
4037
#include <geode/basic/pimpl_impl.h>
4138
#include <geode/basic/uuid.h>
4239

4340
namespace
4441
{
45-
constexpr auto DATA_EXPIRATION = std::chrono::minutes( 1 );
46-
int count{ 0 };
42+
constexpr auto DATA_EXPIRATION = std::chrono::minutes( 3 );
4743
} // namespace
4844

4945
namespace geode
@@ -52,24 +48,24 @@ namespace geode
5248
{
5349
public:
5450
Storage( std::unique_ptr< geode::Identifier >&& data )
55-
: data_{ std::move( data ) }, count_{ count++ }
51+
: data_{ std::move( data ) }
5652
{
57-
Logger::debug( count_, " -> ", "Storage" );
5853
}
5954

6055
~Storage()
6156
{
62-
Logger::debug( count_, " -> ", "~Storage" );
6357
terminate_storage();
6458
std::unique_lock< std::mutex > locking{ lock_ };
65-
do
59+
clean_queue();
60+
while( !condition_.wait_for(
61+
locking, std::chrono::milliseconds( 10 ), [this] {
62+
return queue_.empty();
63+
} ) )
64+
;
6665
{
66+
condition_.notify_all();
6767
clean_queue();
68-
} while( !condition_.wait_for(
69-
locking, std::chrono::microseconds( 10 ), [this] {
70-
return queue_.empty();
71-
} ) );
72-
Logger::debug( count_, " -> ", "~Storage end" );
68+
}
7369
}
7470

7571
bool expired() const
@@ -87,9 +83,6 @@ namespace geode
8783
const std::lock_guard< std::mutex > locking{ lock_ };
8884
counter_++;
8985
last_++;
90-
std::ostringstream oss;
91-
oss << std::this_thread::get_id() << " " << this;
92-
Logger::debug( count_, " -> ", "new ", counter_, " " );
9386
}
9487

9588
void delete_data_reference()
@@ -98,25 +91,19 @@ namespace geode
9891
OPENGEODE_ASSERT(
9992
counter_ > 0, "[Database::Storage] Cannot decrement" );
10093
counter_--;
101-
std::ostringstream oss;
102-
oss << std::this_thread::get_id() << " " << this;
103-
Logger::debug( count_, " -> ", "delete ", counter_, " " );
10494
if( unused() )
10595
{
10696
clean_queue();
10797
wait_for_memory_release();
10898
}
10999
}
110100

111-
// void set_data( std::unique_ptr< geode::Identifier >&& data )
112-
// {
113-
// const std::lock_guard< std::mutex > locking{ lock_ };
114-
// terminate_ = false;
115-
// counter_ = 0;
116-
// count_ = count++;
117-
// Logger::debug( count_, " -> ", "set_data " );
118-
// data_ = std::move( data );
119-
// }
101+
void set_data( std::unique_ptr< geode::Identifier >&& data )
102+
{
103+
const std::lock_guard< std::mutex > locking{ lock_ };
104+
counter_ = 0;
105+
data_ = std::move( data );
106+
}
120107

121108
const std::unique_ptr< geode::Identifier >& data() const
122109
{
@@ -131,13 +118,8 @@ namespace geode
131118
private:
132119
void terminate_storage()
133120
{
134-
std::ostringstream oss;
135-
oss << std::this_thread::get_id() << " " << this;
136-
Logger::debug( count_, " -> ", "begin terminate_storage" );
137121
terminate_ = true;
138-
Logger::debug( count_, " -> ", "calls ", queue_.size(), " " );
139122
condition_.notify_all();
140-
Logger::debug( count_, " -> ", "end terminate_storage" );
141123
}
142124

143125
void clean_queue()
@@ -156,28 +138,17 @@ namespace geode
156138
{
157139
const auto last = last_;
158140
queue_.emplace( async::spawn( [this, last] {
159-
Logger::debug( count_, " -> ", "wait start " );
160-
Logger::debug( count_, " -> ", "wait start 2 " );
161141
std::unique_lock< std::mutex > locking{ lock_ };
162-
Logger::debug( count_, " -> ", "wait 2 + " );
163-
Logger::debug( count_, " -> ", "last ", last, " ", last_ );
164142
if( !condition_.wait_for(
165143
locking, DATA_EXPIRATION, [this, last] {
166-
Logger::debug( count_, " -> ", "terminate ",
167-
terminate_.load(), " " );
168144
return terminate_.load();
169145
} ) )
170146
{
171-
Logger::debug(
172-
count_, " -> ", "wait in", " ", last, " ", last_ );
173147
if( last == last_ )
174148
{
175-
Logger::debug( count_, " -> ", "wait reset", " " );
176149
data_.reset();
177150
}
178151
}
179-
Logger::debug(
180-
count_, " -> ", "wait out + ", queue_.size(), " " );
181152
locking.unlock();
182153
condition_.notify_all();
183154
} ) );
@@ -189,8 +160,7 @@ namespace geode
189160
index_t counter_{ 0 };
190161
std::mutex lock_;
191162
std::condition_variable condition_;
192-
index_t last_;
193-
int count_;
163+
index_t last_{ 0 };
194164
std::queue< async::task< void > > queue_;
195165
};
196166

@@ -211,11 +181,11 @@ namespace geode
211181
return storage_.size();
212182
}
213183

214-
const uuid& register_unique_data( std::unique_ptr< Identifier >&& data )
184+
void register_unique_data(
185+
const uuid& id, std::unique_ptr< Identifier >&& data )
215186
{
216-
const auto& registered_data = register_data( std::move( data ) );
217-
save_data( registered_data );
218-
return registered_data->id();
187+
save_data( id, data );
188+
register_data( id, std::move( data ) );
219189
}
220190

221191
std::shared_ptr< Storage > data( const uuid& id ) const
@@ -233,14 +203,10 @@ namespace geode
233203
auto& storage = storage_.at( id );
234204
if( storage && storage->unused() && !storage->expired() )
235205
{
236-
DEBUG( "in" );
237206
auto* data = storage->steal_data();
238-
DEBUG( "steal" );
239207
storage.reset();
240-
DEBUG( "reset" );
241208
return std::unique_ptr< Identifier >{ data };
242209
}
243-
DEBUG( "load" );
244210
return load_data( id );
245211
}
246212

@@ -258,36 +224,34 @@ namespace geode
258224

259225
private:
260226
const std::unique_ptr< Identifier >& register_data(
261-
std::unique_ptr< Identifier >&& data )
227+
const uuid& id, std::unique_ptr< Identifier >&& data )
262228
{
263-
const auto& id = data->id();
264229
const auto it = storage_.find( id );
265230
if( it != storage_.end() )
266231
{
267-
// if( it->second->unused() )
268-
// {
269-
// it->second->set_data( std::move( data ) );
270-
// return it->second->data();
271-
// }
232+
if( it->second->unused() )
233+
{
234+
it->second->set_data( std::move( data ) );
235+
return it->second->data();
236+
}
272237
delete_data( id );
273-
// return do_register_data( std::move( data ) );
238+
return do_register_data( id, std::move( data ) );
274239
}
275-
return do_register_data( std::move( data ) );
240+
return do_register_data( id, std::move( data ) );
276241
}
277242

278243
const std::unique_ptr< Identifier >& do_register_data(
279-
std::unique_ptr< Identifier >&& data )
244+
const uuid& id, std::unique_ptr< Identifier >&& data )
280245
{
281-
const auto id = data->id();
282246
auto new_storage = std::make_shared< Storage >( std::move( data ) );
283247
return storage_.emplace( id, std::move( new_storage ) )
284248
.first->second->data();
285249
}
286250

287-
void save_data( const std::unique_ptr< Identifier >& data ) const
251+
void save_data(
252+
const uuid& id, const std::unique_ptr< Identifier >& data ) const
288253
{
289-
const auto filename =
290-
absl::StrCat( directory_, "/", data->id().string() );
254+
const auto filename = absl::StrCat( directory_, "/", id.string() );
291255
std::ofstream file{ filename, std::ofstream::binary };
292256
TContext context;
293257
for( const auto& serializer : serializers_ )
@@ -378,10 +342,10 @@ namespace geode
378342
return impl_->nb_data();
379343
}
380344

381-
const uuid& Database::register_unique_data(
382-
std::unique_ptr< Identifier >&& data )
345+
void Database::register_unique_data(
346+
const uuid& id, std::unique_ptr< Identifier >&& data )
383347
{
384-
return impl_->register_unique_data( std::move( data ) );
348+
impl_->register_unique_data( id, std::move( data ) );
385349
}
386350

387351
Database::Data Database::get_data( const uuid& id ) const

tests/basic/test-database.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,14 @@ void register_foo_deserializer( geode::PContext& context )
6060
void test_take_data( geode::Database& database, const geode::uuid& id )
6161
{
6262
auto stolen_foo = database.take_data< Foo >( id );
63-
DEBUG( "take 1" );
6463
OPENGEODE_EXCEPTION(
6564
stolen_foo->value_ == 42, "[Test] Wrong value after take data" );
6665
auto foo_data = database.get_data( id );
67-
DEBUG( "take 2" );
6866
const auto& foo = foo_data.get< Foo >();
69-
DEBUG( "take 3" );
7067
OPENGEODE_EXCEPTION(
7168
foo.value_ == 42, "[Test] Wrong value after register data" );
7269
OPENGEODE_EXCEPTION( stolen_foo.get() != &foo,
7370
"[Test] Objects adresses should be different" );
74-
DEBUG( "take 4" );
7571
}
7672

7773
void test_take_wrong_data( geode::Database& database, const geode::uuid& id )
@@ -91,7 +87,7 @@ void test_take_wrong_data( geode::Database& database, const geode::uuid& id )
9187

9288
geode::uuid test_register_data( geode::Database& database )
9389
{
94-
auto foo = database.register_data( Foo{ 42 } );
90+
auto foo = database.register_new_data( Foo{ 42 } );
9591
auto foo_data = database.get_data( foo );
9692
OPENGEODE_EXCEPTION( foo_data.get< Foo >().value_ == 42,
9793
"[Test] Wrong value after register data" );
@@ -100,7 +96,7 @@ geode::uuid test_register_data( geode::Database& database )
10096

10197
geode::uuid test_register_unique_data( geode::Database& database )
10298
{
103-
auto foo = database.register_data( absl::make_unique< Foo >( 22 ) );
99+
auto foo = database.register_new_data( absl::make_unique< Foo >( 22 ) );
104100
auto foo_data = database.get_data( foo );
105101
OPENGEODE_EXCEPTION( foo_data.get< Foo >().value_ == 22,
106102
"[Test] Wrong value after register unique data" );
@@ -117,7 +113,7 @@ void test_modify_data( geode::Database& database, const geode::uuid& id )
117113
taken_foo->value_ == 12, "[Test] Wrong value after modify taken data" );
118114
OPENGEODE_EXCEPTION(
119115
foo.value_ == 42, "[Test] Wrong value after register data" );
120-
database.register_data( std::move( taken_foo ) );
116+
database.update_data( id, std::move( taken_foo ) );
121117
auto foo_data2 = database.get_data( id );
122118
const auto& foo2 = foo_data2.get< Foo >();
123119
OPENGEODE_EXCEPTION(
@@ -131,17 +127,11 @@ void test()
131127
geode::Database database( "temp" );
132128
database.register_serializer_functions(
133129
register_foo_serializer, register_foo_deserializer );
134-
DEBUG( "functions" );
135130
auto foo0 = test_register_data( database );
136-
DEBUG( "register" );
137131
test_register_unique_data( database );
138-
DEBUG( "register unique" );
139132
test_take_data( database, foo0 );
140-
DEBUG( "take" );
141133
test_modify_data( database, foo0 );
142-
DEBUG( "modify" );
143134
test_take_wrong_data( database, foo0 );
144-
DEBUG( "wrong" );
145135
OPENGEODE_EXCEPTION(
146136
database.nb_data() == 2, "[Test] Database incomplete" );
147137
}

0 commit comments

Comments
 (0)