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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::cassandra::consistency::Consistency;
use crate::cassandra::custom_payload::CustomPayload;
use crate::cassandra::error::*;
use crate::cassandra::future::CassFuture;
use crate::cassandra::policy::retry::RetryPolicy;
use crate::cassandra::statement::Statement;
use crate::cassandra::util::{Protected, ProtectedInner, ProtectedWithSession};
use crate::cassandra_sys::cass_session_execute_batch;
use crate::{CassResult, Session};

use crate::cassandra_sys::cass_batch_add_statement;
use crate::cassandra_sys::cass_batch_free;
use crate::cassandra_sys::cass_batch_new;
use crate::cassandra_sys::cass_batch_set_consistency;
use crate::cassandra_sys::cass_batch_set_custom_payload;
use crate::cassandra_sys::cass_batch_set_retry_policy;
use crate::cassandra_sys::cass_batch_set_serial_consistency;
use crate::cassandra_sys::cass_batch_set_timestamp;
use crate::cassandra_sys::CassBatch as _Batch;
use crate::cassandra_sys::CassBatchType_;

#[derive(Debug)]
struct BatchInner(*mut _Batch);

/// A group of statements that are executed as a single batch.
/// <b>Note:</b> Batches are not supported by the binary protocol version 1.
#[derive(Debug)]
pub struct Batch(BatchInner, Session);

// The underlying C type has no thread-local state, and forbids only concurrent
// mutation/free: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for BatchInner {}
unsafe impl Sync for BatchInner {}

impl ProtectedInner<*mut _Batch> for BatchInner {
    #[inline(always)]
    fn inner(&self) -> *mut _Batch {
        self.0
    }
}

impl Protected<*mut _Batch> for BatchInner {
    #[inline(always)]
    fn build(inner: *mut _Batch) -> Self {
        if inner.is_null() {
            panic!("Unexpected null pointer")
        };
        Self(inner)
    }
}

impl ProtectedInner<*mut _Batch> for Batch {
    #[inline(always)]
    fn inner(&self) -> *mut _Batch {
        self.0.inner()
    }
}

impl ProtectedWithSession<*mut _Batch> for Batch {
    #[inline(always)]
    fn build(inner: *mut _Batch, session: Session) -> Self {
        Self(BatchInner::build(inner), session)
    }

    #[inline(always)]
    fn session(&self) -> &Session {
        &self.1
    }
}

impl Drop for BatchInner {
    /// Frees a batch instance. Batches can be immediately freed after being
    /// executed.
    fn drop(&mut self) {
        unsafe { cass_batch_free(self.0) }
    }
}

impl Batch {
    /// Creates a new batch statement with batch type.
    pub(crate) fn new(batch_type: BatchType, session: Session) -> Batch {
        unsafe { Batch(BatchInner(cass_batch_new(batch_type.inner())), session) }
    }

    /// Returns the session of which this batch is bound to.
    pub fn session(&self) -> &Session {
        ProtectedWithSession::session(self)
    }

    /// Executes this batch.
    pub async fn execute(self) -> Result<CassResult> {
        let (batch, session) = (self.0, self.1);
        let execute_future = {
            let execute_batch =
                unsafe { cass_session_execute_batch(session.inner(), batch.inner()) };
            CassFuture::build(session, execute_batch)
        };
        execute_future.await
    }

    /// Sets the batch's consistency level
    pub fn set_consistency(&mut self, consistency: Consistency) -> Result<&mut Self> {
        unsafe { cass_batch_set_consistency(self.inner(), consistency.inner()).to_result(self) }
    }

    /// Sets the batch's serial consistency level.
    ///
    /// <b>Default:</b> Not set
    pub fn set_serial_consistency(&mut self, consistency: Consistency) -> Result<&mut Self> {
        unsafe {
            cass_batch_set_serial_consistency(self.inner(), consistency.inner()).to_result(self)
        }
    }

    /// Sets the batch's timestamp.
    pub fn set_timestamp(&mut self, timestamp: i64) -> Result<&Self> {
        unsafe { cass_batch_set_timestamp(self.inner(), timestamp).to_result(self) }
    }

    /// Sets the batch's retry policy.
    pub fn set_retry_policy(&mut self, retry_policy: RetryPolicy) -> Result<&mut Self> {
        unsafe { cass_batch_set_retry_policy(self.inner(), retry_policy.inner()).to_result(self) }
    }

    /// Sets the batch's custom payload.
    pub fn set_custom_payload(&mut self, custom_payload: CustomPayload) -> Result<&mut Self> {
        unsafe {
            cass_batch_set_custom_payload(self.inner(), custom_payload.inner()).to_result(self)
        }
    }

    /// Adds a statement to a batch.
    pub fn add_statement(&mut self, statement: Statement) -> Result<&Self> {
        // If their sessions are not the same, we can reject at this level.
        if self.session() != statement.session() {
            return Err(ErrorKind::BatchSessionMismatch(
                self.session().clone(),
                statement.session().clone(),
            )
            .into());
        }
        unsafe { cass_batch_add_statement(self.inner(), statement.inner()).to_result(self) }
    }
}

/// A type of batch.
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
#[allow(missing_docs)] // Meanings are defined in CQL documentation.
#[allow(non_camel_case_types)] // Names are traditional.
pub enum BatchType {
    LOGGED,
    UNLOGGED,
    COUNTER,
}

enhance_nullary_enum!(BatchType, CassBatchType_, {
    (LOGGED, CASS_BATCH_TYPE_LOGGED, "LOGGED"),
    (UNLOGGED, CASS_BATCH_TYPE_UNLOGGED, "UNLOGGED"),
    (COUNTER, CASS_BATCH_TYPE_COUNTER, "COUNTER"),
});