|
1 | 1 | /* |
2 | | - * Copyright (C) 2023 HERE Europe B.V. |
| 2 | + * Copyright (C) 2023-2024 HERE Europe B.V. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
19 | 19 |
|
20 | 20 | #include "AsyncJsonStream.h" |
21 | 21 |
|
22 | | -#include <cstring> |
23 | | - |
24 | 22 | namespace olp { |
25 | 23 | namespace dataservice { |
26 | 24 | namespace read { |
27 | 25 | namespace repository { |
28 | 26 |
|
29 | | -RapidJsonByteStream::Ch RapidJsonByteStream::Peek() const { |
30 | | - std::unique_lock<std::mutex> lock(mutex_); |
31 | | - cv_.wait(lock, [=]() { return !Empty(); }); |
32 | | - return buffer_[count_]; |
| 27 | +RapidJsonByteStream::Ch RapidJsonByteStream::Peek() { |
| 28 | + if (ReadEmpty()) { |
| 29 | + std::unique_lock<std::mutex> lock(mutex_); |
| 30 | + cv_.wait(lock, [&]() { return !WriteEmpty(); }); |
| 31 | + std::swap(read_buffer_, write_buffer_); |
| 32 | + write_buffer_.clear(); |
| 33 | + count_ = 0; |
| 34 | + } |
| 35 | + return read_buffer_[count_]; |
33 | 36 | } |
34 | 37 |
|
35 | 38 | RapidJsonByteStream::Ch RapidJsonByteStream::Take() { |
36 | | - std::unique_lock<std::mutex> lock(mutex_); |
37 | | - cv_.wait(lock, [=]() { return !Empty(); }); |
38 | | - return buffer_[count_++]; |
| 39 | + if (ReadEmpty()) { |
| 40 | + std::unique_lock<std::mutex> lock(mutex_); |
| 41 | + cv_.wait(lock, [&]() { return !WriteEmpty(); }); |
| 42 | + std::swap(read_buffer_, write_buffer_); |
| 43 | + } |
| 44 | + full_count_++; |
| 45 | + return read_buffer_[count_++]; |
39 | 46 | } |
40 | 47 |
|
41 | | -size_t RapidJsonByteStream::Tell() const { return count_; } |
| 48 | +size_t RapidJsonByteStream::Tell() const { return full_count_; } |
42 | 49 |
|
43 | 50 | // Not implemented |
44 | 51 | char* RapidJsonByteStream::PutBegin() { return 0; } |
45 | 52 | void RapidJsonByteStream::Put(char) {} |
46 | 53 | void RapidJsonByteStream::Flush() {} |
47 | 54 | size_t RapidJsonByteStream::PutEnd(char*) { return 0; } |
48 | 55 |
|
49 | | -bool RapidJsonByteStream::Empty() const { return count_ == buffer_.size(); } |
| 56 | +bool RapidJsonByteStream::ReadEmpty() const { |
| 57 | + return count_ == read_buffer_.size(); |
| 58 | +} |
| 59 | +bool RapidJsonByteStream::WriteEmpty() const { return write_buffer_.empty(); } |
50 | 60 |
|
51 | 61 | void RapidJsonByteStream::AppendContent(const char* content, size_t length) { |
52 | 62 | std::unique_lock<std::mutex> lock(mutex_); |
53 | 63 |
|
54 | | - if (Empty()) { |
55 | | - buffer_.resize(length); |
56 | | - std::memcpy(buffer_.data(), content, length); |
57 | | - count_ = 0; |
58 | | - } else { |
59 | | - const auto buffer_size = buffer_.size(); |
60 | | - buffer_.resize(buffer_size + length); |
61 | | - std::memcpy(buffer_.data() + buffer_size, content, length); |
62 | | - } |
| 64 | + const auto buffer_size = write_buffer_.size(); |
| 65 | + write_buffer_.reserve(buffer_size + length); |
| 66 | + write_buffer_.insert(write_buffer_.end(), content, content + length); |
63 | 67 |
|
64 | 68 | cv_.notify_one(); |
65 | 69 | } |
@@ -97,7 +101,7 @@ void AsyncJsonStream::CloseStream(boost::optional<client::ApiError> error) { |
97 | 101 | return; |
98 | 102 | } |
99 | 103 | current_stream_->AppendContent("\0", 1); |
100 | | - error_ = error; |
| 104 | + error_ = std::move(error); |
101 | 105 | closed_ = true; |
102 | 106 | } |
103 | 107 |
|
|
0 commit comments