FlatCityBuf C++ reader 0.10.0
Native C++17 reader for FlatCityBuf, the cloud-optimized CityJSON format
Loading...
Searching...
No Matches
attribute.cpp
Go to the documentation of this file.
2
3#ifdef FCB_WITH_JSON
4
5# include <algorithm>
6# include <cstring>
7# include <limits>
8# include <optional>
9# include <type_traits>
10
11namespace fcb {
12
13namespace {
14
18std::int64_t days_from_civil(int y, unsigned m, unsigned d) {
19 y -= (m <= 2) ? 1 : 0;
20 const std::int64_t era = (y >= 0 ? y : y - 399) / 400;
21 const unsigned yoe = static_cast<unsigned>(y - era * 400);
22 const unsigned doy = (153 * (m + (m > 2 ? static_cast<unsigned>(-3) : 9)) + 2) / 5 + d - 1;
23 const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
24 return era * 146097 + static_cast<std::int64_t>(doe) - 719468;
25}
26
27struct Rfc3339Result {
28 bool ok = false;
29 std::int64_t seconds = 0;
30 std::uint32_t nanos = 0;
31};
32
40Rfc3339Result parse_rfc3339(const std::string& s) {
41 Rfc3339Result r;
42 if (s.size() < 20)
43 return r; // shortest valid form: "YYYY-MM-DDTHH:MM:SSZ"
44
45 auto is_digit = [](char c) { return c >= '0' && c <= '9'; };
46 auto two = [&](std::size_t at) -> int { return (s[at] - '0') * 10 + (s[at + 1] - '0'); };
47
48 for (std::size_t i : {0u, 1u, 2u, 3u, 5u, 6u, 8u, 9u})
49 if (!is_digit(s[i]))
50 return r;
51 if (s[4] != '-' || s[7] != '-')
52 return r;
53 if (s[10] != 'T' && s[10] != 't' && s[10] != ' ')
54 return r;
55 for (std::size_t i : {11u, 12u, 14u, 15u, 17u, 18u})
56 if (!is_digit(s[i]))
57 return r;
58 if (s[13] != ':' || s[16] != ':')
59 return r;
60
61 const int year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
62 const int month = two(5);
63 const int day = two(8);
64 const int hour = two(11);
65 const int minute = two(14);
66 const int second = two(17);
67
68 if (month < 1 || month > 12)
69 return r;
70 static const int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
71 const bool leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
72 const int max_day = (month == 2 && leap) ? 29 : days_in_month[month - 1];
73 if (day < 1 || day > max_day)
74 return r;
75 if (hour > 23 || minute > 59 || second > 60) // 60 == leap second
76 return r;
77
78 std::size_t pos = 19;
79 std::uint32_t nanos = 0;
80 if (pos < s.size() && s[pos] == '.') {
81 const std::size_t start = ++pos;
82 while (pos < s.size() && is_digit(s[pos]))
83 ++pos;
84 if (pos == start)
85 return r; // '.' with no digits after it
86 std::string frac = s.substr(start, pos - start);
87 if (frac.size() > 9)
88 frac.resize(9);
89 else
90 frac.resize(9, '0');
91 nanos = static_cast<std::uint32_t>(std::stoul(frac));
92 }
93 if (pos >= s.size())
94 return r; // no timezone marker at all -- not RFC3339
95
96 std::int64_t offset_seconds = 0;
97 if (s[pos] == 'Z' || s[pos] == 'z') {
98 ++pos;
99 } else if (s[pos] == '+' || s[pos] == '-') {
100 const bool neg = s[pos] == '-';
101 ++pos;
102 if (pos + 5 > s.size() || !is_digit(s[pos]) || !is_digit(s[pos + 1]) || s[pos + 2] != ':' ||
103 !is_digit(s[pos + 3]) || !is_digit(s[pos + 4]))
104 return r;
105 const int oh = two(pos);
106 const int om = two(pos + 3);
107 if (oh > 23 || om > 59)
108 return r;
109 offset_seconds = (oh * 3600 + om * 60) * (neg ? -1 : 1);
110 pos += 5;
111 } else {
112 return r;
113 }
114 if (pos != s.size())
115 return r; // trailing garbage after the offset
116
117 const std::int64_t days =
118 days_from_civil(year, static_cast<unsigned>(month), static_cast<unsigned>(day));
119 // A leap second (:60) is chrono's `NaiveTime` convention: it occupies the
120 // SAME epoch second as :59, flagged by adding 1_000_000_000 to the
121 // nanosecond field, rather than rolling over into the next minute. Rolling
122 // over (as a naive `+ second` would) both produces the wrong epoch second
123 // and can wrap a leap second at day's end onto the NEXT day's midnight.
124 const int second_for_epoch = (second == 60) ? 59 : second;
125 if (second == 60)
126 nanos += 1'000'000'000u;
127 r.ok = true;
128 r.seconds = days * 86400 + hour * 3600 + minute * 60 + second_for_epoch - offset_seconds;
129 r.nanos = nanos;
130 return r;
131}
132
133bool looks_like_rfc3339(const std::string& s) { return parse_rfc3339(s).ok; }
134
135std::optional<::ColumnType> guess_type(const nlohmann::ordered_json& value) {
136 if (value.is_boolean())
137 return ::ColumnType::Bool;
138 if (value.is_number()) {
139 if (value.is_number_float())
140 return ::ColumnType::Double;
141 if (value.is_number_unsigned())
142 return ::ColumnType::ULong;
143 if (value.is_number_integer()) {
144 // serde_json::Number splits PosInt/NegInt by the VALUE's sign,
145 // regardless of the Rust literal's own signed/unsigned type. A
146 // C++ value built via `nlohmann::ordered_json(5)` (as opposed to parsed
147 // from JSON text with no leading '-') lands in nlohmann's signed
148 // `number_integer_t` bucket even though 5 is non-negative, so
149 // this must also decide by value, not by which nlohmann bucket
150 // it landed in, to stay oracle-compatible for callers that
151 // construct JSON directly rather than parsing it.
152 const std::int64_t i = value.get<std::int64_t>();
153 return i < 0 ? ::ColumnType::Long : ::ColumnType::ULong;
154 }
155 return ::ColumnType::ULong;
156 }
157 if (value.is_string())
158 return looks_like_rfc3339(value.get_ref<const std::string&>()) ? ::ColumnType::DateTime
159 : ::ColumnType::String;
160 if (value.is_array() || value.is_object())
161 return ::ColumnType::Json;
162 return std::nullopt; // null, or anything else
163}
164
165template <typename T> void put_le(std::vector<std::uint8_t>& out, std::size_t at, T v) {
166 using U = typename std::make_unsigned<T>::type;
167 const U u = static_cast<U>(v);
168 for (std::size_t i = 0; i < sizeof(T); ++i)
169 out[at + i] = static_cast<std::uint8_t>(u >> (8 * i));
170}
171
172void put_f32(std::vector<std::uint8_t>& out, std::size_t at, float f) {
173 std::uint32_t bits;
174 std::memcpy(&bits, &f, sizeof(bits));
175 put_le<std::uint32_t>(out, at, bits);
176}
177
178void put_f64(std::vector<std::uint8_t>& out, std::size_t at, double d) {
179 std::uint64_t bits;
180 std::memcpy(&bits, &d, sizeof(bits));
181 put_le<std::uint64_t>(out, at, bits);
182}
183
184// The following mirror serde_json::Value::as_i64/as_u64/as_f64/as_bool/as_str:
185// a type/range mismatch yields the fallback rather than throwing, because the
186// Rust writer being ported never throws on this path either -- a value whose
187// JSON shape drifted from the schema's remembered column type is written as
188// the type's zero value, not rejected.
189
190std::int64_t as_i64_or0(const nlohmann::ordered_json& v) {
191 if (v.is_number_unsigned()) {
192 const std::uint64_t u = v.get<std::uint64_t>();
193 return u <= static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())
194 ? static_cast<std::int64_t>(u)
195 : 0;
196 }
197 if (v.is_number_integer())
198 return v.get<std::int64_t>();
199 return 0;
200}
201
202std::uint64_t as_u64_or0(const nlohmann::ordered_json& v) {
203 if (v.is_number_unsigned())
204 return v.get<std::uint64_t>();
205 if (v.is_number_integer()) {
206 const std::int64_t i = v.get<std::int64_t>();
207 return i >= 0 ? static_cast<std::uint64_t>(i) : 0;
208 }
209 return 0;
210}
211
212double as_f64_or0(const nlohmann::ordered_json& v) { return v.is_number() ? v.get<double>() : 0.0; }
213
214bool as_bool_or_false(const nlohmann::ordered_json& v) { return v.is_boolean() && v.get<bool>(); }
215
216std::string as_str_or_empty(const nlohmann::ordered_json& v) {
217 return v.is_string() ? v.get<std::string>() : std::string();
218}
219
220} // namespace
221
222void add_attributes(AttributeSchema& schema, const nlohmann::ordered_json& attrs) {
223 if (!attrs.is_object()) {
224 // Rust's `BTreeMap::insert` here ALWAYS overwrites -- even a "json"
225 // column that already exists gets reassigned a new index equal to
226 // the map's current size, which can orphan whatever previously held
227 // that index. That is a real quirk of the oracle, not a bug to
228 // paper over: `insert_or_assign` (not the no-op-on-existing-key
229 // `emplace`) is what reproduces it exactly.
230 schema.insert_or_assign(
231 "json", std::make_pair(static_cast<std::uint16_t>(schema.size()), ::ColumnType::Json));
232 return;
233 }
234 for (const auto& [key, val] : attrs.items()) {
235 if (schema.find(key) != schema.end() || val.is_null())
236 continue;
237 if (auto coltype = guess_type(val)) {
238 schema.emplace(key,
239 std::make_pair(static_cast<std::uint16_t>(schema.size()), *coltype));
240 }
241 }
242}
243
244std::size_t attr_size(::ColumnType coltype, const nlohmann::ordered_json& colval) {
245 switch (coltype) {
246 case ::ColumnType::Byte:
247 return sizeof(std::int8_t);
248 case ::ColumnType::UByte:
249 return sizeof(std::uint8_t);
250 case ::ColumnType::Bool:
251 return sizeof(std::uint8_t);
252 case ::ColumnType::Short:
253 return sizeof(std::int16_t);
254 case ::ColumnType::UShort:
255 return sizeof(std::uint16_t);
256 case ::ColumnType::Int:
257 return sizeof(std::int32_t);
258 case ::ColumnType::UInt:
259 return sizeof(std::uint32_t);
260 case ::ColumnType::Long:
261 return sizeof(std::int64_t);
262 case ::ColumnType::ULong:
263 return sizeof(std::uint64_t);
264 case ::ColumnType::Float:
265 return sizeof(float);
266 case ::ColumnType::Double:
267 return sizeof(double);
268 case ::ColumnType::String:
269 case ::ColumnType::DateTime:
270 return sizeof(std::uint32_t) + as_str_or_empty(colval).size();
271 case ::ColumnType::Json:
272 return sizeof(std::uint32_t) + colval.dump().size();
273 case ::ColumnType::Binary:
274 return sizeof(std::uint32_t) + as_str_or_empty(colval).size();
275 }
276 throw Error(ErrorCode::UnsupportedColumnType, "attr_size: unknown column type");
277}
278
279std::vector<std::uint8_t> encode_attributes_with_schema(const nlohmann::ordered_json& attr,
280 const AttributeSchema& schema) {
281 std::vector<std::uint8_t> out;
282 if (!attr.is_object() || attr.empty())
283 return out;
284
285 std::vector<std::pair<std::string, std::pair<std::uint16_t, ::ColumnType>>> sorted(
286 schema.begin(), schema.end());
287 std::sort(sorted.begin(), sorted.end(),
288 [](const auto& a, const auto& b) { return a.second.first < b.second.first; });
289
290 for (const auto& [name, idx_type] : sorted) {
291 const auto [index, coltype] = idx_type;
292 auto it = attr.find(name);
293 if (it == attr.end() || it->is_null())
294 continue;
295 const nlohmann::ordered_json& val = *it;
296
297 const std::size_t offset = out.size();
298 const std::size_t size = attr_size(coltype, val);
299 out.resize(offset + sizeof(std::uint16_t) + size, 0);
300 put_le<std::uint16_t>(out, offset, index);
301 const std::size_t value_offset = offset + sizeof(std::uint16_t);
302
303 switch (coltype) {
304 case ::ColumnType::Bool:
305 out[value_offset] = as_bool_or_false(val) ? 1 : 0;
306 break;
307 case ::ColumnType::Int:
308 put_le<std::int32_t>(out, value_offset, static_cast<std::int32_t>(as_i64_or0(val)));
309 break;
310 case ::ColumnType::UInt:
311 put_le<std::uint32_t>(out, value_offset,
312 static_cast<std::uint32_t>(as_u64_or0(val)));
313 break;
314 case ::ColumnType::Byte:
315 out[value_offset] = static_cast<std::uint8_t>(as_i64_or0(val));
316 break;
317 case ::ColumnType::UByte:
318 out[value_offset] = static_cast<std::uint8_t>(as_u64_or0(val));
319 break;
320 case ::ColumnType::Short:
321 put_le<std::int16_t>(out, value_offset, static_cast<std::int16_t>(as_i64_or0(val)));
322 break;
323 case ::ColumnType::UShort:
324 put_le<std::uint16_t>(out, value_offset,
325 static_cast<std::uint16_t>(as_u64_or0(val)));
326 break;
327 case ::ColumnType::Long:
328 put_le<std::int64_t>(out, value_offset, as_i64_or0(val));
329 break;
330 case ::ColumnType::ULong:
331 put_le<std::uint64_t>(out, value_offset, as_u64_or0(val));
332 break;
333 case ::ColumnType::Float:
334 put_f32(out, value_offset, static_cast<float>(as_f64_or0(val)));
335 break;
336 case ::ColumnType::Double:
337 put_f64(out, value_offset, as_f64_or0(val));
338 break;
339 case ::ColumnType::String:
340 case ::ColumnType::DateTime: {
341 const std::string s = as_str_or_empty(val);
342 put_le<std::uint32_t>(out, value_offset, static_cast<std::uint32_t>(s.size()));
343 std::memcpy(out.data() + value_offset + sizeof(std::uint32_t), s.data(), s.size());
344 break;
345 }
346 case ::ColumnType::Json: {
347 const std::string json_str = val.dump();
348 put_le<std::uint32_t>(out, value_offset,
349 static_cast<std::uint32_t>(json_str.size()));
350 std::memcpy(out.data() + value_offset + sizeof(std::uint32_t), json_str.data(),
351 json_str.size());
352 break;
353 }
354 case ::ColumnType::Binary: {
355 const std::string s = as_str_or_empty(val);
356 put_le<std::uint32_t>(out, value_offset, static_cast<std::uint32_t>(s.size()));
357 std::memcpy(out.data() + value_offset + sizeof(std::uint32_t), s.data(), s.size());
358 break;
359 }
360 }
361 }
362 return out;
363}
364
365::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<::Column>>>
366to_columns(::flatbuffers::FlatBufferBuilder& fbb, const AttributeSchema& schema,
367 std::optional<std::uint16_t> id_column) {
368 std::vector<std::pair<std::string, std::pair<std::uint16_t, ::ColumnType>>> sorted(
369 schema.begin(), schema.end());
370 std::sort(sorted.begin(), sorted.end(),
371 [](const auto& a, const auto& b) { return a.second.first < b.second.first; });
372
373 std::vector<::flatbuffers::Offset<::Column>> columns;
374 columns.reserve(sorted.size());
375 for (const auto& [name, idx_type] : sorted) {
376 auto name_off = fbb.CreateString(name);
377 columns.push_back(CreateColumn(fbb, idx_type.first, name_off, idx_type.second));
378 }
379 if (id_column) {
380 auto name_off = fbb.CreateString(kIdColumnName);
381 columns.push_back(CreateColumn(fbb, *id_column, name_off, ::ColumnType::String, 0, 0, -1,
382 -1,
383 /*nullable=*/false, /*unique=*/false,
384 /*primary_key=*/true));
385 }
386 return fbb.CreateVector(columns);
387}
388
389std::optional<std::uint16_t> id_column_index(const std::optional<std::uint16_t>& id_index,
390 const AttributeSchema& schema) {
391 if (!id_index || schema.find(kIdColumnName) != schema.end())
392 return std::nullopt;
393 std::uint16_t next = 0;
394 for (const auto& [name, idx_type] : schema)
395 next = std::max<std::uint16_t>(next, static_cast<std::uint16_t>(idx_type.first + 1));
396 return next;
397}
398
399std::vector<AttributeIndexEntry>
400cityfeature_to_id_index_entries(const nlohmann::ordered_json& city_feature, std::uint16_t index) {
401 std::vector<AttributeIndexEntry> out;
402 auto co_it = city_feature.find("CityObjects");
403 if (co_it == city_feature.end() || !co_it->is_object())
404 return out;
405 std::vector<std::string> object_ids;
406 object_ids.reserve(co_it->size());
407 for (const auto& [id, unused] : co_it->items())
408 object_ids.push_back(id);
409 std::sort(object_ids.begin(), object_ids.end());
410 const KeyKind kind = key_kind_for_column(static_cast<std::uint8_t>(::ColumnType::String));
411 for (const auto& id : object_ids)
412 out.push_back(AttributeIndexEntry{index, KeyValue::from_string(kind, id)});
413 return out;
414}
415
416std::vector<AttributeIndexEntry>
417attribute_to_index_entries(const nlohmann::ordered_json& attr, const AttributeSchema& schema,
418 const std::vector<std::string>& indexing_attr) {
419 std::vector<AttributeIndexEntry> out;
420 if (!attr.is_object() || attr.empty())
421 return out;
422
423 for (const auto& name : indexing_attr) {
424 auto val_it = attr.find(name);
425 if (val_it == attr.end())
426 continue;
427 auto schema_it = schema.find(name);
428 if (schema_it == schema.end())
429 continue;
430 const auto [index, coltype] = schema_it->second;
431 const nlohmann::ordered_json& val = *val_it;
432
433 switch (coltype) {
434 case ::ColumnType::Bool:
435 out.push_back({index, KeyValue::from_bool(as_bool_or_false(val))});
436 break;
437 case ::ColumnType::Int:
438 out.push_back(
439 {index, KeyValue::from_i32(static_cast<std::int32_t>(as_i64_or0(val)))});
440 break;
441 case ::ColumnType::UInt:
442 out.push_back(
443 {index, KeyValue::from_u32(static_cast<std::uint32_t>(as_u64_or0(val)))});
444 break;
445 case ::ColumnType::Long:
446 out.push_back({index, KeyValue::from_i64(as_i64_or0(val))});
447 break;
448 case ::ColumnType::ULong:
449 out.push_back({index, KeyValue::from_u64(as_u64_or0(val))});
450 break;
451 case ::ColumnType::Float:
452 out.push_back({index, KeyValue::from_f32(static_cast<float>(as_f64_or0(val)))});
453 break;
454 case ::ColumnType::Double:
455 out.push_back({index, KeyValue::from_f64(as_f64_or0(val))});
456 break;
457 case ::ColumnType::String:
458 out.push_back(
459 {index, KeyValue::from_string(KeyKind::String50, as_str_or_empty(val))});
460 break;
461 case ::ColumnType::DateTime: {
462 const Rfc3339Result parsed = parse_rfc3339(as_str_or_empty(val));
463 out.push_back({index, KeyValue::from_datetime(parsed.ok ? parsed.seconds : 0,
464 parsed.ok ? parsed.nanos : 0)});
465 break;
466 }
467 case ::ColumnType::Byte:
468 case ::ColumnType::UByte:
469 case ::ColumnType::Short:
470 case ::ColumnType::UShort:
471 case ::ColumnType::Json:
472 case ::ColumnType::Binary:
473 // Not supported for indexing at extraction time -- matches
474 // writer/attribute.rs's `attribute_to_index_entries`.
475 break;
476 }
477 }
478 return out;
479}
480
481std::vector<AttributeIndexEntry>
482cityfeature_to_index_entries(const nlohmann::ordered_json& city_feature,
483 const AttributeSchema& schema,
484 const std::vector<std::string>& indexing_attr) {
485 std::vector<AttributeIndexEntry> out;
486 auto co_it = city_feature.find("CityObjects");
487 if (co_it == city_feature.end() || !co_it->is_object())
488 return out;
489
490 std::vector<std::string> object_ids;
491 object_ids.reserve(co_it->size());
492 for (const auto& [id, unused] : co_it->items())
493 object_ids.push_back(id);
494 std::sort(object_ids.begin(), object_ids.end());
495
496 for (const auto& id : object_ids) {
497 const nlohmann::ordered_json& co = co_it->at(id);
498 auto attr_it = co.find("attributes");
499 if (attr_it == co.end() || attr_it->is_null())
500 continue;
501 auto entries = attribute_to_index_entries(*attr_it, schema, indexing_attr);
502 out.insert(out.end(), entries.begin(), entries.end());
503 }
504 return out;
505}
506
507} // namespace fcb
508
509#endif // FCB_WITH_JSON
Every failure the library reports is one of these.
Definition error.hpp:31
static KeyValue from_datetime(std::int64_t seconds, std::uint32_t nanos)
Definition key.cpp:129
static KeyValue from_i64(std::int64_t v)
Definition key.cpp:104
static KeyValue from_u32(std::uint32_t v)
Definition key.cpp:103
static KeyValue from_f32(float v)
Definition key.cpp:108
static KeyValue from_i32(std::int32_t v)
Definition key.cpp:102
static KeyValue from_f64(double v)
Definition key.cpp:115
static KeyValue from_string(KeyKind kind, const std::string &v)
Definition key.cpp:137
static KeyValue from_u64(std::uint64_t v)
Definition key.cpp:105
static KeyValue from_bool(bool v)
Definition key.cpp:122
std::vector< std::uint8_t > encode_attributes_with_schema(const nlohmann::ordered_json &attr, const AttributeSchema &schema)
Encodes attr (a CityJSON attributes object) against schema: repeated [u16 LE column index][value] rec...
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<::Column > > > to_columns(::flatbuffers::FlatBufferBuilder &fbb, const AttributeSchema &schema, std::optional< std::uint16_t > id_column=std::nullopt)
Builds the Column vector for Header.columns or CityObject.columns, in ascending column-index order.
@ UnsupportedColumnType
Definition error.hpp:19
std::size_t attr_size(::ColumnType coltype, const nlohmann::ordered_json &colval)
Byte width one value of coltype occupies in the attribute blob, EXCLUDING the 2-byte column-index pre...
constexpr const char * kIdColumnName
The name of the header column that stands for a City Object's id.
Definition attribute.hpp:76
std::map< std::string, std::pair< std::uint16_t, ::ColumnType > > AttributeSchema
Attribute schema: name -> (column index, column type).
Definition attribute.hpp:43
KeyKind key_kind_for_column(std::uint8_t column_type)
Column type to key kind, following what the WRITER emits.
Definition key.cpp:387
void add_attributes(AttributeSchema &schema, const nlohmann::ordered_json &attrs)
Adds every member of a JSON object to schema, assigning each new, non-null name the next free column ...
std::vector< AttributeIndexEntry > cityfeature_to_index_entries(const nlohmann::ordered_json &city_feature, const AttributeSchema &schema, const std::vector< std::string > &indexing_attr)
Same, over every object in one CityJSONFeature's CityObjects, visited in ascending object-id order (n...
std::vector< AttributeIndexEntry > cityfeature_to_id_index_entries(const nlohmann::ordered_json &city_feature, std::uint16_t index)
One index entry per City Object of city_feature, keyed by the object's id, for the id column at index...
KeyKind
The concrete key types the B+tree index can hold.
Definition key.hpp:14
std::optional< std::uint16_t > id_column_index(const std::optional< std::uint16_t > &id_index, const AttributeSchema &schema)
The index of the City Object id column: the next free one when id_index asks for an id index and sche...
std::vector< AttributeIndexEntry > attribute_to_index_entries(const nlohmann::ordered_json &attr, const AttributeSchema &schema, const std::vector< std::string > &indexing_attr)
Extracts index entries for indexing_attr from one CityJSON attributes object.
One indexable (column, value) pair pulled out of a feature for the static B+tree builder (M6).
Definition attribute.hpp:88