qiniu_upload_manager/data_partition_provider/
mod.rs

1use auto_impl::auto_impl;
2use dyn_clonable::clonable;
3use qiniu_apis::{http::Extensions, http_client::ResponseError};
4use std::{
5    fmt::Debug,
6    num::NonZeroU64,
7    ops::{Deref, DerefMut},
8    time::Duration,
9};
10
11/// 分片大小获取接口
12#[clonable]
13#[auto_impl(&, &mut, Box, Rc, Arc)]
14pub trait DataPartitionProvider: Clone + Debug + Sync + Send {
15    /// 获取分片大小
16    fn part_size(&self) -> PartSize;
17    /// 反馈分片大小结果
18    fn feedback(&self, feedback: DataPartitionProviderFeedback<'_>);
19}
20
21/// 分片大小
22#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
23pub struct PartSize(NonZeroU64);
24
25impl PartSize {
26    /// 创建分片大小
27    ///
28    /// 如果传入 `0` 将返回 [`None`]。
29    pub fn new(part_size: u64) -> Option<Self> {
30        NonZeroU64::new(part_size).map(Self)
31    }
32
33    /// 创建分片大小
34    ///
35    /// 提供 [`NonZeroU64`] 作为并发数类型。
36    #[inline]
37    pub const fn new_with_non_zero_u64(concurrency: NonZeroU64) -> Self {
38        Self(concurrency)
39    }
40
41    /// 获取并发数
42    ///
43    /// 返回 [`NonZeroU64`] 作为并发数类型。
44    #[inline]
45    pub fn as_non_zero_u64(&self) -> NonZeroU64 {
46        self.0
47    }
48
49    /// 获取并发数
50    #[inline]
51    pub fn as_u64(&self) -> u64 {
52        self.as_non_zero_u64().get()
53    }
54}
55
56impl Default for PartSize {
57    #[inline]
58    fn default() -> Self {
59        Self(
60            #[allow(unsafe_code)]
61            unsafe {
62                NonZeroU64::new_unchecked(1 << 22)
63            },
64        )
65    }
66}
67
68impl From<NonZeroU64> for PartSize {
69    #[inline]
70    fn from(size: NonZeroU64) -> Self {
71        Self(size)
72    }
73}
74
75impl From<PartSize> for NonZeroU64 {
76    #[inline]
77    fn from(size: PartSize) -> Self {
78        size.as_non_zero_u64()
79    }
80}
81
82impl From<PartSize> for u64 {
83    #[inline]
84    fn from(size: PartSize) -> Self {
85        size.as_u64()
86    }
87}
88
89impl Deref for PartSize {
90    type Target = NonZeroU64;
91
92    #[inline]
93    fn deref(&self) -> &Self::Target {
94        &self.0
95    }
96}
97
98impl DerefMut for PartSize {
99    #[inline]
100    fn deref_mut(&mut self) -> &mut Self::Target {
101        &mut self.0
102    }
103}
104
105/// 分片大小提供者反馈
106///
107/// 反馈给提供者分片的效果,包含对象大小,花费时间,以及错误信息。
108#[derive(Debug, Clone)]
109pub struct DataPartitionProviderFeedback<'f> {
110    part_size: PartSize,
111    elapsed: Duration,
112    extensions: &'f Extensions,
113    error: Option<&'f ResponseError>,
114}
115
116impl<'f> DataPartitionProviderFeedback<'f> {
117    /// 创建分片大小提供者反馈构建器
118    #[inline]
119    pub fn builder(
120        part_size: PartSize,
121        elapsed: Duration,
122        extensions: &'f Extensions,
123    ) -> DataPartitionProviderFeedbackBuilder<'f> {
124        DataPartitionProviderFeedbackBuilder::new(part_size, elapsed, extensions)
125    }
126
127    /// 获取分片大小
128    #[inline]
129    pub fn part_size(&self) -> PartSize {
130        self.part_size
131    }
132
133    /// 获取花费时间
134    #[inline]
135    pub fn elapsed(&self) -> Duration {
136        self.elapsed
137    }
138
139    /// 获取扩展信息
140    #[inline]
141    pub fn extensions(&self) -> &Extensions {
142        self.extensions
143    }
144
145    /// 获取错误信息
146    #[inline]
147    pub fn error(&self) -> Option<&'f ResponseError> {
148        self.error
149    }
150}
151
152/// 分片大小提供者反馈构建器
153#[derive(Debug, Clone)]
154pub struct DataPartitionProviderFeedbackBuilder<'f>(DataPartitionProviderFeedback<'f>);
155
156impl<'f> DataPartitionProviderFeedbackBuilder<'f> {
157    /// 创建分片大小提供者反馈构建器
158    #[inline]
159    pub fn new(part_size: PartSize, elapsed: Duration, extensions: &'f Extensions) -> Self {
160        Self(DataPartitionProviderFeedback {
161            part_size,
162            elapsed,
163            extensions,
164            error: None,
165        })
166    }
167
168    /// 设置错误信息
169    #[inline]
170    pub fn error(&mut self, err: &'f ResponseError) -> &mut Self {
171        self.0.error = Some(err);
172        self
173    }
174
175    /// 构建分片大小提供者反馈
176    #[inline]
177    pub fn build(&self) -> DataPartitionProviderFeedback<'f> {
178        self.0.to_owned()
179    }
180}
181
182mod fixed;
183pub use fixed::FixedDataPartitionProvider;
184
185mod limited;
186pub use limited::LimitedDataPartitionProvider;
187
188mod multiply;
189pub use multiply::MultiplyDataPartitionProvider;