26 #ifndef _MEMORYBOOKMARKSTORE_H_ 27 #define _MEMORYBOOKMARKSTORE_H_ 29 #include <amps/BookmarkStore.hpp> 41 #define AMPS_MIN_BOOKMARK_LEN 3 42 #define AMPS_INITIAL_MEMORY_BOOKMARK_SIZE 16384UL 63 typedef std::map<Message::Field, size_t, Message::Field::FieldHash> RecoveryMap;
64 typedef std::map<amps_uint64_t, amps_uint64_t> PublisherMap;
65 typedef std::map<Message::Field, size_t, Message::Field::FieldHash>::iterator
67 typedef std::map<amps_uint64_t, amps_uint64_t>::iterator PublisherIterator;
73 : _current(1), _currentBase(0), _least(1), _leastBase(0)
74 , _recoveryMin(AMPS_UNSET_INDEX), _recoveryBase(AMPS_UNSET_INDEX)
75 , _recoveryMax(AMPS_UNSET_INDEX), _recoveryMaxBase(AMPS_UNSET_INDEX)
76 , _entriesLength(AMPS_INITIAL_MEMORY_BOOKMARK_SIZE), _entries(NULL)
81 _store->resize(_id, (
char**)&_entries,
82 sizeof(Entry)*AMPS_INITIAL_MEMORY_BOOKMARK_SIZE,
false);
83 setLastPersistedToEpoch();
88 Lock<Mutex> guard(_subLock);
91 for (
size_t i = 0; i < _entriesLength; ++i)
93 _entries[i]._val.clear();
96 _store->resize(_id, (
char**)&_entries, 0);
100 _lastPersisted.clear();
103 _recoveryTimestamp.clear();
112 Lock<Mutex> guard(_subLock);
114 size_t index = recover(bookmark_,
true);
115 if (index == AMPS_UNSET_INDEX)
118 if (_current >= _entriesLength)
121 _currentBase += _entriesLength;
125 if ((_current == _least && _leastBase < _currentBase) ||
126 (_current == _recoveryMin && _recoveryBase < _currentBase))
128 if (!_store->resize(_id, (
char**)&_entries,
129 sizeof(Entry) * _entriesLength * 2))
132 return log(bookmark_);
165 if (!BookmarkRange::isRange(bookmark_))
167 _entries[_current]._val.deepCopy(bookmark_);
172 _range.set(bookmark_);
174 if (!_range.isValid())
176 throw CommandException(
"Invalid bookmark range specified.");
178 if (!_range.isStartInclusive())
181 amps_uint64_t publisher, sequence;
182 std::vector<Field> bmList = Field::parseBookmarkList(_range.getStart());
183 for (std::vector<Field>::iterator bkmk = bmList.begin(); bkmk != bmList.end(); ++bkmk)
185 parseBookmark(*bkmk, publisher, sequence);
186 if (publisher != (amps_uint64_t)0)
189 PublisherIterator pub = _publishers.find(publisher);
190 if (pub == _publishers.end() || pub->second < sequence)
192 _publishers[publisher] = sequence;
197 Unlock<Mutex> unlock(_subLock);
198 _store->updateAdapter(
this);
203 _entries[_current]._active =
true;
206 return index + _currentBase;
211 Lock<Mutex> guard(_subLock);
212 return _discard(index_);
222 Lock<Mutex> guard(_subLock);
223 size_t search = _least;
224 size_t searchBase = _leastBase;
225 size_t searchMax = _current;
226 size_t searchMaxBase = _currentBase;
227 if (_least + _leastBase == _current + _currentBase)
229 if (_recoveryMin != AMPS_UNSET_INDEX)
231 search = _recoveryMin;
232 searchBase = _recoveryBase;
233 searchMax = _recoveryMax;
234 searchMaxBase = _recoveryMaxBase;
241 assert(searchMax != AMPS_UNSET_INDEX);
242 assert(searchMaxBase != AMPS_UNSET_INDEX);
243 assert(search != AMPS_UNSET_INDEX);
244 assert(searchBase != AMPS_UNSET_INDEX);
246 while (search + searchBase < searchMax + searchMaxBase)
248 if (_entries[search]._val == bookmark_)
250 return _discard(search + searchBase);
252 if (++search == _entriesLength)
255 searchBase += _entriesLength;
264 amps_uint64_t& publisherId_,
265 amps_uint64_t& sequenceNumber_)
267 Message::Field::parseBookmark(field_, publisherId_, sequenceNumber_);
274 Lock<Mutex> guard(_subLock);
275 if (BookmarkRange::isRange(bookmark_))
280 size_t recoveredIdx = recover(bookmark_,
false);
282 amps_uint64_t publisher, sequence;
283 parseBookmark(bookmark_, publisher, sequence);
285 PublisherIterator pub = _publishers.find(publisher);
286 if (pub == _publishers.end() || pub->second < sequence)
288 _publishers[publisher] = sequence;
289 if (recoveredIdx == AMPS_UNSET_INDEX)
294 if (recoveredIdx != AMPS_UNSET_INDEX)
296 if (!_entries[recoveredIdx]._active)
298 _recovered.erase(bookmark_);
306 if (_store->_recovering)
314 size_t base = _leastBase;
315 for (
size_t i = _least; i + base < _current + _currentBase; i++)
317 if ( i >= _entriesLength )
322 if (_entries[i]._val == bookmark_)
324 return !_entries[i]._active;
331 bool empty(
void)
const 333 if (_least == AMPS_UNSET_INDEX ||
334 ((_least + _leastBase) == (_current + _currentBase) &&
335 _recoveryMin == AMPS_UNSET_INDEX))
342 void updateMostRecent()
344 Lock<Mutex> guard(_subLock);
348 const BookmarkRange& getRange()
const 355 Lock<Mutex> guard(_subLock);
356 bool useLastPersisted = !_lastPersisted.empty() &&
357 _lastPersisted.len() > 1;
362 bool useRecent = !_recent.empty() && _recent.len() > 1;
363 amps_uint64_t lastPublisher = 0;
364 amps_uint64_t lastSeq = 0;
365 amps_uint64_t recentPublisher = 0;
366 amps_uint64_t recentSeq = 0;
367 if (useLastPersisted)
369 parseBookmark(_lastPersisted, lastPublisher, lastSeq);
373 parseBookmark(_recent, recentPublisher, recentSeq);
374 if (empty() && useLastPersisted)
380 if (useLastPersisted && lastPublisher == recentPublisher)
382 if (lastSeq <= recentSeq)
388 useLastPersisted =
false;
394 size_t totalLen = (useLastPersisted ? _lastPersisted.len() + 1 : 0);
397 totalLen += _recent.len() + 1;
402 if (usePublishersList_
403 && ((!useLastPersisted && !useRecent)
406 std::ostringstream os;
407 for (PublisherIterator pub = _publishers.begin();
408 pub != _publishers.end(); ++pub)
410 if (pub->first == 0 && pub->second == 0)
414 if (pub->first == recentPublisher && recentSeq < pub->second)
416 os << recentPublisher <<
'|' << recentSeq <<
"|,";
420 os << pub->first <<
'|' << pub->second <<
"|,";
423 std::string recent = os.str();
424 totalLen = recent.length();
427 if (!_recoveryTimestamp.empty())
429 totalLen += _recoveryTimestamp.len();
430 recent += std::string(_recoveryTimestamp);
435 recent.erase(--totalLen);
440 if (_range.isValid())
442 if (_range.getStart() != recent
445 _range.replaceStart(_recentList,
true);
447 else if (_range.isStartInclusive())
449 amps_uint64_t publisher, sequence;
450 parseBookmark(_range.getStart(), publisher,
452 PublisherIterator pub = _publishers.find(publisher);
453 if (pub != _publishers.end()
454 && pub->second >= sequence)
456 _range.makeStartExclusive();
463 if (_range.isValid())
468 if (!_recoveryTimestamp.empty() && !_range.isValid())
470 totalLen += _recoveryTimestamp.len() + 1;
474 || (_recent.len() < 2 && !empty()))
476 if (_range.isValid())
484 _setLastPersistedToEpoch();
485 return _lastPersisted;
489 char* field =
new char[totalLen];
494 memcpy(field, _recent.data(), len);
500 if (useLastPersisted)
502 memcpy(field + len, _lastPersisted.data(), _lastPersisted.len());
503 len += _lastPersisted.len();
509 if (!_recoveryTimestamp.empty() && !_range.isValid())
511 memcpy(field + len, _recoveryTimestamp.data(),
512 _recoveryTimestamp.len());
519 _recentList.assign(field, totalLen);
520 if (_range.isValid())
524 if (_range.getStart() != _recentList)
526 _range.replaceStart(_recentList,
true);
528 else if (_range.isStartInclusive())
530 amps_uint64_t publisher, sequence;
531 parseBookmark(_range.getStart(), publisher,
533 PublisherIterator pub = _publishers.find(publisher);
534 if (pub != _publishers.end()
535 && pub->second >= sequence)
537 _range.makeStartExclusive();
548 Lock<Mutex> guard(_subLock);
551 if (update_ && _store->_recentChanged)
567 Lock<Mutex> guard(_subLock);
568 return _lastPersisted;
574 _recent.deepCopy(recent_);
577 void setRecoveryTimestamp(
const char* recoveryTimestamp_,
580 _recoveryTimestamp.clear();
581 size_t len = (len_ == 0) ? AMPS_TIMESTAMP_LEN : len_;
582 char* ts =
new char[len];
583 memcpy((
void*)ts, (
const void*)recoveryTimestamp_, len);
584 _recoveryTimestamp.assign(ts, len);
587 void moveEntries(
char* old_,
char* new_,
size_t newSize_)
589 size_t least = _least;
590 size_t leastBase = _leastBase;
591 if (_recoveryMin != AMPS_UNSET_INDEX)
593 least = _recoveryMin;
594 leastBase = _recoveryBase;
599 if (newSize_ - (
sizeof(Entry)*_entriesLength) >
sizeof(Entry)*least)
601 memcpy(new_ + (
sizeof(Entry)*_entriesLength),
602 old_, (
sizeof(Entry)*least));
604 memset(old_, 0,
sizeof(Entry)*least);
608 Entry* buffer =
new Entry[least];
609 memcpy((
void*)buffer, (
void*)old_,
sizeof(Entry)*least);
611 memcpy((
void*)new_, (
void*)((
char*)old_ + (
sizeof(Entry)*least)),
612 (_entriesLength - least)*
sizeof(Entry));
614 memcpy((
void*)((
char*)new_ + ((_entriesLength - least)*
sizeof(Entry))),
615 (
void*)buffer, least *
sizeof(Entry));
625 memcpy((
void*)new_, (
void*)((
char*)old_ + (
sizeof(Entry)*least)),
626 (_entriesLength - least)*
sizeof(Entry));
628 memcpy((
void*)((
char*)new_ + ((_entriesLength - least)*
sizeof(Entry))),
629 (
void*)old_, least *
sizeof(Entry));
634 if (_recoveryMin != AMPS_UNSET_INDEX)
636 _least = least + (_least + _leastBase) - (_recoveryMin + _recoveryBase);
637 _recoveryMax = least + (_recoveryMax + _recoveryMaxBase) -
638 (_recoveryMin + _recoveryBase);
639 _recoveryMaxBase = leastBase;
640 _recoveryMin = least;
641 _recoveryBase = leastBase;
647 _leastBase = leastBase;
649 _currentBase = _leastBase;
650 _current = least + _entriesLength;
655 Lock<Mutex> guard(_subLock);
657 return ((_least + _leastBase) == (_current + _currentBase)) ? AMPS_UNSET_INDEX :
665 || BookmarkRange::isRange(bookmark_))
669 Lock<Mutex> guard(_subLock);
670 return _setLastPersisted(bookmark_);
675 if (!_lastPersisted.empty())
677 amps_uint64_t publisher, publisher_lastPersisted;
678 amps_uint64_t sequence, sequence_lastPersisted;
679 parseBookmark(bookmark_, publisher, sequence);
680 parseBookmark(_lastPersisted, publisher_lastPersisted,
681 sequence_lastPersisted);
682 if (publisher == publisher_lastPersisted &&
683 sequence <= sequence_lastPersisted)
689 _lastPersisted.deepCopy(bookmark_);
690 _store->_recentChanged =
true;
691 _recoveryTimestamp.clear();
697 Lock<Mutex> guard(_subLock);
701 || BookmarkRange::isRange(bookmark))
705 _setLastPersisted(bookmark);
713 size_t recover(
const Message::Field& bookmark_,
bool relogIfNotDiscarded)
715 size_t retVal = AMPS_UNSET_INDEX;
716 if (_recovered.empty() || _recoveryBase == AMPS_UNSET_INDEX)
722 RecoveryIterator item = _recovered.find(bookmark_);
723 if (item != _recovered.end())
725 size_t seqNo = item->second;
726 size_t index = (seqNo - _recoveryBase) % _entriesLength;
729 if (_least + _leastBase == _current + _currentBase &&
730 !_entries[index]._active)
732 _store->_recentChanged =
true;
734 _recent = _entries[index]._val.deepCopy();
735 retVal = moveEntry(index);
736 if (retVal == AMPS_UNSET_INDEX)
738 recover(bookmark_, relogIfNotDiscarded);
741 _leastBase = _currentBase;
743 else if (!_entries[index]._active || relogIfNotDiscarded)
745 retVal = moveEntry(index);
746 if (retVal == AMPS_UNSET_INDEX)
748 recover(bookmark_, relogIfNotDiscarded);
755 _recovered.erase(item);
756 if (_recovered.empty())
758 _recoveryMin = AMPS_UNSET_INDEX;
759 _recoveryBase = AMPS_UNSET_INDEX;
760 _recoveryMax = AMPS_UNSET_INDEX;
761 _recoveryMaxBase = AMPS_UNSET_INDEX;
763 else if (index == _recoveryMin)
765 while (_entries[_recoveryMin]._val.empty() &&
766 (_recoveryMin + _recoveryBase) < (_recoveryMax + _recoveryMaxBase))
768 if (++_recoveryMin == _entriesLength)
771 _recoveryBase += _entriesLength;
791 Entry() : _active(
false)
799 Field::FieldHash _hasher;
801 size_t operator()(
const Entry* entryPtr_)
const 803 return _hasher(entryPtr_->_val);
806 bool operator()(
const Entry* lhsPtr_,
const Entry* rhsPtr_)
const 808 return _hasher(lhsPtr_->_val, rhsPtr_->_val);
813 typedef std::vector<Entry*> EntryPtrList;
815 void getRecoveryEntries(EntryPtrList& list_)
817 if (_recoveryMin == AMPS_UNSET_INDEX ||
818 _recoveryMax == AMPS_UNSET_INDEX)
822 size_t base = _recoveryBase;
823 size_t max = _recoveryMax + _recoveryMaxBase;
824 for (
size_t i = _recoveryMin; i + base < max; ++i)
826 if (i == _entriesLength)
829 base = _recoveryMaxBase;
832 list_.push_back(&(_entries[i]));
837 void getActiveEntries(EntryPtrList& list_)
839 size_t base = _leastBase;
840 for (
size_t i = _least; i + base < _current + _currentBase; ++i)
842 if (i >= _entriesLength)
848 list_.push_back(&(_entries[i]));
853 Entry* getEntryByIndex(
size_t index_)
855 Lock<Mutex> guard(_subLock);
856 size_t base = (_recoveryBase == AMPS_UNSET_INDEX ||
857 index_ >= _least + _leastBase)
858 ? _leastBase : _recoveryBase;
860 size_t min = (_recoveryMin == AMPS_UNSET_INDEX ?
861 _least + _leastBase :
862 _recoveryMin + _recoveryBase);
863 if (index_ >= _current + _currentBase || index_ < min)
867 return &(_entries[(index_ - base) % _entriesLength]);
872 Lock<Mutex> guard(_subLock);
875 getRecoveryEntries(list);
876 setPublishersToDiscarded(&list, &_publishers);
879 void setPublishersToDiscarded(EntryPtrList* recovered_,
880 PublisherMap* publishers_)
886 for (EntryPtrList::iterator i = recovered_->begin();
887 i != recovered_->end(); ++i)
889 if ((*i)->_val.empty())
893 amps_uint64_t publisher = (amps_uint64_t)0;
894 amps_uint64_t sequence = (amps_uint64_t)0;
895 parseBookmark((*i)->_val, publisher, sequence);
896 if (publisher && sequence && (*i)->_active &&
897 (*publishers_)[publisher] >= sequence)
899 (*publishers_)[publisher] = sequence - 1;
904 void clearLastPersisted()
906 Lock<Mutex> guard(_subLock);
907 _lastPersisted.clear();
910 void setLastPersistedToEpoch()
912 Lock<Mutex> guard(_subLock);
913 _setLastPersistedToEpoch();
917 Subscription(
const Subscription&);
918 Subscription& operator=(
const Subscription&);
920 size_t moveEntry(
size_t index_)
923 if (_current >= _entriesLength)
926 _currentBase += _entriesLength;
930 if ((_current == _least % _entriesLength &&
931 _leastBase < _currentBase) ||
932 (_current == _recoveryMin && _recoveryBase < _currentBase))
934 if (!_store->resize(_id, (
char**)&_entries,
935 sizeof(Entry) * _entriesLength * 2))
937 return AMPS_UNSET_INDEX;
942 _entries[_current]._val = _entries[index_]._val;
943 _entries[_current]._active = _entries[index_]._active;
945 _entries[index_]._val.assign(NULL, 0);
946 _entries[index_]._active =
false;
950 void _setLastPersistedToEpoch()
953 char* field =
new char[fieldLen];
955 _lastPersisted.clear();
956 _lastPersisted.assign(field, fieldLen);
959 bool _discard(
size_t index_)
963 assert((_recoveryBase == AMPS_UNSET_INDEX && _recoveryMin == AMPS_UNSET_INDEX) ||
964 (_recoveryBase != AMPS_UNSET_INDEX && _recoveryMin != AMPS_UNSET_INDEX));
965 size_t base = (_recoveryBase == AMPS_UNSET_INDEX
966 || index_ >= _least + _leastBase)
967 ? _leastBase : _recoveryBase;
969 size_t min = (_recoveryMin == AMPS_UNSET_INDEX ? _least + _leastBase :
970 _recoveryMin + _recoveryBase);
971 if (index_ >= _current + _currentBase || index_ < min)
978 Entry& e = _entries[(index_ - base) % _entriesLength];
981 size_t index = index_;
982 if (_recoveryMin != AMPS_UNSET_INDEX &&
983 index_ == _recoveryMin + _recoveryBase)
986 size_t j = _recoveryMin;
987 while (j + _recoveryBase < _recoveryMax + _recoveryMaxBase &&
988 !_entries[j]._active)
1009 if (!bookmark.
empty())
1011 _recovered.erase(bookmark);
1013 amps_uint64_t publisher, sequence;
1014 parseBookmark(bookmark, publisher, sequence);
1015 PublisherIterator pub = _publishers.find(publisher);
1016 if (pub == _publishers.end() || pub->second < sequence)
1018 _publishers[publisher] = sequence;
1020 if (_least + _leastBase == _current + _currentBase ||
1021 ((_least + _leastBase) % _entriesLength) ==
1022 ((_recoveryMin + _recoveryBase + 1)) % _entriesLength)
1026 _store->_recentChanged =
true;
1027 _recoveryTimestamp.clear();
1030 bookmark.assign(NULL, 0);
1040 if (++j == _entriesLength)
1043 _recoveryBase += _entriesLength;
1047 assert(j + _recoveryBase != _recoveryMax + _recoveryMaxBase ||
1048 _recovered.empty());
1049 if (_recovered.empty())
1051 _recoveryMin = AMPS_UNSET_INDEX;
1052 _recoveryBase = AMPS_UNSET_INDEX;
1053 _recoveryMax = AMPS_UNSET_INDEX;
1054 _recoveryMaxBase = AMPS_UNSET_INDEX;
1056 index = _least + _leastBase;
1065 if (index == _least + _leastBase)
1069 while (j + _leastBase < _current + _currentBase &&
1070 !_entries[j]._active)
1074 _recent = _entries[j]._val;
1075 _entries[j]._val.assign(NULL, 0);
1076 _store->_recentChanged =
true;
1078 _recoveryTimestamp.clear();
1081 if (++j == _entriesLength)
1084 _leastBase += _entriesLength;
1093 void _updateMostRecent()
1097 assert((_recoveryBase == AMPS_UNSET_INDEX && _recoveryMin == AMPS_UNSET_INDEX) ||
1098 (_recoveryBase != AMPS_UNSET_INDEX && _recoveryMin != AMPS_UNSET_INDEX));
1099 size_t base = (_recoveryMin == AMPS_UNSET_INDEX) ? _leastBase : _recoveryBase;
1100 size_t start = (_recoveryMin == AMPS_UNSET_INDEX) ? _least : _recoveryMin;
1101 _recoveryMin = AMPS_UNSET_INDEX;
1102 _recoveryBase = AMPS_UNSET_INDEX;
1103 _recoveryMax = AMPS_UNSET_INDEX;
1104 _recoveryMaxBase = AMPS_UNSET_INDEX;
1105 for (
size_t i = start; i + base < _current + _currentBase; i++)
1107 if ( i >= _entriesLength )
1110 base = _currentBase;
1112 if (i >= _recoveryMax + _recoveryBase && i < _least + _leastBase)
1116 Entry& entry = _entries[i];
1117 if (!entry._val.empty())
1119 _recovered[entry._val] = i + base;
1120 if (_recoveryMin == AMPS_UNSET_INDEX)
1123 _recoveryBase = base;
1124 _recoveryMax = _current;
1125 _recoveryMaxBase = _currentBase;
1129 if (_current == _entriesLength)
1132 _currentBase += _entriesLength;
1135 _leastBase = _currentBase;
1142 BookmarkRange _range;
1145 size_t _currentBase;
1148 size_t _recoveryMin;
1149 size_t _recoveryBase;
1150 size_t _recoveryMax;
1151 size_t _recoveryMaxBase;
1152 size_t _entriesLength;
1156 RecoveryMap _recovered;
1158 PublisherMap _publishers;
1167 _serverVersion(AMPS_DEFAULT_MIN_VERSION),
1168 _recentChanged(true),
1170 _recoveryPointAdapter(NULL),
1171 _recoveryPointFactory(NULL)
1174 typedef RecoveryPointAdapter::iterator RecoveryIterator;
1183 RecoveryPointFactory factory_ = NULL)
1187 , _serverVersion(AMPS_DEFAULT_MIN_VERSION)
1188 , _recentChanged(true)
1190 , _recoveryPointAdapter(adapter_)
1191 , _recoveryPointFactory(factory_)
1194 if (!_recoveryPointFactory)
1198 for (RecoveryIterator recoveryPoint = _recoveryPointAdapter.begin();
1199 recoveryPoint != _recoveryPointAdapter.end();
1202 Field subId(recoveryPoint->getSubId());
1203 msg.setSubscriptionHandle(static_cast<amps_subscription_handle>(0));
1205 Field bookmark = recoveryPoint->getBookmark();
1206 if (BookmarkRange::isRange(bookmark))
1213 std::vector<Field> bmList = Field::parseBookmarkList(bookmark);
1214 for (std::vector<Field>::iterator bkmk = bmList.begin(); bkmk != bmList.end(); ++bkmk)
1216 if (Field::isTimestamp(*bkmk))
1218 find(subId)->setRecoveryTimestamp(bkmk->data(), bkmk->len());
1232 _recovering =
false;
1237 if (_recoveryPointAdapter.isValid())
1239 _recoveryPointAdapter.close();
1251 Lock<Mutex> guard(_lock);
1252 return _log(message_);
1262 Lock<Mutex> guard(_lock);
1263 (void)_discard(message_);
1275 Lock<Mutex> guard(_lock);
1276 (void)_discard(subId_, bookmarkSeqNo_);
1286 Lock<Mutex> guard(_lock);
1287 return _getMostRecent(subId_);
1300 Lock<Mutex> guard(_lock);
1301 return _isDiscarded(message_);
1311 Lock<Mutex> guard(_lock);
1322 Lock<Mutex> guard(_lock);
1332 Lock<Mutex> guard(_lock);
1333 return _getOldestBookmarkSeq(subId_);
1344 Lock<Mutex> guard(_lock);
1345 _persisted(find(subId_), bookmark_);
1357 Lock<Mutex> guard(_lock);
1358 return _persisted(find(subId_), bookmark_);
1376 Lock<Mutex> guard(_subsLock);
1377 _serverVersion = version_;
1380 inline bool isWritableBookmark(
size_t length)
1382 return length >= AMPS_MIN_BOOKMARK_LEN;
1385 typedef Subscription::EntryPtrList EntryPtrList;
1390 size_t _log(
Message& message_)
1393 Subscription* pSub = (Subscription*)(message_.getSubscriptionHandle());
1402 message_.setSubscriptionHandle(
1403 static_cast<amps_subscription_handle>(pSub));
1405 size_t retVal = pSub->log(bookmark);
1406 message_.setBookmarkSeqNo(retVal);
1411 bool _discard(
const Message& message_)
1413 size_t bookmarkSeqNo = message_.getBookmarkSeqNo();
1414 Subscription* pSub = (Subscription*)(message_.getSubscriptionHandle());
1424 bool retVal = pSub->discard(bookmarkSeqNo);
1427 updateAdapter(pSub);
1433 bool _discard(
const Message::Field& subId_,
size_t bookmarkSeqNo_)
1435 Subscription* pSub = find(subId_);
1436 bool retVal = pSub->discard(bookmarkSeqNo_);
1439 updateAdapter(pSub);
1446 bool usePublishersList_ =
true)
1448 Subscription* pSub = find(subId_);
1449 return pSub->getMostRecentList(usePublishersList_);
1453 bool _isDiscarded(
Message& message_)
1460 Subscription* pSub = find(subId);
1461 message_.setSubscriptionHandle(
1462 static_cast<amps_subscription_handle>(pSub));
1469 Subscription* pSub = find(subId_);
1470 return pSub->getOldestBookmarkSeq();
1474 virtual void _persisted(Subscription* pSub_,
1477 if (pSub_->lastPersisted(bookmark_))
1479 updateAdapter(pSub_);
1484 virtual Message::Field _persisted(Subscription* pSub_,
size_t bookmark_)
1486 return pSub_->lastPersisted(bookmark_);
1492 if (_recoveryPointAdapter.isValid())
1494 _recoveryPointAdapter.purge();
1503 while (!_subs.empty())
1505 SubscriptionMap::iterator iter = _subs.begin();
1509 delete (iter->second);
1518 if (_recoveryPointAdapter.isValid())
1520 _recoveryPointAdapter.purge(subId_);
1528 Lock<Mutex> guard(_subsLock);
1529 SubscriptionMap::iterator iter = _subs.find(subId_);
1530 if (iter == _subs.end())
1535 delete (iter->second);
1543 find(subId_)->setMostRecent(recent_);
1548 static const char ENTRY_BOOKMARK =
'b';
1549 static const char ENTRY_DISCARD =
'd';
1550 static const char ENTRY_PERSISTED =
'p';
1556 throw StoreException(
"A valid subscription ID must be provided to the Bookmark Store");
1558 Lock<Mutex> guard(_subsLock);
1559 if (_subs.count(subId_) == 0)
1564 _subs[id] =
new Subscription(
this,
id);
1567 return _subs[subId_];
1570 virtual bool resize(
const Message::Field& subId_,
char** newBuffer_,
size_t size_,
1571 bool callResizeHandler_ =
true)
1573 assert(newBuffer_ != 0);
1583 if (callResizeHandler_ && !callResizeHandler(subId_, size_))
1587 char* oldBuffer = *newBuffer_ ? *newBuffer_ : NULL;
1588 *newBuffer_ = (
char*)malloc(size_);
1589 memset(*newBuffer_, 0, size_);
1592 find(subId_)->moveEntries(oldBuffer, *newBuffer_, size_);
1599 void updateAdapter(Subscription* pSub_)
1601 if (_recovering || !_recentChanged || !_recoveryPointAdapter.isValid())
1608 pSub_->getMostRecentList(
false)).deepCopy();
1609 Unlock<Mutex> unlock(_lock);
1610 _recoveryPointAdapter.update(update);
1615 typedef std::map<Message::Field, Subscription*, Message::Field::FieldHash> SubscriptionMap;
1616 SubscriptionMap _subs;
1617 size_t _serverVersion;
1618 bool _recentChanged;
1620 typedef std::set<Subscription*> SubscriptionSet;
1627 #endif //_MEMORYBOOKMARKSTORE_H_ Defines the AMPS::Message class and related classes.
Abstract base class for storing received bookmarks for HA clients.
Definition: BookmarkStore.hpp:77
void clear()
Clear the internal state, possibly reclaiming memory.
Definition: RecoveryPoint.hpp:109
Field getSubscriptionId() const
Retrieves the value of the SubscriptionId header of the Message as a new Field.
Definition: Message.hpp:1427
virtual void purge()
Called to purge the contents of this store.
Definition: MemoryBookmarkStore.hpp:1309
virtual void discard(const Message::Field &subId_, size_t bookmarkSeqNo_)
Log a discard-bookmark entry to the persistent log based on a bookmark sequence number.
Definition: MemoryBookmarkStore.hpp:1273
virtual Message::Field getMostRecent(const Message::Field &subId_)
Returns the most recent bookmark from the log that ought to be used for (re-)subscriptions.
Definition: MemoryBookmarkStore.hpp:1284
virtual Message::Field persisted(const Message::Field &subId_, size_t bookmark_)
Mark the bookmark provided as replicated to all sync replication destinations for the given subscript...
Definition: MemoryBookmarkStore.hpp:1354
Message encapsulates a single message sent to or received from an AMPS server, and provides methods f...
Definition: Message.hpp:531
void clear()
Deletes the data associated with this Field, should only be used on Fields that were created as deepC...
Definition: Field.hpp:247
MemoryBookmarkStore(const RecoveryPointAdapter &adapter_, RecoveryPointFactory factory_=NULL)
Creates a MemoryBookmarkStore.
Definition: MemoryBookmarkStore.hpp:1182
RecoveryPointAdapter a handle class for implementing external storage of subscription recovery points...
Definition: RecoveryPointAdapter.hpp:77
MemoryBookmarkStore()
Creates a MemoryBookmarkStore.
Definition: MemoryBookmarkStore.hpp:1164
Provides access to the subId and bookmark needed to restart a subscription.
Definition: RecoveryPoint.hpp:67
virtual void persisted(const Message::Field &subId_, const Message::Field &bookmark_)
Mark the bookmark provided as replicated to all sync replication destinations for the given subscript...
Definition: MemoryBookmarkStore.hpp:1341
Message & assignBookmark(const std::string &v)
Sets the value of the Bookmark header for this Message.
Definition: Message.hpp:1194
void setServerVersion(size_t version_)
Internally used to set the server version so the store knows how to deal with persisted acks and call...
Definition: MemoryBookmarkStore.hpp:1374
Field getSubscriptionIds() const
Retrieves the value of the SubscriptionIds header of the Message as a new Field.
Definition: Message.hpp:1428
bool empty() const
Returns 'true' if empty, 'false' otherwise.
Definition: Field.hpp:128
Defines the AMPS::Field class, which represents the value of a field in a message.
#define AMPS_BOOKMARK_EPOCH
Start the subscription at the beginning of the journal.
Definition: BookmarkStore.hpp:51
virtual bool isDiscarded(Message &message_)
Called for each arriving message to determine if the application has already seen this bookmark and s...
Definition: MemoryBookmarkStore.hpp:1298
Provides AMPS::RecoveryPointAdapter, an iterface for implementing external storage of bookmark subscr...
void setServerVersion(const VersionInfo &version_)
Internally used to set the server version so the store knows how to deal with persisted acks and call...
Definition: MemoryBookmarkStore.hpp:1365
virtual void discard(const Message &message_)
Log a discard-bookmark entry to the persistent log based on a bookmark sequence number.
Definition: MemoryBookmarkStore.hpp:1260
#define AMPS_BOOKMARK_NOW
Start the subscription at the point in time when AMPS processes the subscription. ...
Definition: BookmarkStore.hpp:55
Message & setSubId(const std::string &v)
Sets the value of the SubscriptionId header for this Message.
Definition: Message.hpp:1427
RecoveryPoint(* RecoveryPointFactory)(const Field &subId_, const Field &bookmark_)
RecoveryPointFactory is a function type for producing a RecoveryPoint that is sent to a RecoveryPoint...
Definition: RecoveryPoint.hpp:126
Provides AMPS::RecoveryPoint, AMPS::RecoveryPointFactory, AMPS::FixedRecoveryPoint, and AMPS::DynamicRecoveryPoint.
A BookmarkStoreImpl implementation that stores bookmarks in memory.
Definition: MemoryBookmarkStore.hpp:57
virtual size_t log(Message &message_)
Log a bookmark to the persistent log and return the corresponding sequence number for this bookmark...
Definition: MemoryBookmarkStore.hpp:1249
Field represents the value of a single field in a Message.
Definition: Field.hpp:86
static RecoveryPoint create(const Field &subId_, const Field &bookmark_)
Use this function in BookmarkStore::setRecoveryPointFactory( std::bind(&FixedRecoveryPoint::create, std::placeholder::_1, std::placeholder::_2))
Definition: RecoveryPoint.hpp:139
virtual size_t getOldestBookmarkSeq(const Message::Field &subId_)
Called to find the oldest bookmark in the store.
Definition: MemoryBookmarkStore.hpp:1330
virtual void purge(const Message::Field &subId_)
Called to purge the contents of this store for particular subId.
Definition: MemoryBookmarkStore.hpp:1320
Message & setBookmark(const std::string &v)
Sets the value of the Bookmark header for this Message.
Definition: Message.hpp:1194
void deepCopy(const Field &orig_)
Makes self a deep copy of the original field.
Definition: Field.hpp:218
Definition: ampsplusplus.hpp:102
Field getBookmark() const
Retrieves the value of the Bookmark header of the Message as a new Field.
Definition: Message.hpp:1194