AMPS C/C++ Client Class Reference AMPS C/C++ Client Version 5.3.5.7

BookmarkStore.hpp
1 //
3 // Copyright (c) 2010-2026 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 "amps/Message.hpp"
29 #include "amps/util.hpp"
30
31 namespace AMPS
32 {
33
47 #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:
80 BookmarkStoreImpl()
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&)
198 {
199 return;
200 }
201
206 size_t getMaxSubIdLength() const
207 {
208 return _maxSubIdLength;
209 }
210
215 void setMaxSubIdLength(size_t maxSubIdLength_)
216 {
217 _maxSubIdLength = maxSubIdLength_;
218 }
219
220 private:
221 BookmarkStoreResizeHandler _resizeHandler;
222 void* _resizeHandlerData;
223 size_t _maxSubIdLength;
224 };
225
228 class BookmarkStore
229 {
230 RefHandle<BookmarkStoreImpl> _body;
231 public:
234 BookmarkStore() = default;
235
238 BookmarkStore(BookmarkStoreImpl* impl_) : _body(impl_) {;}
239
240 BookmarkStore(const BookmarkStore& rhs) : _body(rhs._body) {;}
241
242 BookmarkStore& operator=(const BookmarkStore& rhs)
243 {
244 _body = rhs._body;
245 return *this;
246 }
247
248 ~BookmarkStore() = default;
249
252 void setImplementation(BookmarkStoreImpl* impl_)
253 {
254 _body = impl_;
255 }
256
257 bool isValid() const
258 {
259 return _body.isValid();
260 }
261
268 size_t log(Message& message_)
269 {
270 if (_body.isValid())
271 {
272 return _body.get().log(message_);
273 }
274 return Message::BOOKMARK_NONE;
275 }
276
283 void discard(const Message::Field& subId_, size_t bookmarkSeqNo_)
284 {
285 if (_body.isValid())
286 {
287 _body.get().discard(subId_, bookmarkSeqNo_);
288 }
289 }
290
296 void discard(const Message& message_)
297 {
298 if (_body.isValid())
299 {
300 _body.get().discard(message_);
301 }
302 }
303
310 Message::Field getMostRecent(const Message::Field& subId_)
311 {
312 if (_body.isValid())
313 {
314 return _body.get().getMostRecent(subId_);
315 }
316 return Field::stringCopy(AMPS_BOOKMARK_EPOCH);
317 }
318
327 bool isDiscarded(Message& message_)
328 {
329 if (_body.isValid())
330 {
331 return _body.get().isDiscarded(message_);
332 }
333 return false;
334 }
335
341 void purge()
342 {
343 if (_body.isValid())
344 {
345 _body.get().purge();
346 }
347 }
348
354 void purge(const Message::Field& subId_)
355 {
356 if (_body.isValid())
357 {
358 _body.get().purge(subId_);
359 }
360 }
361
368 void setResizeHandler(BookmarkStoreResizeHandler handler_, void* userData_)
369 {
370 if (_body.isValid())
371 {
372 _body.get().setResizeHandler(handler_, userData_);
373 }
374 }
375
380 size_t getOldestBookmarkSeq(const std::string& subId_)
381 {
382 if (_body.isValid())
383 return _body.get().getOldestBookmarkSeq(Message::Field(subId_.c_str(),
384 subId_.length()));
385 return AMPS_UNSET_INDEX;
386 }
387
392 size_t getOldestBookmarkSeq(const Message::Field& subId_)
393 {
394 if (_body.isValid())
395 {
396 return _body.get().getOldestBookmarkSeq(subId_);
397 }
398 return AMPS_UNSET_INDEX;
399 }
400
405 void persisted(const Message::Field& subId_, const Message::Field& bookmark_)
406 {
407 if (_body.isValid())
408 {
409 _body.get().persisted(subId_, bookmark_);
410 }
411 }
412
417 void persisted(const Message::Field& subId_, size_t bookmark_)
418 {
419 if (_body.isValid())
420 {
421 _body.get().persisted(subId_, bookmark_);
422 }
423 }
424
429 void setServerVersion(size_t version_)
430 {
431 if (_body.isValid())
432 {
433 _body.get().setServerVersion(version_);
434 }
435 }
436
441 void setServerVersion(const VersionInfo& version_)
442 {
443 if (_body.isValid())
444 {
445 _body.get().setServerVersion(version_);
446 }
447 }
448
454 void prune(const std::string& tmpFileName_ = "")
455 {
456 if (_body.isValid())
457 {
458 _body.get().prune(tmpFileName_);
459 }
460 }
461
465 BookmarkStoreImpl* get()
466 {
467 if (_body.isValid())
468 {
469 return &_body.get();
470 }
471 else
472 {
473 return NULL;
474 }
475 }
476
481 size_t getMaxSubIdLength() const
482 {
483 if (_body.isValid())
484 {
485 return _body.get().getMaxSubIdLength();
486 }
487 else
488 {
489 return 0;
490 }
491 }
492
497 void setMaxSubIdLength(size_t maxSubIdLength_)
498 {
499 if (_body.isValid())
500 {
501 _body.get().setMaxSubIdLength(maxSubIdLength_);
502 }
503 }
504
505 };
506
507 inline bool BookmarkStoreImpl::callResizeHandler(const Message::Field& subId_,
508 size_t newSize_)
509 {
510 if (_resizeHandler)
511 {
512 return _resizeHandler(BookmarkStore(this), subId_, newSize_, _resizeHandlerData);
513 }
514 return true;
515 }
516
523 inline bool ThrowawayBookmarkResizeHandler(BookmarkStore store_,
524 const Message::Field& subId_,
525 size_t newSize_, void* data_)
526 {
527 size_t* maxSizep = (size_t*)data_;
528 if (newSize_ > *maxSizep)
529 {
530 size_t discardSeq = store_.getOldestBookmarkSeq(subId_);
531 store_.discard(subId_, discardSeq);
532 store_.persisted(subId_, discardSeq);
533 return false;
534 }
535 return true;
536 }
537
538 }
539
540 #endif //_BOOKMARKSTORE_H_
541
542
Message.hpp
Defines the AMPS::Message class and related classes.
AMPS::BookmarkStoreImpl
Abstract base class for storing received bookmarks for HA clients.
Definition: BookmarkStore.hpp:78
AMPS::BookmarkStore
Interface for BookmarkStoreImpl classes.
Definition: BookmarkStore.hpp:229
AMPS::Field
Field represents the value of a single field in a Message.
Definition: Field.hpp:88
AMPS::Field::stringCopy
static Field stringCopy(const char *str_)
Makes a copy of str_ in a new Field.
Definition: Field.hpp:253
AMPS::Message
Message encapsulates a single message sent to or received from an AMPS server, and provides methods f...
Definition: Message.hpp:518
AMPS::Message::BOOKMARK_NONE
static const size_t BOOKMARK_NONE
An indicator of no bookmark value.
Definition: Message.hpp:532
AMPS::BookmarkStoreImpl::setServerVersion
virtual void setServerVersion(const VersionInfo &version_)=0
Internally used to set the server version so the store knows how to deal with persisted acks and call...
AMPS::BookmarkStoreImpl::purge
virtual void purge()=0
Called to purge the contents of this store.
AMPS::BookmarkStore::setResizeHandler
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:368
AMPS::BookmarkStore::setServerVersion
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:441
AMPS::BookmarkStore::setServerVersion
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:429
AMPS::BookmarkStore::purge
void purge()
Called to purge the contents of this store.
Definition: BookmarkStore.hpp:341
AMPS::BookmarkStore::getOldestBookmarkSeq
size_t getOldestBookmarkSeq(const Message::Field &subId_)
Called to find the oldest bookmark sequence in the store.
Definition: BookmarkStore.hpp:392
AMPS::BookmarkStoreImpl::getOldestBookmarkSeq
virtual size_t getOldestBookmarkSeq(const Message::Field &subId_)=0
Called to find the oldest bookmark sequence in the store.
AMPS::BookmarkStore::discard
void discard(const Message &message_)
Log a discard-bookmark entry to the persistent log based on a Message.
Definition: BookmarkStore.hpp:296
AMPS::BookmarkStoreImpl::persisted
virtual Message::Field persisted(const Message::Field &subId_, size_t bookmark_)=0
Mark the bookmark provided as replicated to all sync replication destinations for the given subscript...
AMPS::BookmarkStore::prune
void prune(const std::string &tmpFileName_="")
Used to trim the size of a store's storage.
Definition: BookmarkStore.hpp:454
AMPS::BookmarkStoreResizeHandler
bool(* BookmarkStoreResizeHandler)(BookmarkStore store_, const Message::Field &subId_, size_t size_, void *userData_)
Function type for BookmarkStore resize events The store_ param is store which is resizing.
Definition: BookmarkStore.hpp:70
AMPS::BookmarkStore::persisted
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:417
AMPS::BookmarkStoreImpl::setResizeHandler
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
AMPS::BookmarkStore::discard
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:283
AMPS::BookmarkStore::log
size_t log(Message &message_)
Log a bookmark to the persistent log.
Definition: BookmarkStore.hpp:268
AMPS::BookmarkStoreImpl::setServerVersion
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...
AMPS::BookmarkStore::BookmarkStore
BookmarkStore(BookmarkStoreImpl *impl_)
Creates a BookmarkStore based on the given implementation.
Definition: BookmarkStore.hpp:238
AMPS::BookmarkStore::setMaxSubIdLength
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:497
AMPS::BookmarkStore::getOldestBookmarkSeq
size_t getOldestBookmarkSeq(const std::string &subId_)
Called to find the oldest bookmark in the store.
Definition: BookmarkStore.hpp:380
AMPS::BookmarkStore::setImplementation
void setImplementation(BookmarkStoreImpl *impl_)
Sets the BookmarkStore to use the given implementation.
Definition: BookmarkStore.hpp:252
AMPS::BookmarkStore::persisted
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:405
AMPS::BookmarkStore::purge
void purge(const Message::Field &subId_)
Called to purge the contents of this store for particular subId.
Definition: BookmarkStore.hpp:354
AMPS::ThrowawayBookmarkResizeHandler
bool ThrowawayBookmarkResizeHandler(BookmarkStore store_, const Message::Field &subId_, size_t newSize_, void *data_)
A BookmarkStoreResizeHandler that discards the oldest bookmark assuming that it was used but not disc...
Definition: BookmarkStore.hpp:523
AMPS::BookmarkStoreImpl::isDiscarded
virtual bool isDiscarded(Message &message_)=0
Called for each arriving message to determine if the application has already seen this bookmark and s...
AMPS::BookmarkStoreImpl::discard
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.
AMPS::BookmarkStoreImpl::setMaxSubIdLength
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:215
AMPS::BookmarkStoreImpl::getMostRecent
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.
AMPS_BOOKMARK_EPOCH
#define AMPS_BOOKMARK_EPOCH
Start the subscription at the beginning of the journal.
Definition: BookmarkStore.hpp:51
AMPS::BookmarkStore::getMaxSubIdLength
size_t getMaxSubIdLength() const
Gets the maximum allowed length for a sub id when recovering a bookmark store from persistent storage...
Definition: BookmarkStore.hpp:481
AMPS::BookmarkStore::isDiscarded
bool isDiscarded(Message &message_)
Called for each arriving message to determine if the application has already seen this bookmark and s...
Definition: BookmarkStore.hpp:327
AMPS::BookmarkStore::BookmarkStore
BookmarkStore()=default
Creates a BookmarkStore that does nothing.
AMPS::BookmarkStore::get
BookmarkStoreImpl * get()
Used to get a pointer to the implementation.
Definition: BookmarkStore.hpp:465
AMPS::BookmarkStoreImpl::log
virtual size_t log(Message &message_)=0
Log a bookmark to the persistent log.
AMPS::BookmarkStoreImpl::discard
virtual void discard(const Message &message_)=0
Log a discard-bookmark entry to the persistent log based on a bookmark sequence number.
AMPS::BookmarkStoreImpl::purge
virtual void purge(const Message::Field &subId_)=0
Called to purge the contents of this store for particular subId.
AMPS::BookmarkStore::getMostRecent
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:310
AMPS::BookmarkStoreImpl::persisted
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...
AMPS::BookmarkStoreImpl::getMaxSubIdLength
size_t getMaxSubIdLength() const
Gets the maximum allowed length for a sub id when recovering a bookmark store from persistent storage...
Definition: BookmarkStore.hpp:206