simdjson  3.6.2
Ridiculously Fast JSON
document_stream.h
1 #ifndef SIMDJSON_GENERIC_ONDEMAND_DOCUMENT_STREAM_H
2 
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_DOCUMENT_STREAM_H
5 #include "simdjson/generic/ondemand/base.h"
6 #include "simdjson/generic/implementation_simdjson_result_base.h"
7 #include "simdjson/generic/ondemand/document.h"
8 #include "simdjson/generic/ondemand/parser.h"
9 #endif // SIMDJSON_CONDITIONAL_INCLUDE
10 
11 #ifdef SIMDJSON_THREADS_ENABLED
12 #include <thread>
13 #include <mutex>
14 #include <condition_variable>
15 #endif
16 
17 namespace simdjson {
18 namespace SIMDJSON_IMPLEMENTATION {
19 namespace ondemand {
20 
21 #ifdef SIMDJSON_THREADS_ENABLED
23 struct stage1_worker {
24  stage1_worker() noexcept = default;
25  stage1_worker(const stage1_worker&) = delete;
26  stage1_worker(stage1_worker&&) = delete;
27  stage1_worker operator=(const stage1_worker&) = delete;
28  ~stage1_worker();
33  void start_thread();
38  void run(document_stream * ds, parser * stage1, size_t next_batch_start);
40  void finish();
41 
42 private:
43 
49  void stop_thread();
50 
51  std::thread thread{};
53  ondemand::parser * stage1_thread_parser{};
54  size_t _next_batch_start{};
55  document_stream * owner{};
60  bool has_work{false};
61  bool can_work{true};
62 
66  std::mutex locking_mutex{};
67  std::condition_variable cond_var{};
68 
69  friend class document_stream;
70 };
71 #endif // SIMDJSON_THREADS_ENABLED
72 
80 public:
89  simdjson_inline document_stream() noexcept;
91  simdjson_inline document_stream(document_stream &&other) noexcept = default;
93  simdjson_inline document_stream &operator=(document_stream &&other) noexcept = default;
94 
95  simdjson_inline ~document_stream() noexcept;
96 
100  inline size_t size_in_bytes() const noexcept;
101 
120  inline size_t truncated_bytes() const noexcept;
121 
122  class iterator {
123  public:
125  using reference = value_type;
126 
127  using difference_type = std::ptrdiff_t;
128 
129  using iterator_category = std::input_iterator_tag;
130 
134  simdjson_inline iterator() noexcept;
138  simdjson_inline simdjson_result<ondemand::document_reference> operator*() noexcept;
142  inline iterator& operator++() noexcept;
147  simdjson_inline bool operator!=(const iterator &other) const noexcept;
163  simdjson_inline size_t current_index() const noexcept;
164 
184  simdjson_inline std::string_view source() const noexcept;
185 
189  inline error_code error() const noexcept;
190 
191  private:
192  simdjson_inline iterator(document_stream *s, bool finished) noexcept;
194  document_stream* stream;
196  bool finished;
197 
198  friend class document;
199  friend class document_stream;
200  friend class json_iterator;
201  };
202 
206  simdjson_inline iterator begin() noexcept;
210  simdjson_inline iterator end() noexcept;
211 
212 private:
213 
214  document_stream &operator=(const document_stream &) = delete; // Disallow copying
215  document_stream(const document_stream &other) = delete; // Disallow copying
216 
226  simdjson_inline document_stream(
227  ondemand::parser &parser,
228  const uint8_t *buf,
229  size_t len,
230  size_t batch_size,
231  bool allow_comma_separated
232  ) noexcept;
233 
238  inline void start() noexcept;
239 
263  inline void next() noexcept;
264 
266  inline void next_document() noexcept;
267 
269  inline size_t next_batch_start() const noexcept;
270 
272  inline error_code run_stage1(ondemand::parser &p, size_t batch_start) noexcept;
273 
274  // Fields
275  ondemand::parser *parser;
276  const uint8_t *buf;
277  size_t len;
278  size_t batch_size;
279  bool allow_comma_separated;
285  document doc{};
287  error_code error;
288  size_t batch_start{0};
289  size_t doc_index{};
290 
291  #ifdef SIMDJSON_THREADS_ENABLED
293  bool use_thread;
294 
295  inline void load_from_stage1_thread() noexcept;
296 
298  inline void start_stage1_thread() noexcept;
299 
301  inline void finish_stage1_thread() noexcept;
302 
304  error_code stage1_thread_error{UNINITIALIZED};
306  std::unique_ptr<stage1_worker> worker{new(std::nothrow) stage1_worker()};
311  ondemand::parser stage1_thread_parser{};
312 
313  friend struct stage1_worker;
314  #endif // SIMDJSON_THREADS_ENABLED
315 
316  friend class parser;
317  friend class document;
318  friend class json_iterator;
319  friend struct simdjson_result<ondemand::document_stream>;
320  friend struct internal::simdjson_result_base<ondemand::document_stream>;
321 }; // document_stream
322 
323 } // namespace ondemand
324 } // namespace SIMDJSON_IMPLEMENTATION
325 } // namespace simdjson
326 
327 namespace simdjson {
328 template<>
329 struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_stream> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::document_stream> {
330 public:
331  simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::document_stream &&value) noexcept;
332  simdjson_inline simdjson_result(error_code error) noexcept;
333  simdjson_inline simdjson_result() noexcept = default;
334 };
335 
336 } // namespace simdjson
337 
338 #endif // SIMDJSON_GENERIC_ONDEMAND_DOCUMENT_STREAM_H
simdjson_inline iterator end() noexcept
The end of the stream, for iterator comparison purposes.
simdjson_inline document_stream(document_stream &&other) noexcept=default
Move one document_stream to another.
size_t size_in_bytes() const noexcept
Returns the input size in bytes.
simdjson_inline document_stream & operator=(document_stream &&other) noexcept=default
Move one document_stream to another.
size_t truncated_bytes() const noexcept
After iterating through the stream, this method returns the number of bytes that were not parsed at t...
simdjson_inline iterator begin() noexcept
Start iterating the documents in the stream.
simdjson_inline document_stream() noexcept
Construct an uninitialized document_stream.
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
error_code
All possible errors returned by simdjson.
Definition: error.h:19
@ UNINITIALIZED
unknown error, or uninitialized document
Definition: error.h:31
The result of a simdjson operation that could fail.
Definition: error.h:214