AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.5.1
PublishStore.hpp
Go to the documentation of this file.
1 //
3 // Copyright (c) 2010-2025 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)
57  : BlockPublishStore(new MMapStoreBuffer(fileName_), 1000,
58  true, errorOnPublishGap_)
59  , _fileName(fileName_)
60  , _initialBlocks(1000)
61  , _truncateOnClose(false)
62  {
63  recover();
64  }
65 
74  PublishStore(const std::string& fileName_, size_t blocksPerRealloc_,
75  bool errorOnPublishGap_ = false)
76  : BlockPublishStore(new MMapStoreBuffer(fileName_, blocksPerRealloc_ * 2048),
77  (amps_uint32_t)blocksPerRealloc_,
78  true, errorOnPublishGap_)
79  , _fileName(fileName_)
80  , _initialBlocks((int)blocksPerRealloc_)
81  , _truncateOnClose(false)
82  {
83  recover();
84  }
85 
98  PublishStore(const std::string& fileName_, size_t blocksPerRealloc_,
99  amps_uint32_t blockSize_, bool errorOnPublishGap_ = false)
100  : BlockPublishStore(new MMapStoreBuffer(fileName_, blocksPerRealloc_ * blockSize_),
101  (amps_uint32_t)blocksPerRealloc_,
102  true, errorOnPublishGap_, blockSize_)
103  , _fileName(fileName_)
104  , _initialBlocks((int)blocksPerRealloc_)
105  , _truncateOnClose(false)
106  {
107  recover();
108  }
109 
110  ~PublishStore()
111  {
112  close();
113  }
114 
120  void truncateOnClose(bool truncate_)
121  {
122  _truncateOnClose = truncate_;
123  }
124 
127  void close()
128  {
129  if (!_blockStore.getBuffer())
130  {
131  return;
132  }
133  amps_uint64_t unpersisted = unpersistedCount();
134  BufferLock guard(_blockStore);
135  reinterpret_cast<MMapStoreBuffer*>(_blockStore.getBuffer())->close();
136  if (_truncateOnClose && unpersisted == 0)
137  {
138 #ifdef _WIN32
139  size_t retries = 0;
140  HANDLE file = INVALID_HANDLE_VALUE;
141  while ( file == INVALID_HANDLE_VALUE && retries++ < 5)
142  {
143  file = CreateFileA(_fileName.c_str(),
144  GENERIC_READ | GENERIC_WRITE, 0, NULL,
145  OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
146  }
147  // Ignore failure, since this could be in destructor
148  if ( file != INVALID_HANDLE_VALUE )
149  {
150  SetFilePointer(file, (LONG)(_initialBlocks * _blockStore.getBlockSize()),
151  NULL, FILE_BEGIN);
152  SetEndOfFile(file);
153  CloseHandle(file);
154  }
155 #else
156  if (truncate(_fileName.c_str(),
157  (off_t)_initialBlocks * (off_t)_blockStore.getBlockSize()) == -1)
158  {
159  // Ignore failure, since this could be in destructor
160  ;
161  }
162 #endif
163  }
164  }
165 
168  void sync()
169  {
170  BufferLock guard(_blockStore);
171  reinterpret_cast<MMapStoreBuffer*>(_blockStore.getBuffer())->sync();
172  }
173  private:
174  std::string _fileName;
175  int _initialBlocks;
176  bool _truncateOnClose;
177  };
178 
179 } //namespace AMPS
180 
181 #endif //_PUBLISHSTORE_H_
182 
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:98
Buffer * getBuffer()
Return the buffer underlying the store for direct write/read.
Definition: BlockStore.hpp:597
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:120
Provides AMPS::BlockPublishStore, a concrete implementation of a store that breaks the allocated stor...
A StoreImpl implementation that uses a memory-mapped file to provide a publish store that persists ac...
Definition: PublishStore.hpp:46
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:49
void sync()
Force the PublishStore to sync to disk.
Definition: PublishStore.hpp:168
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:74
void close()
Close the PublishStore and associated file.
Definition: PublishStore.hpp:127
amps_uint32_t getBlockSize() const
Get the size of each Block, as set in the constructor.
Definition: BlockStore.hpp:160
Definition: ampsplusplus.hpp:103
PublishStore(const std::string &fileName_, bool errorOnPublishGap_=false)
Create a PublishStore that uses fileName_ for the storage.
Definition: PublishStore.hpp:56