Skip to content

Commit c5197de

Browse files
committed
done
1 parent 723463f commit c5197de

File tree

3 files changed

+61
-89
lines changed

3 files changed

+61
-89
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: 29 additions & 68 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,21 @@ 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_ };
6559
do
6660
{
6761
clean_queue();
6862
} while( !condition_.wait_for(
69-
locking, std::chrono::microseconds( 10 ), [this] {
63+
locking, std::chrono::milliseconds( 10 ), [this] {
7064
return queue_.empty();
7165
} ) );
72-
Logger::debug( count_, " -> ", "~Storage end" );
7366
}
7467

7568
bool expired() const
@@ -87,9 +80,6 @@ namespace geode
8780
const std::lock_guard< std::mutex > locking{ lock_ };
8881
counter_++;
8982
last_++;
90-
std::ostringstream oss;
91-
oss << std::this_thread::get_id() << " " << this;
92-
Logger::debug( count_, " -> ", "new ", counter_, " " );
9383
}
9484

9585
void delete_data_reference()
@@ -98,25 +88,19 @@ namespace geode
9888
OPENGEODE_ASSERT(
9989
counter_ > 0, "[Database::Storage] Cannot decrement" );
10090
counter_--;
101-
std::ostringstream oss;
102-
oss << std::this_thread::get_id() << " " << this;
103-
Logger::debug( count_, " -> ", "delete ", counter_, " " );
10491
if( unused() )
10592
{
10693
clean_queue();
10794
wait_for_memory_release();
10895
}
10996
}
11097

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-
// }
98+
void set_data( std::unique_ptr< geode::Identifier >&& data )
99+
{
100+
const std::lock_guard< std::mutex > locking{ lock_ };
101+
counter_ = 0;
102+
data_ = std::move( data );
103+
}
120104

121105
const std::unique_ptr< geode::Identifier >& data() const
122106
{
@@ -131,13 +115,8 @@ namespace geode
131115
private:
132116
void terminate_storage()
133117
{
134-
std::ostringstream oss;
135-
oss << std::this_thread::get_id() << " " << this;
136-
Logger::debug( count_, " -> ", "begin terminate_storage" );
137118
terminate_ = true;
138-
Logger::debug( count_, " -> ", "calls ", queue_.size(), " " );
139119
condition_.notify_all();
140-
Logger::debug( count_, " -> ", "end terminate_storage" );
141120
}
142121

143122
void clean_queue()
@@ -156,28 +135,17 @@ namespace geode
156135
{
157136
const auto last = last_;
158137
queue_.emplace( async::spawn( [this, last] {
159-
Logger::debug( count_, " -> ", "wait start " );
160-
Logger::debug( count_, " -> ", "wait start 2 " );
161138
std::unique_lock< std::mutex > locking{ lock_ };
162-
Logger::debug( count_, " -> ", "wait 2 + " );
163-
Logger::debug( count_, " -> ", "last ", last, " ", last_ );
164139
if( !condition_.wait_for(
165140
locking, DATA_EXPIRATION, [this, last] {
166-
Logger::debug( count_, " -> ", "terminate ",
167-
terminate_.load(), " " );
168141
return terminate_.load();
169142
} ) )
170143
{
171-
Logger::debug(
172-
count_, " -> ", "wait in", " ", last, " ", last_ );
173144
if( last == last_ )
174145
{
175-
Logger::debug( count_, " -> ", "wait reset", " " );
176146
data_.reset();
177147
}
178148
}
179-
Logger::debug(
180-
count_, " -> ", "wait out + ", queue_.size(), " " );
181149
locking.unlock();
182150
condition_.notify_all();
183151
} ) );
@@ -189,8 +157,7 @@ namespace geode
189157
index_t counter_{ 0 };
190158
std::mutex lock_;
191159
std::condition_variable condition_;
192-
index_t last_;
193-
int count_;
160+
index_t last_{ 0 };
194161
std::queue< async::task< void > > queue_;
195162
};
196163

@@ -211,11 +178,11 @@ namespace geode
211178
return storage_.size();
212179
}
213180

214-
const uuid& register_unique_data( std::unique_ptr< Identifier >&& data )
181+
void register_unique_data(
182+
const uuid& id, std::unique_ptr< Identifier >&& data )
215183
{
216-
const auto& registered_data = register_data( std::move( data ) );
217-
save_data( registered_data );
218-
return registered_data->id();
184+
save_data( id, data );
185+
register_data( id, std::move( data ) );
219186
}
220187

221188
std::shared_ptr< Storage > data( const uuid& id ) const
@@ -233,14 +200,10 @@ namespace geode
233200
auto& storage = storage_.at( id );
234201
if( storage && storage->unused() && !storage->expired() )
235202
{
236-
DEBUG( "in" );
237203
auto* data = storage->steal_data();
238-
DEBUG( "steal" );
239204
storage.reset();
240-
DEBUG( "reset" );
241205
return std::unique_ptr< Identifier >{ data };
242206
}
243-
DEBUG( "load" );
244207
return load_data( id );
245208
}
246209

@@ -258,36 +221,34 @@ namespace geode
258221

259222
private:
260223
const std::unique_ptr< Identifier >& register_data(
261-
std::unique_ptr< Identifier >&& data )
224+
const uuid& id, std::unique_ptr< Identifier >&& data )
262225
{
263-
const auto& id = data->id();
264226
const auto it = storage_.find( id );
265227
if( it != storage_.end() )
266228
{
267-
// if( it->second->unused() )
268-
// {
269-
// it->second->set_data( std::move( data ) );
270-
// return it->second->data();
271-
// }
229+
if( it->second->unused() )
230+
{
231+
it->second->set_data( std::move( data ) );
232+
return it->second->data();
233+
}
272234
delete_data( id );
273-
// return do_register_data( std::move( data ) );
235+
return do_register_data( id, std::move( data ) );
274236
}
275-
return do_register_data( std::move( data ) );
237+
return do_register_data( id, std::move( data ) );
276238
}
277239

278240
const std::unique_ptr< Identifier >& do_register_data(
279-
std::unique_ptr< Identifier >&& data )
241+
const uuid& id, std::unique_ptr< Identifier >&& data )
280242
{
281-
const auto id = data->id();
282243
auto new_storage = std::make_shared< Storage >( std::move( data ) );
283244
return storage_.emplace( id, std::move( new_storage ) )
284245
.first->second->data();
285246
}
286247

287-
void save_data( const std::unique_ptr< Identifier >& data ) const
248+
void save_data(
249+
const uuid& id, const std::unique_ptr< Identifier >& data ) const
288250
{
289-
const auto filename =
290-
absl::StrCat( directory_, "/", data->id().string() );
251+
const auto filename = absl::StrCat( directory_, "/", id.string() );
291252
std::ofstream file{ filename, std::ofstream::binary };
292253
TContext context;
293254
for( const auto& serializer : serializers_ )
@@ -378,10 +339,10 @@ namespace geode
378339
return impl_->nb_data();
379340
}
380341

381-
const uuid& Database::register_unique_data(
382-
std::unique_ptr< Identifier >&& data )
342+
void Database::register_unique_data(
343+
const uuid& id, std::unique_ptr< Identifier >&& data )
383344
{
384-
return impl_->register_unique_data( std::move( data ) );
345+
impl_->register_unique_data( id, std::move( data ) );
385346
}
386347

387348
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)