simdjson  3.11.0
Ridiculously Fast JSON
document_stream.h
1 #ifndef SIMDJSON_DOCUMENT_STREAM_H
2 #define SIMDJSON_DOCUMENT_STREAM_H
3 
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/parser.h"
6 
7 #ifdef SIMDJSON_THREADS_ENABLED
8 #include <thread>
9 #include <mutex>
10 #include <condition_variable>
11 #endif
12 
13 namespace simdjson {
14 namespace dom {
15 
16 #ifdef SIMDJSON_THREADS_ENABLED
18 struct stage1_worker {
19  stage1_worker() noexcept = default;
20  stage1_worker(const stage1_worker&) = delete;
21  stage1_worker(stage1_worker&&) = delete;
22  stage1_worker operator=(const stage1_worker&) = delete;
23  ~stage1_worker();
28  void start_thread();
33  void run(document_stream * ds, dom::parser * stage1, size_t next_batch_start);
35  void finish();
36 
37 private:
38 
44  void stop_thread();
45 
46  std::thread thread{};
48  dom::parser * stage1_thread_parser{};
49  size_t _next_batch_start{};
50  document_stream * owner{};
55  bool has_work{false};
56  bool can_work{true};
57 
61  std::mutex locking_mutex{};
62  std::condition_variable cond_var{};
63 };
64 #endif
65 
73 public:
82  simdjson_inline document_stream() noexcept;
84  simdjson_inline document_stream(document_stream &&other) noexcept = default;
86  simdjson_inline document_stream &operator=(document_stream &&other) noexcept = default;
87 
88  simdjson_inline ~document_stream() noexcept;
92  inline size_t size_in_bytes() const noexcept;
111  inline size_t truncated_bytes() const noexcept;
115  class iterator {
116  public:
118  using reference = value_type;
119 
120  using difference_type = std::ptrdiff_t;
121 
122  using iterator_category = std::input_iterator_tag;
123 
127  simdjson_inline iterator() noexcept;
131  simdjson_inline reference operator*() noexcept;
135  inline iterator& operator++() noexcept;
140  simdjson_inline bool operator!=(const iterator &other) const noexcept;
156  simdjson_inline size_t current_index() const noexcept;
176  simdjson_inline std::string_view source() const noexcept;
177 
178  private:
179  simdjson_inline iterator(document_stream *s, bool finished) noexcept;
181  document_stream* stream;
183  bool finished;
184  friend class document_stream;
185  };
186 
190  simdjson_inline iterator begin() noexcept;
194  simdjson_inline iterator end() noexcept;
195 
196 private:
197 
198  document_stream &operator=(const document_stream &) = delete; // Disallow copying
199  document_stream(const document_stream &other) = delete; // Disallow copying
200 
210  simdjson_inline document_stream(
211  dom::parser &parser,
212  const uint8_t *buf,
213  size_t len,
214  size_t batch_size
215  ) noexcept;
216 
221  inline void start() noexcept;
222 
246  inline void next() noexcept;
247 
252  inline void load_batch() noexcept;
253 
255  inline size_t next_batch_start() const noexcept;
256 
258  inline error_code run_stage1(dom::parser &p, size_t batch_start) noexcept;
259 
260  dom::parser *parser;
261  const uint8_t *buf;
262  size_t len;
263  size_t batch_size;
265  error_code error;
266  size_t batch_start{0};
267  size_t doc_index{};
268 #ifdef SIMDJSON_THREADS_ENABLED
270  bool use_thread;
271 
272  inline void load_from_stage1_thread() noexcept;
273 
275  inline void start_stage1_thread() noexcept;
276 
278  inline void finish_stage1_thread() noexcept;
279 
281  error_code stage1_thread_error{UNINITIALIZED};
283  friend struct stage1_worker;
284  std::unique_ptr<stage1_worker> worker{new(std::nothrow) stage1_worker()};
289  dom::parser stage1_thread_parser{};
290 #endif // SIMDJSON_THREADS_ENABLED
291 
292  friend class dom::parser;
293  friend struct simdjson_result<dom::document_stream>;
294  friend struct internal::simdjson_result_base<dom::document_stream>;
295 
296 }; // class document_stream
297 
298 } // namespace dom
299 
300 template<>
301 struct simdjson_result<dom::document_stream> : public internal::simdjson_result_base<dom::document_stream> {
302 public:
303  simdjson_inline simdjson_result() noexcept;
304  simdjson_inline simdjson_result(error_code error) noexcept;
305  simdjson_inline simdjson_result(dom::document_stream &&value) noexcept;
306 
307 #if SIMDJSON_EXCEPTIONS
308  simdjson_inline dom::document_stream::iterator begin() noexcept(false);
309  simdjson_inline dom::document_stream::iterator end() noexcept(false);
310 #else // SIMDJSON_EXCEPTIONS
311 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
312  [[deprecated("parse_many() and load_many() may return errors. Use document_stream stream; error = parser.parse_many().get(doc); instead.")]]
313  simdjson_inline dom::document_stream::iterator begin() noexcept;
314  [[deprecated("parse_many() and load_many() may return errors. Use document_stream stream; error = parser.parse_many().get(doc); instead.")]]
315  simdjson_inline dom::document_stream::iterator end() noexcept;
316 #endif // SIMDJSON_DISABLE_DEPRECATED_API
317 #endif // SIMDJSON_EXCEPTIONS
318 }; // struct simdjson_result<dom::document_stream>
319 
320 } // namespace simdjson
321 
322 #endif // SIMDJSON_DOCUMENT_STREAM_H
An iterator through a forward-only stream of documents.
A forward-only stream of documents.
simdjson_inline document_stream & operator=(document_stream &&other) noexcept=default
Move one document_stream to another.
size_t size_in_bytes() const noexcept
Returns the input size in bytes.
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(document_stream &&other) noexcept=default
Move one document_stream to another.
simdjson_inline iterator end() noexcept
The end of the stream, for iterator comparison purposes.
simdjson_inline document_stream() noexcept
Construct an uninitialized document_stream.
A persistent document parser.
Definition: parser.h:30
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:32
The result of a simdjson operation that could fail.
Definition: error.h:215