AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.4.3
PublishStore.hpp
Go to the documentation of this file.
1 //
3 // Copyright (c) 2010-2024 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_),
77  (amps_uint32_t)blocksPerRealloc_,
78  true, errorOnPublishGap_)
79  , _fileName(fileName_)
80  , _initialBlocks((int)blocksPerRealloc_)
81  , _truncateOnClose(false)
82  {
83  recover();
84  }
85 
86  ~PublishStore()
87  {
88  close();
89  }
90 
96  void truncateOnClose(bool truncate_)
97  {
98  _truncateOnClose = truncate_;
99  }
100 
103  void close()
104  {
105  if (!_blockStore.getBuffer())
106  {
107  return;
108  }
109  amps_uint64_t unpersisted = unpersistedCount();
110  BufferLock guard(_blockStore);
111  reinterpret_cast<MMapStoreBuffer*>(_blockStore.getBuffer())->close();
112  if (_truncateOnClose && unpersisted == 0)
113  {
114 #ifdef _WIN32
115  size_t retries = 0;
116  HANDLE file = INVALID_HANDLE_VALUE;
117  while ( file == INVALID_HANDLE_VALUE && retries++ < 5)
118  {
119  file = CreateFileA(_fileName.c_str(),
120  GENERIC_READ | GENERIC_WRITE, 0, NULL,
121  OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
122  }
123  // Ignore failure, since this could be in destructor
124  if ( file != INVALID_HANDLE_VALUE )
125  {
126  SetFilePointer(file, (LONG)(_initialBlocks * _blockStore.getBlockSize()),
127  NULL, FILE_BEGIN);
128  SetEndOfFile(file);
129  CloseHandle(file);
130  }
131 #else
132  if (truncate(_fileName.c_str(),
133  (off_t)_initialBlocks * (off_t)_blockStore.getBlockSize()) == -1)
134  {
135  // Ignore failure, since this could be in destructor
136  ;
137  }
138 #endif
139  }
140  }
141 
144  void sync()
145  {
146  BufferLock guard(_blockStore);
147  reinterpret_cast<MMapStoreBuffer*>(_blockStore.getBuffer())->sync();
148  }
149  private:
150  std::string _fileName;
151  int _initialBlocks;
152  bool _truncateOnClose;
153  };
154 
155 } //namespace AMPS
156 
157 #endif //_PUBLISHSTORE_H_
158 
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:96
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:144
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:103
amps_uint32_t getBlockSize() const
Get the size of each Block, as set in the constructor.
Definition: BlockStore.hpp:160
Definition: ampsplusplus.hpp:102
PublishStore(const std::string &fileName_, bool errorOnPublishGap_=false)
Create a PublishStore that uses fileName_ for the storage.
Definition: PublishStore.hpp:56