sylvia_iot_broker/models/
dldata_buffer.rs

1//! Traits and structs for device routes.
2
3use std::error::Error as StdError;
4
5use async_trait::async_trait;
6use chrono::{DateTime, Utc};
7
8/// The item content.
9#[derive(Debug, PartialEq)]
10pub struct DlDataBuffer {
11    pub data_id: String,
12    pub unit_id: String,
13    pub unit_code: String,
14    pub application_id: String,
15    pub application_code: String,
16    pub network_id: String,
17    pub network_addr: String,
18    pub device_id: String,
19    pub created_at: DateTime<Utc>,
20    pub expired_at: DateTime<Utc>,
21}
22
23/// The sort keys for the list operation.
24pub enum SortKey {
25    CreatedAt,
26    ExpiredAt,
27    ApplicationCode,
28}
29
30/// The sort condition for the list operation.
31pub struct SortCond {
32    pub key: SortKey,
33    pub asc: bool,
34}
35
36/// The list operation options.
37pub struct ListOptions<'a> {
38    /// The query conditions.
39    pub cond: &'a ListQueryCond<'a>,
40    /// The data offset.
41    pub offset: Option<u64>,
42    /// The maximum number to query.
43    pub limit: Option<u64>,
44    /// The sort conditions.
45    pub sort: Option<&'a [SortCond]>,
46    /// The maximum number items one time the `list()` returns.
47    ///
48    /// Use cursors until reaching `limit` or all data.
49    pub cursor_max: Option<u64>,
50}
51
52/// The query condition to get item(s).
53#[derive(Default)]
54pub struct QueryCond<'a> {
55    pub data_id: Option<&'a str>,
56    pub unit_id: Option<&'a str>,
57    pub application_id: Option<&'a str>,
58    pub network_id: Option<&'a str>,
59    pub network_addrs: Option<&'a Vec<&'a str>>,
60    pub device_id: Option<&'a str>,
61}
62
63/// The query condition for the list operation.
64#[derive(Default)]
65pub struct ListQueryCond<'a> {
66    /// To get downlink data buffers of the specified unit.
67    pub unit_id: Option<&'a str>,
68    /// To get downlink data buffers of the specified application.
69    pub application_id: Option<&'a str>,
70    /// To get device data of the specified network.
71    pub network_id: Option<&'a str>,
72    /// To get device data of the specified device.
73    pub device_id: Option<&'a str>,
74}
75
76/// Model operations.
77#[async_trait]
78pub trait DlDataBufferModel: Sync {
79    /// To create and initialize the table/collection.
80    async fn init(&self) -> Result<(), Box<dyn StdError>>;
81
82    /// To get item count for the query condition.
83    ///
84    /// **Note**: this may take a long time.
85    async fn count(&self, cond: &ListQueryCond) -> Result<u64, Box<dyn StdError>>;
86
87    /// To get item list. The maximum number of returned items will be controlled by the
88    /// `cursor_max` of the list option.
89    ///
90    /// For the first time, `cursor` MUST use `None`. If one cursor is returned, it means that
91    /// there are more items to get. Use the returned cursor to get more data items.
92    ///
93    /// **Note**: using cursors is recommended to prevent exhausting memory.
94    async fn list(
95        &self,
96        opts: &ListOptions,
97        cursor: Option<Box<dyn Cursor>>,
98    ) -> Result<(Vec<DlDataBuffer>, Option<Box<dyn Cursor>>), Box<dyn StdError>>;
99
100    /// To get an item.
101    async fn get(&self, data_id: &str) -> Result<Option<DlDataBuffer>, Box<dyn StdError>>;
102
103    /// To add an item.
104    async fn add(&self, data: &DlDataBuffer) -> Result<(), Box<dyn StdError>>;
105
106    /// To delete one or more items.
107    async fn del(&self, cond: &QueryCond) -> Result<(), Box<dyn StdError>>;
108}
109
110/// The operations for cursors.
111///
112/// All functions are private to let programs to pass them as arguments directly without any
113/// operation.
114#[async_trait]
115pub trait Cursor: Send {
116    async fn try_next(&mut self) -> Result<Option<DlDataBuffer>, Box<dyn StdError>>;
117
118    fn offset(&self) -> u64;
119}