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
use cassandra::consistency::Consistency;
use cassandra::policy::retry::RetryPolicy;
use cassandra::statement::Statement;
use cassandra::util::Protected;
use cassandra_sys::CASS_OK;
pub use cassandra_sys::CassBatch as _Batch;
pub use cassandra_sys::CassBatchType as BatchType;
use cassandra_sys::CassConsistency;
use cassandra_sys::CassCustomPayload as _CassCustomPayload;
use cassandra_sys::CassError;
use cassandra_sys::cass_batch_add_statement;
use cassandra_sys::cass_batch_free;
use cassandra_sys::cass_batch_new;
use cassandra_sys::cass_batch_set_consistency;
use cassandra_sys::cass_batch_set_custom_payload;
use cassandra_sys::cass_batch_set_retry_policy;
use cassandra_sys::cass_batch_set_serial_consistency;
use cassandra_sys::cass_batch_set_timestamp;
use cassandra_sys::cass_custom_payload_free;
use cassandra_sys::cass_custom_payload_new;
use cassandra_sys::cass_custom_payload_set;
use std::ffi::CString;
use std::ffi::NulError;
#[derive(Debug)]
pub struct Batch(*mut _Batch);
impl Protected<*mut _Batch> for Batch {
fn inner(&self) -> *mut _Batch { self.0 }
fn build(inner: *mut _Batch) -> Self { Batch(inner) }
}
#[derive(Debug)]
pub struct CustomPayload(*mut _CassCustomPayload);
impl Protected<*mut _CassCustomPayload> for CustomPayload {
fn inner(&self) -> *mut _CassCustomPayload { self.0 }
fn build(inner: *mut _CassCustomPayload) -> Self { CustomPayload(inner) }
}
impl Default for CustomPayload {
fn default() -> Self { unsafe { CustomPayload(cass_custom_payload_new()) } }
}
impl CustomPayload {
pub fn set(&self, name: String, value: &[u8]) -> Result<(), NulError> {
unsafe {
Ok(cass_custom_payload_set(self.0,
CString::new(name)?.as_ptr(),
value.as_ptr(),
value.len()))
}
}
}
impl Drop for CustomPayload {
fn drop(&mut self) { unsafe { cass_custom_payload_free(self.0) } }
}
impl Drop for Batch {
fn drop(&mut self) { unsafe { cass_batch_free(self.0) } }
}
impl Batch {
pub fn new(batch_type: BatchType) -> Batch { unsafe { Batch(cass_batch_new(batch_type)) } }
pub fn set_consistency(&mut self, consistency: CassConsistency) -> Result<&Self, CassError> {
unsafe {
match cass_batch_set_consistency(self.0, consistency) {
CASS_OK => Ok(self),
err => Err(err),
}
}
}
pub fn set_serial_consistency(&mut self, consistency: Consistency) -> Result<&Self, CassError> {
unsafe {
match cass_batch_set_serial_consistency(self.0, consistency.inner()) {
CASS_OK => Ok(self),
err => Err(err),
}
}
}
pub fn set_timestamp(&mut self, timestamp: i64) -> Result<&Self, CassError> {
unsafe {
match cass_batch_set_timestamp(self.0, timestamp) {
CASS_OK => Ok(self),
err => Err(err),
}
}
}
pub fn set_retry_policy(&mut self, retry_policy: RetryPolicy) -> Result<&Self, CassError> {
unsafe {
match cass_batch_set_retry_policy(self.0, retry_policy.inner()) {
CASS_OK => Ok(self),
err => Err(err),
}
}
}
pub fn set_custom_payload(&mut self, custom_payload: CustomPayload) -> Result<&Self, CassError> {
unsafe {
match cass_batch_set_custom_payload(self.0, custom_payload.0) {
CASS_OK => Ok(self),
err => Err(err),
}
}
}
pub fn add_statement(&mut self, statement: &Statement) -> Result<&Self, CassError> {
unsafe {
match cass_batch_add_statement(self.0, statement.inner()) {
CASS_OK => Ok(self),
err => Err(err),
}
}
}
}