-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataAccess.h
558 lines (447 loc) · 14.4 KB
/
DataAccess.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#pragma once
#include"RxStreamer/IncludeExtLibs.h"
#include"RxStreamer/DataAccessPolicy.h"
#include"RxStreamer/Export.h"
namespace Reactor
{
class DataAccessStatus;
// ---------------------------------------------------
// DataAccessBase
// ---------------------------------------------------
class DataAccessBase
: public Templates::SubscribeMethod<bool>
, public Templates::IsSuccessMethod
, public Templates::IsValidMethod<bool>
, public Templates::UnsubscribeMethod<bool>
, public Templates::IsSubscribedMethod
, public Templates::RefreshMethod<bool>
, public Templates::StatusConstMethod<DataAccessStatus>
{
public:
virtual ~DataAccessBase()
{ }
CLASS_TRAITS(DataAccessBase)
virtual RxData::CacheDescription CacheId() const = 0;
virtual RxObserverSubjectNew<DataAccessBase::Ptr> &dataAccessSubject() = 0;
};
// ---------------------------------------------------
// DataAccessStatus
// ---------------------------------------------------
class DataAccessStatus
{
public:
DataAccessStatus()
{ }
virtual ~DataAccessStatus()
{ }
Status::ExecutionStatus &executionStatus()
{
return executionStatus_;
}
const Status::ExecutionStatus &executionStatus() const
{
return executionStatus_;
}
Status::AccessStatus &accessStatus()
{
return accessStatus_;
}
const Status::AccessStatus &accessStatus() const
{
return accessStatus_;
}
private:
Status::ExecutionStatus executionStatus_;
Status::AccessStatus accessStatus_;
};
// ---------------------------------------------------
// DataAccessState
// ---------------------------------------------------
template<typename K, typename V>
class DataAccessState
{
public:
typedef IMap<K, V> Return;
typedef KeyValueRxEventsSubject<DataAccessBase *, Return> AccessSubject;
public:
DataAccessState(RxData::CacheDescription description, RxThreadPool::Ptr pool)
: description_(description)
, commandController_()
, accessSubject_()
, dataAccessSubject_()
, subscription_(CommandControllerSubscription<Return>::NoOp())
, threadPool_(pool)
{ }
virtual ~DataAccessState()
{ }
RxData::ObjectAccessProxy<V, K> Access()
{
return RxData::ObjectAccessProxy<V, K>(cache(), GetTypeName());
}
typename RxData::ObjectHome<V, K>::Ptr Home()
{
return cache()->template getOrCreateHome<V, K>(GetTypeName());
}
typename CommandControllerKeyValue<K, V>::Ptr Executor() const
{
return commandController_;
}
bool SetController(typename CommandControllerKeyValue<K, V>::Ptr controller)
{
commandController_ = controller;
return commandController_ != nullptr;
}
bool hasController() const
{
return commandController_ != nullptr;
}
AccessSubject &accessSubject()
{
return accessSubject_;
}
RxObserverSubjectNew<DataAccessBase::Ptr> &dataAccessSubject()
{
return dataAccessSubject_;
}
CommandControllerSubscription<Return> &subscription()
{
return subscription_;
}
const CommandControllerSubscription<Return> &subscription() const
{
return subscription_;
}
void SetSubscription(CommandControllerSubscription<Return> subscription)
{
subscription_ = subscription;
}
bool isSubscribed() const
{
return commandController_ != nullptr && commandController_->IsExecuting();
}
void SetControllerPool(RxThreadPool::Ptr threadPool)
{
threadPool_ = threadPool;
}
RxThreadPool::Ptr ControllerPool() const
{
return threadPool_;
}
RxData::CacheDescription CacheId() const
{
return description_;
}
private:
std::string GetTypeName() const
{
return BaseLib::Utility::GetTypeName<V>();
}
RxData::Cache::Ptr cache()
{
return RxData::CacheFactory::Instance().getOrCreateCache(description_);
}
private:
RxData::CacheDescription description_;
typename CommandControllerKeyValue<K, V>::Ptr commandController_;
AccessSubject accessSubject_;
RxObserverSubjectNew<DataAccessBase::Ptr> dataAccessSubject_;
CommandControllerSubscription<Return> subscription_;
RxThreadPool::Ptr threadPool_;
};
// ---------------------------------------------------
// DataAccess
// ---------------------------------------------------
template<typename K, typename V>
class DataAccess
: public DataAccessBase
, public Templates::ResultMethod<CommandControllerSubscription<IMap<K, V>>>
, public RxObserverNew<IMap<K, V>>
, public RxData::ObjectObserver<V>
, public Templates::LockableType<DataAccess<K, V>>
, public Templates::ContextObjectShared<DataAccessPolicy, DataAccessState<K, V>, DataAccessStatus>
, public ENABLE_SHARED_FROM_THIS_T2(DataAccess, K, V)
{
public:
typedef IMap<K, V> Return;
typedef MutexTypeLocker<DataAccess<K, V>> Locker;
public:
DataAccess(RxData::CacheDescription description, DataAccessPolicy policy = DataAccessPolicy::Default(), RxThreadPool::Ptr pool = RxThreadPoolFactory::ControllerPool())
: RxObserverNew<IMap<K, V>>()
, Templates::ContextObjectShared<DataAccessPolicy, DataAccessState<K, V>, DataAccessStatus>
(
new DataAccessPolicy(policy),
new DataAccessState<K, V>(description, pool),
new DataAccessStatus()
)
{ }
virtual ~DataAccess()
{
this->data()->Home()->Disconnect(this);
if(this->data()->hasController())
{
this->data()->Executor()->RxSubject().state().Unsubscribe(this->shared_from_this());
}
}
CLASS_TRAITS(DataAccess)
typename DataAccess<K, V>::Ptr GetPtr()
{
return this->shared_from_this();
}
// ---------------------------------------------------------------
// Interface DataAccessBase
// ---------------------------------------------------------------
virtual bool Subscribe()
{
Locker locker(this);
return this->doSubscribe();
}
virtual bool Refresh()
{
Locker locker(this);
if(!this->data()->hasController()) return false;
this->data()->Executor()->Trigger();
return true;
}
virtual bool Unsubscribe()
{
Locker locker(this);
return this->doUnsubscribe();
}
virtual bool IsSubscribed() const
{
Locker locker(this);
return this->data()->isSubscribed();
}
virtual bool IsSuccess() const
{
Locker locker(this);
if(!this->data()->hasController()) return false;
return this->data()->Executor()->IsSuccess();
}
virtual bool IsValid() const
{
return true;
}
virtual CommandControllerSubscription<Return> Result() const
{
return this->data()->subscription();
}
virtual const DataAccessStatus& StatusConst() const
{
return this->status().delegate();
}
// ---------------------------------------------------------------
// Add command
// ---------------------------------------------------------------
bool Add(typename CommandActions<Return>::Ptr action)
{
Locker locker(this);
return this->getOrCreateController()->Add(action);
}
bool SetCommands(CommandsGroup commands)
{
Locker locker(this);
return this->getOrCreateController()->SetCommands(commands);
}
void SetControllerPool(RxThreadPool::Ptr threadPool)
{
Locker locker(this);
this->data()->SetControllerPool(threadPool);
}
virtual RxObserverSubjectNew<DataAccessBase::Ptr> &dataAccessSubject()
{
return this->data()->dataAccessSubject();
}
// ------------------------------------------------------
// Access the cache and executor
// ------------------------------------------------------
RxData::ObjectAccessProxy<V, K> Data()
{
return this->data()->Access();
}
CommandController::Ptr Executor() const
{
return this->data()->Executor();
}
virtual RxData::CacheDescription CacheId() const
{
return this->data()->CacheId();
}
// ------------------------------------------------------
// Access to the subject
// ------------------------------------------------------
typename DataAccessState<K, V>::AccessSubject &Subject()
{
return this->data()->accessSubject();
}
// ------------------------------------------------------
// interface RxObserver
// ------------------------------------------------------
virtual bool OnComplete()
{
IINFO() << "Completed commands";
Locker locker(this);
processOnComplete();
return true;
}
virtual bool OnNext(Return values)
{
IINFO() << "Next value " << values;
if(this->status()->executionStatus().IsFailed())
{
IINFO() << "Execution failed, ignoring next value";
return true;
}
Locker locker(this);
processOnNext(values);
return true;
}
virtual bool OnError(BaseLib::GeneralException exception)
{
IINFO() << "Error command" << exception.what();
Locker locker(this);
processOnError(exception);
return true;
}
// --------------------------------------------------------
// Interface ObjectObserver<K>
// --------------------------------------------------------
virtual bool OnObjectCreated(V )
{
if(this->config()->Reload().IsOnCreate())
{
if(isDonePrivate())
{
IINFO() << "DataAccess is done. Ignoring reload.";
return true;
}
doSubscribe();
}
return true;
}
virtual bool OnObjectDeleted(V )
{
if(this->config()->Reload().IsOnDelete())
{
if(isDonePrivate())
{
IINFO() << "DataAccess is done. Ignoring reload.";
return true;
}
doSubscribe();
}
return true;
}
virtual bool OnObjectModified(V )
{
if(this->config()->Reload().IsOnModify())
{
if(isDonePrivate())
{
IINFO() << "DataAccess is done. Ignoring reload.";
return true;
}
doSubscribe();
}
return true;
}
private:
// -----------------------------------------------------------
// private functions
// -----------------------------------------------------------
typename CommandControllerKeyValue<K, V>::Ptr getOrCreateController()
{
if(!this->data()->hasController())
{
typename CommandControllerKeyValue<K, V>::Ptr commandController =
CommandFactory::Instance().CreateCommandControllerKeyValue<K, V>(
this->config()->ControllerPolicy(),
this->data()->ControllerPool()
);
this->data()->SetController(commandController);
this->status()->accessStatus().Modify();
}
return this->data()->Executor();
}
bool doSubscribe()
{
if(this->data()->isSubscribed())
{
IINFO() << "DataAccess is already subscribed";
return false;
}
this->data()->Home()->Connect(this);
getOrCreateController()->RxSubject().state().Subscribe(this->shared_from_this());
//this->event().Subscribe(this->shared_from_this());
CommandControllerSubscription<Return> result = getOrCreateController()->Execute();
this->data()->SetSubscription(result);
this->status()->executionStatus().Start();
return true;
}
bool doUnsubscribe()
{
if(!this->data()->hasController()) return false;
// TODO: Wait for shutdown? Review and move to controller reset?
bool unsubscribe = getOrCreateController()->Shutdown();
getOrCreateController()->RxSubject().state().Unsubscribe();
bool isReset = getOrCreateController()->Reset();
ASSERT(isReset);
this->data()->Home()->Disconnect(this);
this->data()->accessSubject().state().Unsubscribe();
this->data()->subscription().Cancel();
return unsubscribe;
}
bool isDonePrivate() const
{
if(!this->data()->hasController())
{
IINFO() << "Not done";
return false;
}
// if(this->status()->executionStatus().IsExecuting()) {
// IINFO() << "Not done";
// return false;
// }
if(this->data()->Executor()->IsExecuting())
{
IINFO() << "Not done";
return false;
}
bool inAttempt = Strategy::IsInAttempt::Perform(
this->status()->executionStatus(),
this->config()->attempt()
);
bool inLifespan = Strategy::IsInLifetime::Perform(
this->status()->accessStatus(),
this->config()->lifetime()
);
IINFO() << "Is done? " << (!inAttempt && !inLifespan) << " || " << !inLifespan;
return (!inAttempt && !inLifespan) || !inLifespan;
}
// -----------------------------------------------------------
// private functions to process callbacks
// -----------------------------------------------------------
void processOnNext(const IMap<K, V> &values)
{
this->data()->Access().WriteAll(values);
this->data()->accessSubject().event().Next(this, values);
this->data()->dataAccessSubject().event().Next(this->GetPtr());
if(this->config()->leasePlan().IsRenewOnWrite() || this->config()->leasePlan().IsRenewOnAccess())
{
this->status()->accessStatus().RenewLease();
}
}
void processOnError(BaseLib::GeneralException exception)
{
this->status()->executionStatus().Failure();
this->data()->accessSubject().event().Error(this, exception);
this->data()->dataAccessSubject().event().Error(exception);
}
void processOnComplete()
{
this->status()->executionStatus().Success();
this->data()->accessSubject().event().Complete(this);
this->data()->dataAccessSubject().event().Complete();
}
};
}