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
use super::*;
use crate::prelude::RowsDecoder;
use std::{
fmt::Debug,
marker::PhantomData,
};
pub struct ValueWorker<H, V, R> {
pub request: R,
pub handle: H,
pub page_size: Option<i32>,
pub paging_state: Option<Vec<u8>>,
pub retries: usize,
_val: PhantomData<fn(V) -> V>,
}
impl<H, V, R> Debug for ValueWorker<H, V, R>
where
H: Debug,
R: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ValueWorker")
.field("request", &self.request)
.field("handle", &self.handle)
.field("page_size", &self.page_size)
.field("paging_state", &self.paging_state)
.field("retries", &self.retries)
.finish()
}
}
impl<H, V, R> Clone for ValueWorker<H, V, R>
where
R: Clone,
H: Clone,
{
fn clone(&self) -> Self {
Self {
request: self.request.clone(),
handle: self.handle.clone(),
page_size: self.page_size.clone(),
paging_state: self.paging_state.clone(),
retries: self.retries.clone(),
_val: PhantomData,
}
}
}
impl<H, V, R> ValueWorker<H, V, R> {
pub fn new(request: R, handle: H) -> Box<Self> {
Box::new(Self {
request,
handle,
page_size: None,
paging_state: None,
retries: 0,
_val: PhantomData,
})
}
pub(crate) fn from(BasicRetryWorker { request, retries }: BasicRetryWorker<R>, handle: H) -> Box<Self>
where
H: 'static + HandleResponse<Option<V>> + HandleError + Debug + Send + Sync,
R: 'static + Request + Debug + Send + Sync,
V: 'static + RowsDecoder + Send,
{
Self::new(request, handle).with_retries(retries)
}
pub fn with_paging<P: Into<Option<Vec<u8>>>>(mut self: Box<Self>, page_size: i32, paging_state: P) -> Box<Self> {
self.page_size = Some(page_size);
self.paging_state = paging_state.into();
self
}
}
impl<H, V, R> ValueWorker<H, V, R>
where
V: 'static + Send + RowsDecoder,
R: 'static + Send + Debug + Clone + Request + Sync,
H: 'static + HandleResponse<Option<V>> + HandleError + Debug + Clone + Send + Sync,
{
pub fn with_inbox<I>(self: Box<Self>, inbox: I) -> Box<SpawnableRespondWorker<R, I, Self>> {
SpawnableRespondWorker::new(inbox, *self)
}
}
impl<H, V, R> Worker for ValueWorker<H, V, R>
where
V: 'static + Send + RowsDecoder,
H: 'static + HandleResponse<Option<V>> + HandleError + Debug + Send + Sync,
R: 'static + Send + Debug + Request + Sync,
{
fn handle_response(self: Box<Self>, giveload: Vec<u8>) -> anyhow::Result<()> {
match Decoder::try_from(giveload) {
Ok(decoder) => match V::try_decode_rows(decoder) {
Ok(res) => self.handle.handle_response(res),
Err(e) => self.handle.handle_error(WorkerError::Other(e)),
},
Err(e) => self.handle.handle_error(WorkerError::Other(e)),
}
}
fn handle_error(self: Box<Self>, mut error: WorkerError, reporter: Option<&ReporterHandle>) -> anyhow::Result<()> {
error!("{}", error);
if let WorkerError::Cql(ref mut cql_error) = error {
if let (Some(id), Some(reporter)) = (cql_error.take_unprepared_id(), reporter) {
handle_unprepared_error(self, id, reporter).or_else(|worker| {
error!("Error trying to reprepare query: {}", worker.request().statement());
worker.handle.handle_error(error)
})
} else {
if let Some(worker) = self.retry()? {
worker.handle.handle_error(error)
} else {
Ok(())
}
}
} else {
if let Some(worker) = self.retry()? {
worker.handle.handle_error(error)
} else {
Ok(())
}
}
}
}
impl<H, V, R> RetryableWorker<R> for ValueWorker<H, V, R>
where
H: 'static + HandleResponse<Option<V>> + HandleError + Debug + Send + Sync,
V: 'static + Send + RowsDecoder,
R: 'static + Send + Debug + Request + Sync,
{
fn retries(&self) -> usize {
self.retries
}
fn request(&self) -> &R {
&self.request
}
fn retries_mut(&mut self) -> &mut usize {
&mut self.retries
}
}
impl<R, H, V> RespondingWorker<R, H, Option<V>> for ValueWorker<H, V, R>
where
H: 'static + HandleResponse<Option<V>> + HandleError + Debug + Send + Sync,
R: 'static + Send + Debug + Request + Sync,
V: 'static + Send + RowsDecoder,
{
fn handle(&self) -> &H {
&self.handle
}
}