aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/searchsummary/docsummary/positionsdfw.cpp
blob: 9c82c00c3efe5f0df0a20b948d715aada9ba3462 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "positionsdfw.h"
#include "docsumstate.h"
#include <vespa/searchlib/attribute/iattributemanager.h>
#include <vespa/searchlib/common/location.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <cmath>
#include <climits>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.docsummary.positionsdfw");

namespace search::docsummary {

using search::attribute::IAttributeContext;
using search::attribute::IAttributeVector;
using search::attribute::BasicType;
using search::common::Location;

AbsDistanceDFW::AbsDistanceDFW(const vespalib::string & attrName) :
    AttrDFW(attrName)
{ }

uint64_t
AbsDistanceDFW::findMinDistance(uint32_t docid, GetDocsumsState *state)
{
    search::common::Location &location = *state->_parsedLocation;
    const IAttributeVector & attribute(vec(*state));

    uint64_t absdist = std::numeric_limits<int64_t>::max();
    int32_t docx = 0;
    int32_t docy = 0;
    std::vector<IAttributeVector::largeint_t> pos(16);
    uint32_t numValues = attribute.get(docid, &pos[0], pos.size());
    if (numValues > pos.size()) {
        pos.resize(numValues);
        numValues = attribute.get(docid, &pos[0], pos.size());
        assert(numValues <= pos.size());
    }
    for (uint32_t i = 0; i < numValues; i++) {
        int64_t docxy(pos[i]);
        vespalib::geo::ZCurve::decode(docxy, &docx, &docy);
        uint32_t dx;
        if (location.getX() > docx) {
            dx = location.getX() - docx;
        } else {
            dx = docx - location.getX();
        }
        if (location.getXAspect() != 0) {
            dx = ((uint64_t) dx * location.getXAspect()) >> 32;
        }
        uint32_t dy;
        if (location.getY() > docy) {
            dy = location.getY() - docy;
        } else {
            dy = docy - location.getY();
        }
        uint64_t dist2 = dx * (uint64_t) dx +
                         dy * (uint64_t) dy;
        if (dist2 < absdist) {
            absdist = dist2;
        }
    }
    return (uint64_t) std::sqrt((double) absdist);
}

void
AbsDistanceDFW::insertField(uint32_t docid, GetDocsumsState *state, ResType type, vespalib::slime::Inserter &target)
{
    bool forceEmpty = true;

    const vespalib::string &locationStr = state->_args.getLocation();
    if (locationStr.size() > 0) {
        if (!state->_parsedLocation) {
            state->_callback.ParseLocation(state);
        }
        assert(state->_parsedLocation);
        if (state->_parsedLocation->getParseError() == nullptr) {
            forceEmpty = false;
        }
    }
    if (forceEmpty) return;

    uint64_t absdist = findMinDistance(docid, state);

    if (type == RES_INT) {
        target.insertLong(absdist);
    } else {
        vespalib::string value = vespalib::stringify(absdist);
        vespalib::Memory data(value.c_str(), value.size());

        if (type == RES_STRING      ||
            type == RES_LONG_STRING ||
            type == RES_XMLSTRING)
        {
            target.insertString(data);
        }
        if (type == RES_LONG_DATA ||
            type == RES_DATA)
        {
            target.insertData(data);
        }
    }
}

//--------------------------------------------------------------------------

PositionsDFW::PositionsDFW(const vespalib::string & attrName) :
    AttrDFW(attrName)
{
}

namespace {
vespalib::asciistream
formatField(const attribute::IAttributeVector &attribute, uint32_t docid, ResType type) {
    vespalib::asciistream target;
    int32_t docx = 0;
    int32_t docy = 0;

    std::vector<IAttributeVector::largeint_t> pos(16);
    uint32_t numValues = attribute.get(docid, &pos[0], pos.size());
    if (numValues > pos.size()) {
        pos.resize(numValues);
        numValues = attribute.get(docid, &pos[0], pos.size());
        assert(numValues <= pos.size());
    }
    LOG(debug, "docid=%d, numValues=%d", docid, numValues);

    bool isShort = !IDocsumFieldWriter::IsBinaryCompatible(type, RES_LONG_STRING);
    for (uint32_t i = 0; i < numValues; i++) {
        int64_t docxy(pos[i]);
        vespalib::geo::ZCurve::decode(docxy, &docx, &docy);
        if (docx == 0 && docy == INT_MIN) {
            LOG(spam, "skipping empty zcurve value");
            continue;
        }
        double degrees_ns = docy;
        degrees_ns /= 1000000.0;
        double degrees_ew = docx;
        degrees_ew /= 1000000.0;

        target << "<position x=\"" << docx << "\" y=\"" << docy << "\"";
        target << " latlong=\"";
        target << vespalib::FloatSpec::fixed;
        if (degrees_ns < 0) {
            target << "S" << (-degrees_ns);
        } else {
            target << "N" << degrees_ns;
        }
        target << ";";
        if (degrees_ew < 0) {
            target << "W" << (-degrees_ew);
        } else {
            target << "E" << degrees_ew;
        }
        target << "\" />";
        if (isShort && target.size() > 30000) {
            target << "<overflow />";
            break;
        }
    }
    return target;
}
}

void
PositionsDFW::insertField(uint32_t docid, GetDocsumsState * dsState, ResType type, vespalib::slime::Inserter &target)
{
    vespalib::asciistream val(formatField(vec(*dsState), docid, type));
    target.insertString(vespalib::Memory(val.c_str(), val.size()));
}

//--------------------------------------------------------------------------

PositionsDFW::UP createPositionsDFW(const char *attribute_name, IAttributeManager *attribute_manager)
{
    PositionsDFW::UP ret;
    if (attribute_manager != nullptr) {
        if (!attribute_name) {
            LOG(debug, "createPositionsDFW: missing attribute name '%p'", attribute_name);
            return ret;
        }
        IAttributeContext::UP context = attribute_manager->createContext();
        if (!context.get()) {
            LOG(debug, "createPositionsDFW: could not create context from attribute manager");
            return ret;
        }
        const IAttributeVector *attribute = context->getAttribute(attribute_name);
        if (!attribute) {
            LOG(debug, "createPositionsDFW: could not get attribute '%s' from context", attribute_name);
            return ret;
        }
    }
    return std::make_unique<PositionsDFW>(attribute_name);
}

AbsDistanceDFW::UP createAbsDistanceDFW(const char *attribute_name, IAttributeManager *attribute_manager)
{
    AbsDistanceDFW::UP ret;
    if (attribute_manager != nullptr) {
        if (!attribute_name) {
            LOG(debug, "createAbsDistanceDFW: missing attribute name '%p'", attribute_name);
            return ret;
        }
        IAttributeContext::UP context = attribute_manager->createContext();
        if (!context.get()) {
            LOG(debug, "createAbsDistanceDFW: could not create context from attribute manager");
            return ret;
        }
        const IAttributeVector *attribute = context->getAttribute(attribute_name);
        if (!attribute) {
            LOG(debug, "createAbsDistanceDFW: could not get attribute '%s' from context", attribute_name);
            return ret;
        }
    }
    return std::make_unique<AbsDistanceDFW>(attribute_name);
}

}