AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.5.17
PublishStore.hpp
Go to the documentation of this file.
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 
26 #ifndef _PUBLISHSTORE_H_
27 #define _PUBLISHSTORE_H_
28 
29 #include <amps/ampsplusplus.hpp>
31 #include <amps/MMapStoreBuffer.hpp>
32 #ifndef _WIN32
33  #include <unistd.h>
34 #endif
35 
40 
41 namespace AMPS
42 {
47  {
48  public:
56  PublishStore(const std::string& fileName_, bool errorOnPublishGap_ = false)
58  fileName_, AMPS_MEMORYBUFFER_DEFAULT_LENGTH,
59  DEFAULT_BLOCK_SIZE, true), 1000,
60  true, errorOnPublishGap_)
61  , _fileName(fileName_)
62  , _initialBlocks(1000)
63  , _truncateOnClose(false)
64  {
65  recoverUsingPersistedBlockSize();
66  }
67 
76  PublishStore(const std::string& fileName_, size_t blocksPerRealloc_,
77  bool errorOnPublishGap_ = false)
79  fileName_,
80  getInitialBufferSize(blocksPerRealloc_, DEFAULT_BLOCK_SIZE),
81  normalizeBlockSize(
82  blocksPerRealloc_, DEFAULT_BLOCK_SIZE), true),
83  normalizeBlocksPerRealloc(
84  blocksPerRealloc_, DEFAULT_BLOCK_SIZE),
85  true, errorOnPublishGap_,
86  normalizeBlockSize(
87  blocksPerRealloc_, DEFAULT_BLOCK_SIZE))
88  , _fileName(fileName_)
89  , _initialBlocks((int)normalizeBlocksPerRealloc(
90  blocksPerRealloc_, DEFAULT_BLOCK_SIZE))
91  , _truncateOnClose(false)
92  {
93  recoverUsingPersistedBlockSize();
94  }
95 
108  PublishStore(const std::string& fileName_, size_t blocksPerRealloc_,
109  amps_uint32_t blockSize_, bool errorOnPublishGap_ = false)
111  fileName_,
112  getInitialBufferSize(blocksPerRealloc_, blockSize_),
113  normalizeBlockSize(blocksPerRealloc_, blockSize_), true),
114  normalizeBlocksPerRealloc(
115  blocksPerRealloc_, blockSize_),
116  true, errorOnPublishGap_,
117  normalizeBlockSize(blocksPerRealloc_, blockSize_))
118  , _fileName(fileName_)
119  , _initialBlocks((int)normalizeBlocksPerRealloc(
120  blocksPerRealloc_, blockSize_))
121  , _truncateOnClose(false)
122  {
123  recoverUsingPersistedBlockSize();
124  }
125 
126  ~PublishStore()
127  {
128  close(); // -V1053 safe for our classes
129  }
130 
136  void truncateOnClose(bool truncate_)
137  {
138  _truncateOnClose = truncate_;
139  }
140 
143  void close()
144  {
145  if (!_blockStore.getBuffer())
146  {
147  return;
148  }
149  amps_uint64_t unpersisted = unpersistedCount();
150  BufferLock guard(_blockStore);
151  reinterpret_cast<MMapStoreBuffer*>(_blockStore.getBuffer())->close();
152  if (_truncateOnClose && unpersisted == 0)
153  {
154 #ifdef _WIN32
155  size_t retries = 0;
156  HANDLE file = INVALID_HANDLE_VALUE;
157  while ( file == INVALID_HANDLE_VALUE && retries++ < 5)
158  {
159  file = CreateFileA(_fileName.c_str(),
160  GENERIC_READ | GENERIC_WRITE, 0, NULL,
161  OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
162  }
163  // Ignore failure, since this could be in destructor
164  if ( file != INVALID_HANDLE_VALUE )
165  {
166  SetFilePointer(file, (LONG)(_initialBlocks * _blockStore.getBlockSize()),
167  NULL, FILE_BEGIN);
168  SetEndOfFile(file);
169  CloseHandle(file);
170  }
171 #else
172  if (truncate(_fileName.c_str(),
173  (off_t)_initialBlocks * (off_t)_blockStore.getBlockSize()) == -1)
174  {
175  // Ignore failure, since this could be in destructor
176  ;
177  }
178 #endif
179  }
180  }
181 
184  void sync()
185  {
186  BufferLock guard(_blockStore);
187  reinterpret_cast<MMapStoreBuffer*>(_blockStore.getBuffer())->sync();
188  }
189  private:
190  void recoverUsingPersistedBlockSize()
191  {
192  {
193  BufferLock guard(_blockStore);
194  MMapStoreBuffer* buffer = reinterpret_cast<MMapStoreBuffer*>(
195  _blockStore.getBuffer());
196  if (buffer->getSize() >= sizeof(amps_uint32_t))
197  {
198  buffer->setPosition(0);
199  amps_uint32_t persistedBlockSize = buffer->getUint32();
200  // Zero indicates a newly-created store. One indicates the old
201  // publish-store format, which recovers using the configured size.
202  if (persistedBlockSize > 1)
203  {
204  _blockStore.setBlockSize(persistedBlockSize);
205  buffer->setSizeAlignment(persistedBlockSize);
206  }
207  }
208  }
209  BlockPublishStore::recover();
210  }
211 
212  std::string _fileName;
213  int _initialBlocks;
214  bool _truncateOnClose;
215  };
216 
217 } //namespace AMPS
218 
219 #endif //_PUBLISHSTORE_H_
220 
virtual amps_uint32_t getUint32()
Get the unsigned 32-bit int value at the current buffer position and advance past it...
Definition: MemoryStoreBuffer.hpp:170
amps_uint32_t setBlockSize(amps_uint32_t blockSize_)
Set the size to use for all Blocks.
Definition: BlockStore.hpp:648
PublishStore(const std::string &fileName_, size_t blocksPerRealloc_, amps_uint32_t blockSize_, bool errorOnPublishGap_=false)
Create a PublishStore that uses fileName_ for the storage.
Definition: PublishStore.hpp:108
Buffer * getBuffer()
Return the buffer underlying the store for direct write/read.
Definition: BlockStore.hpp:677
Provides AMPS::MMapStoreBuffer, an AMPS::Buffer implementation used by the AMPS::MMapBookmarkStore.
void truncateOnClose(bool truncate_)
Tell the PublishStore if it should return the file to its initial capacity when the store is closed i...
Definition: PublishStore.hpp:136
Provides AMPS::BlockPublishStore, a concrete implementation of a store that breaks the allocated stor...
virtual size_t getSize() const
Get the current size of the Buffer in bytes.
Definition: MemoryStoreBuffer.hpp:62
A StoreImpl implementation that uses a memory-mapped file to provide a publish store that persists ac...
Definition: PublishStore.hpp:46
void setSizeAlignment(size_t sizeAlignment_)
Set the allocation-unit alignment for logical buffer sizes.
Definition: MMapStoreBuffer.hpp:200
Core type, function, and class declarations for the AMPS C++ client.
size_t unpersistedCount() const
Method to return the count of messages that currently in the Store because they have not been discard...
Definition: BlockPublishStore.hpp:642
A Buffer implementation that uses a memory mapped file as its storage.
Definition: MMapStoreBuffer.hpp:50
virtual void setPosition(size_t position_)
Set the buffer postion to a location.
Definition: MemoryStoreBuffer.hpp:88
void sync()
Force the PublishStore to sync to disk.
Definition: PublishStore.hpp:184
Used as a base class for other stores in the AMPS C++ client, this is an implementation of StoreImpl ...
Definition: BlockPublishStore.hpp:60
PublishStore(const std::string &fileName_, size_t blocksPerRealloc_, bool errorOnPublishGap_=false)
Create a PublishStore that uses fileName_ for the storage.
Definition: PublishStore.hpp:76
void close()
Close the PublishStore and associated file.
Definition: PublishStore.hpp:143
amps_uint32_t getBlockSize() const
Get the size of each Block, as set in the constructor.
Definition: BlockStore.hpp:189
Definition: AMPSException.hpp:32
PublishStore(const std::string &fileName_, bool errorOnPublishGap_=false)
Create a PublishStore that uses fileName_ for the storage.
Definition: PublishStore.hpp:56