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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
#pragma once
#include <util/stream/zlib.h>
#include "microbdb.h"
#include "safeopen.h"
class TCompressedInputFileManip: public TInputFileManip {
public:
inline i64 GetLength() const {
return -1; // Some microbdb logic rely on unknown size of compressed files
}
inline i64 Seek(i64 offset, int whence) {
i64 oldPos = DoGetPosition();
i64 newPos = offset;
switch (whence) {
case SEEK_CUR:
newPos += oldPos;
[[fallthrough]]; // Complier happy. Please fix it!
case SEEK_SET:
break;
default:
return -1L;
}
if (oldPos > newPos) {
VerifyRandomAccess();
DoSeek(0, SEEK_SET, IsStreamOpen());
oldPos = 0;
}
const size_t bufsize = 1 << 12;
char buf[bufsize];
for (i64 i = oldPos; i < newPos; i += bufsize)
InputStream->Read(buf, (i + (i64)bufsize < newPos) ? bufsize : (size_t)(newPos - i));
return newPos;
}
i64 RealSeek(i64 offset, int whence) {
InputStream.Destroy();
i64 ret = DoSeek(offset, whence, !!CompressedInput);
if (ret != -1)
DoStreamOpen(DoCreateStream(), true);
return ret;
}
protected:
IInputStream* CreateStream(const TFile& file) override {
CompressedInput.Reset(new TUnbufferedFileInput(file));
return DoCreateStream();
}
inline IInputStream* DoCreateStream() {
return new TZLibDecompress(CompressedInput.Get(), ZLib::GZip);
//return new TLzqDecompress(CompressedInput.Get());
}
THolder<IInputStream> CompressedInput;
};
class TCompressedBufferedInputFileManip: public TCompressedInputFileManip {
protected:
IInputStream* CreateStream(const TFile& file) override {
CompressedInput.Reset(new TFileInput(file, 0x100000));
return DoCreateStream();
}
};
using TCompressedInputPageFile = TInputPageFileImpl<TCompressedInputFileManip>;
using TCompressedBufferedInputPageFile = TInputPageFileImpl<TCompressedBufferedInputFileManip>;
template <class TVal>
struct TGzKey {
ui64 Offset;
TVal Key;
static const ui32 RecordSig = TVal::RecordSig + 0x50495a47;
TGzKey() {
}
TGzKey(ui64 offset, const TVal& key)
: Offset(offset)
, Key(key)
{
}
size_t SizeOf() const {
if (this)
return sizeof(Offset) + ::SizeOf(&Key);
else {
size_t sizeOfKey = ::SizeOf((TVal*)NULL);
return sizeOfKey ? (sizeof(Offset) + sizeOfKey) : 0;
}
}
};
template <class TVal>
class TInZIndexFile: protected TInDatFileImpl<TGzKey<TVal>> {
typedef TInDatFileImpl<TGzKey<TVal>> TDatFile;
typedef TGzKey<TVal> TGzVal;
typedef typename TDatFile::TRecIter TRecIter;
typedef typename TRecIter::TPageIter TPageIter;
public:
TInZIndexFile()
: Index0(nullptr)
{
}
int Open(const char* fname, size_t pages = 1, int pagesOrBytes = 1, ui32* gotRecordSig = nullptr) {
int ret = TDatFile::Open(fname, pages, pagesOrBytes, gotRecordSig);
if (ret)
return ret;
if (!(Index0 = (TDatPage*)malloc(TPageIter::GetPageSize()))) {
TDatFile::Close();
return MBDB_NO_MEMORY;
}
if (SizeOf((TGzVal*)NULL))
RecsOnPage = (TPageIter::GetPageSize() - sizeof(TDatPage)) / DatCeil(SizeOf((TGzVal*)NULL));
TDatFile::Next();
memcpy(Index0, TPageIter::Current(), TPageIter::GetPageSize());
return 0;
}
int Close() {
free(Index0);
Index0 = NULL;
return TDatFile::Close();
}
inline int GetError() const {
return TDatFile::GetError();
}
int FindKey(const TVal* akey, const typename TExtInfoType<TVal>::TResult* = NULL) {
assert(IsOpen());
if (!SizeOf((TVal*)NULL))
return FindVszKey(akey);
int pageno;
i64 offset;
FindKeyOnPage(pageno, offset, Index0, akey);
TDatPage* page = TPageIter::GotoPage(pageno + 1);
int num_add = (int)offset;
FindKeyOnPage(pageno, offset, page, akey);
return pageno + num_add;
}
using TDatFile::IsOpen;
int FindVszKey(const TVal* akey, const typename TExtInfoType<TVal>::TResult* = NULL) {
int pageno;
i64 offset;
FindVszKeyOnPage(pageno, offset, Index0, akey);
TDatPage* page = TPageIter::GotoPage(pageno + 1);
int num_add = (int)offset;
FindVszKeyOnPage(pageno, offset, page, akey);
return pageno + num_add;
}
i64 FindPage(int pageno) {
if (!SizeOf((TVal*)NULL))
return FindVszPage(pageno);
int recsize = DatCeil(SizeOf((TGzVal*)NULL));
TDatPage* page = TPageIter::GotoPage(1 + pageno / RecsOnPage);
if (!page) // can happen if pageno is beyond EOF
return -1;
unsigned int localpageno = pageno % RecsOnPage;
if (localpageno >= page->RecNum) // can happen if pageno is beyond EOF
return -1;
TGzVal* v = (TGzVal*)((char*)page + sizeof(TDatPage) + localpageno * recsize);
return v->Offset;
}
i64 FindVszPage(int pageno) {
TGzVal* cur = (TGzVal*)((char*)Index0 + sizeof(TDatPage));
TGzVal* prev = cur;
unsigned int n = 0;
while (n < Index0->RecNum && cur->Offset <= (unsigned int)pageno) {
prev = cur;
cur = (TGzVal*)((char*)cur + DatCeil(SizeOf(cur)));
n++;
}
TDatPage* page = TPageIter::GotoPage(n);
unsigned int num_add = (unsigned int)(prev->Offset);
n = 0;
cur = (TGzVal*)((char*)page + sizeof(TDatPage));
while (n < page->RecNum && n + num_add < (unsigned int)pageno) {
cur = (TGzVal*)((char*)cur + DatCeil(SizeOf(cur)));
n++;
}
if (n == page->RecNum) // can happen if pageno is beyond EOF
return -1;
return cur->Offset;
}
protected:
void FindKeyOnPage(int& pageno, i64& offset, TDatPage* page, const TVal* Key) {
int left = 0;
int right = page->RecNum - 1;
int recsize = DatCeil(SizeOf((TGzVal*)NULL));
while (left < right) {
int middle = (left + right) >> 1;
if (((TGzVal*)((char*)page + sizeof(TDatPage) + middle * recsize))->Key < *Key)
left = middle + 1;
else
right = middle;
}
//borders check (left and right)
pageno = (left == 0 || ((TGzVal*)((char*)page + sizeof(TDatPage) + left * recsize))->Key < *Key) ? left : left - 1;
offset = ((TGzVal*)((char*)page + sizeof(TDatPage) + pageno * recsize))->Offset;
}
void FindVszKeyOnPage(int& pageno, i64& offset, TDatPage* page, const TVal* key) {
TGzVal* cur = (TGzVal*)((char*)page + sizeof(TDatPage));
ui32 RecordSig = page->RecNum;
i64 tmpoffset = cur->Offset;
for (; RecordSig > 0 && cur->Key < *key; --RecordSig) {
tmpoffset = cur->Offset;
cur = (TGzVal*)((char*)cur + DatCeil(SizeOf(cur)));
}
int idx = page->RecNum - RecordSig - 1;
pageno = (idx >= 0) ? idx : 0;
offset = tmpoffset;
}
TDatPage* Index0;
int RecsOnPage;
};
template <class TKey>
class TCompressedIndexedInputPageFile: public TCompressedInputPageFile {
public:
int GotoPage(int pageno);
protected:
TInZIndexFile<TKey> KeyFile;
};
template <class TVal, class TKey>
class TDirectCompressedInDatFile: public TDirectInDatFile<TVal, TKey,
TInDatFileImpl<TVal, TInputRecordIterator<TVal,
TInputPageIterator<TCompressedIndexedInputPageFile<TKey>>>>> {
};
class TCompressedOutputFileManip: public TOutputFileManip {
public:
inline i64 GetLength() const {
return -1; // Some microbdb logic rely on unknown size of compressed files
}
inline i64 Seek(i64 offset, int whence) {
i64 oldPos = DoGetPosition();
i64 newPos = offset;
switch (whence) {
case SEEK_CUR:
newPos += oldPos;
[[fallthrough]]; // Compler happy. Please fix it!
case SEEK_SET:
break;
default:
return -1L;
}
if (oldPos > newPos)
return -1L;
const size_t bufsize = 1 << 12;
char buf[bufsize] = {0};
for (i64 i = oldPos; i < newPos; i += bufsize)
OutputStream->Write(buf, (i + (i64)bufsize < newPos) ? bufsize : (size_t)(newPos - i));
return newPos;
}
i64 RealSeek(i64 offset, int whence) {
OutputStream.Destroy();
i64 ret = DoSeek(offset, whence, !!CompressedOutput);
if (ret != -1)
DoStreamOpen(DoCreateStream(), true);
return ret;
}
protected:
IOutputStream* CreateStream(const TFile& file) override {
CompressedOutput.Reset(new TUnbufferedFileOutput(file));
return DoCreateStream();
}
inline IOutputStream* DoCreateStream() {
return new TZLibCompress(CompressedOutput.Get(), ZLib::GZip, 1);
}
THolder<IOutputStream> CompressedOutput;
};
class TCompressedBufferedOutputFileManip: public TCompressedOutputFileManip {
protected:
IOutputStream* CreateStream(const TFile& file) override {
CompressedOutput.Reset(new TUnbufferedFileOutput(file));
return DoCreateStream();
}
inline IOutputStream* DoCreateStream() {
return new TZLibCompress(CompressedOutput.Get(), ZLib::GZip, 1, 0x100000);
}
};
using TCompressedOutputPageFile = TOutputPageFileImpl<TCompressedOutputFileManip>;
using TCompressedBufferedOutputPageFile = TOutputPageFileImpl<TCompressedBufferedOutputFileManip>;
template <class TVal>
class TOutZIndexFile: public TOutDatFileImpl<
TGzKey<TVal>,
TOutputRecordIterator<TGzKey<TVal>, TOutputPageIterator<TOutputPageFile>, TCallbackIndexer>> {
typedef TOutDatFileImpl<
TGzKey<TVal>,
TOutputRecordIterator<TGzKey<TVal>, TOutputPageIterator<TOutputPageFile>, TCallbackIndexer>>
TDatFile;
typedef TOutZIndexFile<TVal> TMyType;
typedef TGzKey<TVal> TGzVal;
typedef typename TDatFile::TRecIter TRecIter;
typedef typename TRecIter::TPageIter TPageIter;
typedef typename TRecIter::TIndexer TIndexer;
public:
TOutZIndexFile() {
TotalRecNum = 0;
TIndexer::SetCallback(this, DispatchCallback);
}
int Open(const char* fname, size_t pagesize, size_t pages, int pagesOrBytes = 1) {
int ret = TDatFile::Open(fname, pagesize, pages, pagesOrBytes);
if (ret)
return ret;
if ((ret = TRecIter::GotoPage(1)))
TDatFile::Close();
return ret;
}
int Close() {
TPageIter::Unfreeze();
if (TRecIter::RecNum)
NextPage(TPageIter::Current());
int ret = 0;
if (Index0.size() && !(ret = TRecIter::GotoPage(0))) {
typename std::vector<TGzVal>::iterator it, end = Index0.end();
for (it = Index0.begin(); it != end; ++it)
TRecIter::Push(&*it);
ret = (TPageIter::GetPageNum() != 0) ? MBDB_PAGE_OVERFLOW : TPageIter::GetError();
}
Index0.clear();
int ret1 = TDatFile::Close();
return ret ? ret : ret1;
}
protected:
int TotalRecNum; // should be enough because we have GotoPage(int)
std::vector<TGzVal> Index0;
void NextPage(const TDatPage* page) {
TGzVal* rec = (TGzVal*)((char*)page + sizeof(TDatPage));
Index0.push_back(TGzVal(TotalRecNum, rec->Key));
TotalRecNum += TRecIter::RecNum;
}
static void DispatchCallback(void* This, const TDatPage* page) {
((TMyType*)This)->NextPage(page);
}
};
template <class TVal, class TKey, class TPageFile = TCompressedOutputPageFile>
class TOutDirectCompressedFileImpl: public TOutDatFileImpl<
TVal,
TOutputRecordIterator<TVal, TOutputPageIterator<TPageFile>, TCallbackIndexer>> {
typedef TOutDatFileImpl<
TVal,
TOutputRecordIterator<TVal, TOutputPageIterator<TPageFile>, TCallbackIndexer>>
TDatFile;
typedef TOutDirectCompressedFileImpl<TVal, TKey, TPageFile> TMyType;
typedef typename TDatFile::TRecIter TRecIter;
typedef typename TRecIter::TPageIter TPageIter;
typedef typename TRecIter::TIndexer TIndexer;
typedef TGzKey<TKey> TMyKey;
typedef TOutZIndexFile<TKey> TKeyFile;
protected:
using TDatFile::Tell;
public:
TOutDirectCompressedFileImpl() {
TIndexer::SetCallback(this, DispatchCallback);
}
int Open(const char* fname, size_t pagesize, size_t ipagesize = 0) {
char iname[FILENAME_MAX];
int ret;
if (ipagesize == 0)
ipagesize = pagesize;
ret = TDatFile::Open(fname, pagesize, 1, 1);
ret = ret ? ret : DatNameToIdx(iname, fname);
ret = ret ? ret : KeyFile.Open(iname, ipagesize, 1, 1);
if (ret)
TDatFile::Close();
return ret;
}
int Close() {
if (TRecIter::RecNum)
NextPage(TPageIter::Current());
int ret = KeyFile.Close();
int ret1 = TDatFile::Close();
return ret1 ? ret1 : ret;
}
int GetError() const {
return TDatFile::GetError() ? TDatFile::GetError() : KeyFile.GetError();
}
protected:
TKeyFile KeyFile;
void NextPage(const TDatPage* page) {
size_t sz = SizeOf((TMyKey*)NULL);
TMyKey* rec = KeyFile.Reserve(sz ? sz : MaxSizeOf<TMyKey>());
if (rec) {
rec->Offset = Tell();
rec->Key = *(TVal*)((char*)page + sizeof(TDatPage));
KeyFile.ResetDat();
}
}
static void DispatchCallback(void* This, const TDatPage* page) {
((TMyType*)This)->NextPage(page);
}
};
template <class TKey>
int TCompressedIndexedInputPageFile<TKey>::GotoPage(int pageno) {
if (Error)
return Error;
Eof = 0;
i64 offset = KeyFile.FindPage(pageno);
if (!offset)
return Error = MBDB_BAD_FILE_SIZE;
if (offset != FileManip.RealSeek(offset, SEEK_SET))
Error = MBDB_BAD_FILE_SIZE;
return Error;
}
template <typename TVal>
class TCompressedInDatFile: public TInDatFile<TVal, TCompressedInputPageFile> {
public:
TCompressedInDatFile(const char* name, size_t pages, int pagesOrBytes = 1)
: TInDatFile<TVal, TCompressedInputPageFile>(name, pages, pagesOrBytes)
{
}
};
template <typename TVal>
class TCompressedOutDatFile: public TOutDatFile<TVal, TFakeCompression, TCompressedOutputPageFile> {
public:
TCompressedOutDatFile(const char* name, size_t pagesize, size_t pages, int pagesOrBytes = 1)
: TOutDatFile<TVal, TFakeCompression, TCompressedOutputPageFile>(name, pagesize, pages, pagesOrBytes)
{
}
};
template <typename TVal, typename TKey, typename TPageFile = TCompressedOutputPageFile>
class TOutDirectCompressedFile: protected TOutDirectCompressedFileImpl<TVal, TKey, TPageFile> {
typedef TOutDirectCompressedFileImpl<TVal, TKey, TPageFile> TBase;
public:
TOutDirectCompressedFile(const char* name, size_t pagesize, size_t ipagesize = 0)
: Name(strdup(name))
, PageSize(pagesize)
, IdxPageSize(ipagesize)
{
}
~TOutDirectCompressedFile() {
Close();
free(Name);
Name = NULL;
}
void Open(const char* fname) {
int ret = TBase::Open(fname, PageSize, IdxPageSize);
if (ret)
ythrow yexception() << ErrorMessage(ret, "Failed to open output file", fname);
free(Name);
Name = strdup(fname);
}
void Close() {
int ret;
if ((ret = TBase::GetError()))
if (!std::uncaught_exception())
ythrow yexception() << ErrorMessage(ret, "Error before closing output file", Name);
if ((ret = TBase::Close()))
if (!std::uncaught_exception())
ythrow yexception() << ErrorMessage(ret, "Error while closing output file", Name);
}
const char* GetName() const {
return Name;
}
using TBase::Freeze;
using TBase::Push;
using TBase::Reserve;
using TBase::Unfreeze;
protected:
char* Name;
size_t PageSize, IdxPageSize;
};
class TCompressedInterFileTypes {
public:
typedef TCompressedBufferedOutputPageFile TOutPageFile;
typedef TCompressedBufferedInputPageFile TInPageFile;
};
|