-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal.cpp
More file actions
398 lines (389 loc) · 9.63 KB
/
Copy pathjournal.cpp
File metadata and controls
398 lines (389 loc) · 9.63 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#define __USE_MINGW_ANSI_STDIO 1
#include <stdio.h>
#include <stdexcept>
#include <sstream>
#include <vector>
#include <errno.h>
#include <sys/stat.h>
#include <memory>
#include "journal.h"
#include "static_exception.h"
extern bool verbose;
namespace Journal
{
struct Atoms : public std::vector<std::string>
{
std::string get(uint32_t n)
{
if (n >= size())
{
return std::string("Undefined atom number ") + std::to_string(n);
}
return at(n);
}
};
void dumpIt(Reader& file)
{
struct stat64 st{};
if (fstat64(file.f, &st) == -1)
{
fprintf(stderr, "fstat error %u\n", errno);
}
printf("Replication journal %s, length %llu\n", to_string(file.fileName).c_str(), st.st_size);
char signature[SignatureSize];
// Here we read separate pieces of header instead of whole one to handle different endianness
file.readBuffer(signature, SignatureSize);
printf("\tSignature: %.12s\n", signature);
if (memcmp(signature, Signature, SignatureSize) != 0)
{
throw static_exception("Signature is wrong");
}
const unsigned short version = file.getInt16();
printf("\tVersion: %u\n", version);
if (version != 1)
{
throw static_exception("Unrecognized journal version");
}
const unsigned short state = file.getInt16();
printf("\tState: ");
switch (state)
{
case SEGMENT_STATE_FREE:
{
printf("free\n");
break;
}
case SEGMENT_STATE_USED:
{
printf("used\n");
break;
}
case SEGMENT_STATE_FULL:
{
printf("full\n");
break;
}
case SEGMENT_STATE_ARCH:
{
printf("archived\n");
break;
}
default:
{
printf("unknown (%x)\n", state);
break;
}
}
UUID uuid;
uuid.Data1 = file.getInt32();
uuid.Data2 = file.getInt16();
uuid.Data3 = file.getInt16();
file.readBuffer(uuid.Data4, sizeof(uuid.Data4));
printf("\tUUID: {%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}\n",
(unsigned)uuid.Data1,
uuid.Data2,
uuid.Data3,
uuid.Data4[0], uuid.Data4[1], uuid.Data4[2],
uuid.Data4[3], uuid.Data4[4], uuid.Data4[5],
uuid.Data4[6], uuid.Data4[7]);
const uint64_t sequence = file.getInt64();
printf("\tSequence: %llu\n", sequence);
uint64_t length = file.getInt64();
printf("\tLength: %llu\n", length);
length -= sizeof(SegmentHeader);
// Physical size of file can be bigger than size of data inside
while (length > 0)
{
size_t pos = file.offset();
uint64_t transactionNumber = file.getInt64();
uint16_t protocol = file.getInt16();
uint16_t flags = file.getInt16();
uint32_t blockLength = file.getInt32();
printf("Transaction number %llu, protocol %x, flags %04x (", transactionNumber, protocol, flags);
bool comma = false;
if (flags & BLOCK_BEGIN_TRANS)
{
printf("begin");
comma = true;
}
if (flags & BLOCK_END_TRANS)
{
printf("%send", comma ? "," : "");
}
if (flags & ~(BLOCK_BEGIN_TRANS | BLOCK_END_TRANS))
{
printf("%sunknown flag", comma ? "," : "");
}
printf("), length: %u, offset: %zu\n", blockLength, pos);
if (length < blockLength + sizeof(Block))
{
fprintf(stderr, "Structure error: too long transaction block\n");
return;
}
length -= blockLength + sizeof(Block);
// Atoms are individual for each block
Atoms atoms;
while (blockLength > 0)
{
pos = file.offset();
const Operation op = static_cast<Operation>(file.getInt8());
size_t opLength = 1;
switch (op)
{
case opStartTransaction:
{
printf("\tStart transaction\n");
break;
}
case opPrepareTransaction:
{
printf("\tPrepare transaction\n");
break;
}
case opCommitTransaction:
{
printf("\tCommit transaction\n");
break;
}
case opRollbackTransaction:
{
printf("\tRollback transaction\n");
break;
}
case opCleanupTransaction:
{
printf("\tCleanup transaction\n");
break;
}
case opStartSavepoint:
{
printf("\tStart savepoint\n");
break;
}
case opReleaseSavepoint:
{
printf("\tRelease savepoint\n");
break;
}
case opRollbackSavepoint:
{
printf("\tRollback savepoint\n");
break;
}
case opInsertRecord:
{
uint32_t atom = file.getInt32();
std::string name = atoms.get(atom);
opLength += 4;
if (protocol > 1)
{
atom = file.getInt32();
opLength += 4;
name += '.';
name += atoms.get(atom);
}
uint32_t dataLength = file.getInt32();
opLength += 4 + dataLength;
printf("\tInsert record into %s, data length %u\n", name.c_str(), dataLength);
if (verbose)
{
CompactHexDumper dumper;
printf("\t\t");
file.dumpTo(dumper, dataLength);
printf("\n");
}
else
{
file.skip(dataLength);
}
break;
}
case opUpdateRecord:
{
uint32_t atom = file.getInt32();
std::string name = atoms.get(atom);
opLength += 4;
if (protocol > 1)
{
atom = file.getInt32();
opLength += 4;
name += '.';
name += atoms.get(atom);
}
uint32_t oldDataLength = file.getInt32();
opLength += 4 + oldDataLength;
std::unique_ptr<unsigned char[]> oldData;
if (verbose)
{
oldData.reset(new unsigned char[oldDataLength]);
file.readBuffer(oldData.get(), oldDataLength);
}
else
{
file.skip(oldDataLength);
}
uint32_t newDataLength = file.getInt32();
opLength += 4 + newDataLength;
printf("\tUpdate record in %s, data length %u-%u\n", name.c_str(), oldDataLength, newDataLength);
if (verbose)
{
CompactHexDumper dumper;
printf("\t\t");
dumper.dumpIt(oldData.get(), oldDataLength);
printf("\n\t\t");
file.dumpTo(dumper, newDataLength);
printf("\n");
}
else
{
file.skip(newDataLength);
}
break;
}
case opDeleteRecord:
{
uint32_t atom = file.getInt32();
std::string name = atoms.get(atom);
opLength += 4;
if (protocol > 1)
{
atom = file.getInt32();
opLength += 4;
name += '.';
name += atoms.get(atom);
}
uint32_t dataLength = file.getInt32();
opLength += 4 + dataLength;
printf("\tDelete record from %s, data length %u\n", name.c_str(), dataLength);
if (verbose)
{
CompactHexDumper dumper;
printf("\t\t");
file.dumpTo(dumper, dataLength);
printf("\n");
}
else
{
file.skip(dataLength);
}
break;
}
case opStoreBlob:
{
uint32_t bidHigh = file.getInt32();
uint32_t bidLow = file.getInt32();
opLength += 8 + 2; // Include closing empty segment
printf("\tBlob with id (%x:%x)\n", bidHigh, bidLow);
while (uint16_t segmentLength = file.getInt16())
{
printf("\t\tSegment of length %hu\n", segmentLength);
opLength += 2 + segmentLength;
if (opLength > blockLength)
{
fprintf(stderr, "Structure error: blob data length bigger than block length\n");
return;
}
if (verbose)
{
HexDumper dumper;
file.dumpTo(dumper, segmentLength);
}
else
{
file.skip(segmentLength);
}
}
break;
}
case opExecuteSql:
{
uint32_t atom = file.getInt32();
std::string userName = atoms.get(atom);
uint32_t sqlLength = file.getInt32();
opLength += 4 + 4 + sqlLength;
printf("\tExecute SQL as %s, length %u\n", userName.c_str(), sqlLength);
if (verbose)
{
HexDumper dumper;
file.dumpTo(dumper, sqlLength);
}
else
{
file.skip(sqlLength);
}
break;
}
case opSetSequence:
{
uint32_t atom = file.getInt32();
std::string name = atoms.get(atom);
if (protocol > 1)
{
atom = file.getInt32();
opLength += 4;
name += '.';
name += atoms.get(atom);
}
uint64_t value = file.getInt64();
opLength += 4 + 8;
printf("\tSet sequence %s to %lld\n", name.c_str(), value);
break;
}
case opExecuteSqlIntl:
{
uint32_t atom = file.getInt32();
std::string userName = atoms.get(atom);
if (protocol > 1)
{
atom = file.getInt32();
opLength += 4;
}
uint8_t charset = file.getInt8();
uint32_t sqlLength = file.getInt32();
opLength += 4 + 4 + 1 + sqlLength;
printf("\tExecute SQL as %s, length %u, charset %u\n", userName.c_str(), sqlLength, charset);
if (protocol > 1)
{
printf("\t\tSchema path: %s\n", atoms.get(atom).c_str());
}
if (verbose)
{
HexDumper dumper;
file.dumpTo(dumper, sqlLength);
}
else
{
file.skip(sqlLength);
}
break;
}
case opDefineAtom:
{
uint8_t atomLength = file.getInt8();
char buffer[atomLength];
file.readBuffer(buffer, atomLength);
opLength += 1 + atomLength;
printf("\tAtom: %.*s\n", atomLength, buffer);
atoms.emplace_back(static_cast<const char*>(buffer), static_cast<size_t>(atomLength));
break;
}
default:
{
throw std::runtime_error(std::string("Unknown operation ") + std::to_string(op) + " at offset " + std::to_string(pos));
}
}
if (opLength > blockLength)
{
fprintf(stderr, "Structure error: operation length bigger than block length\n");
return;
}
//printf("block length = %u, op length = %zu\n", blockLength, opLength);
blockLength -= opLength;
}
}
if (!file.eof())
{
printf("*** some garbage beyond data ***\n");
}
}
}