26 #ifndef _RECOVERYPOINTADAPTER_H_ 27 #define _RECOVERYPOINTADAPTER_H_ 35 #include <unordered_map> 45 class RecoveryPointAdapter;
62 virtual void purge() = 0;
67 virtual void close() = 0;
83 if (!_pAdapter || !_pAdapter->
next(_current))
99 bool operator==(
const iterator& rhs)
101 return _pAdapter == rhs._pAdapter;
103 bool operator!=(
const iterator& rhs)
105 return _pAdapter != rhs._pAdapter;
107 void operator++(
void) { advance(); }
114 : _body(body_, isRef_) { }
125 iterator
begin() {
return iterator(&(_body.get())); }
129 iterator
end() {
return iterator(); }
135 _body.get().update(recoveryPoint_);
139 void purge() { _body.get().purge(); }
143 void purge(
const Field& subId_) { _body.get().purge(subId_); }
146 void close() { _body.get().close(); }
149 void prune() { _body.get().prune(); }
154 return _body.isValid();
157 BorrowRefHandle<RecoveryPointAdapterImpl> _body;
181 const std::shared_ptr<RecoveryPointAdapterImpl>& delegate_,
182 unsigned updateThreshold_ = 10,
183 double timeoutMillis_ = 2000.0,
184 long updateIntervalMillis_ = 2000
187 , _delegate(delegate_)
188 , _updateThreshold(updateThreshold_)
189 , _timeoutMillis(timeoutMillis_)
190 , _updateIntervalMillis(updateIntervalMillis_)
194 _thread = std::thread(&ConflatingRecoveryPointAdapter::updateThread,
202 for (UpdateIter purged = _latestUpdates.begin();
203 purged != _latestUpdates.end(); ++purged)
205 Field clearableSubId = purged->first;
206 purged->second.
clear();
207 clearableSubId.
clear();
217 return _delegate->next(current_);
225 Lock<Mutex> lock(_lock);
226 UpdateIter lastUpdate = _latestUpdates.find(subId);
227 if (lastUpdate == _latestUpdates.end())
231 _latestUpdates[subId] = recoveryPoint_.
deepCopy();
233 if (_timeoutMillis != 0.0)
235 Timer timer(_timeoutMillis);
237 _timers[subId] = timer;
243 lastUpdate->second.deepCopy(recoveryPoint_);
245 if (++_counts[subId] >= _updateThreshold)
257 Lock<Mutex> lock(_lock);
260 for (UpdateIter purged = _latestUpdates.begin();
261 purged != _latestUpdates.end(); ++purged)
263 Field clearableSubId = purged->first;
264 purged->second.
clear();
265 clearableSubId.
clear();
267 _latestUpdates.clear();
274 _delegate->purge(subId_);
275 Lock<Mutex> lock(_lock);
276 UpdateIter purged = _latestUpdates.find(subId_);
277 if (purged != _latestUpdates.end())
279 Field clearableSubId = purged->first;
280 purged->second.
clear();
281 _latestUpdates.erase(purged);
282 _counts.erase(subId_);
283 _timers.erase(subId_);
284 clearableSubId.
clear();
297 Lock<Mutex> lock(_lock);
305 while (!_counts.empty())
318 Lock<Mutex> lock(_lock);
328 std::vector<SavedUpdate> _queuedUpdates;
331 DeferLock<Mutex> lock(_lock);
334 _lock.wait(_updateIntervalMillis);
337 for (TimerMap::iterator timer = _timers.begin();
338 timer != _timers.end(); )
340 if (timer->second.check())
342 UpdateIter
update = _latestUpdates.find(timer->first);
343 if (update != _latestUpdates.end())
347 _queuedUpdates.push_back(*update);
348 _counts.erase(update->first);
349 timer = _timers.erase(timer);
350 _latestUpdates.erase(update);
365 volatile bool updateAll = _updateAll;
367 for (CountMap::iterator count = _counts.begin();
368 count != _counts.end(); )
370 if (updateAll || _timeoutMillis == 0.0
371 || count->second >= _updateThreshold)
373 UpdateIter
update = _latestUpdates.find(count->first);
374 if (update != _latestUpdates.end())
378 _queuedUpdates.push_back(*update);
379 count = _counts.erase(count);
380 _timers.erase(update->first);
381 _latestUpdates.erase(update);
400 for (std::vector<SavedUpdate>::iterator
update = _queuedUpdates.begin(), end = _queuedUpdates.end();
update != end; ++
update)
402 _delegate->update(
update->second);
404 clearableSubId.
clear();
407 _queuedUpdates.clear();
417 volatile bool _closed;
420 std::shared_ptr<RecoveryPointAdapterImpl> _delegate;
426 typedef std::unordered_map<Field, RecoveryPoint, Field::FieldHash> UpdateMap;
427 typedef std::pair<Field, RecoveryPoint> SavedUpdate;
428 typedef UpdateMap::value_type Update;
429 typedef UpdateMap::iterator UpdateIter;
430 typedef std::unordered_map<Field, Timer, Field::FieldHash> TimerMap;
431 typedef TimerMap::iterator TimerIter;
432 typedef std::unordered_map<Field, unsigned, Field::FieldHash> CountMap;
433 typedef CountMap::iterator CountIter;
436 UpdateMap _latestUpdates;
448 unsigned _updateThreshold;
451 double _timeoutMillis;
454 long _updateIntervalMillis;
457 volatile bool _updateAll;
462 #endif //_RECOVERYPOINTADAPTER_H_ virtual void close()
Take any necessary actions to close the associated storage.
Definition: RecoveryPointAdapter.hpp:289
void purge()
Remove all data from the storage.
Definition: RecoveryPointAdapter.hpp:139
virtual void purge(const Field &subId_)
Remove the specified subId_ from the storage.
Definition: RecoveryPointAdapter.hpp:272
virtual void purge()=0
Remove all data from the storage.
void prune()
Take any necessary actions to close the associated storage.
Definition: RecoveryPointAdapter.hpp:149
RecoveryPointAdapter implementation that delegates storage to another RecoveryPointAdapter but provid...
Definition: RecoveryPointAdapter.hpp:163
void purge(const Field &subId_)
Remove the specified subId_ from the storage.
Definition: RecoveryPointAdapter.hpp:143
virtual void _runUpdateAll()
Lock is already held.
Definition: RecoveryPointAdapter.hpp:302
virtual void purge()
Remove all data from the storage.
Definition: RecoveryPointAdapter.hpp:254
void clear()
Deletes the data associated with this Field, should only be used on Fields that were created as deepC...
Definition: Field.hpp:241
virtual void prune()
Take any necessary actions to reduce associated storage size.
Definition: RecoveryPointAdapter.hpp:69
RecoveryPointAdapter a handle class for implementing external storage of subscription recovery points...
Definition: RecoveryPointAdapter.hpp:74
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:152
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:180
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:133
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:215
iterator begin()
To recover from an adapter, iterate over the adapter from begin() to end() with a RecoveryPointIterat...
Definition: RecoveryPointAdapter.hpp:125
Field represents the value of a single field in a Message.
Definition: Field.hpp:84
virtual void updateAll()
Push all updates to underlying adapter.
Definition: RecoveryPointAdapter.hpp:295
void close()
Take any necessary actions to close the associated storage.
Definition: RecoveryPointAdapter.hpp:146
RecoveryPointAdapterImpl virtual base class for implementing external storage of subscription recover...
Definition: RecoveryPointAdapter.hpp:49
void deepCopy(const Field &orig_)
Makes self a deep copy of the original field.
Definition: Field.hpp:212
Definition: ampsplusplus.hpp:103
iterator end()
Return the end of recovery marker.
Definition: RecoveryPointAdapter.hpp:129
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:222