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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use cassandra::data_type::ConstDataType;
use cassandra::field::Field;
use cassandra::schema::aggregate_meta::AggregateMeta;
use cassandra::schema::column_meta::ColumnMeta;
use cassandra::schema::function_meta::FunctionMeta;
use cassandra::schema::keyspace_meta::KeyspaceMeta;
use cassandra::schema::table_meta::TableMeta;
use cassandra::util::Protected;
use cassandra::value::Value;
use cassandra::error::*;

// use cassandra_sys::CassIteratorType as _CassIteratorType;
use cassandra_sys::CassIterator as _CassIterator;
use cassandra_sys::cass_false;
// use cassandra_sys::cass_iterator_type;
use cassandra_sys::cass_iterator_free;
use cassandra_sys::cass_iterator_get_aggregate_meta;
use cassandra_sys::cass_iterator_get_column_meta;
use cassandra_sys::cass_iterator_get_function_meta;
use cassandra_sys::cass_iterator_get_keyspace_meta;
use cassandra_sys::cass_iterator_get_map_key;
use cassandra_sys::cass_iterator_get_map_value;
use cassandra_sys::cass_iterator_get_meta_field_name;
use cassandra_sys::cass_iterator_get_meta_field_value;
use cassandra_sys::cass_iterator_get_table_meta;
use cassandra_sys::cass_iterator_get_user_type;
use cassandra_sys::cass_iterator_get_value;
use cassandra_sys::cass_iterator_next;
use cassandra_sys::cass_true;
use std::{mem, slice, str};

/// Iterates over the  aggregate metadata entries(??)
#[derive(Debug)]
pub struct AggregateIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for AggregateIterator {}

impl Drop for AggregateIterator {
    fn drop(&mut self) { unsafe { cass_iterator_free(self.0) } }
}

impl Iterator for AggregateIterator {
    type Item = AggregateMeta;
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => {
                    let field_value = cass_iterator_get_aggregate_meta(self.0);
                    Some(AggregateMeta::build(field_value))
                }
            }
        }
    }
}

/// Iterater over the fields of a UDT
#[derive(Debug)]
pub struct UserTypeIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for UserTypeIterator {}

impl Drop for UserTypeIterator {
    fn drop(&mut self) { unsafe { cass_iterator_free(self.0) } }
}

impl Iterator for UserTypeIterator {
    type Item = ConstDataType;
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(ConstDataType(cass_iterator_get_user_type(self.0))),
            }
        }
    }
}

impl UserTypeIterator {
    //    pub fn get_field_name(&mut self)-> Value {unsafe{
    //
    //        Value::new(cass_iterator_get_user_type_field_name(self.0))
    //    }}
}


/// Iterater over the  function metadata entries(??)
#[derive(Debug)]
pub struct FunctionIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for FunctionIterator {}

impl Iterator for FunctionIterator {
    type Item = FunctionMeta;
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(FunctionMeta::build(cass_iterator_get_function_meta(self.0))),
            }
        }
    }
}


/// Iterater over the table's metadata entries(??)
#[derive(Debug)]
pub struct TableIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for TableIterator {}

impl Iterator for TableIterator {
    type Item = TableMeta;
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(TableMeta::build(cass_iterator_get_table_meta(self.0))),
            }
        }
    }
}

/// Iterater over the keyspace's metadata entries(??)
#[derive(Debug)]
pub struct KeyspaceIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for KeyspaceIterator {}

impl Iterator for KeyspaceIterator {
    type Item = KeyspaceMeta;
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(KeyspaceMeta::build(cass_iterator_get_keyspace_meta(self.0))),
            }
        }
    }
}

/// Iterater over the columns's metadata entries(??)
#[derive(Debug)]
pub struct ColumnIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for ColumnIterator {}

impl Iterator for ColumnIterator {
    type Item = ColumnMeta;
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(ColumnMeta::build(cass_iterator_get_column_meta(self.0))),
            }
        }
    }
}

/// Iterater over the field's metadata entries(??)
#[derive(Debug)]
pub struct FieldIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for FieldIterator {}

impl Iterator for FieldIterator {
    type Item = Field;
    #[allow(cast_possible_truncation)]
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => {
                    let mut name = mem::zeroed();
                    let mut name_length = mem::zeroed();
                    cass_iterator_get_meta_field_name(self.0, &mut name, &mut name_length).to_result(())
                        .and_then(|_| {
                            let slice = slice::from_raw_parts(name as *const u8, name_length as usize);
                            let name = str::from_utf8(slice)?.to_owned();
                            let value = Value::build(cass_iterator_get_meta_field_value(self.0));
                            Ok(Some(Field { name, value }))
                        }
                    )
                }.expect("Cassandra error during iteration")
            }
        }
    }
}


// pub struct CassIteratorType(_CassIteratorType);

// impl CassIteratorType {
//    pub fn new(_type: _CassIteratorType) -> Self { CassIteratorType(_type) }
// }

// impl Protected<*mut _Batch> for CassIterator {
//    fn inner(&self) -> *mut _CassIterator {
//        self.0
//    }
//    fn build(inner: *mut _CassIterator) -> Self {
//        CassIterator(inner)
//    }
// }

impl Protected<*mut _CassIterator> for UserTypeIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { UserTypeIterator(inner) }
}

impl Protected<*mut _CassIterator> for AggregateIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { AggregateIterator(inner) }
}

impl Protected<*mut _CassIterator> for FunctionIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { FunctionIterator(inner) }
}

impl Protected<*mut _CassIterator> for KeyspaceIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { KeyspaceIterator(inner) }
}

impl Protected<*mut _CassIterator> for FieldIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { FieldIterator(inner) }
}

impl Protected<*mut _CassIterator> for ColumnIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { ColumnIterator(inner) }
}

impl Protected<*mut _CassIterator> for TableIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { TableIterator(inner) }
}

impl Protected<*mut _CassIterator> for MapIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { MapIterator(inner) }
}

impl Protected<*mut _CassIterator> for SetIterator {
    fn inner(&self) -> *mut _CassIterator { self.0 }
    fn build(inner: *mut _CassIterator) -> Self { SetIterator(inner) }
}


/// Iterater over the set's metadata entries(??)
#[derive(Debug)]
pub struct SetIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for SetIterator {}

// impl<'a> Display for &'a SetIterator {
//    fn fmt(&self, f:&mut Formatter) -> fmt::Result {
//        for item in self {
//            try!(write!(f, "{}\t", item));
//        }
//        Ok(())
//    }
// }

impl Drop for SetIterator {
    fn drop(&mut self) { unsafe { cass_iterator_free(self.0) } }
}


impl Iterator for SetIterator {
    type Item = Value;

    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(self.get_value()),
            }
        }
    }
}

impl SetIterator {
    fn get_value(&mut self) -> Value { unsafe { Value::build(cass_iterator_get_value(self.0)) } }
}

/// An iterator over the k/v pair in the map
#[derive(Debug)]
pub struct MapIterator(*mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for MapIterator {}

impl MapIterator {
    fn get_key(&mut self) -> Value { unsafe { Value::build(cass_iterator_get_map_key(self.0)) } }
    fn get_value(&mut self) -> Value { unsafe { Value::build(cass_iterator_get_map_value(self.0)) } }

    /// Gets the next k/v pair in the map
    pub fn get_pair(&mut self) -> (Value, Value) { (self.get_key(), self.get_value()) }
}

/// An iterator over the elements of a Cassandra tuple
#[derive(Debug)]
pub struct TupleIterator(pub *mut _CassIterator);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for TupleIterator {}

impl Drop for TupleIterator {
    fn drop(&mut self) { unsafe { cass_iterator_free(self.0) } }
}

impl Iterator for TupleIterator {
    type Item = Value;
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(self.get_value()),
            }
        }
    }
}

impl TupleIterator {
    fn get_value(&mut self) -> Value { unsafe { Value::build(cass_iterator_get_value(self.0)) } }
}



impl Drop for MapIterator {
    fn drop(&mut self) { unsafe { cass_iterator_free(self.0) } }
}

impl Iterator for MapIterator {
    type Item = (Value, Value);
    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        unsafe {
            match cass_iterator_next(self.0) {
                cass_false => None,
                cass_true => Some(self.get_pair()),
            }
        }
    }
}