AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.3.0
BookmarkStore.hpp
1 //
3 // Copyright (c) 2010-2021 60East Technologies Inc., All Rights Reserved.
4 //
5 // This computer software is owned by 60East Technologies Inc. and is
6 // protected by U.S. copyright laws and other laws and by international
7 // treaties. This computer software is furnished by 60East Technologies
8 // Inc. pursuant to a written license agreement and may be used, copied,
9 // transmitted, and stored only in accordance with the terms of such
10 // license agreement and with the inclusion of the above copyright notice.
11 // This computer software or any other copies thereof may not be provided
12 // or otherwise made available to any other person.
13 //
14 // U.S. Government Restricted Rights. This computer software: (a) was
15 // developed at private expense and is in all respects the proprietary
16 // information of 60East Technologies Inc.; (b) was not developed with
17 // government funds; (c) is a trade secret of 60East Technologies Inc.
18 // for all purposes of the Freedom of Information Act; and (d) is a
19 // commercial item and thus, pursuant to Section 12.212 of the Federal
20 // Acquisition Regulations (FAR) and DFAR Supplement Section 227.7202,
21 // Government's use, duplication or disclosure of the computer software
22 // is subject to the restrictions set forth by 60East Technologies Inc..
23 //
25 #ifndef _BOOKMARKSTORE_H_
26 #define _BOOKMARKSTORE_H_
27 #include <string>
28 #include "Message.hpp"
29 #include "util.hpp"
30 
31 namespace AMPS
32 {
33 
44 #define AMPS_BOOKMARK_RECENT "recent"
48 
51 #define AMPS_BOOKMARK_EPOCH "0"
52 
55 #define AMPS_BOOKMARK_NOW "0|1|"
56 
57 /* @} */
58 
59 class BookmarkStore;
60 
70 typedef bool (*BookmarkStoreResizeHandler)(BookmarkStore store_,
71  const Message::Field& subId_,
72  size_t size_,
73  void* userData_);
74 
77 class BookmarkStoreImpl : public RefBody
78 {
79 public:
81  : _resizeHandler(NULL)
82  , _resizeHandlerData(NULL)
83  , _maxSubIdLength(AMPS_MAX_SUBID_LEN)
84  {;}
85 
86  virtual ~BookmarkStoreImpl() {;}
87 
94  virtual size_t log(Message& message_) = 0;
95 
102  virtual void discard(const Message::Field& subId_,
103  size_t bookmarkSeqNo_) = 0;
104 
110  virtual void discard(const Message& message_) = 0;
111 
118  virtual Message::Field getMostRecent(const Message::Field& subId_) = 0;
119 
128  virtual bool isDiscarded(Message& message_) = 0;
129 
135  virtual void purge() = 0;
136 
142  virtual void purge(const Message::Field& subId_) = 0;
143 
148  virtual size_t getOldestBookmarkSeq(const Message::Field& subId_) = 0;
149 
156  virtual void setResizeHandler(BookmarkStoreResizeHandler handler_, void* userData_)
157  {
158  _resizeHandler = handler_;
159  _resizeHandlerData = userData_;
160  }
161 
167  virtual void persisted(const Message::Field& subId_,
168  const Message::Field& bookmark_) = 0;
169 
176  virtual Message::Field persisted(const Message::Field& subId_, size_t bookmark_) = 0;
177 
182  virtual void setServerVersion(size_t version_) = 0;
183 
188  virtual void setServerVersion(const VersionInfo& version_) = 0;
189 
190  bool callResizeHandler(const Message::Field& subId_, size_t newSize_);
191 
192  inline void prune(const std::string& tmpFileName_ = std::string())
193  {
194  _prune(tmpFileName_);
195  }
196 
197  virtual void _prune(const std::string&) { return; }
198 
203  size_t getMaxSubIdLength() const
204  {
205  return _maxSubIdLength;
206  }
207 
212  void setMaxSubIdLength(size_t maxSubIdLength_)
213  {
214  _maxSubIdLength = maxSubIdLength_;
215  }
216 
217 private:
218  BookmarkStoreResizeHandler _resizeHandler;
219  void* _resizeHandlerData;
220  size_t _maxSubIdLength;
221 };
222 
226 {
227  RefHandle<BookmarkStoreImpl> _body;
228 public:
232 
235  BookmarkStore(BookmarkStoreImpl* impl_) : _body(impl_) {;}
236 
237  BookmarkStore(const BookmarkStore& rhs) : _body(rhs._body) {;}
238 
239  BookmarkStore& operator=(const BookmarkStore& rhs)
240  {
241  _body = rhs._body;
242  return *this;
243  }
244 
245  ~BookmarkStore() {;}
246 
250  {
251  _body = impl_;
252  }
253 
254  bool isValid() const
255  {
256  return _body.isValid();
257  }
258 
265  size_t log(Message& message_)
266  {
267  if (_body.isValid())
268  return _body.get().log(message_);
269  return Message::BOOKMARK_NONE;
270  }
271 
278  void discard(const Message::Field& subId_, size_t bookmarkSeqNo_)
279  {
280  if (_body.isValid())
281  _body.get().discard(subId_, bookmarkSeqNo_);
282  }
283 
289  void discard(const Message& message_)
290  {
291  if (_body.isValid())
292  _body.get().discard(message_);
293  }
294 
302  {
303  if (_body.isValid())
304  return _body.get().getMostRecent(subId_);
306  }
307 
316  bool isDiscarded(Message& message_)
317  {
318  if (_body.isValid())
319  return _body.get().isDiscarded(message_);
320  return false;
321  }
322 
328  void purge()
329  {
330  if (_body.isValid())
331  _body.get().purge();
332  }
333 
339  void purge(const Message::Field& subId_)
340  {
341  if (_body.isValid())
342  _body.get().purge(subId_);
343  }
344 
351  void setResizeHandler(BookmarkStoreResizeHandler handler_, void* userData_)
352  {
353  if (_body.isValid())
354  _body.get().setResizeHandler(handler_, userData_);
355  }
356 
361  size_t getOldestBookmarkSeq(const std::string& subId_)
362  {
363  if (_body.isValid())
364  return _body.get().getOldestBookmarkSeq(Message::Field(subId_.c_str(),
365  subId_.length()));
366  return AMPS_UNSET_INDEX;
367  }
368 
373  size_t getOldestBookmarkSeq(const Message::Field& subId_)
374  {
375  if (_body.isValid())
376  return _body.get().getOldestBookmarkSeq(subId_);
377  return AMPS_UNSET_INDEX;
378  }
379 
384  void persisted(const Message::Field& subId_, const Message::Field& bookmark_)
385  {
386  if (_body.isValid())
387  _body.get().persisted(subId_, bookmark_);
388  }
389 
394  void persisted(const Message::Field& subId_, size_t bookmark_)
395  {
396  if (_body.isValid())
397  _body.get().persisted(subId_, bookmark_);
398  }
399 
404  void setServerVersion(size_t version_)
405  {
406  if (_body.isValid())
407  _body.get().setServerVersion(version_);
408  }
409 
414  void setServerVersion(const VersionInfo& version_)
415  {
416  if (_body.isValid())
417  _body.get().setServerVersion(version_);
418  }
419 
425  void prune(const std::string& tmpFileName_ = "")
426  {
427  if (_body.isValid())
428  _body.get().prune(tmpFileName_);
429  }
430 
435  {
436  if (_body.isValid())
437  return &_body.get();
438  else
439  return NULL;
440  }
441 
446  size_t getMaxSubIdLength() const
447  {
448  if (_body.isValid())
449  return _body.get().getMaxSubIdLength();
450  else
451  return 0;
452  }
453 
458  void setMaxSubIdLength(size_t maxSubIdLength_)
459  {
460  if (_body.isValid())
461  _body.get().setMaxSubIdLength(maxSubIdLength_);
462  }
463 
464 };
465 
466 inline bool BookmarkStoreImpl::callResizeHandler(const Message::Field& subId_,
467  size_t newSize_)
468 {
469  if (_resizeHandler)
470  return _resizeHandler(BookmarkStore(this), subId_, newSize_, _resizeHandlerData);
471  return true;
472 }
473 
480 inline bool ThrowawayBookmarkResizeHandler(BookmarkStore store_,
481  const Message::Field& subId_,
482  size_t newSize_, void* data_)
483 {
484  size_t* maxSizep = (size_t*)data_;
485  if (newSize_ > *maxSizep)
486  {
487  size_t discardSeq = store_.getOldestBookmarkSeq(subId_);
488  store_.discard(subId_, discardSeq);
489  store_.persisted(subId_, discardSeq);
490  return false;
491  }
492  return true;
493 }
494 
495 }
496 
497 #endif //_BOOKMARKSTORE_H_
498 
499 
void purge(const Message::Field &subId_)
Called to purge the contents of this store for particular subId.
Definition: BookmarkStore.hpp:339
Defines the AMPS::Message class and related classes.
void setMaxSubIdLength(size_t maxSubIdLength_)
Sets the maximum allowed length for a sub id when recovering a bookmark store from persistent storage...
Definition: BookmarkStore.hpp:458
Abstract base class for storing received bookmarks for HA clients.
Definition: BookmarkStore.hpp:77
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: BookmarkStore.hpp:404
void discard(const Message &message_)
Log a discard-bookmark entry to the persistent log based on a Message.
Definition: BookmarkStore.hpp:289
virtual void setResizeHandler(BookmarkStoreResizeHandler handler_, void *userData_)
Set a handler on the bookmark store that will get called whenever a resize of the store is required d...
Definition: BookmarkStore.hpp:156
void persisted(const Message::Field &subId_, const Message::Field &bookmark_)
Called internally to indicate messages up to and including bookmark are replicated to all replication...
Definition: BookmarkStore.hpp:384
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: BookmarkStore.hpp:278
bool isDiscarded(Message &message_)
Called for each arriving message to determine if the application has already seen this bookmark and s...
Definition: BookmarkStore.hpp:316
void purge()
Called to purge the contents of this store.
Definition: BookmarkStore.hpp:328
Message encapsulates a single message sent to or received from an AMPS server, and provides methods f...
Definition: Message.hpp:447
BookmarkStore(BookmarkStoreImpl *impl_)
Creates a BookmarkStore based on the given implementation.
Definition: BookmarkStore.hpp:235
virtual Message::Field getMostRecent(const Message::Field &subId_)=0
Returns the most recent bookmark from the log that ought to be used for (re-)subscriptions.
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: BookmarkStore.hpp:414
void setResizeHandler(BookmarkStoreResizeHandler handler_, void *userData_)
Set a handler on the bookmark store that will get called whenever a resize of the store is required d...
Definition: BookmarkStore.hpp:351
void setImplementation(BookmarkStoreImpl *impl_)
Sets the BookmarkStore to use the given implementation.
Definition: BookmarkStore.hpp:249
virtual size_t getOldestBookmarkSeq(const Message::Field &subId_)=0
Called to find the oldest bookmark sequence in the store.
Interface for BookmarkStoreImpl classes.
Definition: BookmarkStore.hpp:225
#define AMPS_BOOKMARK_EPOCH
Start the subscription at the beginning of the journal.
Definition: BookmarkStore.hpp:51
virtual void discard(const Message::Field &subId_, size_t bookmarkSeqNo_)=0
Log a discard-bookmark entry to the persistent log based on a bookmark sequence number.
void persisted(const Message::Field &subId_, size_t bookmark_)
Called internally to indicate messages up to and including bookmark are replicated to all replication...
Definition: BookmarkStore.hpp:394
virtual bool isDiscarded(Message &message_)=0
Called for each arriving message to determine if the application has already seen this bookmark and s...
size_t getOldestBookmarkSeq(const Message::Field &subId_)
Called to find the oldest bookmark sequence in the store.
Definition: BookmarkStore.hpp:373
size_t log(Message &message_)
Log a bookmark to the persistent log.
Definition: BookmarkStore.hpp:265
virtual void persisted(const Message::Field &subId_, const Message::Field &bookmark_)=0
Mark the bookmark provided as replicated to all sync replication destinations for the given subscript...
Field represents the value of a single field in a Message.
Definition: Field.hpp:84
virtual void purge()=0
Called to purge the contents of this store.
virtual void setServerVersion(size_t version_)=0
Internally used to set the server version so the store knows how to deal with persisted acks and call...
size_t getMaxSubIdLength() const
Gets the maximum allowed length for a sub id when recovering a bookmark store from persistent storage...
Definition: BookmarkStore.hpp:203
size_t getMaxSubIdLength() const
Gets the maximum allowed length for a sub id when recovering a bookmark store from persistent storage...
Definition: BookmarkStore.hpp:446
Definition: ampsplusplus.hpp:103
size_t getOldestBookmarkSeq(const std::string &subId_)
Called to find the oldest bookmark in the store.
Definition: BookmarkStore.hpp:361
Message::Field getMostRecent(const Message::Field &subId_)
Returns the most recent bookmark from the log that ought to be used for (re-)subscriptions.
Definition: BookmarkStore.hpp:301
virtual size_t log(Message &message_)=0
Log a bookmark to the persistent log.
void setMaxSubIdLength(size_t maxSubIdLength_)
Sets the maximum allowed length for a sub id when recovering a bookmark store from persistent storage...
Definition: BookmarkStore.hpp:212
static const size_t BOOKMARK_NONE
An indicator of no bookmark value.
Definition: Message.hpp:462
BookmarkStore()
Creates a BookmarkStore that does nothing.
Definition: BookmarkStore.hpp:231
void prune(const std::string &tmpFileName_="")
Used to trim the size of a store&#39;s storage.
Definition: BookmarkStore.hpp:425