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
//! Public domain types returned by client operations.
use crate::errors::OxiaError;
use crate::key;
use crate::proto;
use bytes::Bytes;
use std::cmp::Ordering;
use std::fmt;
/// Metadata about the state of a record.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Version {
/// The unique identifier of this version of the record. It changes on every
/// modification and is the value to pass to
/// [`expected_version_id`](crate::PutBuilder::expected_version_id) for
/// compare-and-swap operations.
pub version_id: i64,
/// The number of modifications made to the record since it was created.
pub modifications_count: i64,
/// The creation timestamp of the record, in milliseconds since the epoch.
pub created_timestamp: u64,
/// The timestamp of the latest modification, in milliseconds since the epoch.
pub modified_timestamp: u64,
/// The identifier of the owning session, when the record is ephemeral.
pub session_id: Option<i64>,
/// The identity of the client that last modified an ephemeral record.
pub client_identity: Option<String>,
}
impl Version {
/// Whether the record is ephemeral (bound to a client session).
pub fn is_ephemeral(&self) -> bool {
self.session_id.is_some()
}
}
impl From<proto::Version> for Version {
fn from(v: proto::Version) -> Self {
Version {
version_id: v.version_id,
modifications_count: v.modifications_count,
created_timestamp: v.created_timestamp,
modified_timestamp: v.modified_timestamp,
session_id: v.session_id,
client_identity: v.client_identity,
}
}
}
/// The key comparison applied by a [`get`](crate::OxiaClient::get) operation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ComparisonType {
/// The stored key must be equal to the requested key.
#[default]
Equal,
/// Return the record with the highest key `<=` the requested key.
Floor,
/// Return the record with the lowest key `>=` the requested key.
Ceiling,
/// Return the record with the highest key `<` the requested key.
Lower,
/// Return the record with the lowest key `>` the requested key.
Higher,
}
impl ComparisonType {
pub(crate) fn to_proto(self) -> proto::KeyComparisonType {
match self {
ComparisonType::Equal => proto::KeyComparisonType::Equal,
ComparisonType::Floor => proto::KeyComparisonType::Floor,
ComparisonType::Ceiling => proto::KeyComparisonType::Ceiling,
ComparisonType::Lower => proto::KeyComparisonType::Lower,
ComparisonType::Higher => proto::KeyComparisonType::Higher,
}
}
}
/// The result of a successful [`put`](crate::OxiaClient::put).
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PutResult {
/// The key of the stored record. Differs from the requested key when the
/// server generated it (sequential keys).
pub key: String,
/// The version of the newly written record.
pub version: Version,
}
/// A record returned by [`get`](crate::OxiaClient::get) or
/// [`range_scan`](crate::OxiaClient::range_scan).
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct GetResult {
/// The key of the record. Differs from the requested key for non-`Equal`
/// comparisons (e.g. floor/ceiling queries).
pub key: String,
/// The value, unless the record has none or the query excluded it with
/// [`include_value(false)`](crate::GetBuilder::include_value).
pub value: Option<Bytes>,
/// The version of the record.
pub version: Version,
/// The matching secondary-index key, when the query used
/// [`use_index`](crate::GetBuilder::use_index).
pub secondary_index_key: Option<String>,
}
impl GetResult {
/// Converts a wire response. The server omits the key for exact-match gets
/// (it equals the request key, passed as `fallback_key`); scan records must
/// always carry one.
pub(crate) fn from_proto(
response: proto::GetResponse,
fallback_key: Option<String>,
) -> Result<Self, OxiaError> {
let version = response
.version
.ok_or_else(|| OxiaError::Decode("missing version in get response".to_string()))?;
let key = response
.key
.or(fallback_key)
.ok_or_else(|| OxiaError::Decode("missing key in record".to_string()))?;
Ok(GetResult {
key,
value: response.value,
version: version.into(),
secondary_index_key: response.secondary_index_key,
})
}
pub(crate) fn compare_keys(&self, other: &Self) -> Ordering {
key::compare(&self.key, &other.key)
}
}
/// A change notification streamed from
/// [`OxiaClient::notifications`](crate::OxiaClient::notifications).
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Notification {
/// A new record was created.
KeyCreated {
/// The key of the created record.
key: String,
/// The version id of the created record.
version_id: Option<i64>,
},
/// An existing record was modified.
KeyModified {
/// The key of the modified record.
key: String,
/// The new version id of the record.
version_id: Option<i64>,
},
/// A record was deleted.
KeyDeleted {
/// The key of the deleted record.
key: String,
},
/// A range of records was deleted.
KeyRangeDeleted {
/// The start of the deleted range (inclusive).
key: String,
/// The end of the deleted range (exclusive).
key_range_last: Option<String>,
},
}
impl Notification {
/// Converts a wire notification entry; `None` for types this client version
/// does not recognize (they are skipped).
pub(crate) fn from_proto(entry: proto::NotificationEntry) -> Option<Self> {
let key = entry.key.unwrap_or_default();
let notification = entry.value?;
match proto::NotificationType::try_from(notification.r#type).ok()? {
proto::NotificationType::KeyCreated => Some(Notification::KeyCreated {
key,
version_id: notification.version_id,
}),
proto::NotificationType::KeyModified => Some(Notification::KeyModified {
key,
version_id: notification.version_id,
}),
proto::NotificationType::KeyDeleted => Some(Notification::KeyDeleted { key }),
proto::NotificationType::KeyRangeDeleted => Some(Notification::KeyRangeDeleted {
key,
key_range_last: notification.key_range_last,
}),
}
}
/// The key the notification refers to (the range start for
/// [`Notification::KeyRangeDeleted`]).
pub fn key(&self) -> &str {
match self {
Notification::KeyCreated { key, .. }
| Notification::KeyModified { key, .. }
| Notification::KeyDeleted { key }
| Notification::KeyRangeDeleted { key, .. } => key,
}
}
}
impl fmt::Display for Notification {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Notification::KeyCreated { key, version_id } => {
write!(f, "KeyCreated(key={key}, version_id={version_id:?})")
}
Notification::KeyModified { key, version_id } => {
write!(f, "KeyModified(key={key}, version_id={version_id:?})")
}
Notification::KeyDeleted { key } => write!(f, "KeyDeleted(key={key})"),
Notification::KeyRangeDeleted {
key,
key_range_last,
} => {
write!(f, "KeyRangeDeleted(key={key}, last={key_range_last:?})")
}
}
}
}
/// The version id that a conditional operation compares against to assert that
/// the record does not exist.
pub(crate) const VERSION_ID_NOT_EXISTS: i64 = -1;