simdjson 4.0.7
Ridiculously Fast JSON
Loading...
Searching...
No Matches
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
17namespace simdjson {
18namespace SIMDJSON_IMPLEMENTATION {
19namespace ondemand {
20
21#ifdef SIMDJSON_THREADS_ENABLED
23struct stage1_worker {
33 void start_thread();
38 void run(document_stream * ds, parser * stage1, size_t next_batch_start);
40 void finish();
41
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
80public:
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
101
121
123 public:
126 using pointer = void;
127 using difference_type = std::ptrdiff_t;
128 using iterator_category = std::input_iterator_tag;
129
133 simdjson_inline iterator() noexcept;
134 simdjson_inline iterator(const iterator &other) noexcept = default;
138 simdjson_inline reference operator*() noexcept;
142 inline iterator& operator++() noexcept;
147 simdjson_inline bool operator!=(const iterator &other) const noexcept;
148 simdjson_inline bool operator==(const iterator &other) const noexcept;
164 simdjson_inline size_t current_index() const noexcept;
165
185 simdjson_inline std::string_view source() const noexcept;
186
190 inline error_code error() const noexcept;
191
195 inline bool at_end() const noexcept;
196
197 private:
198 simdjson_inline iterator(document_stream *s, bool finished) noexcept;
200 document_stream* stream;
202 bool finished;
203
204 friend class document;
205 friend class document_stream;
206 friend class json_iterator;
207 };
209
213 simdjson_inline iterator begin() noexcept;
217 simdjson_inline iterator end() noexcept;
218
219private:
220
221 document_stream &operator=(const document_stream &) = delete; // Disallow copying
222 document_stream(const document_stream &other) = delete; // Disallow copying
223
233 simdjson_inline document_stream(
234 ondemand::parser &parser,
235 const uint8_t *buf,
236 size_t len,
237 size_t batch_size,
238 bool allow_comma_separated
239 ) noexcept;
240
245 inline void start() noexcept;
246
270 inline void next() noexcept;
271
273 inline void next_document() noexcept;
274
276 inline size_t next_batch_start() const noexcept;
277
279 inline error_code run_stage1(ondemand::parser &p, size_t batch_start) noexcept;
280
281 // Fields
282 ondemand::parser *parser;
283 const uint8_t *buf;
284 size_t len;
285 size_t batch_size;
286 bool allow_comma_separated;
292 document doc{};
294 error_code error;
295 size_t batch_start{0};
296 size_t doc_index{};
297
298 #ifdef SIMDJSON_THREADS_ENABLED
300 bool use_thread;
301
302 inline void load_from_stage1_thread() noexcept;
303
305 inline void start_stage1_thread() noexcept;
306
308 inline void finish_stage1_thread() noexcept;
309
311 error_code stage1_thread_error{UNINITIALIZED};
313 std::unique_ptr<stage1_worker> worker{new(std::nothrow) stage1_worker()};
318 ondemand::parser stage1_thread_parser{};
319
320 friend struct stage1_worker;
321 #endif // SIMDJSON_THREADS_ENABLED
322
323 friend class parser;
324 friend class document;
325 friend class json_iterator;
326 friend struct simdjson_result<ondemand::document_stream>;
327 friend struct simdjson::internal::simdjson_result_base<ondemand::document_stream>;
328}; // document_stream
329
330} // namespace ondemand
331} // namespace SIMDJSON_IMPLEMENTATION
332} // namespace simdjson
333
334namespace simdjson {
335template<>
336struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_stream> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::document_stream> {
337public:
338 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::document_stream &&value) noexcept;
339 simdjson_inline simdjson_result(error_code error) noexcept;
340 simdjson_inline simdjson_result() noexcept = default;
341};
342
343} // namespace simdjson
344
345#endif // SIMDJSON_GENERIC_ONDEMAND_DOCUMENT_STREAM_H
simdjson_inline array() noexcept=default
Create a new invalid array.
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.
simdjson_unused simdjson_inline bool operator==(const raw_json_string &a, std::string_view c) noexcept
Comparisons between raw_json_string and std::string_view instances are potentially unsafe: the user i...
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