aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/test/fakedata/fakeword.cpp
blob: b9c2b3cb5bb40ca33fc979582b54a188e76314e0 (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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "fakeword.h"

#include <vespa/searchlib/index/postinglistfile.h>
#include <vespa/searchlib/index/postinglistcountfile.h>
#include <vespa/searchlib/index/docidandfeatures.h>
#include <vespa/searchlib/bitcompression/compression.h>
#include <vespa/searchlib/bitcompression/posocccompression.h>
#include <vespa/searchlib/bitcompression/posocc_fields_params.h>
#include <vespa/vespalib/util/size_literals.h>

using search::fef::TermFieldMatchData;
using search::fef::TermFieldMatchDataPosition;
using search::index::WordDocElementFeatures;
using search::index::WordDocElementWordPosFeatures;
using search::index::PostingListFileSeqWrite;
using search::index::DocIdAndFeatures;
using search::index::DocIdAndPosOccFeatures;
using search::index::PostingListCounts;
using search::index::PostingListFileSeqRead;
using search::diskindex::FieldReader;
using search::diskindex::FieldWriter;

namespace search
{

namespace fakedata
{


static void
fillbitset(search::BitVector *bitvector,
           unsigned int size,
           vespalib::Rand48 &rnd)
{
    unsigned int range;
    unsigned int idx;
    unsigned int j;

    range = bitvector->size();
    assert(range > 0);
    --range;
    bitvector->invalidateCachedCount();

    assert(size <= range);
    if (size > range / 2) {
        if (range > 0)
            bitvector->setInterval(1, range);

        for (j = range; j > size; --j) {
            do {
                idx = (rnd.lrand48() % range) + 1u;
            } while (!bitvector->testBit(idx));
            bitvector->clearBit(idx);
        }
    } else {
        // bitvector->reset();
        bitvector->invalidateCachedCount();
        for (j = bitvector->countTrueBits(); j < size; j++) {
            do {
                idx = (rnd.lrand48() % range) + 1u;
            } while (bitvector->testBit(idx));
            bitvector->setBit(idx);
        }
    }
}


static void
fillcorrelatedbitset(search::BitVector &bitvector,
                     unsigned int size,
                     const FakeWord &otherword,
                     vespalib::Rand48 &rnd)
{
    const FakeWord::DocWordFeatureList &opostings = otherword._postings;

    unsigned int range = opostings.size();
    search::BitVector::UP corrmap(search::BitVector::create(range + 1));

    if (size > range)
        size = range;
    fillbitset(corrmap.get(), size, rnd);

    unsigned int idx = corrmap->getNextTrueBit(1u);
    while (idx < range) {
        unsigned int docId = opostings[idx - 1]._docId;
        bitvector.setBit(docId);
        ++idx;
        if (idx > range)
            break;
        idx = corrmap->getNextTrueBit(idx);
    }
}


FakeWord::DocWordPosFeature::DocWordPosFeature()
    : _elementId(0),
      _wordPos(0),
      _elementWeight(1),
      _elementLen(0)
{
}


FakeWord::DocWordPosFeature::~DocWordPosFeature()
{
}


FakeWord::DocWordCollapsedFeature::DocWordCollapsedFeature()
    : _field_len(0),
      _num_occs(0)
{
}

FakeWord::DocWordCollapsedFeature::~DocWordCollapsedFeature() = default;

FakeWord::DocWordFeature::DocWordFeature()
    : _docId(0),
      _collapsedDocWordFeatures(),
      _positions(0),
      _accPositions(0)
{
}

FakeWord::DocWordFeature::~DocWordFeature()
{
}

FakeWord::FakeWord(uint32_t docIdLimit,
                   const std::vector<uint32_t> & docIds,
                   const std::string &name,
                   const PosOccFieldsParams &fieldsParams,
                   uint32_t packedIndex)
    : _postings(),
      _wordPosFeatures(),
      _extraPostings(),
      _extraWordPosFeatures(),
      _docIdLimit(docIdLimit),
      _name(name),
      _fieldsParams(fieldsParams),
      _packedIndex(packedIndex)
{
    search::BitVector::UP bitmap(search::BitVector::create(docIdLimit));
    for (uint32_t docId : docIds) {
        bitmap->setBit(docId);
    }
    vespalib::Rand48 rnd;
    fakeup(*bitmap, rnd, _postings, _wordPosFeatures);
}

FakeWord::FakeWord(uint32_t docIdLimit,
                   uint32_t wordDocs,
                   uint32_t tempWordDocs,
                   const std::string &name,
                   vespalib::Rand48 &rnd,
                   const PosOccFieldsParams &fieldsParams,
                   uint32_t packedIndex)
    : _postings(),
      _wordPosFeatures(),
      _extraPostings(),
      _extraWordPosFeatures(),
      _docIdLimit(docIdLimit),
      _name(name),
      _fieldsParams(fieldsParams),
      _packedIndex(packedIndex)
{
    search::BitVector::UP bitmap(search::BitVector::create(docIdLimit));

    fillbitset(bitmap.get(), wordDocs, rnd);

    fakeup(*bitmap, rnd, _postings, _wordPosFeatures);
    fakeupTemps(rnd, docIdLimit, tempWordDocs);
    setupRandomizer(rnd);
}


FakeWord::FakeWord(uint32_t docIdLimit,
                   uint32_t wordDocs,
                   uint32_t tempWordDocs,
                   const std::string &name,
                   const FakeWord &otherWord,
                   size_t overlapDocs,
                   vespalib::Rand48 &rnd,
                   const PosOccFieldsParams &fieldsParams,
                   uint32_t packedIndex)
    : _postings(),
      _wordPosFeatures(),
      _docIdLimit(docIdLimit),
      _name(name),
      _fieldsParams(fieldsParams),
      _packedIndex(packedIndex)
{
    search::BitVector::UP bitmap(search::BitVector::create(docIdLimit));

    if (wordDocs * 2 < docIdLimit &&
        overlapDocs > 0)
        fillcorrelatedbitset(*bitmap, overlapDocs, otherWord, rnd);
    fillbitset(bitmap.get(), wordDocs, rnd);

    fakeup(*bitmap, rnd, _postings, _wordPosFeatures);
    fakeupTemps(rnd, docIdLimit, tempWordDocs);
    setupRandomizer(rnd);
}


FakeWord::~FakeWord()
{
}


void
FakeWord::fakeup(search::BitVector &bitmap,
                 vespalib::Rand48 &rnd,
                 DocWordFeatureList &postings,
                 DocWordPosFeatureList &wordPosFeatures)
{
    DocWordPosFeatureList wpf;
    unsigned int idx;
    uint32_t numFields = _fieldsParams.getNumFields();
    assert(numFields == 1u);
    (void) numFields;
    uint32_t docIdLimit = bitmap.size();
    idx = bitmap.getNextTrueBit(1u);
    while (idx < docIdLimit) {
        DocWordFeature dwf;
        unsigned int positions;

        dwf._docId = idx;
        positions = ((rnd.lrand48() % 10) == 0) ? 2 : 1;
        dwf._positions = positions;
        wpf.clear();
        for (unsigned int j = 0; j < positions; ++j) {
            DocWordPosFeature dwpf;
            dwpf._wordPos = rnd.lrand48() % 8_Ki;
            dwpf._elementId = 0;
            if (_fieldsParams.getFieldParams()[0]._hasElements) {
                dwpf._elementId = rnd.lrand48() % 4;
            }
            wpf.push_back(dwpf);
        }
        if (positions > 1) {
            /* Sort wordpos list and "avoid" duplicate positions */
            std::sort(wpf.begin(), wpf.end());
        }
        uint32_t field_len = 0;
        do {
            auto ie = wpf.end();
            auto i = wpf.begin();
            while (i != ie) {
                uint32_t lastwordpos = i->_wordPos;
                auto pi = i;
                ++i;
                while (i != ie &&
                       pi->_elementId == i->_elementId) {
                    if (i->_wordPos <= lastwordpos)
                        i->_wordPos = lastwordpos + 1;
                    lastwordpos = i->_wordPos;
                    ++i;
                }
                uint32_t elementLen = (rnd.lrand48() % 8_Ki) + 1 + lastwordpos;
                int32_t elementWeight = 1;
                if (_fieldsParams.getFieldParams()[0].
                    _hasElementWeights) {
                    uint32_t uWeight = rnd.lrand48() % 2001;
                    if ((uWeight & 1) != 0)
                        elementWeight = - (uWeight >> 1) - 1;
                    else
                        elementWeight = (uWeight >> 1);
                    assert(elementWeight <= 1000);
                    assert(elementWeight >= -1000);
                }
                while (pi != i) {
                    pi->_elementLen = elementLen;
                    pi->_elementWeight = elementWeight;
                    ++pi;
                }
                field_len += elementLen;
            }
            if (_fieldsParams.getFieldParams()[0]._hasElements) {
                field_len += ((rnd.lrand48() % 10) + 10);
            }
        } while (0);
        dwf._collapsedDocWordFeatures._field_len = field_len;
        dwf._collapsedDocWordFeatures._num_occs = dwf._positions;
        dwf._accPositions = wordPosFeatures.size();
        assert(dwf._positions == wpf.size());
        postings.push_back(dwf);
        for (const auto& elem : wpf) {
            wordPosFeatures.push_back(elem);
        }
        ++idx;
        if (idx >= docIdLimit)
            break;
        idx = bitmap.getNextTrueBit(idx);
    }
}


void
FakeWord::fakeupTemps(vespalib::Rand48 &rnd,
                      uint32_t docIdLimit,
                      uint32_t tempWordDocs)
{
    uint32_t maxTempWordDocs = docIdLimit / 2;
    tempWordDocs = std::min(tempWordDocs, maxTempWordDocs);
    if (tempWordDocs > 0) {
        search::BitVector::UP bitmap(search::BitVector::create(docIdLimit));
        fillbitset(bitmap.get(), tempWordDocs, rnd);
        fakeup(*bitmap, rnd, _extraPostings, _extraWordPosFeatures);
    }
}

void
FakeWord::setupRandomizer(vespalib::Rand48 &rnd)
{
    Randomizer randomAdd;
    Randomizer randomRem;

    auto d = _postings.begin();
    auto de = _postings.end();
    int32_t ref = 0;

    while (d != de) {
        do {
            randomAdd._random = rnd.lrand48();
        } while (randomAdd._random < 10000);
        randomAdd._ref = ref;
        assert(!randomAdd.isExtra());
        assert(!randomAdd.isRemove());
        _randomizer.push_back(randomAdd);
        ++d;
        ++ref;
    }

    auto ed = _extraPostings.begin();
    auto ede = _extraPostings.end();

    int32_t eref = -1;
    uint32_t tref = 0;
    ref = 0;
    int32_t refmax = _randomizer.size();
    while (ed != ede) {
        while (ref < refmax && _postings[ref]._docId < ed->_docId)
            ++ref;
        if (ref < refmax && _postings[ref]._docId == ed->_docId) {
            randomAdd._random = rnd.lrand48() % (_randomizer[ref]._random - 1);
            randomRem._random = _randomizer[ref]._random - 1;
        } else {
            do {
                randomAdd._random = rnd.lrand48();
                randomRem._random = rnd.lrand48();
            } while (randomAdd._random >= randomRem._random);
        }
        randomAdd._ref = eref;
        randomRem._ref = eref - 1;
        assert(randomAdd.isExtra());
        assert(!randomAdd.isRemove());
        assert(randomAdd.extraIdx() == tref);
        assert(randomRem.isExtra());
        assert(randomRem.isRemove());
        assert(randomRem.extraIdx() == tref);
        _randomizer.push_back(randomAdd);
        _randomizer.push_back(randomRem);
        ++ed;
        eref -= 2;
        ++tref;
    }
    std::sort(_randomizer.begin(), _randomizer.end());
}


void
FakeWord::addDocIdBias(uint32_t docIdBias)
{
    auto d = _postings.begin();
    auto de = _postings.end();
    for (; d != de; ++d) {
        d->_docId += docIdBias;
    }
    d = _extraPostings.begin();
    de = _extraPostings.end();
    for (; d != de; ++d) {
        d->_docId += docIdBias;
    }
    _docIdLimit += docIdBias;
}


bool
FakeWord::validate(search::queryeval::SearchIterator *iterator,
                   const fef::TermFieldMatchDataArray &matchData,
                   uint32_t stride,
                   bool unpack_normal_features,
                   bool unpack_interleaved_features,
                   bool verbose) const
{
    iterator->initFullRange();
    uint32_t docId = 0;

    using TMDPI = TermFieldMatchData::PositionsIterator;

    auto d = _postings.begin();
    auto de = _postings.end();
    auto p = _wordPosFeatures.begin();
    auto pe = _wordPosFeatures.end();

    if (verbose)
        printf("Start validate word '%s'\n", _name.c_str());
    int strideResidue = stride;
    while (d != de) {
        if (strideResidue > 1) {
            --strideResidue;
            unsigned int positions = d->_positions;
            while (positions > 0) {
                ++p;
                --positions;
            }
        } else {
            strideResidue = stride;
            docId = d->_docId;
            bool seekRes = iterator->seek(docId);
            assert(seekRes);
            (void) seekRes;
            assert(d != de);
            unsigned int positions = d->_positions;
            iterator->unpack(docId);
            for (size_t lfi = 0; lfi < matchData.size(); ++lfi) {
                if (matchData[lfi]->getDocId() != docId)
                    continue;
                if (unpack_interleaved_features) {
                    assert(d->_collapsedDocWordFeatures._field_len == matchData[lfi]->getFieldLength());
                    assert(d->_collapsedDocWordFeatures._num_occs == matchData[lfi]->getNumOccs());
                } else {
                    assert(matchData[lfi]->getFieldLength() == 0u);
                    assert(matchData[lfi]->getNumOccs() == 0u);
                }
                if (unpack_normal_features) {
                    TMDPI mdpe = matchData[lfi]->end();
                    TMDPI mdp = matchData[lfi]->begin();
                    while (mdp != mdpe) {
                        assert(p != pe);
                        assert(positions > 0);
                        assert(p->_wordPos == mdp->getPosition());
                        assert(p->_elementId == mdp->getElementId());
                        assert(p->_elementWeight == mdp->getElementWeight());
                        assert(p->_elementLen == mdp->getElementLen());
                        ++p;
                        ++mdp;
                        --positions;
                    }
                } else {
                    assert(matchData[lfi]->size() == 0u);
                }
            }
            assert(positions == 0 || !unpack_normal_features);
        }
        ++d;
    }
    assert(p == pe || !unpack_normal_features);
    assert(d == de);
    if (verbose)
        printf("word '%s' validated successfully with unpack\n",
               _name.c_str());
    return true;
}


bool
FakeWord::validate(search::queryeval::SearchIterator *iterator,
                   const fef::TermFieldMatchDataArray &matchData,
                   bool unpack_normal_features,
                   bool unpack_interleaved_features,
                   bool verbose) const
{
    iterator->initFullRange();
    uint32_t docId = 1;

    using TMDPI = TermFieldMatchData::PositionsIterator;

    auto d = _postings.begin();
    auto de = _postings.end();
    auto p = _wordPosFeatures.begin();
    auto pe = _wordPosFeatures.end();

    if (verbose)
        printf("Start validate word '%s'\n", _name.c_str());
    for (;;) {
        if (iterator->seek(docId)) {
            assert(d != de);
            assert(d->_docId == docId);
            iterator->unpack(docId);
            unsigned int positions = d->_positions;
            for (size_t lfi = 0; lfi < matchData.size(); ++lfi) {
                if (matchData[lfi]->getDocId() != docId)
                    continue;
                if (unpack_interleaved_features) {
                    assert(d->_collapsedDocWordFeatures._field_len == matchData[lfi]->getFieldLength());
                    assert(d->_collapsedDocWordFeatures._num_occs == matchData[lfi]->getNumOccs());
                } else {
                    assert(matchData[lfi]->getFieldLength() == 0u);
                    assert(matchData[lfi]->getNumOccs() == 0u);
                }
                if (unpack_normal_features) {
                    TMDPI mdpe = matchData[lfi]->end();
                    TMDPI mdp = matchData[lfi]->begin();
                    while (mdp != mdpe) {
                        assert(p != pe);
                        assert(positions > 0);
                        assert(p->_wordPos == mdp->getPosition());
                        assert(p->_elementId == mdp->getElementId());
                        assert(p->_elementWeight == mdp->getElementWeight());
                        assert(p->_elementLen == mdp->getElementLen());
                        ++p;
                        ++mdp;
                        --positions;
                    }
                } else {
                    assert(matchData[lfi]->size() == 0u);
                }
            }
            assert(positions == 0 || !unpack_normal_features);
            ++d;
            ++docId;
        } else {
            if (iterator->getDocId() > docId)
                docId = iterator->getDocId();
            else
                ++docId;
        }
        if (docId >= _docIdLimit)
            break;
    }
    assert(p == pe || !unpack_normal_features);
    assert(d == de);
    if (verbose)
        printf("word '%s' validated successfully with unpack\n",
               _name.c_str());
    return true;
}


bool
FakeWord::validate(search::queryeval::SearchIterator *iterator, bool verbose) const
{
    iterator->initFullRange();
    uint32_t docId = 1;

    auto d = _postings.begin();
    auto de = _postings.end();

    if (verbose)
        printf("Start validate word '%s'\n", _name.c_str());
    for (;;) {
        if (iterator->seek(docId)) {
            assert(d != de);
            assert(d->_docId == docId);
            ++d;
            ++docId;
        } else {
            if (iterator->getDocId() > docId)
                docId = iterator->getDocId();
            else
                ++docId;
        }
        if (docId >= _docIdLimit)
            break;
    }
    assert(d == de);
    if (verbose)
        printf("word '%s' validated successfully without unpack\n",
               _name.c_str());
    return true;
}


bool
FakeWord::validate(FieldReader &fieldReader,
                   uint32_t wordNum,
                   const fef::TermFieldMatchDataArray &matchData,
                   bool decode_interleaved_features,
                   bool verbose) const
{
    uint32_t docId = 0;
    uint32_t numDocs;
    uint32_t residue;
    uint32_t presidue;
    bool unpres;

    using TMDPI = TermFieldMatchData::PositionsIterator;

    auto d = _postings.begin();
    auto de = _postings.end();
    auto p = _wordPosFeatures.begin();
    auto pe = _wordPosFeatures.end();

    if (verbose)
        printf("Start validate word '%s'\n", _name.c_str());
#ifdef notyet
    // Validate word number
#else
    (void) wordNum;
#endif
    numDocs = _postings.size();
    for (residue = numDocs; residue > 0; --residue) {
        assert(fieldReader._wordNum == wordNum);
        DocIdAndFeatures &features(fieldReader._docIdAndFeatures);
        docId = features.doc_id();
        assert(d != de);
        assert(d->_docId == docId);
        if (decode_interleaved_features) {
            assert(d->_collapsedDocWordFeatures._field_len == features.field_length());
            assert(d->_collapsedDocWordFeatures._num_occs == features.num_occs());
        }
        if (matchData.valid()) {
#ifdef notyet
            unpres = features.unpack(matchData);
            assert(unpres);
#else
            (void) unpres;

            auto element = features.elements().begin();
            auto position = features.word_positions().begin();

            TermFieldMatchData *tfmd = matchData[0];
            assert(tfmd != 0);
            tfmd->reset(features.doc_id());

            uint32_t elementResidue = features.elements().size();
            while (elementResidue != 0) {
                uint32_t positionResidue = element->getNumOccs();
                while (positionResidue != 0) {
                    uint32_t wordPos = position->getWordPos();
                    TermFieldMatchDataPosition pos(element->getElementId(),
                                                   wordPos,
                                                   element->getWeight(),
                                                   element->getElementLen());
                    tfmd->appendPosition(pos);
                    ++position;
                    --positionResidue;
                }
                ++element;
                --elementResidue;
            }
#endif
            unsigned int positions = d->_positions;
            presidue = positions;
            for (size_t lfi = 0; lfi < matchData.size(); ++lfi) {
                if (matchData[lfi]->getDocId() != docId)
                    continue;
                TMDPI mdpe = matchData[lfi]->end();
                TMDPI mdp = matchData[lfi]->begin();
                while (mdp != mdpe) {
                    assert(p != pe);
                    assert(presidue > 0);
                    assert(p->_wordPos == mdp->getPosition());
                    assert(p->_elementId == mdp->getElementId());
                    assert(p->_elementWeight == mdp->getElementWeight());
                    assert(p->_elementLen == mdp->getElementLen());
                    ++p;
                    ++mdp;
                    --presidue;
                }
            }
            assert(presidue == 0);
            ++d;
        }
        fieldReader.read();
    }
    if (matchData.valid()) {
        assert(p == pe);
        assert(d == de);
    }
    if (verbose)
        printf("word '%s' validated successfully %s unpack\n",
               _name.c_str(),
               matchData.valid() ? "with" : "without");
    return true;
}


void
FakeWord::validate(const std::vector<uint32_t> &docIds) const
{
    auto d = _postings.begin();
    auto de = _postings.end();
    auto di = docIds.begin();
    auto die = docIds.end();

    while (d != de) {
        assert(di != die);
        assert(d->_docId == *di);
        ++d;
        ++di;
    }
    assert(di == die);
}


void
FakeWord::validate(const search::BitVector &bv) const
{
    auto d = _postings.begin();
    auto de = _postings.end();
    uint32_t bitHits = bv.countTrueBits();
    assert(bitHits == _postings.size());
    (void) bitHits;
    uint32_t bi = bv.getNextTrueBit(1u);
    while (d != de) {
        assert(d->_docId == bi);
        ++d;
        bi = bv.getNextTrueBit(bi + 1);
    }
    assert(bi >= bv.size());
}


bool
FakeWord::dump(FieldWriter &fieldWriter,
               bool verbose) const
{
    uint32_t numDocs;
    uint32_t residue;
    DocIdAndPosOccFeatures features;

    auto d = _postings.begin();
    auto de = _postings.end();
    auto p = _wordPosFeatures.begin();
    auto pe = _wordPosFeatures.end();

    if (verbose)
        printf("Start dumping word '%s'\n", _name.c_str());
    numDocs = _postings.size();
    for (residue = numDocs; residue > 0; --residue) {
        assert(d != de);
        setupFeatures(*d, &*p, features);
        p += d->_positions;
        fieldWriter.add(features);
        ++d;
    }
    assert(p == pe);
    assert(d == de);
    if (verbose)
        printf("word '%s' dumped successfully\n",
               _name.c_str());
    return true;
}


FakeWord::RandomizedReader::RandomizedReader()
    : _r(),
      _fw(NULL),
      _wordIdx(0u),
      _valid(false),
      _ri(),
      _re()
{
}


void
FakeWord::RandomizedReader::read()
{
    if (_ri != _re) {
        _r = *_ri;
        ++_ri;
    } else
        _valid = false;
}


void
FakeWord::RandomizedReader::setup(const FakeWord *fw,
                                  uint32_t wordIdx)
{
    _fw = fw;
    _wordIdx = wordIdx;
    _ri = fw->_randomizer.begin();
    _re = fw->_randomizer.end();
    _valid = _ri != _re;
}


FakeWord::RandomizedWriter::~RandomizedWriter()
{
}


} // namespace fakedata

} // namespace search