Ton

Форк
0
/
StreamToFileActor.cpp 
112 строк · 3.3 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

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.
8

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.
13

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/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19
#include "StreamToFileActor.h"
20

21
namespace td {
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)) {
24
}
25
void StreamToFileActor::set_callback(td::unique_ptr<Callback> callback) {
26
  callback_ = std::move(callback);
27
  callback_->on_sync_state_changed();
28
}
29

30
Result<bool> StreamToFileActor::is_closed() {
31
  if (!reader_.is_writer_closed()) {
32
    return false;
33
  }
34
  return reader_.writer_status().clone();
35
}
36

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;
46
  }
47
  return Status::OK();
48
}
49

50
Status StreamToFileActor::do_sync() {
51
  if (flushed_size_ == synced_size_) {
52
    return Status::OK();
53
  }
54
  TRY_STATUS(fd_.sync());
55
  synced_size_ = flushed_size_;
56
  return Status::OK();
57
}
58

59
void StreamToFileActor::schedule_sync() {
60
  if (synced_size_ == flushed_size_) {
61
    return;
62
  }
63
  if (sync_state_.get_requested_synced_size() > synced_size_) {
64
    sync_at_.relax(Timestamp::in(options_.immediate_sync_delay));
65
  } else {
66
    sync_at_.relax(Timestamp::in(options_.lazy_sync_delay));
67
  }
68
}
69

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());
75

76
  // Flush all data that is awailable on the at the beginning of loop
77
  TRY_STATUS(do_flush_once());
78

79
  if ((sync_at_ && sync_at_.is_in_past()) || is_closed) {
80
    TRY_STATUS(do_sync());
81
    sync_at_ = {};
82
  }
83

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();
87
  }
88

89
  if (reader_.reader_size() == 0 && is_closed) {
90
    return true;
91
  }
92

93
  schedule_sync();
94
  return false;
95
}
96

97
void StreamToFileActor::start_up() {
98
  schedule_sync();
99
}
100

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());
105
    return stop();
106
  } else if (r_is_closed.ok()) {
107
    reader_.close_reader(Status::OK());
108
    return stop();
109
  }
110
  alarm_timestamp() = sync_at_;
111
}
112
}  // namespace td
113

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.