Provision for different mvt_values to share a string pool

This commit is contained in:
Erica Fischer
2024-01-25 14:19:02 -08:00
parent 697b9d6e11
commit 7409d17f13
6 changed files with 58 additions and 31 deletions
+7 -7
View File
@@ -11,7 +11,7 @@
static std::string mvt_value_to_string(mvt_value const &one, bool &fail) { static std::string mvt_value_to_string(mvt_value const &one, bool &fail) {
switch (one.type) { switch (one.type) {
case mvt_string: case mvt_string:
return *(one.string_value); return one.get_string_value();
case mvt_float: case mvt_float:
return milo::dtoa_milo(one.numeric_value.float_value); return milo::dtoa_milo(one.numeric_value.float_value);
case mvt_double: case mvt_double:
@@ -53,7 +53,7 @@ int compare_fsl(mvt_value const &one, json_object *two, bool &fail) {
switch (one.type) { switch (one.type) {
case mvt_string: { case mvt_string: {
char *endptr = NULL; char *endptr = NULL;
const char *s = one.string_value->c_str(); const char *s = one.c_str();
lhs = strtod(s, &endptr); lhs = strtod(s, &endptr);
if (endptr == s) { if (endptr == s) {
fail = true; // non-numeric-string op number => null fail = true; // non-numeric-string op number => null
@@ -106,7 +106,7 @@ int compare_fsl(mvt_value const &one, json_object *two, bool &fail) {
switch (one.type) { switch (one.type) {
case mvt_string: case mvt_string:
lhs = one.string_value->size() > 0; lhs = one.get_string_view().size() > 0;
break; break;
case mvt_float: case mvt_float:
lhs = one.numeric_value.float_value != 0; lhs = one.numeric_value.float_value != 0;
@@ -152,7 +152,7 @@ int compare(mvt_value const &one, json_object *two, bool &fail) {
return false; // string vs non-string return false; // string vs non-string
} }
return strcmp(one.string_value->c_str(), two->value.string.string); return strcmp(one.c_str(), two->value.string.string);
case mvt_double: case mvt_double:
case mvt_float: case mvt_float:
@@ -706,13 +706,13 @@ bool evaluate(mvt_feature const &feat, mvt_layer const &layer, json_object *filt
if (feat.type == mvt_point) { if (feat.type == mvt_point) {
const static std::string point = "Point"; const static std::string point = "Point";
v.string_value = point; v.set_string_value(point);
} else if (feat.type == mvt_linestring) { } else if (feat.type == mvt_linestring) {
const static std::string linestring = "LineString"; const static std::string linestring = "LineString";
v.string_value = linestring; v.set_string_value(linestring);
} else if (feat.type == mvt_polygon) { } else if (feat.type == mvt_polygon) {
const static std::string polygon = "Polygon"; const static std::string polygon = "Polygon";
v.string_value = polygon; v.set_string_value(polygon);
} }
return v; return v;
} }
+8 -8
View File
@@ -157,7 +157,7 @@ bool mvt_tile::decode(const std::string &message, bool &was_compressed) {
switch (value_reader.tag()) { switch (value_reader.tag()) {
case 1: /* string */ case 1: /* string */
value.type = mvt_string; value.type = mvt_string;
value.string_value = value_reader.get_string(); value.set_string_value(value_reader.get_string());
break; break;
case 2: /* float */ case 2: /* float */
@@ -332,7 +332,7 @@ std::string mvt_tile::encode() {
switch (pbv.type) { switch (pbv.type) {
case mvt_string: case mvt_string:
value_writer.add_string(1, *(pbv.string_value)); value_writer.add_string(1, pbv.get_string_value());
break; break;
case mvt_float: case mvt_float:
value_writer.add_float(2, pbv.numeric_value.float_value); value_writer.add_float(2, pbv.numeric_value.float_value);
@@ -461,7 +461,7 @@ bool mvt_value::operator<(const mvt_value &o) const {
if (type == o.type) { if (type == o.type) {
switch (type) { switch (type) {
case mvt_string: case mvt_string:
return *string_value < *(o.string_value); return get_string_view() < o.get_string_view();
case mvt_float: case mvt_float:
return numeric_value.float_value < o.numeric_value.float_value; return numeric_value.float_value < o.numeric_value.float_value;
@@ -497,7 +497,7 @@ bool mvt_value::operator==(const mvt_value &o) const {
if (type == o.type) { if (type == o.type) {
switch (type) { switch (type) {
case mvt_string: case mvt_string:
return *string_value == *(o.string_value); return get_string_view() == o.get_string_view();
case mvt_float: case mvt_float:
return numeric_value.float_value == o.numeric_value.float_value; return numeric_value.float_value == o.numeric_value.float_value;
@@ -553,7 +553,7 @@ static std::string quote(std::string const &s) {
std::string mvt_value::toString() const { std::string mvt_value::toString() const {
switch (type) { switch (type) {
case mvt_string: case mvt_string:
return quote(*string_value); return quote(get_string_value());
case mvt_int: case mvt_int:
return std::to_string(numeric_value.int_value); return std::to_string(numeric_value.int_value);
case mvt_double: { case mvt_double: {
@@ -753,7 +753,7 @@ mvt_value stringified_to_mvt_value(int type, const char *s) {
break; break;
default: default:
tv.type = mvt_string; tv.type = mvt_string;
tv.string_value = s; tv.set_string_value(s);
} }
return tv; return tv;
@@ -770,7 +770,7 @@ serial_val mvt_value_to_serial_val(mvt_value const &v) {
switch (v.type) { switch (v.type) {
case mvt_string: case mvt_string:
sv.type = mvt_string; sv.type = mvt_string;
sv.s = *(v.string_value); sv.s = v.get_string_value();
break; break;
case mvt_float: case mvt_float:
sv.type = mvt_double; sv.type = mvt_double;
@@ -812,7 +812,7 @@ serial_val mvt_value_to_serial_val(mvt_value const &v) {
long long mvt_value_to_long_long(mvt_value const &v) { long long mvt_value_to_long_long(mvt_value const &v) {
switch (v.type) { switch (v.type) {
case mvt_string: case mvt_string:
return atoll(v.string_value->c_str()); return atoll(v.c_str());
case mvt_float: case mvt_float:
return v.numeric_value.float_value; return v.numeric_value.float_value;
case mvt_double: case mvt_double:
+33 -6
View File
@@ -3,10 +3,12 @@
#include <sqlite3.h> #include <sqlite3.h>
#include <string> #include <string>
#include <string_view>
#include <unordered_map> #include <unordered_map>
#include <set> #include <set>
#include <vector> #include <vector>
#include <optional> #include <optional>
#include <memory>
#include "errors.hpp" #include "errors.hpp"
@@ -75,7 +77,8 @@ enum mvt_value_type {
struct mvt_value { struct mvt_value {
mvt_value_type type; mvt_value_type type;
std::optional<std::string> string_value; std::shared_ptr<std::string> s;
union { union {
float float_value; float float_value;
double double_value; double double_value;
@@ -84,8 +87,36 @@ struct mvt_value {
long long sint_value; long long sint_value;
bool bool_value; bool bool_value;
int null_value; int null_value;
struct {
size_t off;
size_t len;
} string_value;
} numeric_value; } numeric_value;
std::string get_string_value() const {
return std::string(*s, numeric_value.string_value.off, numeric_value.string_value.len);
}
std::string_view get_string_view() const {
return std::string_view(s->c_str() + numeric_value.string_value.off, numeric_value.string_value.len);
}
const char *c_str() const {
return s->c_str() + numeric_value.string_value.off;
}
void set_string_value(const std::string &val) {
if (s == nullptr) {
s = std::make_shared<std::string>();
}
type = mvt_string;
numeric_value.string_value.off = s->size();
numeric_value.string_value.len = val.size();
s->append(val);
s->push_back('\0');
}
bool operator<(const mvt_value &o) const; bool operator<(const mvt_value &o) const;
bool operator==(const mvt_value &o) const; bool operator==(const mvt_value &o) const;
std::string toString() const; std::string toString() const;
@@ -99,13 +130,9 @@ struct mvt_value {
template <> template <>
struct std::hash<mvt_value> { struct std::hash<mvt_value> {
std::size_t operator()(const mvt_value &k) const { std::size_t operator()(const mvt_value &k) const {
using std::hash;
using std::size_t;
using std::string;
switch (k.type) { switch (k.type) {
case mvt_string: case mvt_string:
return std::hash<string>()(*(k.string_value)); return std::hash<std::string_view>()(k.get_string_view());
case mvt_float: case mvt_float:
return std::hash<float>()(k.numeric_value.float_value); return std::hash<float>()(k.numeric_value.float_value);
+1 -1
View File
@@ -197,7 +197,7 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::map<st
mvt_value outval; mvt_value outval;
if (attr_type == mvt_string) { if (attr_type == mvt_string) {
outval.type = mvt_string; outval.type = mvt_string;
outval.string_value = joinval; outval.set_string_value(joinval);
} else { } else {
outval.type = mvt_double; outval.type = mvt_double;
outval.numeric_value.double_value = atof(joinval.c_str()); outval.numeric_value.double_value = atof(joinval.c_str());
+8 -8
View File
@@ -217,7 +217,7 @@ void decode_meta(std::vector<long long> const &metakeys, std::vector<long long>
mvt_value key = retrieve_string(metakeys[i], stringpool, NULL); mvt_value key = retrieve_string(metakeys[i], stringpool, NULL);
mvt_value value = retrieve_string(metavals[i], stringpool, &otype); mvt_value value = retrieve_string(metavals[i], stringpool, &otype);
layer.tag(feature, *(key.string_value), value); layer.tag(feature, key.get_string_value(), value);
} }
} }
@@ -227,9 +227,9 @@ static int metacmp(const std::vector<long long> &keys1, const std::vector<long l
mvt_value key1 = retrieve_string(keys1[i], stringpool1, NULL); mvt_value key1 = retrieve_string(keys1[i], stringpool1, NULL);
mvt_value key2 = retrieve_string(keys2[i], stringpool2, NULL); mvt_value key2 = retrieve_string(keys2[i], stringpool2, NULL);
if (*(key1.string_value) < *(key2.string_value)) { if (key1.get_string_view() < key2.get_string_view()) {
return -1; return -1;
} else if (*(key1.string_value) > *(key2.string_value)) { } else if (key1.get_string_view() > key2.get_string_view()) {
return 1; return 1;
} }
@@ -259,7 +259,7 @@ static int metacmp(const std::vector<long long> &keys1, const std::vector<long l
} }
} }
static mvt_value find_attribute_value(const struct coalesce *c1, std::string key) { static mvt_value find_attribute_value(const struct coalesce *c1, std::string const &key) {
if (key == ORDER_BY_SIZE) { if (key == ORDER_BY_SIZE) {
mvt_value v; mvt_value v;
v.type = mvt_double; v.type = mvt_double;
@@ -273,7 +273,7 @@ static mvt_value find_attribute_value(const struct coalesce *c1, std::string key
for (size_t i = 0; i < keys1.size(); i++) { for (size_t i = 0; i < keys1.size(); i++) {
mvt_value key1 = retrieve_string(keys1[i], stringpool1, NULL); mvt_value key1 = retrieve_string(keys1[i], stringpool1, NULL);
if (key == *(key1.string_value)) { if (key == key1.get_string_value()) {
return retrieve_string(values1[i], stringpool1, NULL); return retrieve_string(values1[i], stringpool1, NULL);
} }
} }
@@ -1628,11 +1628,11 @@ serial_feature next_feature(decompressor *geoms, std::atomic<long long> *geompos
v.type = mvt_string; v.type = mvt_string;
if (sf.t == mvt_point) { if (sf.t == mvt_point) {
v.string_value = "Point"; v.set_string_value("Point");
} else if (sf.t == mvt_linestring) { } else if (sf.t == mvt_linestring) {
v.string_value = "LineString"; v.set_string_value("LineString");
} else if (sf.t == mvt_polygon) { } else if (sf.t == mvt_polygon) {
v.string_value = "Polygon"; v.set_string_value("Polygon");
} }
attributes.insert(std::pair<std::string, mvt_value>("$type", v)); attributes.insert(std::pair<std::string, mvt_value>("$type", v));
+1 -1
View File
@@ -339,7 +339,7 @@ void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y
switch (val.type) { switch (val.type) {
case mvt_string: case mvt_string:
state.json_write_string(key); state.json_write_string(key);
state.json_write_string(*(val.string_value)); state.json_write_string(val.get_string_value());
break; break;
case mvt_int: case mvt_int:
state.json_write_string(key); state.json_write_string(key);