futu_cache/
history_kline_quota.rs1use futu_domain_qot_history_kline::{
2 HistoryKlineCloudItem, HistoryKlineCloudMergeOutcome, HistoryKlineQuotaError,
3 HistoryKlineQuotaKey, HistoryKlineQuotaReservation, HistoryKlineQuotaSnapshot,
4 HistoryKlineQuotaState,
5};
6use parking_lot::Mutex;
7
8pub const DEFAULT_HISTORY_KLINE_STOCK_LIMIT: i32 = 100;
11
12pub struct HistoryKlineQuotaCache {
13 state: Mutex<HistoryKlineQuotaState>,
14}
15
16impl HistoryKlineQuotaCache {
17 #[must_use]
18 pub fn new(stock_limit: i32, cloud_ready: bool) -> Self {
19 Self {
20 state: Mutex::new(HistoryKlineQuotaState::new(
21 normalize_limit(stock_limit),
22 cloud_ready,
23 )),
24 }
25 }
26
27 pub fn set_limits(&self, stock_limit: i32) -> usize {
28 self.state
29 .lock()
30 .set_stock_limit(normalize_limit(stock_limit))
31 }
32
33 pub fn set_cloud_ready(&self, ready: bool) {
34 self.state.lock().set_cloud_ready(ready);
35 }
36
37 #[must_use]
38 pub fn is_cloud_ready(&self) -> bool {
39 self.state.lock().is_cloud_ready()
40 }
41
42 pub fn reserve(
43 &self,
44 key: HistoryKlineQuotaKey,
45 now: i64,
46 ) -> Result<HistoryKlineQuotaReservation, HistoryKlineQuotaError> {
47 self.state.lock().reserve(key, now)
48 }
49
50 pub fn restore(&self, reservation: HistoryKlineQuotaReservation) {
51 self.state.lock().restore(reservation);
52 }
53
54 pub fn merge_cloud(
55 &self,
56 items: &[HistoryKlineCloudItem],
57 now: i64,
58 ) -> HistoryKlineCloudMergeOutcome {
59 self.state.lock().merge_cloud(items, now)
60 }
61
62 #[must_use]
63 pub fn snapshot(&self, now: i64, include_details: bool) -> HistoryKlineQuotaSnapshot {
64 self.state.lock().snapshot(now, include_details)
65 }
66
67 #[must_use]
68 pub fn upload_snapshot(&self) -> Vec<HistoryKlineCloudItem> {
69 self.state.lock().upload_snapshot()
70 }
71
72 #[must_use]
73 pub fn request_time(&self, key: HistoryKlineQuotaKey) -> Option<i64> {
74 self.state.lock().request_time(key)
75 }
76}
77
78impl Default for HistoryKlineQuotaCache {
79 fn default() -> Self {
80 Self::new(DEFAULT_HISTORY_KLINE_STOCK_LIMIT, false)
81 }
82}
83
84fn normalize_limit(limit: i32) -> u32 {
85 if limit <= 0 { 0 } else { limit as u32 }
86}