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
use std::rc::Rc;
use std::vec::IntoIter;
use crate::error::Error;
use crate::index::IteratorType;
use crate::tuple::{Encode, ToTupleBuffer, Tuple};
use super::inner::ConnInner;
use super::protocol;
use super::Options;
/// Remote index (a group of key values and pointers)
pub struct RemoteIndex {
conn_inner: Rc<ConnInner>,
space_id: u32,
index_id: u32,
}
impl RemoteIndex {
pub(crate) fn new(conn_inner: Rc<ConnInner>, space_id: u32, index_id: u32) -> Self {
RemoteIndex {
conn_inner,
space_id,
index_id,
}
}
/// The remote-call equivalent of the local call `Index::get(...)`
/// (see [details](../index/struct.Index.html#method.get)).
pub fn get<K>(&self, key: &K, options: &Options) -> Result<Option<Tuple>, Error>
where
K: ToTupleBuffer + ?Sized,
{
Ok(self
.select(
IteratorType::Eq,
key,
&Options {
offset: 0,
limit: Some(1),
..options.clone()
},
)?
.next())
}
/// The remote-call equivalent of the local call `Index::select(...)`
/// (see [details](../index/struct.Index.html#method.select)).
pub fn select<K>(
&self,
iterator_type: IteratorType,
key: &K,
options: &Options,
) -> Result<RemoteIndexIterator, Error>
where
K: ToTupleBuffer + ?Sized,
{
self.conn_inner.request(
|buf, sync| {
protocol::encode_select(
buf,
sync,
self.space_id,
self.index_id,
options.limit.unwrap_or(u32::max_value()),
options.offset,
iterator_type,
key,
)
},
|buf, _| {
protocol::decode_multiple_rows(buf, None).map(|result| RemoteIndexIterator {
inner: result.into_iter(),
})
},
options,
)
}
/// The remote-call equivalent of the local call `Space::update(...)`
/// (see [details](../index/struct.Index.html#method.update)).
pub fn update<K, Op>(
&self,
key: &K,
ops: &[Op],
options: &Options,
) -> Result<Option<Tuple>, Error>
where
K: ToTupleBuffer + ?Sized,
Op: Encode,
{
self.conn_inner.request(
|buf, sync| protocol::encode_update(buf, sync, self.space_id, self.index_id, key, ops),
protocol::decode_single_row,
options,
)
}
/// The remote-call equivalent of the local call `Space::upsert(...)`
/// (see [details](../index/struct.Index.html#method.upsert)).
pub fn upsert<T, Op>(
&self,
value: &T,
ops: &[Op],
options: &Options,
) -> Result<Option<Tuple>, Error>
where
T: ToTupleBuffer + ?Sized,
Op: Encode,
{
self.conn_inner.request(
|buf, sync| {
protocol::encode_upsert(buf, sync, self.space_id, self.index_id, value, ops)
},
protocol::decode_single_row,
options,
)
}
/// The remote-call equivalent of the local call `Space::delete(...)`
/// (see [details](../index/struct.Index.html#method.delete)).
pub fn delete<K>(&self, key: &K, options: &Options) -> Result<Option<Tuple>, Error>
where
K: ToTupleBuffer + ?Sized,
{
self.conn_inner.request(
|buf, sync| protocol::encode_delete(buf, sync, self.space_id, self.index_id, key),
protocol::decode_single_row,
options,
)
}
}
/// Remote index iterator. Can be used with `for` statement
pub struct RemoteIndexIterator {
inner: IntoIter<Tuple>,
}
impl Iterator for RemoteIndexIterator {
type Item = Tuple;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}