simdjson  0.3.1
Ridiculously Fast JSON
document_stream.h
1 #ifndef SIMDJSON_INLINE_DOCUMENT_STREAM_H
2 #define SIMDJSON_INLINE_DOCUMENT_STREAM_H
3 
4 #include "simdjson/document_stream.h"
5 #include <algorithm>
6 #include <limits>
7 #include <stdexcept>
8 #include <thread>
9 
10 namespace simdjson::internal {
11 
35 inline size_t find_last_json_buf_idx(const uint8_t *buf, size_t size, const dom::parser &parser) {
36  // this function can be generally useful
37  if (parser.n_structural_indexes == 0)
38  return 0;
39  auto last_i = parser.n_structural_indexes - 1;
40  if (parser.structural_indexes[last_i] == size) {
41  if (last_i == 0)
42  return 0;
43  last_i = parser.n_structural_indexes - 2;
44  }
45  auto arr_cnt = 0;
46  auto obj_cnt = 0;
47  for (auto i = last_i; i > 0; i--) {
48  auto idxb = parser.structural_indexes[i];
49  switch (buf[idxb]) {
50  case ':':
51  case ',':
52  continue;
53  case '}':
54  obj_cnt--;
55  continue;
56  case ']':
57  arr_cnt--;
58  continue;
59  case '{':
60  obj_cnt++;
61  break;
62  case '[':
63  arr_cnt++;
64  break;
65  }
66  auto idxa = parser.structural_indexes[i - 1];
67  switch (buf[idxa]) {
68  case '{':
69  case '[':
70  case ':':
71  case ',':
72  continue;
73  }
74  if (!arr_cnt && !obj_cnt) {
75  return last_i + 1;
76  }
77  return i;
78  }
79  return 0;
80 }
81 
82 // returns true if the provided byte value is an ASCII character
83 static inline bool is_ascii(char c) {
84  return ((unsigned char)c) <= 127;
85 }
86 
87 // if the string ends with UTF-8 values, backtrack
88 // up to the first ASCII character. May return 0.
89 static inline size_t trimmed_length_safe_utf8(const char * c, size_t len) {
90  while ((len > 0) and (not is_ascii(c[len - 1]))) {
91  len--;
92  }
93  return len;
94 }
95 
96 } // namespace simdjson::internal
97 
98 namespace simdjson::dom {
99 
100 really_inline document_stream::document_stream(
101  dom::parser &_parser,
102  const uint8_t *buf,
103  size_t len,
104  size_t batch_size,
105  error_code _error
106 ) noexcept : parser{_parser}, _buf{buf}, _len{len}, _batch_size(batch_size), error{_error} {
107  if (!error) { error = json_parse(); }
108 }
109 
110 inline document_stream::~document_stream() noexcept {
111 #ifdef SIMDJSON_THREADS_ENABLED
112  if (stage_1_thread.joinable()) {
113  stage_1_thread.join();
114  }
115 #endif
116 }
117 
119  return iterator(*this, false);
120 }
121 
123  return iterator(*this, true);
124 }
125 
126 really_inline document_stream::iterator::iterator(document_stream& _stream, bool is_end) noexcept
127  : stream{_stream}, finished{is_end} {
128 }
129 
131  error_code err = stream.error == SUCCESS_AND_HAS_MORE ? SUCCESS : stream.error;
132  if (err) { return err; }
133  return stream.parser.doc.root();
134 }
135 
137  if (stream.error == SUCCESS_AND_HAS_MORE) {
138  stream.error = stream.json_parse();
139  } else {
140  finished = true;
141  }
142  return *this;
143 }
144 
145 really_inline bool document_stream::iterator::operator!=(const document_stream::iterator &other) const noexcept {
146  return finished != other.finished;
147 }
148 
149 #ifdef SIMDJSON_THREADS_ENABLED
150 
151 // threaded version of json_parse
152 // todo: simplify this code further
153 inline error_code document_stream::json_parse() noexcept {
154  error = parser.ensure_capacity(_batch_size);
155  if (error) { return error; }
156  error = parser_thread.ensure_capacity(_batch_size);
157  if (error) { return error; }
158 
159  if (unlikely(load_next_batch)) {
160  // First time loading
161  if (!stage_1_thread.joinable()) {
162  _batch_size = (std::min)(_batch_size, remaining());
163  _batch_size = internal::trimmed_length_safe_utf8((const char *)buf(), _batch_size);
164  if (_batch_size == 0) {
165  return simdjson::UTF8_ERROR;
166  }
167  auto stage1_is_ok = error_code(simdjson::active_implementation->stage1(buf(), _batch_size, parser, true));
168  if (stage1_is_ok != simdjson::SUCCESS) {
169  return stage1_is_ok;
170  }
171  size_t last_index = internal::find_last_json_buf_idx(buf(), _batch_size, parser);
172  if (last_index == 0) {
173  if (parser.n_structural_indexes == 0) {
174  return simdjson::EMPTY;
175  }
176  } else {
177  parser.n_structural_indexes = last_index + 1;
178  }
179  }
180  // the second thread is running or done.
181  else {
182  stage_1_thread.join();
183  if (stage1_is_ok_thread != simdjson::SUCCESS) {
184  return stage1_is_ok_thread;
185  }
186  std::swap(parser.structural_indexes, parser_thread.structural_indexes);
187  parser.n_structural_indexes = parser_thread.n_structural_indexes;
188  advance(last_json_buffer_loc);
189  n_bytes_parsed += last_json_buffer_loc;
190  }
191  // let us decide whether we will start a new thread
192  if (remaining() - _batch_size > 0) {
193  last_json_buffer_loc =
194  parser.structural_indexes[internal::find_last_json_buf_idx(buf(), _batch_size, parser)];
195  _batch_size = (std::min)(_batch_size, remaining() - last_json_buffer_loc);
196  if (_batch_size > 0) {
197  _batch_size = internal::trimmed_length_safe_utf8(
198  (const char *)(buf() + last_json_buffer_loc), _batch_size);
199  if (_batch_size == 0) {
200  return simdjson::UTF8_ERROR;
201  }
202  // let us capture read-only variables
203  const uint8_t *const b = buf() + last_json_buffer_loc;
204  const size_t bs = _batch_size;
205  // we call the thread on a lambda that will update
206  // this->stage1_is_ok_thread
207  // there is only one thread that may write to this value
208  stage_1_thread = std::thread([this, b, bs] {
209  this->stage1_is_ok_thread = error_code(simdjson::active_implementation->stage1(b, bs, this->parser_thread, true));
210  });
211  }
212  }
213  next_json = 0;
214  load_next_batch = false;
215  } // load_next_batch
216  error_code res = simdjson::active_implementation->stage2(buf(), remaining(), parser, next_json);
217  if (res == simdjson::SUCCESS_AND_HAS_MORE) {
218  n_parsed_docs++;
219  current_buffer_loc = parser.structural_indexes[next_json];
220  load_next_batch = (current_buffer_loc == last_json_buffer_loc);
221  } else if (res == simdjson::SUCCESS) {
222  n_parsed_docs++;
223  if (remaining() > _batch_size) {
224  current_buffer_loc = parser.structural_indexes[next_json - 1];
225  load_next_batch = true;
226  res = simdjson::SUCCESS_AND_HAS_MORE;
227  }
228  }
229  return res;
230 }
231 
232 #else // SIMDJSON_THREADS_ENABLED
233 
234 // single-threaded version of json_parse
235 inline error_code document_stream::json_parse() noexcept {
236  error = parser.ensure_capacity(_batch_size);
237  if (error) { return error; }
238 
239  if (unlikely(load_next_batch)) {
240  advance(current_buffer_loc);
241  n_bytes_parsed += current_buffer_loc;
242  _batch_size = (std::min)(_batch_size, remaining());
243  _batch_size = internal::trimmed_length_safe_utf8((const char *)buf(), _batch_size);
244  auto stage1_is_ok = (error_code)simdjson::active_implementation->stage1(buf(), _batch_size, parser, true);
245  if (stage1_is_ok != simdjson::SUCCESS) {
246  return stage1_is_ok;
247  }
248  size_t last_index = internal::find_last_json_buf_idx(buf(), _batch_size, parser);
249  if (last_index == 0) {
250  if (parser.n_structural_indexes == 0) {
251  return EMPTY;
252  }
253  } else {
254  parser.n_structural_indexes = last_index + 1;
255  }
256  load_next_batch = false;
257  } // load_next_batch
258  error_code res = simdjson::active_implementation->stage2(buf(), remaining(), parser, next_json);
259  if (likely(res == simdjson::SUCCESS_AND_HAS_MORE)) {
260  n_parsed_docs++;
261  current_buffer_loc = parser.structural_indexes[next_json];
262  } else if (res == simdjson::SUCCESS) {
263  n_parsed_docs++;
264  if (remaining() > _batch_size) {
265  current_buffer_loc = parser.structural_indexes[next_json - 1];
266  next_json = 1;
267  load_next_batch = true;
268  res = simdjson::SUCCESS_AND_HAS_MORE;
269  }
270  }
271  return res;
272 }
273 #endif // SIMDJSON_THREADS_ENABLED
274 
275 } // namespace simdjson::dom
276 #endif // SIMDJSON_INLINE_DOCUMENT_STREAM_H
simdjson::dom::document_stream::iterator
An iterator through a forward-only stream of documents.
Definition: document_stream.h:22
simdjson::dom::document_stream::iterator::operator*
really_inline simdjson_result< element > operator*() noexcept
Get the current document (or error).
Definition: document_stream.h:130
simdjson::dom::document_stream::begin
really_inline iterator begin() noexcept
Start iterating the documents in the stream.
Definition: document_stream.h:118
simdjson::simdjson_result
The result of a simdjson operation that could fail.
Definition: error.h:166
simdjson::dom::document_stream::iterator::operator++
iterator & operator++() noexcept
Advance to the next document.
Definition: document_stream.h:136
simdjson::dom::parser
A persistent document parser.
Definition: document.h:620
simdjson::dom::document_stream::iterator::operator!=
really_inline bool operator!=(const iterator &other) const noexcept
Check if we're at the end yet.
Definition: document_stream.h:145
simdjson::dom::document_stream
A forward-only stream of documents.
Definition: document_stream.h:15
simdjson::dom::document::root
element root() const noexcept
Get the root element of this document as a JSON array.
Definition: document.h:191
simdjson::dom::document_stream::end
really_inline iterator end() noexcept
The end of the stream, for iterator comparison purposes.
Definition: document_stream.h:122