aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/microbdb/sorter.h
blob: b2e7390377d8ca46a5b9a9764490530203630af3 (plain) (blame)
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#pragma once

#include <util/ysaveload.h>
#include <util/generic/algorithm.h>
#include <contrib/libs/libc_compat/include/link/link.h>

#include "header.h"
#include "heap.h"
#include "extinfo.h"
#include "input.h"
#include "output.h"

#ifdef TEST_MERGE
#define MBDB_SORT_FUN ::StableSort
#else
#define MBDB_SORT_FUN ::Sort
#endif

template <class TVal, class TCompare, typename TCompress, typename TSieve, typename TOutPageFile, typename TFileTypes>
class TDatSorterImpl;

template <class TVal>
struct TFakeSieve {
    static inline int Sieve(TVal*, const TVal*) noexcept {
        return 0;
    }
};

template <class TSieve>
struct TIsSieveFake {
    static const bool Result = false;
};

template <class T>
struct TIsSieveFake<TFakeSieve<T>> {
    static const bool Result = true;
};

class TDefInterFileTypes {
public:
    typedef TOutputPageFile TOutPageFile;
    typedef TInputPageFile TInPageFile;
};

//class TCompressedInterFileTypes;

template <class TVal, class TCompare, typename TCompress, typename TSieve, typename TOutPageFile = TOutputPageFile, typename TFileTypes = TDefInterFileTypes>
class TDatSorterImplBase: protected  THeapIter<TVal, TInDatFileImpl<TVal, TInputRecordIterator<TVal, TInputPageIterator<typename TFileTypes::TInPageFile>>>, TCompare> {
    typedef TOutputRecordIterator<TVal, TOutputPageIterator<typename TFileTypes::TOutPageFile>, TFakeIndexer, TCompress> TTmpRecIter;
    typedef TInputRecordIterator<TVal, TInputPageIterator<typename TFileTypes::TInPageFile>> TInTmpRecIter;

public:
    typedef TOutDatFileImpl<TVal, TTmpRecIter> TTmpOut;
    typedef TInDatFileImpl<TVal, TInTmpRecIter> TTmpIn;

    typedef TOutDatFileImpl<TVal, TOutputRecordIterator<TVal, TOutputPageIterator<TOutPageFile>, TFakeIndexer, TCompress>> TOut;
    typedef THeapIter<TVal, TTmpIn, TCompare> TMyHeap;
    typedef TVector<const TVal*> TMyVector;
    typedef typename TMyVector::iterator TMyIterator;

    class IPortionSorter {
    public:
        virtual ~IPortionSorter() {
        }

        virtual void Sort(TMyVector&, TTmpOut*) = 0;
    };

    class TDefaultSorter: public IPortionSorter {
    public:
        void Sort(TMyVector& vector, TTmpOut* out) override {
            MBDB_SORT_FUN(vector.begin(), vector.end(), TCompare());

            const typename TMyVector::const_iterator
                end = (TIsSieveFake<TSieve>::Result) ? vector.end() : TDatSorterImplBase::SieveRange(vector.begin(), vector.end());

            for (typename TMyVector::const_iterator it = vector.begin(); it != end; ++it) {
                out->PushWithExtInfo(*it);
            }
        }
    };

    class TSegmentedSorter: public IPortionSorter {
        class TAdaptor {
            typedef typename TMyVector::const_iterator TConstIterator;

        public:
            TAdaptor(TConstIterator b, TConstIterator e)
                : Curr_(b)
                , End_(e)
            {
                --Curr_;
            }

            inline const TVal* Current() const {
                return *Curr_;
            }

            inline const TVal* Next() {
                ++Curr_;

                if (Curr_ == End_) {
                    return nullptr;
                }

                return *Curr_;
            }

        private:
            TConstIterator Curr_;
            TConstIterator End_;
        };

        typedef THeapIter<TVal, TAdaptor, TCompare> TPortionsHeap;

    public:
        void Sort(TMyVector& vector, TTmpOut* out) override {
            TVector<TAdaptor> bounds;
            typename TMyVector::iterator
                it = vector.begin();
            const size_t portions = Max<size_t>(1, (vector.size() * sizeof(TVal)) / (4 << 20));
            const size_t step = vector.size() / portions;

            // Sort segments
            while (it != vector.end()) {
                const typename TMyVector::iterator
                    end = Min(it + step, vector.end());

                MBDB_SORT_FUN(it, end, TCompare());

                bounds.push_back(TAdaptor(it, end));

                it = end;
            }

            //
            // Merge result
            //

            TPortionsHeap heap(bounds);

            if (TIsSieveFake<TSieve>::Result) {
                while (const TVal* val = heap.Next()) {
                    out->PushWithExtInfo(val);
                }
            } else {
                const TVal* val = heap.Next();
                const TVal* prev = out->PushWithExtInfo(val);

                for (val = heap.Next(); val && prev; val = heap.Next()) {
                    if (TSieve::Sieve((TVal*)prev, val)) {
                        continue;
                    }

                    prev = out->PushWithExtInfo(val);
                }

                if (prev) {
                    TSieve::Sieve((TVal*)prev, prev);
                }
            }
        }
    };

public:
    TDatSorterImplBase()
        : Sorter(new TDefaultSorter)
    {
        InFiles = nullptr;
        TempBuf = nullptr;
        Ptr = Vector.end();
        Cur = nullptr;
        Portions = CPortions = Error = 0;
    }

    ~TDatSorterImplBase() {
        Close();
    }

    int Open(const char* templ, size_t pagesize, size_t pages, int pagesOrBytes = 1) {
        Portions = CPortions = Error = 0;
        TempBuf = strdup(templ);
        Pagesize = pagesize;
        if (pagesOrBytes)
            Pages = pages;
        else
            Pages = pages / pagesize;
        Pages = Max(1, Pages);
        return 0;
    }

    void Push(const TVal* v) {
        // Serialized extInfo must follow a record being pushed, therefore, to avoid
        // unintentional misusage (as if when you are adding TExtInfo in your record
        // type: you may forget to check your sorting routines and get a segfault as
        // a result).
        // PushWithExtInfo(v) should be called on records with extInfo.
        static_assert(!TExtInfoType<TVal>::Exists, "expect !TExtInfoType<TVal>::Exists");

        Vector.push_back(v);
    }

    void PushWithExtInfo(const TVal* v) {
        Vector.push_back(v);
    }

    int SortPortion() {
        Ptr = Vector.end();
        Cur = nullptr;
        if (!Vector.size() || Error)
            return Error;

        MBDB_SORT_FUN(Vector.begin(), Vector.end(), TCompare());

        if (!TIsSieveFake<TSieve>::Result) {
            const typename TMyVector::iterator
                end = SieveRange(Vector.begin(), Vector.end());

            Vector.resize(end - Vector.begin());
        }

        Ptr = Vector.begin();
        Cur = nullptr;
        return 0;
    }

    const TVal* Nextp() {
        Cur = Ptr == Vector.end() ? nullptr : *Ptr++;
        return Cur;
    }

    const TVal* Currentp() const {
        return Cur;
    }

    void Closep() {
        Vector.clear();
        Ptr = Vector.end();
        Cur = nullptr;
    }

    int NextPortion(bool direct = false) {
        if (!Vector.size() || Error)
            return Error;

        TTmpOut out;
        int ret, ret1;
        char fname[FILENAME_MAX];

        snprintf(fname, sizeof(fname), TempBuf, Portions++);
        if ((ret = out.Open(fname, Pagesize, Pages, 1, direct)))
            return Error = ret;

        Sorter->Sort(Vector, &out);

        Vector.erase(Vector.begin(), Vector.end());
        ret = out.GetError();
        ret1 = out.Close();
        Error = Error ? Error : ret ? ret : ret1;
        if (Error)
            unlink(fname);
        return Error;
    }

    int SavePortions(const char* mask, bool direct = false) {
        char srcname[PATH_MAX], dstname[PATH_MAX];
        if (Vector.size())
            NextPortion(direct);
        for (int i = 0; i < Portions; i++) {
            char num[10];
            sprintf(num, "%i", i);
            snprintf(srcname, sizeof(srcname), TempBuf, i);
            snprintf(dstname, sizeof(dstname), mask, num);
            int res = rename(srcname, dstname);
            if (res)
                return res;
        }
        snprintf(dstname, sizeof(dstname), mask, "count");
        TOFStream fcount(dstname);
        Save(&fcount, Portions);
        fcount.Finish();
        return 0;
    }

    int RestorePortions(const char* mask) {
        char srcname[PATH_MAX], dstname[PATH_MAX];
        snprintf(srcname, sizeof(srcname), mask, "count");
        TIFStream fcount(srcname);
        Load(&fcount, Portions);
        for (int i = 0; i < Portions; i++) {
            char num[10];
            sprintf(num, "%i", i);
            snprintf(dstname, sizeof(dstname), TempBuf, i);
            snprintf(srcname, sizeof(srcname), mask, num);
            unlink(dstname);
            int res = link(srcname, dstname);
            if (res)
                return res;
        }
        return 0;
    }

    int RestorePortions(const char* mask, ui32 count) {
        char srcname[PATH_MAX], dstname[PATH_MAX];
        ui32 portions;
        TVector<ui32> counts;
        for (ui32 j = 0; j < count; j++) {
            snprintf(srcname, sizeof(srcname), mask, j, "count");
            TIFStream fcount(srcname);
            Load(&fcount, portions);
            counts.push_back(portions);
            Portions += portions;
        }
        ui32 p = 0;
        for (ui32 j = 0; j < count; j++) {
            int cnt = counts[j];
            for (int i = 0; i < cnt; i++, p++) {
                char num[10];
                sprintf(num, "%i", i);
                snprintf(dstname, sizeof(dstname), TempBuf, p);
                snprintf(srcname, sizeof(srcname), mask, j, num);
                unlink(dstname);
                int res = link(srcname, dstname);
                if (res) {
                    fprintf(stderr, "Can not link %s to %s\n", srcname, dstname);
                    return res;
                }
            }
        }
        return 0;
    }

    int Sort(size_t memory, int maxportions = 1000, bool direct = false) {
        int ret, end, beg, i;
        char fname[FILENAME_MAX];

        if (Vector.size())
            NextPortion();

        if (Error)
            return Error;
        if (!Portions) {
            TMyHeap::Init(&DummyFile, 1); // closed file
            HPages = 1;
            return 0;
        }

        Optimize(memory, maxportions);
        if (!(InFiles = new TTmpIn[MPortions]))
            return MBDB_NO_MEMORY;

        for (beg = 0; beg < Portions && !Error; beg = end) {
            end = (int)Min(beg + FPortions, Portions);
            for (i = beg; i < end && !Error; i++) {
                snprintf(fname, sizeof(fname), TempBuf, i);
                if ((ret = InFiles[i - beg].Open(fname, HPages, 1, nullptr, direct)))
                    Error = Error ? Error : ret;
            }
            if (Error)
                return Error;
            TMyHeap::Init(InFiles, end - beg);
            if (end != Portions) {
                TTmpOut out;
                const TVal* v;
                snprintf(fname, sizeof(fname), TempBuf, Portions++);
                if ((ret = out.Open(fname, Pagesize, HPages)))
                    return Error = Error ? Error : ret;
                while ((v = TMyHeap::Next()))
                    out.PushWithExtInfo(v);
                ret = out.GetError();
                Error = Error ? Error : ret;
                ret = out.Close();
                Error = Error ? Error : ret;
                for (i = beg; i < end; i++) {
                    ret = InFiles[i - beg].Close();
                    Error = Error ? Error : ret;
                    snprintf(fname, sizeof(fname), TempBuf, CPortions++);
                    unlink(fname);
                }
            }
            FPortions = MPortions;
        }
        return Error;
    }

    int Close() {
        char fname[FILENAME_MAX];
        delete[] InFiles;
        InFiles = nullptr;
        Closep();
        for (int i = CPortions; i < Portions; i++) {
            snprintf(fname, sizeof(fname), TempBuf, i);
            unlink(fname);
        }
        CPortions = Portions = 0;
        free(TempBuf);
        TempBuf = nullptr;
        return Error;
    }

    void UseSegmentSorter() {
        Sorter.Reset(new TSegmentedSorter);
    }

    inline int GetError() const {
        return Error;
    }

    inline int GetPages() const {
        return Pages;
    }

    inline int GetPageSize() const {
        return Pagesize;
    }

private:
    static TMyIterator SieveRange(const TMyIterator begin, const TMyIterator end) {
        TMyIterator it = begin;
        TMyIterator prev = begin;

        for (++it; it != end; ++it) {
            if (TSieve::Sieve((TVal*)*prev, *it)) {
                continue;
            }

            ++prev;

            if (it != prev) {
                *prev = *it;
            }
        }

        TSieve::Sieve((TVal*)*prev, *prev);

        return ++prev;
    }

protected:
    void Optimize(size_t memory, int maxportions, size_t fbufmax = 256u << 20) {
        maxportions = (int)Min((size_t)maxportions, memory / Pagesize) - 1;
        size_t maxpages = Max((size_t)1u, fbufmax / Pagesize);

        if (maxportions <= 2) {
            FPortions = MPortions = 2;
            HPages = 1;
            return;
        }
        if (maxportions >= Portions) {
            FPortions = MPortions = Portions;
            HPages = (int)Min(memory / ((Portions + 1) * Pagesize), maxpages);
            return;
        }
        if (((Portions + maxportions - 1) / maxportions) <= maxportions) {
            while (((Portions + maxportions - 1) / maxportions) <= maxportions)
                --maxportions;
            MPortions = ++maxportions;
            int total = ((Portions + maxportions - 1) / maxportions) + Portions;
            FPortions = (total % maxportions) ? (total % maxportions) : MPortions;
            HPages = (int)Min(memory / ((MPortions + 1) * Pagesize), maxpages);
            return;
        }
        FPortions = MPortions = maxportions;
        HPages = (int)Min(memory / ((MPortions + 1) * Pagesize), maxpages);
    }

    TMyVector Vector;
    typename TMyVector::iterator Ptr;
    const TVal* Cur;
    TTmpIn *InFiles, DummyFile;
    char* TempBuf;
    int Portions, CPortions, Pagesize, Pages, Error;
    int FPortions, MPortions, HPages;
    THolder<IPortionSorter> Sorter;
};

template <class TVal, class TCompare, typename TCompress>
class TDatSorterImpl<TVal, TCompare, TCompress, TFakeSieve<TVal>, TOutputPageFile, TDefInterFileTypes>
   : public TDatSorterImplBase<TVal, TCompare, TCompress, TFakeSieve<TVal>, TOutputPageFile, TDefInterFileTypes> {
    typedef TDatSorterImplBase<TVal, TCompare, TCompress, TFakeSieve<TVal>, TOutputPageFile, TDefInterFileTypes> TBase;

public:
    int SortToFile(const char* name, size_t memory, int maxportions = 1000) {
        int ret = TBase::Sort(memory, maxportions);
        if (ret)
            return ret;
        typename TBase::TOut out;
        if ((ret = out.Open(name, TBase::Pagesize, TBase::HPages)))
            return ret;
        const TVal* rec;
        while ((rec = Next()))
            out.PushWithExtInfo(rec);
        if ((ret = out.GetError()))
            return ret;
        if ((ret = out.Close()))
            return ret;
        if ((ret = TBase::Close()))
            return ret;
        return 0;
    }

    int SortToStream(TAutoPtr<IOutputStream> output, size_t memory, int maxportions = 1000) {
        int ret = TBase::Sort(memory, maxportions);
        if (ret)
            return ret;
        typename TBase::TOut out;
        if ((ret = out.Open(output, TBase::Pagesize, TBase::HPages)))
            return ret;
        const TVal* rec;
        while ((rec = Next()))
            out.PushWithExtInfo(rec);
        if ((ret = out.GetError()))
            return ret;
        if ((ret = out.Close()))
            return ret;
        if ((ret = TBase::Close()))
            return ret;
        return 0;
    }

    const TVal* Next() {
        return TBase::TMyHeap::Next();
    }

    const TVal* Current() const {
        return TBase::TMyHeap::Current();
    }

    bool GetExtInfo(typename TExtInfoType<TVal>::TResult* extInfo) const {
        return TBase::TMyHeap::GetExtInfo(extInfo);
    }

    const ui8* GetExtInfoRaw(size_t* len) const {
        return TBase::TMyHeap::GetExtInfoRaw(len);
    }
};

template <class TVal, class TCompare, typename TCompress, typename TSieve,
          typename TOutPageFile = TOutputPageFile, typename TFileTypes = TDefInterFileTypes>
class TDatSorterImpl: public TDatSorterImplBase<TVal, TCompare, TCompress, TSieve, TOutPageFile, TFileTypes> {
    typedef TDatSorterImplBase<TVal, TCompare, TCompress, TSieve, TOutPageFile, TFileTypes> TBase;

public:
    TDatSorterImpl()
        : Cur(nullptr)
        , Prev(nullptr)
    {
    }

    int SortToFile(const char* name, size_t memory, int maxportions = 1000) {
        int ret = Sort(memory, maxportions);
        if (ret)
            return ret;
        typename TBase::TOut out;
        if ((ret = out.Open(name, TBase::Pagesize, TBase::HPages)))
            return ret;
        const TVal* rec;
        while ((rec = Next()))
            out.PushWithExtInfo(rec);
        if ((ret = out.GetError()))
            return ret;
        if ((ret = out.Close()))
            return ret;
        if ((ret = TBase::Close()))
            return ret;
        return 0;
    }

    int SortToStream(TAutoPtr<IOutputStream> output, size_t memory, int maxportions = 1000) {
        int ret = Sort(memory, maxportions);
        if (ret)
            return ret;
        typename TBase::TOut out;
        if ((ret = out.Open(output, TBase::Pagesize, TBase::HPages)))
            return ret;
        const TVal* rec;
        while ((rec = Next()))
            out.PushWithExtInfo(rec);
        if ((ret = out.GetError()))
            return ret;
        if ((ret = out.Close()))
            return ret;
        if ((ret = TBase::Close()))
            return ret;
        return 0;
    }

    int Open(const char* templ, size_t pagesize, size_t pages, int pagesOrBytes = 1) {
        int res = TBase::Open(templ, pagesize, pages, pagesOrBytes);
        Prev = nullptr;
        Cur = nullptr;
        return res;
    }

    int Sort(size_t memory, int maxportions = 1000, bool direct = false) {
        int res = TBase::Sort(memory, maxportions, direct);
        if (!res) {
            const TVal* rec = TBase::TMyHeap::Next();
            if (rec) {
                size_t els, es;
                size_t sz = NMicroBDB::SizeOfExt(rec, &els, &es);
                sz += els + es;
                if (!TExtInfoType<TVal>::Exists)
                    Cur = (TVal*)malloc(sizeof(TVal));
                else
                    Cur = (TVal*)malloc(TBase::Pagesize);
                memcpy(Cur, rec, sz);
            }
        }
        return res;
    }

    // Prev = last returned
    // Cur = current accumlating with TSieve

    const TVal* Next() {
        if (!Cur) {
            if (Prev) {
                free(Prev);
                Prev = nullptr;
            }
            return nullptr;
        }
        const TVal* rec;

        if (TIsSieveFake<TSieve>::Result)
            rec = TBase::TMyHeap::Next();
        else {
            do {
                rec = TBase::TMyHeap::Next();
            } while (rec && TSieve::Sieve((TVal*)Cur, rec));
        }

        if (!Prev) {
            if (!TExtInfoType<TVal>::Exists)
                Prev = (TVal*)malloc(sizeof(TVal));
            else
                Prev = (TVal*)malloc(TBase::Pagesize);
        }
        size_t els, es;
        size_t sz = NMicroBDB::SizeOfExt(Cur, &els, &es);
        sz += els + es;
        memcpy(Prev, Cur, sz);

        if (rec) {
            sz = NMicroBDB::SizeOfExt(rec, &els, &es);
            sz += els + es;
            memcpy(Cur, rec, sz);
        } else {
            TSieve::Sieve((TVal*)Cur, Cur);
            free(Cur);
            Cur = nullptr;
        }
        return Prev;
    }

    const TVal* Current() const {
        return Prev;
    }

    int Close() {
        int res = TBase::Close();
        if (Prev) {
            free(Prev);
            Prev = nullptr;
        }
        if (Cur) {
            free(Cur);
            Cur = nullptr;
        }
        return res;
    }

protected:
    TVal* Cur;
    TVal* Prev;
};