-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStreamCache.h
executable file
·293 lines (249 loc) · 8.29 KB
/
StreamCache.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
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(_STREAM_CACHE_H)
#define _STREAM_CACHE_H
#include "Mutex.h"
//#include "Log.h"
#include <fstream>
#include <errno.h>
#include <string>
namespace safmq {
struct StreamCacheEntry {
std::string filename;
std::fstream *stream;
bool inuse;
std::ios::openmode openmode;
StreamCacheEntry(): stream(NULL), inuse(false) {
}
StreamCacheEntry(const StreamCacheEntry& src) {
*this = src;
}
virtual ~StreamCacheEntry() {}
const StreamCacheEntry& operator=(const StreamCacheEntry& src) {
filename = src.filename;
stream = src.stream;
inuse = src.inuse;
openmode = src.openmode;
return *this;
}
};
/**
* A class to cache open file streams
*/
class StreamCache {
protected:
int cacheSize;
int entryCount;
SAFMQ_UINT32 accessCount;
SAFMQ_UINT32 hitCount;
SAFMQ_UINT32 reopenCount;
StreamCacheEntry *entries;
Mutex mtx;
public:
/**
* Constructs the stream cache setting the cache size.
*/
StreamCache(int cacheSize) {
this->cacheSize = cacheSize;
entryCount = 0;
entries = new StreamCacheEntry[cacheSize];
accessCount = hitCount = reopenCount = 0;
}
/**
* Destroys the cache and releases the contestns
*/
virtual ~StreamCache() {
for(int x=0; x<entryCount; ++x) {
entries[x].stream->flush();
entries[x].stream->close();
delete entries[x].stream;
}
delete [] entries;
}
/**
* The number of times the cache has been accessed
* @return The total number of times the cache has been accessed
*/
SAFMQ_UINT32 getAccessCount() {
return accessCount;
}
/**
* The number of times the requested file was found in the cache
* @return The number of times the requested file was found
*/
SAFMQ_UINT32 getHitCount() {
return hitCount;
}
/**
* The number of times the file had to be re-opened
* @return The number of partial hits (file had to be reopened for i/o access)
*/
SAFMQ_UINT32 getReopenCount() {
return reopenCount;
}
// TODO: What to do with ios::trunc?
/**
* Obtains a stream from the cache
* @param filename The name of the file
* @param mode The access mode for the file (i.e. ios::in, ios::out, etc)
*/
std::fstream* get(const std::string& filename, std::ios::openmode mode) {
MutexLock lock(&mtx); // unlocks upon exit from method
++accessCount;
//cout << "Request to open: " << filename << endl;
//Log::getLog()->Info("Request to open %s", filename.c_str());
int x;
StreamCacheEntry *entry = NULL;
for(x=0; x < entryCount; ++x) {
if (filename == entries[x].filename) {
entry = entries + x;
break; // to preserve x's value
}
}
if (entry != NULL) {
//Log::getLog()->Info("Found existing file handle: %s", entry->filename.c_str());
++hitCount;
// The entry was found
if (x > 0) {
StreamCacheEntry tmp = entries[x];
entries[x] = entries[x-1];
entries[x-1] = tmp;
entry = entries + (x-1);
}
// validate openmode, if the current mode masked by mode does not equal the mode then reopen
if ((entry->openmode & mode & (std::ios::in|std::ios::out)) != (mode & (std::ios::in|std::ios::out)) ) {
++reopenCount;
//Log::getLog()->Info("Reopening: %s", entry->filename.c_str());
//std::cout << "Reopening:" << entry->filename << std::endl;
// reopen the file
entry->stream->flush();
entry->stream->close();
delete entry->stream;
entry->stream = new std::fstream();
entry->openmode = std::ios::in | std::ios::out | (mode & ~(std::ios::app|std::ios::ate));
entry->stream->open(entry->filename.c_str(), entry->openmode);
}
if (mode & (std::ios::app|std::ios::ate)) {
//Log::getLog()->Info("Pre Seek: %d", (int)entry->stream->good());
//errno = 0;
if (entry->openmode & std::ios::out)
entry->stream->seekp(0, std::ios::end);
//Log::getLog()->Info("Post seekp: %d", (int)entry->stream->good());
//errno = 0;
//Log::getLog()->Info("tellg(): %d", entry->stream->tellg());
if (entry->openmode & std::ios::in)
entry->stream->seekg(0, std::ios::beg);
int err = errno;
//Log::getLog()->Info("Post seekg: good:%d, bad:%d, fail:%d, eof:%d %s", (int)entry->stream->good(), (int)entry->stream->bad(), (int)entry->stream->fail(), (int)entry->stream->eof(), strerror(err));
} else {
//Log::getLog()->Info("else Pre Seek: %d", (int)entry->stream->good());
if (entry->openmode & std::ios::out)
entry->stream->seekp(0, std::ios::beg);
//Log::getLog()->Info("else Post seekp: %d", (int)entry->stream->good());
if (entry->openmode & std::ios::in)
entry->stream->seekg(0, std::ios::beg);
//Log::getLog()->Info("else Post seekg: %d", (int)entry->stream->good());
}
} else {
if (entryCount < cacheSize) {
entry = entries + entryCount;
++entryCount;
} else {
if (!entries[entryCount-1].inuse) {
entries[entryCount-1].stream->close();
delete entries[entryCount-1].stream;
}
entry = entries + (entryCount - 1);
}
//std::cout << "First Open:" << filename << std::endl;
//Log::getLog()->Info("First Open: %s", filename.c_str());
entry->filename = filename;
entry->stream = new std::fstream();
entry->openmode = mode;
//Log::getLog()->Info("Info: mode&ios::in %d", mode & std::ios::in);
//Log::getLog()->Info("Info: mode&ios::out %d", mode & std::ios::out);
//Log::getLog()->Info("Info: mode&ios::app %d", mode & std::ios::app);
//Log::getLog()->Info("Info: mode&ios::ate %d", mode & std::ios::ate);
//errno = 0;
entry->stream->open(filename.c_str(), entry->openmode);
int err = errno; // save errno
//std::cout << "errno: " << err << std::endl;
//Log::getLog()->Info("errno: %ld", err);
errno = err;
if (!(entry->stream->good())) {
//std::cout << "First Open Error: " << strerror(err) << std::endl;
//Log::getLog()->Info("%s, %s", filename.c_str(), strerror(err));
}
if (entry->stream->fail()) {
//std::cout << "First Open Error/Fail: " << strerror(err) << std::endl;
//Log::getLog()->Info("%s, %s", filename.c_str(), strerror(err));
}
errno = err;
}
entry->inuse = true;
//Log::getLog()->Info("Got Stream: %p", entry->stream);
return entry->stream;
}
/**
* releases the stream back to the cache for reuse
* @param f the stream from the cache
*/
void release(std::fstream* f) {
MutexLock lock(&mtx); // unlocks upon exit from method
//Log::getLog()->Info("Release Stream: %p", f);
for(int x=0; x<entryCount; ++x) {
if (entries[x].stream == f) {
//Log::getLog()->Info("Releaseing: %s stream good: %d", entries[x].filename.c_str(), (int)f->good());
entries[x].stream->flush();
//Log::getLog()->Info("After flush state: %d", (int)f->good());
entries[x].inuse = false;
return;
}
}
f->close(); // close the stream it isn't in the cache any longer
delete f;
}
/**
* delete the file and remove from the cache
* @param filename the file to be removed
*/
void remove(const std::string& filename) {
MutexLock lock(&mtx); // unlocks upon exit from method
for(int x=0; x<entryCount; ++x) {
if (entries[x].filename == filename) {
// remove the entry
if (!entries[x].inuse) {
//Log::getLog()->Info("NOTE: Closing and removing %s", filename.c_str());
entries[x].stream->close();
delete entries[x].stream;
} else {
//Log::getLog()->Info("NOTE: Unable to close %s, inuse", filename.c_str());
}
// shift the remaining entries up
for(++x; x < entryCount; ++x) {
entries[x-1] = entries[x];
}
--entryCount;
break;
}
}
//Log::getLog()->Info("NOTE: Removing file %s", filename.c_str());
::remove(filename.c_str());
}
void listEntries() {
for(int x=0;x<entryCount;++x)
std::cout << entries[x].filename << " " << entries[x].inuse << std::endl;
}
};
}
#endif