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
use std::cell::RefCell;
use std::iter::{IntoIterator, Iterator};
use std::rc::Rc;
use std::vec::IntoIter;
use serde::de::DeserializeOwned;
use tokio_core::reactor::Core;
use rincon_client::cursor::methods::*;
use rincon_client::cursor::types::{Cursor, CursorStatistics, Warning};
use rincon_core::api::connector::{Connector, Execute};
use rincon_core::api::method::{Method, Prepare};
use rincon_core::api::types::{Empty, EMPTY};
use super::Result;
#[derive(Debug)]
pub struct CursorSession<T, C> {
cursor: Cursor<T>,
database_name: String,
connector: Rc<C>,
core: Rc<RefCell<Core>>,
}
impl<T, C> CursorSession<T, C>
where T: 'static + DeserializeOwned, C: 'static + Connector
{
pub(crate) fn new(
cursor: Cursor<T>,
database_name: String,
connector: Rc<C>,
core: Rc<RefCell<Core>>,
) -> Self {
CursorSession {
cursor,
database_name,
connector,
core,
}
}
fn execute<M>(&self, method: M) -> Result<<M as Method>::Result>
where M: 'static + Method + Prepare
{
self.core.borrow_mut().run(
self.connector.connection(&self.database_name)
.execute(method)
)
}
pub fn database_name(&self) -> &str {
&self.database_name
}
pub fn entity(&self) -> &Cursor<T> {
&self.cursor
}
pub fn unwrap(self) -> Cursor<T> {
self.cursor
}
pub fn id(&self) -> Option<&String> {
self.cursor.id()
}
pub fn result(&self) -> &[T] {
self.cursor.result()
}
pub fn has_more(&self) -> bool {
self.cursor.has_more()
}
pub fn is_cached(&self) -> bool {
self.cursor.is_cached()
}
pub fn count(&self) -> Option<u64> {
self.cursor.count()
}
pub fn stats(&self) -> Option<&CursorStatistics> {
self.cursor.stats()
}
pub fn warnings(&self) -> Option<&Vec<Warning>> {
self.cursor.warnings()
}
pub fn next_cursor(&self) -> Option<Result<CursorSession<T, C>>> {
self.cursor.id().map(|v| v.to_owned()).map(|id|
self.execute(ReadNextBatchFromCursor::with_id(id))
.map(|cursor| CursorSession {
cursor,
database_name: self.database_name.clone(),
connector: self.connector.clone(),
core: self.core.clone(),
})
)
}
pub fn delete(self) -> Result<Empty> {
if let Some(id) = self.cursor.id() {
self.execute(DeleteCursor::with_id_ref(id))
} else {
Ok(EMPTY)
}
}
}
impl<T, C> IntoIterator for CursorSession<T, C>
where T: 'static + DeserializeOwned, C: 'static + Connector
{
type Item = Result<T>;
type IntoIter = CursorSessionIntoIter<T, C>;
fn into_iter(self) -> Self::IntoIter {
let has_more = self.cursor.has_more();
let (cursor_id, count, result) = self.cursor.unwrap();
CursorSessionIntoIter {
batch: result.into_iter(),
count,
has_more,
cursor_id,
database_name: self.database_name,
connector: self.connector,
core: self.core,
}
}
}
#[derive(Debug)]
pub struct CursorSessionIntoIter<T, C> {
batch: IntoIter<T>,
count: Option<u64>,
has_more: bool,
cursor_id: Option<String>,
database_name: String,
connector: Rc<C>,
core: Rc<RefCell<Core>>,
}
impl<T, C> CursorSessionIntoIter<T, C>
where T: 'static + DeserializeOwned, C: 'static + Connector
{
fn execute<M>(&self, method: M) -> Result<<M as Method>::Result>
where M: 'static + Method + Prepare
{
self.core.borrow_mut().run(
self.connector.connection(&self.database_name)
.execute(method)
)
}
}
impl<T, C> Iterator for CursorSessionIntoIter<T, C>
where T: 'static + DeserializeOwned, C: 'static + Connector
{
type Item = Result<T>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(next) = self.batch.next() {
Some(Ok(next))
} else if self.has_more {
self.cursor_id.clone().and_then(|id| {
let cursor = self.execute(ReadNextBatchFromCursor::new(id));
match cursor {
Ok(cursor) => {
self.has_more = cursor.has_more();
let (id, count, result) = cursor.unwrap();
self.cursor_id = id;
self.count = count;
self.batch = result.into_iter();
self.batch.next().map(Ok)
},
Err(error) => Some(Err(error)),
}
})
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.batch.len(), self.count.map(|c| c as usize))
}
fn count(self) -> usize where Self: Sized {
self.count.map(|c| c as usize)
.unwrap_or_else(|| self.fold(0, |cnt, _| cnt + 1))
}
}