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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
#ifndef AWKWARD_IO_JSON_H_
#define AWKWARD_IO_JSON_H_
#include <complex>
#include <cstdio>
#include <string>
#include "awkward/common.h"
#include "awkward/builder/Builder.h"
#include "awkward/builder/ArrayBuilder.h"
#include "awkward/BuilderOptions.h"
#include "awkward/GrowableBuffer.h"
#include "awkward/util.h"
namespace awkward {
/// @class FileLikeObject
///
/// @brief Abstract class to represent a file-like object, something with
/// a `read(num_bytes)` method. Satisfies RapidJSON's Stream interface.
class FileLikeObject {
public:
virtual int64_t read(int64_t num_bytes, char* buffer) = 0;
};
/// @brief Parses a JSON-encoded file-like object using an
/// ArrayBuilder.
///
/// @param source File-like object wrapped with the FileLikeObject
/// abstraction (borrowed reference).
/// @param builder To build the array.
/// @param buffersize Number of bytes for an intermediate buffer.
/// @param read_one If true, read only one JSON object (with an error if
/// there's more); otherwise, read a stream of concatenated objects (may
/// be separated by newlines, but we don't check).
/// @param nan_string User-defined string for a not-a-number (NaN) value
/// representation in JSON format.
/// @param infinity_string User-defined string for a positive infinity
/// representation in JSON format.
/// @param minus_infinity_string User-defined string for a negative
/// infinity representation in JSON format.
EXPORT_SYMBOL void
fromjsonobject(FileLikeObject* source,
ArrayBuilder& builder,
int64_t buffersize,
bool read_one,
const char* nan_string = nullptr,
const char* posinf_string = nullptr,
const char* neginf_string = nullptr);
class EXPORT_SYMBOL FromJsonObjectSchema {
public:
FromJsonObjectSchema(FileLikeObject* source,
int64_t buffersize,
bool read_one,
const char* nan_string,
const char* posinf_string,
const char* neginf_string,
const char* jsonassembly,
int64_t initial,
double resize); // Delete copy constructor
FromJsonObjectSchema(const FromJsonObjectSchema&) = delete;
// Delete copy-assignment constructor
FromJsonObjectSchema& operator=(FromJsonObjectSchema&) = delete;
/// @brief HERE
inline int64_t current_stack_depth() const noexcept {
return current_stack_depth_;
}
/// @brief HERE
inline int64_t current_instruction() const noexcept {
return current_instruction_;
}
/// @brief HERE
inline int64_t instruction() const noexcept {
return instructions_.data()[static_cast<size_t>(current_instruction_) * 4];
}
/// @brief HERE
inline int64_t argument1() const noexcept {
return instructions_.data()[static_cast<size_t>(current_instruction_) * 4 + 1];
}
/// @brief HERE
inline int64_t argument2() const noexcept {
return instructions_.data()[static_cast<size_t>(current_instruction_) * 4 + 2];
}
/// @brief HERE
inline int64_t argument3() const noexcept {
return instructions_.data()[static_cast<size_t>(current_instruction_) * 4 + 3];
}
/// @brief HERE
inline void step_forward() noexcept {
current_instruction_++;
}
/// @brief HERE
inline void step_backward() noexcept {
current_instruction_--;
}
/// @brief HERE
inline void push_stack(int64_t jump_to) noexcept {
instruction_stack_.data()[static_cast<size_t>(current_stack_depth_)] = current_instruction_;
current_stack_depth_++;
current_instruction_ = jump_to;
}
/// @brief HERE
inline void pop_stack() noexcept {
current_stack_depth_--;
current_instruction_ = instruction_stack_.data()[static_cast<size_t>(current_stack_depth_)];
}
/// @brief HERE
inline int64_t find_enum(const char* str) noexcept {
int64_t* offsets = string_offsets_.data();
char* chars = characters_.data();
int64_t stringsstart = argument2();
int64_t start;
int64_t stop;
for (int64_t i = stringsstart; i < argument3(); i++) {
start = offsets[i];
stop = offsets[i + 1];
if (strncmp(str, &chars[start], static_cast<size_t>(stop - start)) == 0) {
return i - stringsstart;
}
}
return -1;
}
/// @brief HERE
inline int64_t find_key(const char* str) noexcept {
int64_t* offsets = string_offsets_.data();
char* chars = characters_.data();
int64_t i = 0;
int64_t j = 0;
int64_t stringi;
int64_t start;
int64_t stop;
uint64_t chunkmask;
// optimistic: fields in data are in the order specified by the schema
if (argument1() != 0) {
// increment the current (last seen) field with wrap-around
record_current_field_[static_cast<size_t>(argument2())]++;
if (record_current_field_[static_cast<size_t>(argument2())] == argument1()) {
record_current_field_[static_cast<size_t>(argument2())] = 0;
}
j = record_current_field_[static_cast<size_t>(argument2())];
// use the record_current_field_ (as j)
i = current_instruction_ + 1 + j;
stringi = instructions_.data()[static_cast<size_t>(i) * 4 + 1];
start = offsets[stringi];
stop = offsets[stringi + 1];
if (strncmp(str, &chars[start], static_cast<size_t>(stop - start)) == 0) {
// ensure that the checklist bit is 1
chunkmask = static_cast<uint64_t>(1) << (j & 0x3f);
if ((record_checklist_[static_cast<size_t>(argument2())][static_cast<size_t>(j >> 6)] & chunkmask) == 0) {
return -1; // ignore the value of a duplicate key
}
// set the checklist bit to 0
record_checklist_[static_cast<size_t>(argument2())][static_cast<size_t>(j >> 6)] &= ~chunkmask;
return key_instruction_at(i);
}
}
// pessimistic: try all field names, starting from the first
for (i = current_instruction_ + 1; i <= current_instruction_ + argument1(); i++) {
// not including the one optimistic trial
if (i != current_instruction_ + 1 + record_current_field_[static_cast<size_t>(argument2())]) {
stringi = instructions_.data()[static_cast<size_t>(i) * 4 + 1];
start = offsets[stringi];
stop = offsets[stringi + 1];
if (strncmp(str, &chars[start], static_cast<size_t>(stop - start)) == 0) {
// set the record_current_field_
j = i - (current_instruction_ + 1);
record_current_field_[static_cast<size_t>(argument2())] = j;
// ensure that the checklist bit is 1
chunkmask = static_cast<uint64_t>(1) << (j & 0x3f);
if ((record_checklist_[static_cast<size_t>(argument2())][static_cast<size_t>(j >> 6)] & chunkmask) == 0) {
return -1; // ignore the value of a duplicate key
}
// set the checklist bit to 0
record_checklist_[static_cast<size_t>(argument2())][static_cast<size_t>(j >> 6)] &= ~chunkmask;
return key_instruction_at(i);
}
}
}
return -1;
}
/// @brief HERE
inline bool key_already_filled(int64_t record_identifier, int64_t j) const noexcept {
uint64_t chunkmask = static_cast<uint64_t>(1) << (j & 0x3f);
return (record_checklist_[static_cast<size_t>(record_identifier)][static_cast<size_t>(j >> 6)] & chunkmask) == 0;
}
/// @brief HERE
inline int64_t key_instruction_at(int64_t i) const noexcept {
return instructions_.data()[static_cast<size_t>(i) * 4 + 2];
}
/// @brief HERE
inline void start_object(int64_t keytableheader_instruction) noexcept {
int64_t record_identifier = instructions_.data()[static_cast<size_t>(keytableheader_instruction) * 4 + 2];
record_checklist_[static_cast<size_t>(record_identifier)].assign(
record_checklist_init_[static_cast<size_t>(record_identifier)].begin(),
record_checklist_init_[static_cast<size_t>(record_identifier)].end()
);
}
/// @brief HERE
inline bool end_object(int64_t keytableheader_instruction) const noexcept {
int64_t record_identifier = instructions_.data()[static_cast<size_t>(keytableheader_instruction) * 4 + 2];
uint64_t should_be_zero = 0;
for (uint64_t chunk : record_checklist_[static_cast<size_t>(record_identifier)]) {
should_be_zero |= chunk;
}
return should_be_zero == 0;
}
/// @brief HERE
inline void write_int8(int64_t index, int8_t x) noexcept {
buffers_uint8_[static_cast<size_t>(index)].append(*reinterpret_cast<uint8_t*>(&x));
}
/// @brief HERE
inline void write_uint8(int64_t index, uint8_t x) noexcept {
buffers_uint8_[static_cast<size_t>(index)].append(x);
}
/// @brief HERE
inline void write_many_uint8(int64_t index, int64_t num_items, const uint8_t* values) noexcept {
buffers_uint8_[static_cast<size_t>(index)].extend(values, static_cast<size_t>(num_items));
}
/// @brief HERE
inline void write_int64(int64_t index, int64_t x) noexcept {
buffers_int64_[static_cast<size_t>(index)].append(x);
}
/// @brief HERE
inline void write_uint64(int64_t index, uint64_t x) noexcept {
buffers_int64_[static_cast<size_t>(index)].append(static_cast<int64_t>(x));
}
/// @brief HERE
inline void write_add_int64(int64_t index, int64_t x) noexcept {
buffers_int64_[static_cast<size_t>(index)].append(buffers_int64_[static_cast<size_t>(index)].last() + x);
}
/// @brief HERE
inline void write_float64(int64_t index, double x) noexcept {
buffers_float64_[static_cast<size_t>(index)].append(x);
}
/// @brief HERE
inline int64_t get_and_increment(int64_t index) noexcept {
return counters_[static_cast<size_t>(index)]++;
}
/// @brief HERE
int64_t length() const noexcept {
return length_;
}
/// @brief HERE
inline void add_to_length(int64_t length) noexcept {
length_ += length;
}
/// @brief HERE
std::string debug() const noexcept;
/// @brief HERE
int64_t num_outputs() const {
return static_cast<int64_t>(output_names_.size());
}
/// @brief HERE
std::string output_name(int64_t i) const {
return output_names_[static_cast<size_t>(i)];
}
/// @brief HERE
std::string output_dtype(int64_t i) const {
switch (output_dtypes_[static_cast<size_t>(i)]) {
case util::dtype::int8:
return "int8";
case util::dtype::uint8:
return "uint8";
case util::dtype::int64:
return "int64";
case util::dtype::float64:
return "float64";
default:
return "unknown";
}
}
/// @brief HERE
int64_t output_num_items(int64_t i) const {
switch (output_dtypes_[static_cast<size_t>(i)]) {
case util::dtype::int8:
return static_cast<int64_t>(buffers_uint8_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].nbytes());
case util::dtype::uint8:
return static_cast<int64_t>(buffers_uint8_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].nbytes());
case util::dtype::int64:
return static_cast<int64_t>(buffers_int64_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].nbytes() / 8);
case util::dtype::float64:
return static_cast<int64_t>(buffers_float64_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].nbytes() / 8);
default:
return -1;
}
}
/// @brief HERE
void output_fill(int64_t i, void* external_pointer) const {
switch (output_dtypes_[static_cast<size_t>(i)]) {
case util::dtype::int8:
buffers_uint8_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].concatenate(
reinterpret_cast<uint8_t*>(external_pointer)
);
break;
case util::dtype::uint8:
buffers_uint8_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].concatenate(
reinterpret_cast<uint8_t*>(external_pointer)
);
break;
case util::dtype::int64:
buffers_int64_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].concatenate(
reinterpret_cast<int64_t*>(external_pointer)
);
break;
case util::dtype::float64:
buffers_float64_[static_cast<size_t>(output_which_[static_cast<size_t>(i)])].concatenate(
reinterpret_cast<double*>(external_pointer)
);
break;
default:
break;
}
}
private:
std::vector<int64_t> instructions_;
std::vector<char> characters_;
std::vector<int64_t> string_offsets_;
std::vector<int64_t> record_current_field_;
std::vector<std::vector<uint64_t>> record_checklist_init_;
std::vector<std::vector<uint64_t>> record_checklist_;
std::vector<std::string> output_names_;
std::vector<util::dtype> output_dtypes_;
std::vector<int64_t> output_which_;
std::vector<GrowableBuffer<uint8_t>> buffers_uint8_;
std::vector<GrowableBuffer<int64_t>> buffers_int64_;
std::vector<GrowableBuffer<double>> buffers_float64_;
int64_t current_instruction_;
std::vector<int64_t> instruction_stack_;
int64_t current_stack_depth_;
std::vector<int64_t> counters_;
int64_t length_;
};
}
#endif // AWKWARD_IO_JSON_H_
|