2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
#include "td/telegram/files/FileBitmask.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
struct Part {
|
|
|
|
int id;
|
|
|
|
int64 offset;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PartsManager {
|
|
|
|
public:
|
2018-01-30 13:33:02 +01:00
|
|
|
Status init(int64 size, int64 expected_size, bool is_size_final, size_t part_size,
|
2019-07-31 17:04:38 +02:00
|
|
|
const std::vector<int> &ready_parts, bool use_part_count_limit, bool is_upload) TD_WARN_UNUSED_RESULT;
|
2019-02-26 15:14:07 +01:00
|
|
|
bool may_finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
bool ready();
|
|
|
|
bool unchecked_ready();
|
|
|
|
Status finish() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
// returns empty part if nothing to return
|
|
|
|
Result<Part> start_part() TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_part_ok(int32 id, size_t part_size, size_t actual_size) TD_WARN_UNUSED_RESULT;
|
|
|
|
void on_part_failed(int32 id);
|
2018-01-30 13:33:02 +01:00
|
|
|
Status set_known_prefix(size_t size, bool is_ready);
|
2018-12-31 20:04:05 +01:00
|
|
|
void set_need_check();
|
|
|
|
void set_checked_prefix_size(int64 size);
|
2020-08-25 17:58:37 +02:00
|
|
|
int32 set_streaming_offset(int64 offset, int64 limit);
|
2019-02-18 20:08:05 +01:00
|
|
|
void set_streaming_limit(int64 limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int64 get_checked_prefix_size() const;
|
|
|
|
int64 get_unchecked_ready_prefix_size();
|
|
|
|
int64 get_size() const;
|
|
|
|
int64 get_size_or_zero() const;
|
|
|
|
int64 get_expected_size() const;
|
2019-02-18 20:08:05 +01:00
|
|
|
int64 get_estimated_extra() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 get_ready_size() const;
|
|
|
|
size_t get_part_size() const;
|
|
|
|
int32 get_part_count() const;
|
2018-05-19 23:56:40 +02:00
|
|
|
int32 get_unchecked_ready_prefix_count();
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 get_ready_prefix_count();
|
2019-07-30 20:27:39 +02:00
|
|
|
int64 get_streaming_offset() const;
|
2018-12-27 19:06:30 +01:00
|
|
|
string get_bitmask();
|
2020-08-25 15:32:22 +02:00
|
|
|
int32 get_pending_count() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2020-07-03 16:31:06 +02:00
|
|
|
static constexpr int MAX_PART_COUNT = 4000;
|
2022-05-11 16:17:20 +02:00
|
|
|
static constexpr int MAX_PART_COUNT_PREMIUM = 8000;
|
|
|
|
static constexpr size_t MAX_PART_SIZE = 512 << 10;
|
|
|
|
static constexpr int64 MAX_FILE_SIZE = static_cast<int64>(MAX_PART_SIZE) * MAX_PART_COUNT_PREMIUM;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class PartStatus : int32 { Empty, Pending, Ready };
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-31 17:04:38 +02:00
|
|
|
bool is_upload_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
bool need_check_{false};
|
|
|
|
int64 checked_prefix_size_{0};
|
|
|
|
|
|
|
|
bool known_prefix_flag_{false};
|
2021-11-11 15:39:09 +01:00
|
|
|
int64 known_prefix_size_{0};
|
|
|
|
|
|
|
|
int64 size_{0};
|
|
|
|
int64 expected_size_{0};
|
|
|
|
int64 min_size_{0};
|
|
|
|
int64 max_size_{0};
|
|
|
|
bool unknown_size_flag_{false};
|
|
|
|
int64 ready_size_{0};
|
|
|
|
int64 streaming_ready_size_{0};
|
|
|
|
|
|
|
|
size_t part_size_{0};
|
|
|
|
int part_count_{0};
|
|
|
|
int pending_count_{0};
|
|
|
|
int first_empty_part_{0};
|
|
|
|
int first_not_ready_part_{0};
|
2018-11-11 12:38:04 +01:00
|
|
|
int64 streaming_offset_{0};
|
2019-02-18 20:08:05 +01:00
|
|
|
int64 streaming_limit_{0};
|
2021-11-11 15:39:09 +01:00
|
|
|
int first_streaming_empty_part_{0};
|
|
|
|
int first_streaming_not_ready_part_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<PartStatus> part_status_;
|
2018-11-11 12:38:04 +01:00
|
|
|
Bitmask bitmask_;
|
2021-11-11 15:39:09 +01:00
|
|
|
bool use_part_count_limit_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-31 15:48:59 +02:00
|
|
|
Status init_common(const vector<int> &ready_parts);
|
2018-12-31 20:04:05 +01:00
|
|
|
Status init_known_prefix(int64 known_prefix, size_t part_size,
|
|
|
|
const std::vector<int> &ready_parts) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status init_no_size(size_t part_size, const std::vector<int> &ready_parts) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
static Part get_empty_part();
|
|
|
|
|
2019-02-26 15:14:07 +01:00
|
|
|
Part get_part(int id) const;
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_part_start(int32 id);
|
|
|
|
void update_first_empty_part();
|
|
|
|
void update_first_not_ready_part();
|
2019-02-26 15:14:07 +01:00
|
|
|
|
|
|
|
bool is_streaming_limit_reached();
|
|
|
|
bool is_part_in_streaming_limit(int part_i) const;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|