-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoragefile.cpp
More file actions
349 lines (318 loc) · 9.89 KB
/
storagefile.cpp
File metadata and controls
349 lines (318 loc) · 9.89 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "storagefile.h"
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
using namespace tt_core_ns;
//TODO: Delete this
#define LOGID 999
namespace
{
enum
{
HEADER_MAGIC = 0x3f342287,
HEADER_VERSION = 1,
MAX_FILEID = 32
};
_int64 _GetTime()
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft); // UTC
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart;
}
}
namespace transactioncounting_ns
{
namespace detail
{
struct FileHeader
{
int magic;
int version;
char fileid[MAX_FILEID]; // including NULL
};
struct RecordHeader
{
size_t size;
int recordId;
_int64 created;
_int64 modified;
};
}
StorageFile::StorageFile()
:m_fd(-1)
, m_bReadOnly(false)
{
}
StorageFile::~StorageFile()
{
Close();
return;
}
bool StorageFile::Open(const std::string& file, bool bReadOnly, const char *fileId)
{
m_bReadOnly = bReadOnly;
if (m_fd > 0)
{
tt_log::GetCustomLog().Log( tt_log::SV_ERROR, LOGID, "[TransactionCounting] Opened failed, %s is already opened", file.c_str());
return false;
}
if (m_bReadOnly)
{
m_fd = _open(file.c_str(), _O_RDONLY | _O_BINARY);
}
else
{
if (_access(file.c_str(), 0) < 0)
{
// file does not exist
m_fd = _open(file.c_str(), _O_RDWR | _O_CREAT | _O_TRUNC | _O_BINARY, _S_IREAD | _S_IWRITE);
if (m_fd < 0)
{
tt_log::GetCustomLog().Log( tt_log::SV_ERROR, LOGID, "[TransactionCounting] Error creating %s error: %s.", file.c_str(), _sys_errlist[errno] );
return false;
}
detail::FileHeader hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.magic = HEADER_MAGIC;
hdr.version = HEADER_VERSION;
strncpy(hdr.fileid, fileId, MAX_FILEID);
hdr.fileid[MAX_FILEID-1] = '\0';
WriteFileHeader(hdr);
}
else
{
m_fd = _open(file.c_str(), _O_RDWR | _O_BINARY);
}
}
if (m_fd < 0)
{
tt_log::GetCustomLog().Log( tt_log::SV_ERROR, LOGID, "[TransactionCounting] Error opening %s error: %s.", file.c_str(), _sys_errlist[errno] );
return false;
}
detail::FileHeader hdr;
if (false == ReadFileHeader(hdr))
{
tt_log::GetCustomLog().Log( tt_log::SV_ERROR, LOGID, "[TransactionCounting] %s is not a storage file", file.c_str());
return false;
}
if (hdr.magic != HEADER_MAGIC)
{
tt_log::GetCustomLog().Log( tt_log::SV_ERROR, LOGID, "[TransactionCounting] %s is not a storage file", file.c_str());
return false;
}
if (hdr.version != HEADER_VERSION)
{
tt_log::GetCustomLog().Log( tt_log::SV_ERROR, LOGID, "[TransactionCounting] %s unsupported version, expected %d got %d", file.c_str(), HEADER_VERSION, hdr.version);
return false;
}
if (strncmp(hdr.fileid, fileId, MAX_FILEID) != 0)
{
tt_log::GetCustomLog().Log( tt_log::SV_ERROR, LOGID, "[TransactionCounting] %s fileid mispatch, expected %d got %d", file.c_str(), fileId, hdr.fileid);
return false;
}
m_fileLength = _lseek(m_fd, 0, SEEK_END);
return true;
}
bool StorageFile::Close()
{
if (m_fd < 0)
{
return true;
}
_close(m_fd);
m_fd = -1;
return true;
}
void StorageFile::AddRecord(const void *data, size_t size, int recordId, StoragePosition& pos)
{
TT_ASSERT(m_fd > 0);
TT_ASSERT(m_bReadOnly == false);
pos.offset = _lseek(m_fd, 0, SEEK_END);
detail::RecordHeader rh;
rh.recordId = recordId;
rh.size = size;
rh.modified = 0;
rh.created = _GetTime();
pos.recordId = recordId;
pos.size = size;
_write(m_fd, &rh, sizeof(detail::RecordHeader));
_write(m_fd, data, size);
m_fileLength = _lseek(m_fd, 0, SEEK_END);
}
void StorageFile::ReadRecord(void *data, size_t size, const StoragePosition& pos)
{
TT_ASSERT(m_fd > 0);
TT_ASSERT(pos.offset < m_fileLength);
#if defined(_DEBUG)
ValidateRecordHeader(pos);
#endif
_lseek(m_fd, pos.offset + sizeof(detail::RecordHeader), SEEK_SET);
int r = _read(m_fd, data, size);
if (r < 0)
{
stringstream strm;
strm << "Read error: " << _sys_errlist[errno];
throw std::runtime_error(strm.str());
}
if ((size_t)r != size)
{
throw std::runtime_error("Unexpected EOF");
}
}
void StorageFile::UpdateRecord(const void *data, size_t size, const StoragePosition& pos)
{
TT_ASSERT(m_fd > 0);
TT_ASSERT(m_bReadOnly == false);
TT_ASSERT(pos.offset < m_fileLength);
detail::RecordHeader rh;
ReadRecordHeader(pos, rh);
if (pos.size != size)
{
throw std::runtime_error("Size does not match record size");
}
rh.modified = _GetTime();
WriteRecordHeader(pos, rh);
_lseek(m_fd, pos.offset+sizeof(detail::RecordHeader), SEEK_SET);
_write(m_fd, data, size);
}
bool StorageFile::WriteFileHeader(const detail::FileHeader& hdr)
{
TT_ASSERT(m_fd > 0);
TT_ASSERT(m_bReadOnly == false);
if (m_bReadOnly)
{
return false;
}
_lseek(m_fd, 0, SEEK_SET);
int r =_write(m_fd, &hdr, sizeof(hdr));
TT_ASSERT(r == sizeof(hdr));
if (r != sizeof(hdr))
{
return false;
}
return true;
}
bool StorageFile::ReadFileHeader(detail::FileHeader& hdr)
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, 0, SEEK_SET);
int r = _read(m_fd, &hdr, sizeof(hdr));
if (r != sizeof(hdr))
{
return false;
}
hdr.fileid[MAX_FILEID-1] = '\0';
return true;
}
void StorageFile::WriteRecordHeader(const StoragePosition& pos, const detail::RecordHeader& hdr)
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, pos.offset, SEEK_SET);
_write(m_fd, &hdr, sizeof(hdr));
}
void StorageFile::ReadRecordHeader(const StoragePosition& pos, detail::RecordHeader& hdr)
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, pos.offset, SEEK_SET);
int r = _read(m_fd, &hdr, sizeof(hdr));
if (r < 0)
{
stringstream strm;
strm << "Read error: " << _sys_errlist[errno];
throw std::runtime_error(strm.str());
}
if (r != sizeof(hdr))
{
throw std::runtime_error("Unexpected EOF");
}
TT_ASSERT(hdr.recordId == pos.recordId);
TT_ASSERT(hdr.size == pos.size);
}
StoragePosition StorageFile::Begin(long offset)
{
TT_ASSERT(offset < m_fileLength);
StoragePosition pos;
if (offset >= m_fileLength)
{
pos.offset = 0;
pos.recordId = 0;
pos.size = 0;
return pos;
}
pos.offset = _lseek(m_fd, offset, SEEK_SET);
detail::RecordHeader rh;
_read(m_fd, &rh, sizeof(rh));
pos.recordId = rh.recordId;
pos.size = rh.size;
return pos;
}
StoragePosition StorageFile::Begin()
{
TT_ASSERT(m_fd > 0);
StoragePosition pos;
pos.offset = _lseek(m_fd, sizeof(detail::FileHeader), SEEK_SET);
if (pos.offset == m_fileLength)
{
pos.offset = 0;
pos.recordId = 0;
pos.size = 0;
pos.created = 0;
pos.modified = 0;
return pos;
}
detail::RecordHeader rh;
ReadRecordHeader(pos, rh);
pos.recordId = rh.recordId;
pos.size = rh.size;
return pos;
}
bool StorageFile::Next(StoragePosition& pos)
{
TT_ASSERT(m_fd > 0);
TT_ASSERT(pos.offset != 0);
if (pos.offset == 0)
{
return false;
}
pos.offset = pos.offset + sizeof(detail::RecordHeader) + pos.size;
if (pos.offset >= m_fileLength)
{
pos.offset = 0;
pos.recordId = 0;
pos.size = 0;
pos.created = 0;
pos.modified = 0;
return false; // eof
}
detail::RecordHeader rh;
ReadRecordHeader(pos, rh);
pos.recordId = rh.recordId;
pos.created = rh.created;
pos.modified = rh.modified;
pos.size = rh.size;
return true;
}
void StorageFile::ValidateRecordHeader(const StoragePosition& pos)
{
_lseek(m_fd, pos.offset, SEEK_SET);
detail::RecordHeader rh;
int r = _read(m_fd, &rh, sizeof(rh));
if (r < 0)
{
stringstream strm;
strm << "Read error: " << _sys_errlist[errno];
throw std::runtime_error(strm.str());
}
if (r != sizeof(rh))
{
throw std::runtime_error("Unexpected EOF");
}
TT_ASSERT(rh.recordId == pos.recordId);
TT_ASSERT(rh.size == pos.size);
}
}