aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/locationiterators.cpp
blob: 3a350da9fec5273b552bca896dcd74ea3ce77f30 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "locationiterators.h"
#include <vespa/searchlib/bitcompression/compression.h>
#include <vespa/searchlib/attribute/attributevector.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.common.locationiterators");

namespace search::common {

class LocationIterator : public search::queryeval::SearchIterator
{
private:
    static constexpr double pi = 3.14159265358979323846;
    // microdegrees -> degrees -> radians -> km (using Earth mean radius)
    static constexpr double udeg_to_km = 1.0e-6 * (pi / 180.0) * 6371.0088;
    search::fef::TermFieldMatchData & _tfmd;
    const unsigned int _numDocs;
    const bool         _strict;
    const Location &   _location;
    uint32_t           _num_values;
    std::vector<search::AttributeVector::largeint_t> _pos;

    void doSeek(uint32_t docId) override;
    void doUnpack(uint32_t docId) override;
public:
    LocationIterator(search::fef::TermFieldMatchData &tfmd,
                     unsigned int numDocs,
                     bool strict,
                     const Location & location);
    ~LocationIterator() override;
};

LocationIterator::LocationIterator(search::fef::TermFieldMatchData &tfmd,
                                   unsigned int numDocs,
                                   bool strict,
                                   const Location & location)
  : SearchIterator(),
    _tfmd(tfmd),
    _numDocs(numDocs),
    _strict(strict),
    _location(location),
    _num_values(0),
    _pos()
{
    _pos.resize(1);  //Need at least 1 entry as the singlevalue attributes does not honour given size.
    LOG(debug, "created LocationIterator(numDocs=%u)\n", numDocs);
};


LocationIterator::~LocationIterator() = default;

void
LocationIterator::doSeek(uint32_t docId)
{
    while (__builtin_expect(docId < getEndId(), true)) {
        if (__builtin_expect(docId >= _numDocs, false)) {
            break;
        }
        _num_values = _location.getVec()->get(docId, &_pos[0], _pos.size());
        while (_num_values > _pos.size()) {
            _pos.resize(_num_values);
            _num_values = _location.getVec()->get(docId, &_pos[0], _pos.size());
        }
        for (uint32_t i = 0; i < _num_values; i++) {
            int64_t docxy(_pos[i]);
            if (_location.inside_limit(docxy)) {
                setDocId(docId);
                return;
            }
        }
        if (!_strict) {
            return;
        }
        ++docId;
    }
    setAtEnd();
}

void
LocationIterator::doUnpack(uint32_t docId)
{
    uint64_t sqabsdist = std::numeric_limits<uint64_t>::max();
    int32_t docx = 0;
    int32_t docy = 0;
    // use _num_values from _pos fetched in doSeek()
    for (uint32_t i = 0; i < _num_values; i++) {
        int64_t docxy(_pos[i]);
        vespalib::geo::ZCurve::decode(docxy, &docx, &docy);
        uint64_t sqdist = _location.sq_distance_to({docx, docy});
        if (sqdist < sqabsdist) {
            sqabsdist = sqdist;
        }
    }
    double dist = std::sqrt(double(sqabsdist));
    double score = 1.0 / (1.0 + (udeg_to_km * dist));
    LOG(debug, "unpack LI(%u) score %f\n", docId, score);
    LOG(debug, "distance: %f micro-degrees ~= %f km", dist, udeg_to_km * dist);
    _tfmd.setRawScore(docId, score);
}

std::unique_ptr<search::queryeval::SearchIterator>
create_location_iterator(search::fef::TermFieldMatchData &tfmd, unsigned int numDocs,
                         bool strict, const Location & location)
{
    return std::make_unique<LocationIterator>(tfmd, numDocs, strict, location);
}

} // namespace

using namespace search::common;

class FastS_2DZLocationIterator : public search::queryeval::SearchIterator
{
private:
    const unsigned int _numDocs;
    const bool         _strict;
    const Location &   _location;
    std::vector<search::AttributeVector::largeint_t> _pos;

    void doSeek(uint32_t docId) override;
    void doUnpack(uint32_t docId) override;
public:
    FastS_2DZLocationIterator(unsigned int numDocs, bool strict, const Location & location);

    ~FastS_2DZLocationIterator() override;
};


FastS_2DZLocationIterator::
FastS_2DZLocationIterator(unsigned int numDocs,
                          bool strict,
                          const Location & location)
    : SearchIterator(),
      _numDocs(numDocs),
      _strict(strict),
      _location(location),
      _pos()
{
    _pos.resize(1);  //Need at least 1 entry as the singlevalue attributes does not honour given size.
};


FastS_2DZLocationIterator::~FastS_2DZLocationIterator() = default;


void
FastS_2DZLocationIterator::doSeek(uint32_t docId)
{
    LOG(debug, "FastS_2DZLocationIterator: seek(%u) with numDocs=%u endId=%u",
        docId, _numDocs, getEndId());
    if (__builtin_expect(docId >= _numDocs, false)) {
        setAtEnd();
        return;
    }

    const Location &location = _location;
    std::vector<search::AttributeVector::largeint_t> &pos = _pos;

    for (;;) {
        uint32_t numValues =
            location.getVec()->get(docId, &pos[0], pos.size());
        if (numValues > pos.size()) {
            pos.resize(numValues);
            numValues = location.getVec()->get(docId, &pos[0], pos.size());
        }
        for (uint32_t i = 0; i < numValues; i++) {
            int64_t docxy(pos[i]);
            if (location.inside_limit(docxy)) {
                setDocId(docId);
                return;
            }
        }

        if (__builtin_expect(docId + 1 >= _numDocs, false)) {
            setAtEnd();
            return;
        }

        if (!_strict) {
            return;
        }
        docId++;
    }
}


void
FastS_2DZLocationIterator::doUnpack(uint32_t docId)
{
    (void) docId;
}


std::unique_ptr<search::queryeval::SearchIterator>
FastS_AllocLocationIterator(unsigned int numDocs, bool strict, const Location & location)
{
    return std::make_unique<FastS_2DZLocationIterator>(numDocs, strict, location);
}