summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/resultset.cpp
blob: 69f3b13d7091c0f6c2329337c81f0ae1cd92090d (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Copyright (C) 1998-2003 Fast Search & Transfer ASA
// Copyright (C) 2003 Overture Services Norway AS

#include "resultset.h"
#include "bitvector.h"
#include <cstring>

using vespalib::alloc::Alloc;

namespace search {

//Above 32M we hand back to the OS directly.
constexpr size_t MMAP_LIMIT = 0x2000000;

ResultSet::ResultSet()
    : _elemsUsedInRankedHitsArray(0u),
      _rankedHitsArrayAllocElements(0u),
      _bitOverflow(),
      _rankedHitsArray()
{
}


ResultSet::ResultSet(const ResultSet &other)
    : _elemsUsedInRankedHitsArray(0),
      _rankedHitsArrayAllocElements(0),
      _bitOverflow(),
      _rankedHitsArray()
{
    allocArray(other._elemsUsedInRankedHitsArray);
    _elemsUsedInRankedHitsArray = other._elemsUsedInRankedHitsArray;
    if (_elemsUsedInRankedHitsArray > 0)
        memcpy(_rankedHitsArray.get(),
               other._rankedHitsArray.get(),
               _elemsUsedInRankedHitsArray * sizeof(RankedHit));

    if (other._bitOverflow) {
        _bitOverflow = BitVector::create(*other._bitOverflow);
    }
}


ResultSet::~ResultSet()
{
}


void
ResultSet::allocArray(unsigned int arrayAllocated)
{
    if (arrayAllocated > 0) {
        Alloc::alloc(arrayAllocated * sizeof(RankedHit), MMAP_LIMIT).swap(_rankedHitsArray);
    } else {
        Alloc().swap(_rankedHitsArray);
    }
    _rankedHitsArrayAllocElements = arrayAllocated;
    _elemsUsedInRankedHitsArray = 0;
}


void
ResultSet::setArrayUsed(unsigned int arrayUsed)
{
    assert(arrayUsed <= _rankedHitsArrayAllocElements);
    _elemsUsedInRankedHitsArray = arrayUsed;
}


void
ResultSet::setBitOverflow(BitVector::UP newBitOverflow)
{
    _bitOverflow = std::move(newBitOverflow);
}


//////////////////////////////////////////////////////////////////////
// Find number of hits
//////////////////////////////////////////////////////////////////////
unsigned int
ResultSet::getNumHits() const
{
    return (_bitOverflow) ? _bitOverflow->countTrueBits() : _elemsUsedInRankedHitsArray;
}


void
ResultSet::mergeWithBitOverflow()
{
    if ( ! _bitOverflow) {
        return;
    }

    const BitVector *bitVector = _bitOverflow.get();

    const RankedHit *oldA     = getArray();
    const RankedHit *oldAEnd  = oldA + _elemsUsedInRankedHitsArray;
    uint32_t        bidx     = bitVector->getFirstTrueBit();

    uint32_t  actualHits = getNumHits();
    Alloc newHitsAlloc = Alloc::alloc(actualHits*sizeof(RankedHit), MMAP_LIMIT);
    RankedHit *newHitsArray = static_cast<RankedHit *>(newHitsAlloc.get());

    RankedHit * tgtA    = newHitsArray;
    RankedHit * tgtAEnd = newHitsArray + actualHits;

    if (oldAEnd > oldA) { // we have array hits
        uint32_t firstArrayHit = oldA->_docId;
        uint32_t lastArrayHit  = (oldAEnd - 1)->_docId;

        // bitvector hits before array hits
        while (bidx < firstArrayHit) {
            tgtA->_docId = bidx;
            tgtA->_rankValue = 0;
            tgtA++;
            bidx = bitVector->getNextTrueBit(bidx + 1);
        }

        // merge bitvector and array hits
        while (bidx <= lastArrayHit) {
            tgtA->_docId = bidx;
            if (bidx == oldA->_docId) {
                tgtA->_rankValue = oldA->_rankValue;
                oldA++;
            } else {
                tgtA->_rankValue = 0;
            }
            tgtA++;
            bidx = bitVector->getNextTrueBit(bidx + 1);
        }
    }
    assert(oldA == oldAEnd);

    // bitvector hits after array hits
    while (tgtA < tgtAEnd) {
        tgtA->_docId = bidx;
        tgtA->_rankValue = 0;
        tgtA++;
        bidx = bitVector->getNextTrueBit(bidx + 1);
    }
    _rankedHitsArrayAllocElements =  actualHits;
    _elemsUsedInRankedHitsArray =  actualHits;
    _rankedHitsArray.swap(newHitsAlloc);
    setBitOverflow(NULL);
}

} // namespace search