2
This file is part of TON Blockchain Library.
4
TON Blockchain Library is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Lesser General Public License as published by
6
the Free Software Foundation, either version 2 of the License, or
7
(at your option) any later version.
9
TON Blockchain Library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public License
15
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
17
Copyright 2017-2020 Telegram Systems LLP
19
#include "StreamToFileActor.h"
22
StreamToFileActor::StreamToFileActor(StreamReader reader, FileFd fd, FileSyncState::Writer sync_state, Options options)
23
: reader_(std::move(reader)), fd_(std::move(fd)), sync_state_(std::move(sync_state)) {
25
void StreamToFileActor::set_callback(td::unique_ptr<Callback> callback) {
26
callback_ = std::move(callback);
27
callback_->on_sync_state_changed();
30
Result<bool> StreamToFileActor::is_closed() {
31
if (!reader_.is_writer_closed()) {
34
return reader_.writer_status().clone();
37
Status StreamToFileActor::do_flush_once() {
38
auto size = reader_.reader_size();
39
size_t total_written = 0;
40
while (total_written < size) {
41
auto io_slices = reader_.prepare_readv();
42
TRY_RESULT(written, fd_.writev(io_slices));
43
reader_.confirm_read(written);
44
flushed_size_ += written;
45
total_written += written;
50
Status StreamToFileActor::do_sync() {
51
if (flushed_size_ == synced_size_) {
54
TRY_STATUS(fd_.sync());
55
synced_size_ = flushed_size_;
59
void StreamToFileActor::schedule_sync() {
60
if (synced_size_ == flushed_size_) {
63
if (sync_state_.get_requested_synced_size() > synced_size_) {
64
sync_at_.relax(Timestamp::in(options_.immediate_sync_delay));
66
sync_at_.relax(Timestamp::in(options_.lazy_sync_delay));
70
Result<bool> StreamToFileActor::do_loop() {
71
// We must first check if writer is closed and then drain all data from reader
72
// Otherwise there will be a race and some of data could be lost.
73
// Also it could be useful to check error and stop immediately.
74
TRY_RESULT(is_closed, is_closed());
76
// Flush all data that is awailable on the at the beginning of loop
77
TRY_STATUS(do_flush_once());
79
if ((sync_at_ && sync_at_.is_in_past()) || is_closed) {
80
TRY_STATUS(do_sync());
84
bool need_update = sync_state_.set_synced_size(synced_size_) | sync_state_.set_flushed_size(flushed_size_);
85
if (need_update && callback_) {
86
callback_->on_sync_state_changed();
89
if (reader_.reader_size() == 0 && is_closed) {
97
void StreamToFileActor::start_up() {
101
void StreamToFileActor::loop() {
102
auto r_is_closed = do_loop();
103
if (r_is_closed.is_error()) {
104
reader_.close_reader(r_is_closed.move_as_error());
106
} else if (r_is_closed.ok()) {
107
reader_.close_reader(Status::OK());
110
alarm_timestamp() = sync_at_;