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

#include "geo_location_parser.h"
#include <limits>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/data/slime/json_format.h>

#include <vespa/vespalib/util/issue.h>
using vespalib::Issue;

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

namespace {

int getInt(const char * &p) {
    uint32_t val;
    bool isminus;
    val = 0;
    isminus = false;
    if (*p == '-') {
        isminus = true;
        p++;
    }
    while (*p >= '0' && *p <= '9') {
        val *= 10;
        val += (*p++ - '0');
    }
    return isminus ? - val : val;
}

} // namespace <unnamed>

namespace search::common {

GeoLocationParser::GeoLocationParser()
    : _valid(false),
      _has_point(false),
      _has_bounding_box(false),
      _field_name(),
      _x(0),
      _y(0),
      _x_aspect(0u),
      _radius(std::numeric_limits<uint32_t>::max()),
      _min_x(std::numeric_limits<int32_t>::min()),
      _max_x(std::numeric_limits<int32_t>::max()),
      _min_y(std::numeric_limits<int32_t>::min()),
      _max_y(std::numeric_limits<int32_t>::max()),
      _parseError(nullptr)
{}

bool
GeoLocationParser::correctDimensionalitySkip(const char * &p) {
    if (*p == '2') {
        p++;
        if (*p != ',') {
            _parseError = "Missing comma after 2D dimensionality";
            return false;
        }
        p++;
        return true;
    }
    _parseError = "Bad dimensionality spec, not 2D";
    return false;
}

bool
GeoLocationParser::parseOldFormat(const std::string &locStr)
{
    bool foundBoundingBox = false;
    bool foundLoc = false;
    const char *p = locStr.c_str();
    while (*p != '\0') {
        if (*p == '[') {
            p++;
            if (foundBoundingBox) {
                _parseError = "Duplicate bounding box";
                return false;
            }
            foundBoundingBox = true;
            if (!correctDimensionalitySkip(p)) {
                return false;
            }
            _min_x = getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after minx";
                return false;
            }
            p++;
            _min_y = getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after miny";
                return false;
            }
            p++;
            _max_x = getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after maxx";
                return false;
            }
            p++;
            _max_y = getInt(p);
            if (*p != ']') {
                _parseError = "Missing ']' after maxy";
                return false;
            }
            p++;
        } else if (*p == '(') {
            p++;
            if (foundLoc) {
                _parseError = "Duplicate location";
                return false;
            }
            foundLoc = true;
            if (!correctDimensionalitySkip(p)) {
                return false;
            }
            _x = getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after x position";
                return false;
            }
            p++;
            _y = getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after y position";
                return false;
            }
            p++;
            _radius = getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after radius";
                return false;
            }
            p++;
            /* _tableID = */ (void) getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after tableID";
                return false;
            }
            p++;
            /* _rankMultiplier = */ (void) getInt(p);
            if (*p != ',') {
                _parseError = "Missing ',' after rank multiplier";
                return false;
            }
            p++;
            /* _rankOnlyOnDistance = */ (void) getInt(p);
            if (*p == ',') {
                p++;
                _x_aspect = getInt(p);
                if (*p != ')') {
                    _parseError = "Missing ')' after xAspect";
                    return false;
                }
            } else {
                if (*p != ')') {
                    _parseError = "Missing ')' after rankOnlyOnDistance flag";
                    return false;
                }
            }
            p++;
        } else if (*p == ' ') {
            p++;
        } else {
            _parseError = "Unexpected char in location spec";
            return false;
        }
    }
    _has_point = foundLoc;
    _has_bounding_box = foundBoundingBox;
    _valid = (_has_point || _has_bounding_box);
    return _valid;
}

bool
GeoLocationParser::parseWithField(const std::string &str)
{
     auto sep = str.find(':');
     if (sep == std::string::npos) {
         _parseError = "Location string lacks field specification";
         return false;
     }
     _field_name = str.substr(0, sep);
     std::string only_loc = str.substr(sep + 1);
     return parseNoField(only_loc);
}

bool
GeoLocationParser::parseNoField(const std::string &str)
{
    if (str.empty()) {
        _parseError = "Location string is empty";
        return false;
    }
    if (str[0] == '(' || str[0] == '[') {
        return parseOldFormat(str);
    }
    if (str[0] != '{') {
        _parseError = "Location string should start with '{'";
        return false;
    }
    return parseJsonFormat(str);
}

bool
GeoLocationParser::parseJsonFormat(const std::string &str)
{
    vespalib::Slime slime;
    size_t decoded = vespalib::slime::JsonFormat::decode(str, slime);
    if (decoded == 0) {
        Issue::report("GeoLocationParser: bad location JSON: %s\n>> %s <<",
                      slime.get()["error_message"].asString().make_string().c_str(),
                      str.c_str());
        _parseError = "Failed decoding JSON format location";
        return false;
    }
 // fprintf(stderr, "parsed location JSON %s -> %s\n", str.c_str(), slime.toString().c_str());
    const auto &root = slime.get();
    const auto &point = root["p"];
    const auto &radius = root["r"];
    const auto &aspect = root["a"];
    const auto &bbox = root["b"];

    if (point.valid()) {
        _x = point["x"].asLong();
        _y = point["y"].asLong();
        _has_point = true;
    }
    if (radius.valid()) {
        _radius = radius.asLong();
    }
    if (aspect.valid()) {
        _x_aspect = aspect.asLong();
    }
    if (bbox.valid()) {
        _min_x = bbox["x"][0].asLong();
        _max_x = bbox["x"][1].asLong();
        _min_y = bbox["y"][0].asLong();
        _max_y = bbox["y"][1].asLong();
        _has_bounding_box = true;
    }
    if (_has_point || _has_bounding_box) {
        _valid = true;
    } else {
        _parseError = "Neither point nor bounding box found";
    }
    return _valid;
}

GeoLocation
GeoLocationParser::getGeoLocation() const
{
    if (! _valid) {
        return GeoLocation();
    }
    GeoLocation::Aspect aspect(_x_aspect);
    if (_has_bounding_box) {
        GeoLocation::Range x_range{_min_x, _max_x};
        GeoLocation::Range y_range{_min_y, _max_y};
        GeoLocation::Box bounding_box{x_range, y_range};
        if (_has_point) {
            GeoLocation::Point point{_x, _y};
            if (_radius == GeoLocation::radius_inf) {
                return GeoLocation(bounding_box, point, aspect);
            }
            return GeoLocation(bounding_box, point, _radius, aspect);
        }
        return GeoLocation(bounding_box);
    }
    if (_has_point) {
        GeoLocation::Point point{_x, _y};
        if (_radius == GeoLocation::radius_inf) {
            return GeoLocation(point, aspect);
        }
        return GeoLocation(point, _radius, aspect);
    }
    return GeoLocation();
}

} // namespace