forked from jan-hoelscher-mss/safmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QStorage.h
executable file
·257 lines (207 loc) · 8.33 KB
/
QStorage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
Copyright 2004-2008 Matthew J. Battey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
This software implements a platform independent Store and Forward Message Queue.
*/
#if !defined(_QSTORAGE_H_)
#define _QSTORAGE_H_
#pragma warning(disable: 4786)
#include "safmq.h"
#include "Mutex.h"
#include "safmq-Signal.h"
#include "randpq.h"
#include "Stats.h"
#include <fstream>
#include <memory.h>
#include <set>
#include <map>
using namespace safmq;
/**
Operator to compare two uuid's. If the two uuid's are identical
then operator function returns true.
@param lhs The first uuid to compare
@param rhs The second uuid to compare
@return true If the two uuid's are equal
*/
inline bool operator==(const uuid& lhs, const uuid& rhs) {
return memcmp(&lhs, &rhs, sizeof(uuid)) == 0;
}
/**
Operator to compare two uuid's. If the first uuid is less than
the second then the operator function return strue
@param lhs The first uuid to compare
@param rhs The second uuid to compare
@return true If the first uuid is less than the second
*/
inline bool operator<(const uuid& lhs, const uuid& rhs) {
return memcmp(&lhs, &rhs, sizeof(uuid)) < 0;
}
/**
A class containing in-memory data to index the on-disk data in a message queue.
*/
struct QueueIndex {
/// The priority of the message
safmq::MessagePriorityEnum MessagePriority;
/// The identification of the message
uuid MessageID;
/// The class of the message
MessageClassEnum MessageClass;
/// The return-id of the message
uuid ReciptID;
/// The time the message was created
time_t Timestamp;
/// The number of seconds this message has to live
SAFMQ_INT32 TTL;
/// The id of the file the message resides in
SAFMQ_INT32 FileID;
/// The offset into the file where the message resides
std::fstream::off_type DataOffset;
/// A flag indicating the message has been or is about to be purged from disk
bool Purged;
QueueIndex() {
DataOffset = 0;
FileID = 0;
MessageClass = MC_USERMSG;
MessagePriority = MP_STANDARD;
MessageID = UUID_NULL;
ReciptID = UUID_NULL;
Purged = true;
}
QueueIndex(const QueueIndex& src) {
copy(src);
}
QueueIndex(MessagePriorityEnum MessagePriority, uuid MessageID, MessageClassEnum MessageClass, uuid ReciptID, time_t Timestamp, SAFMQ_INT32 TTL, SAFMQ_INT32 FileID, std::fstream::off_type DataOffset, bool Purged) {
this->MessagePriority = MessagePriority;
this->MessageID = MessageID;
this->MessageClass = MessageClass;
this->ReciptID = ReciptID;
this->Timestamp = Timestamp;
this->TTL = TTL;
this->FileID = FileID;
this->DataOffset = DataOffset;
this->Purged = Purged;
}
QueueIndex& operator=(const QueueIndex& src) {
copy(src);
return *this;
}
bool operator<(const QueueIndex& rhs) const {
return MessagePriority > rhs.MessagePriority;
}
void copy(const QueueIndex& src) {
MessagePriority = src.MessagePriority;
MessageID = src.MessageID;
MessageClass = src.MessageClass;
ReciptID = src.ReciptID;
Timestamp = src.Timestamp;
TTL = src.TTL;
FileID = src.FileID;
DataOffset = src.DataOffset;
Purged = src.Purged;
}
};
/**
Basic definition for data storage into
@field MsgMarker Always equal to 'QMsg' inorder to accurately locate the beginning of a message.
@field Purged Flag indicating whether the message has been purged from the queue and should be ignored.
@field Length Length of this record
@field Msg The message data
*/
#pragma pack(1)
struct QMsgStorageHdr {
QMsgStorageHdr() : MsgMarker(0), Purged(false), Length(0xFFFFFFFF) { }
SAFMQ_INT32 MsgMarker;
bool Purged;
SAFMQ_UINT32 Length;
};
#pragma pack()
namespace safmq {
/**
Class to control queue storage...
*/
class QStorage
{
public: //behavior
typedef ra_priority_queue<QueueIndex> IndexQueue;
typedef std::map<SAFMQ_FILE_ID, size_t> FileUtilizationMap;
typedef SAFMQ_INT32 CursorHandle;
typedef std::map<CursorHandle, IndexQueue::cursor> CursorHandleMap;
typedef std::set<uuid> IDSet;
QStorage(const std::string& name, const std::string& storagedirectory, SAFMQ_INT32 maxfilesize=40*1024*1024);
virtual ~QStorage();
const std::string& getName() { return name; }
ErrorCode GetStatistics(QueueStatistics& stats, bool includeStorageBytes, bool includeMessageBytes);
/**
Place a message on the queue.
*/
ErrorCode Enqueue(QueueMessage& msg, QStorage::CursorHandle& enqCursorID);
ErrorCode FinalizeEnqueue(QStorage::CursorHandle finalizeID);
ErrorCode CancelEnqueue(QStorage::CursorHandle cancelID);
ErrorCode BeginRetrieve(bool retrievebody, QueueMessage& msg, QStorage::CursorHandle& retrieveID);
ErrorCode BeginRetrieveID(bool retrievebody, const uuid& id, QueueMessage& msg, QStorage::CursorHandle& retrieveID);
// TODO: ErrorCode BeginRetrievePriority(bool retrievebody, MessagePriorityenum priority, QueueMessage& msg, QStorage::CursorHandle& retrieveID);
ErrorCode CancelRetrieve(QStorage::CursorHandle retrieveID);
ErrorCode FinalizeRetrieve(QStorage::CursorHandle retrieveID);
ErrorCode FinalizeRetrieveCursor(QStorage::CursorHandle retrieveID);
ErrorCode PeekID(bool retrievebody, const uuid& id, QueueMessage& msg);
ErrorCode PeekFront(bool retrievebody, QueueMessage& msg);
ErrorCode OpenCursor(QStorage::CursorHandle& cursorID);
ErrorCode CloseCursor(QStorage::CursorHandle cursorID);
ErrorCode CursorCopy(QStorage::CursorHandle& dest, QStorage::CursorHandle& src);
ErrorCode AdvanceCursor(QStorage::CursorHandle cursorID);
ErrorCode SeekID(const uuid& id, QStorage::CursorHandle cursorID);
// TODO: ErrorCode SeekFront(QStorage::CursorHandle cursorID);
// TODO: ErrorCode SeekFirstPriority(MessagePriorityEnum priority, QStorage::CursorHandle cursorID);
ErrorCode TestCursor(QStorage::CursorHandle cursorID);
ErrorCode PeekCursor(bool retrievebody, QStorage::CursorHandle cursorID, QueueMessage& msg);
ErrorCode BeginRetrieveCursor(bool retrievebody, QStorage::CursorHandle cursorID, QueueMessage& msg);
ErrorCode WaitForID(const uuid& id, SAFMQ_INT32 timeout, QStorage::CursorHandle& messageCursor);
ErrorCode WaitFront(SAFMQ_INT32 timeout);
ErrorCode TTLPurge();
protected: // behavior
void init();
void recover();
void recover_file(SAFMQ_FILE_ID fileid);
SAFMQ_FILE_ID getFileIDFromName(const std::string& name);
std::string& getFileNameFromID(std::string& dest, SAFMQ_INT32 fileid);
std::string& getCurrentFileName(std::string& dest);
QStorage::CursorHandle nextCursorID();
ErrorCode statMessage(QStorage::CursorHandle cursorID, bool purged);
ErrorCode statMessage(QStorage::IndexQueue::cursor& idxcur, bool purged);
ErrorCode testCursor(QStorage::CursorHandle cursorID, QStorage::CursorHandleMap::iterator& cur);
ErrorCode peekCursor(bool retrievebody, QStorage::CursorHandle cursorID, QueueMessage& msg);
ErrorCode peekCursor(bool retrievebody, const QStorage::IndexQueue::cursor& idxcur, QueueMessage& message);
ErrorCode openCursor(QStorage::CursorHandle& cursorID);
ErrorCode closeCursor(QStorage::CursorHandle cursorID);
ErrorCode seekID(const uuid& id, QStorage::CursorHandle cursorID);
ErrorCode retrieveCursor(QStorage::CursorHandle cursorID, QueueMessage& message);
SAFMQ_UINT32 getStorageBytes();
SAFMQ_UINT32 getMessageBytes();
private: // behavior
public: // data
protected: // data
IndexQueue index;
FileUtilizationMap fileutilization;
CursorHandleMap cursors;
SAFMQ_FILE_ID currentfileid;
std::string storagedirectory;
size_t maxfilesize;
Mutex qmtx;
IDSet waitset;
Signal enqsignal;
Signal idsignal;
std::string name;
bool hasTTL;
Stats statistics;
static const std::string DIRSEP;
private: // data
};
} // end of safmq namespace
#endif // !defined(_QSTORAGE_H_)