1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use_prelude!();

use std::sync::Arc;

use crate::error::{DittoError, ErrorKind};

/// A handle for a Ditto Subscription to documents in a collection matching a
/// query.
///
/// When the Subscription is dropped, it will automatically trigger a clean up
/// of itself.
pub struct Subscription {
    pub(super) ditto: Arc<ffi_sdk::BoxedDitto>,
    pub(super) collection_name: char_p::Box,
    pub(super) query: char_p::Box,
    pub(super) query_args: Option<Vec<u8>>,
}

impl Subscription {
    pub fn new(
        ditto: Arc<ffi_sdk::BoxedDitto>,
        collection_name: char_p::Box,
        query: &str,
        query_args: Option<Vec<u8>>,
    ) -> Self {
        let query = char_p::new(query);
        unsafe {
            let code = ffi_sdk::ditto_add_subscription(
                &ditto,
                collection_name.as_ref(),
                query.as_ref(),
                query_args.as_ref().map(|qa| (&qa[..]).into()),
            );
            if code != 0 {
                ::log::error!(
                    "Error adding subscription for collection {} query {}. Error: {}",
                    &collection_name,
                    &query,
                    DittoError::from_ffi(ErrorKind::InvalidInput)
                );
            }
        };
        Subscription {
            ditto,
            collection_name,
            query,
            query_args,
        }
    }
}

impl Drop for Subscription {
    fn drop(&mut self) {
        ::log::debug!(
            "Dropping subscription for collection {} query {}",
            &self.collection_name,
            &self.query
        );
        unsafe {
            let code = ffi_sdk::ditto_remove_subscription(
                &self.ditto,
                self.collection_name.as_ref(),
                self.query.as_ref(),
                self.query_args.as_ref().map(|qa| (&qa[..]).into()),
            );
            if code != 0 {
                ::log::error!(
                    "Error removing subscription for collection {} query {}. Error: {}",
                    &self.collection_name,
                    &self.query,
                    DittoError::from_ffi(ErrorKind::InvalidInput)
                );
            }
        }
    }
}