26 #ifndef _RECOVERYPOINTADAPTER_H_ 27 #define _RECOVERYPOINTADAPTER_H_ 35 #include <unordered_map> 36 #if __cplusplus >= 201100L || _MSC_VER >= 1900 48 class RecoveryPointAdapter;
65 virtual void purge() = 0;
70 virtual void close() = 0;
86 if (!_pAdapter || !_pAdapter->
next(_current))
97 : _pAdapter(pAdapter_)
102 bool operator==(
const iterator& rhs)
const 104 return _pAdapter == rhs._pAdapter;
106 bool operator!=(
const iterator& rhs)
const 108 return _pAdapter != rhs._pAdapter;
110 void operator++(
void)
126 : _body(body_, isRef_) { }
139 return iterator(&(_body.get()));
153 _body.get().update(recoveryPoint_);
166 _body.get().purge(subId_);
184 return _body.isValid();
187 BorrowRefHandle<RecoveryPointAdapterImpl> _body;
211 const std::shared_ptr<RecoveryPointAdapterImpl>& delegate_,
212 unsigned updateThreshold_ = 10,
213 double timeoutMillis_ = 2000.0,
214 long updateIntervalMillis_ = 2000
216 : _delegate(delegate_)
217 , _updateThreshold(updateThreshold_)
218 , _timeoutMillis(timeoutMillis_)
219 , _updateIntervalMillis(updateIntervalMillis_)
224 _thread = std::thread(&ConflatingRecoveryPointAdapter::updateThread,
232 for (UpdateIter purged = _latestUpdates.begin();
233 purged != _latestUpdates.end(); ++purged)
235 Field clearableSubId = purged->first;
236 purged->second.
clear();
237 clearableSubId.
clear();
247 return _delegate->next(current_);
259 Lock<Mutex> lock(_lock);
260 UpdateIter lastUpdate = _latestUpdates.find(subId);
261 if (lastUpdate == _latestUpdates.end())
265 _latestUpdates[subId] = recoveryPoint_.
deepCopy();
267 if (_timeoutMillis != 0.0)
269 Timer timer(_timeoutMillis);
271 _timers[subId] = timer;
277 lastUpdate->second.deepCopy(recoveryPoint_);
279 if (++_counts[subId] >= _updateThreshold)
295 Lock<Mutex> lock(_lock);
298 for (UpdateIter purged = _latestUpdates.begin();
299 purged != _latestUpdates.end(); ++purged)
301 Field clearableSubId = purged->first;
302 purged->second.
clear();
303 clearableSubId.
clear();
305 _latestUpdates.clear();
316 _delegate->purge(subId_);
317 Lock<Mutex> lock(_lock);
318 UpdateIter purged = _latestUpdates.find(subId_);
319 if (purged != _latestUpdates.end())
321 Field clearableSubId = purged->first;
322 purged->second.
clear();
323 _latestUpdates.erase(purged);
324 _counts.erase(subId_);
325 _timers.erase(subId_);
326 clearableSubId.
clear();
339 Lock<Mutex> lock(_lock);
351 while (!_counts.empty())
364 Lock<Mutex> lock(_lock);
374 std::vector<SavedUpdate> _queuedUpdates;
377 DeferLock<Mutex> lock(_lock);
380 _lock.wait(_updateIntervalMillis);
383 for (TimerMap::iterator timer = _timers.begin();
384 timer != _timers.end(); )
386 if (timer->second.check())
388 UpdateIter
update = _latestUpdates.find(timer->first);
389 if (update != _latestUpdates.end())
393 _queuedUpdates.push_back(*update);
394 _counts.erase(update->first);
395 timer = _timers.erase(timer);
396 _latestUpdates.erase(update);
411 bool updateAll = (bool)_updateAll;
413 for (CountMap::iterator count = _counts.begin();
414 count != _counts.end(); )
416 if (updateAll || _timeoutMillis == 0.0
417 || count->second >= _updateThreshold)
419 UpdateIter
update = _latestUpdates.find(count->first);
420 if (update != _latestUpdates.end())
424 _queuedUpdates.push_back(*update);
425 count = _counts.erase(count);
426 _timers.erase(update->first);
427 _latestUpdates.erase(update);
446 for (std::vector<SavedUpdate>::iterator
update = _queuedUpdates.begin(), end = _queuedUpdates.end();
update != end; ++
update)
448 _delegate->update(
update->second);
450 clearableSubId.
clear();
453 _queuedUpdates.clear();
463 std::shared_ptr<RecoveryPointAdapterImpl> _delegate;
469 typedef std::unordered_map<Field, RecoveryPoint, Field::FieldHash> UpdateMap;
470 typedef std::pair<Field, RecoveryPoint> SavedUpdate;
471 typedef UpdateMap::value_type Update;
472 typedef UpdateMap::iterator UpdateIter;
473 typedef std::unordered_map<Field, Timer, Field::FieldHash> TimerMap;
474 typedef TimerMap::iterator TimerIter;
475 typedef std::unordered_map<Field, unsigned, Field::FieldHash> CountMap;
476 typedef CountMap::iterator CountIter;
479 UpdateMap _latestUpdates;
491 unsigned _updateThreshold;
494 double _timeoutMillis;
497 long _updateIntervalMillis;
499 #if __cplusplus >= 201100L || _MSC_VER >= 1900 501 std::atomic<bool> _closed;
504 std::atomic<bool> _updateAll;
507 volatile bool _closed;
510 volatile bool _updateAll;
516 #endif //_RECOVERYPOINTADAPTER_H_ virtual void close()
Take any necessary actions to close the associated storage.
Definition: RecoveryPointAdapter.hpp:331
void purge()
Remove all data from the storage.
Definition: RecoveryPointAdapter.hpp:157
virtual void purge(const Field &subId_)
Remove the specified subId_ from the storage.
Definition: RecoveryPointAdapter.hpp:310
virtual void purge()=0
Remove all data from the storage.
void prune()
Take any necessary actions to close the associated storage.
Definition: RecoveryPointAdapter.hpp:176
RecoveryPointAdapter implementation that delegates storage to another RecoveryPointAdapter but provid...
Definition: RecoveryPointAdapter.hpp:193
void purge(const Field &subId_)
Remove the specified subId_ from the storage.
Definition: RecoveryPointAdapter.hpp:164
virtual void _runUpdateAll()
Lock is already held.
Definition: RecoveryPointAdapter.hpp:344
virtual void purge()
Remove all data from the storage.
Definition: RecoveryPointAdapter.hpp:288
void clear()
Deletes the data associated with this Field, should only be used on Fields that were created as deepC...
Definition: Field.hpp:260
virtual void prune()
Take any necessary actions to reduce associated storage size.
Definition: RecoveryPointAdapter.hpp:72
RecoveryPointAdapter a handle class for implementing external storage of subscription recovery points...
Definition: RecoveryPointAdapter.hpp:77
virtual void update(RecoveryPoint &recoveryPoint_)=0
Update the storage information with the given recovery point.
virtual bool next(RecoveryPoint ¤t_)=0
Recovery is done by iteration over elements in storage.
bool isValid() const
Return if this has a valid implementation.
Definition: RecoveryPointAdapter.hpp:182
ConflatingRecoveryPointAdapter(const std::shared_ptr< RecoveryPointAdapterImpl > &delegate_, unsigned updateThreshold_=10, double timeoutMillis_=2000.0, long updateIntervalMillis_=2000)
Conflate updates to delegate_ where they will only be processed every updateIntervalMillis_ for subsc...
Definition: RecoveryPointAdapter.hpp:210
Provides access to the subId and bookmark needed to restart a subscription.
Definition: RecoveryPoint.hpp:67
Defines the AMPS::Field class, which represents the value of a field in a message.
RecoveryPoint deepCopy()
Return a deep copy of self.
Definition: RecoveryPoint.hpp:97
Provides AMPS::RecoveryPoint, AMPS::RecoveryPointFactory, AMPS::FixedRecoveryPoint, and AMPS::DynamicRecoveryPoint.
void update(RecoveryPoint &recoveryPoint_)
Update the storage information with the given recovery point.
Definition: RecoveryPointAdapter.hpp:151
virtual void close()=0
Take any necessary actions to close the associated storage.
virtual bool next(RecoveryPoint ¤t_)
Recovery is done by iteration over elements in storage.
Definition: RecoveryPointAdapter.hpp:245
iterator begin()
To recover from an adapter, iterate over the adapter from begin() to end() with a RecoveryPointIterat...
Definition: RecoveryPointAdapter.hpp:137
Field represents the value of a single field in a Message.
Definition: Field.hpp:86
virtual void updateAll()
Push all updates to underlying adapter.
Definition: RecoveryPointAdapter.hpp:337
void close()
Take any necessary actions to close the associated storage.
Definition: RecoveryPointAdapter.hpp:170
RecoveryPointAdapterImpl virtual base class for implementing external storage of subscription recover...
Definition: RecoveryPointAdapter.hpp:52
void deepCopy(const Field &orig_)
Makes self a deep copy of the original field.
Definition: Field.hpp:218
Definition: ampsplusplus.hpp:103
iterator end()
Return the end of recovery marker.
Definition: RecoveryPointAdapter.hpp:144
const Field & getSubId() const
Get the sub id for this recovery point.
Definition: RecoveryPoint.hpp:84
virtual void update(RecoveryPoint &recoveryPoint_)
Update the storage information with the given recovery point.
Definition: RecoveryPointAdapter.hpp:252