surrealdb_core/vs/
mod.rs

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
//! vs is a module to handle Versionstamps.
//! This module is supplemental to the kvs::tx module and is not intended to be used directly
//! by applications.
//! This module might be migrated into the kvs or kvs::tx module in the future.

pub use std::{error, fmt, mem};

use revision::Revisioned;

/// Versionstamp is a 10-byte array used to identify a specific version of a key.
/// The first 8 bytes are significant (the u64), and the remaining 2 bytes are not significant, but used for extra precision.
/// To convert to and from this module, see the conv module in this same directory.
///
/// You're going to want these
/// 65536
/// 131072
/// 196608
/// 262144
/// 327680
/// 393216
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug, PartialOrd)]
pub struct VersionStamp([u8; 10]);

impl serde::Serialize for VersionStamp {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		self.0.serialize(serializer)
	}
}

impl<'de> serde::Deserialize<'de> for VersionStamp {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		let res = <[u8; 10]>::deserialize(deserializer)?;
		Ok(VersionStamp(res))
	}
}

// Version stamp was previously a normal array so it doesn't have any kind of revision tracking and
// is serialized just like any other array.
impl Revisioned for VersionStamp {
	fn revision() -> u16 {
		0
	}

	fn serialize_revisioned<W: std::io::Write>(&self, w: &mut W) -> Result<(), revision::Error> {
		self.0.serialize_revisioned(w)
	}

	fn deserialize_revisioned<R: std::io::Read>(r: &mut R) -> Result<Self, revision::Error>
	where
		Self: Sized,
	{
		Revisioned::deserialize_revisioned(r).map(VersionStamp)
	}
}

impl Default for VersionStamp {
	fn default() -> Self {
		VersionStamp::ZERO
	}
}

pub struct VersionStampError(());

impl fmt::Display for VersionStampError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "Invalid version stamp conversion")
	}
}
impl fmt::Debug for VersionStampError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Display::fmt(self, f)
	}
}
impl error::Error for VersionStampError {}

impl VersionStamp {
	pub const ZERO: VersionStamp = VersionStamp([0; 10]);

	pub const fn from_u64(v: u64) -> Self {
		let b = v.to_be_bytes();
		VersionStamp([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], 0, 0])
	}

	pub const fn from_u64_u16(v: u64, v2: u16) -> Self {
		let b1 = v.to_be_bytes();
		let b2 = v2.to_be_bytes();
		VersionStamp([b1[0], b1[1], b1[2], b1[3], b1[4], b1[5], b1[6], b1[7], b2[0], b2[1]])
	}

	pub const fn try_from_u128(v: u128) -> Result<Self, VersionStampError> {
		if (v >> 80) > 0 {
			return Err(VersionStampError(()));
		}
		let b = v.to_be_bytes();
		Ok(VersionStamp([b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]]))
	}

	/// Convert the VersionStamp into a u64 ignoring the 2 normally zero bytes.
	pub const fn into_u64_u16(self) -> (u64, u16) {
		let b = self.0;
		(
			u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]),
			u16::from_be_bytes([b[8], b[9]]),
		)
	}

	/// Convert the VersionStamp into a u64 ignoring the 2 normally zero bytes.
	pub const fn into_u64_lossy(self) -> u64 {
		let b = self.0;
		u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])
	}

	pub const fn try_into_u64(self) -> Result<u64, VersionStampError> {
		if self.0[8] > 0 || self.0[9] > 0 {
			return Err(VersionStampError(()));
		}
		Ok(self.into_u64_lossy())
	}

	pub const fn into_u128(self) -> u128 {
		let b = self.0;
		u128::from_be_bytes([
			0, 0, 0, 0, 0, 0, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9],
		])
	}

	pub const fn as_bytes(self) -> [u8; 10] {
		self.0
	}

	pub const fn from_bytes(bytes: [u8; 10]) -> Self {
		Self(bytes)
	}

	pub const fn from_slice(slice: &[u8]) -> Result<Self, VersionStampError> {
		if slice.len() != 10 {
			return Err(VersionStampError(()));
		}
		Ok(VersionStamp([
			slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
			slice[8], slice[9],
		]))
	}

	pub fn next(self) -> Option<Self> {
		let (v, suffix) = self.into_u64_u16();
		let v = v.checked_add(1)?;
		let next = VersionStamp::from_u64_u16(v, suffix);
		Some(next)
	}

	/// Returns an iterator of version stamps starting with the current version stamp.
	pub fn iter(self) -> VersionStampIter {
		VersionStampIter {
			cur: Some(self),
		}
	}
}

pub struct VersionStampIter {
	cur: Option<VersionStamp>,
}

impl Iterator for VersionStampIter {
	type Item = VersionStamp;

	fn next(&mut self) -> Option<Self::Item> {
		let next = self.cur?.next();
		mem::replace(&mut self.cur, next)
	}
}

#[cfg(test)]
mod test {
	use super::VersionStamp;

	#[test]
	pub fn generate_one_vs() {
		let vs = VersionStamp::ZERO.iter().take(1).collect::<Vec<_>>();
		assert_eq!(vs.len(), 1, "Should be 1, but was {:?}", vs);
		assert_eq!(vs[0], VersionStamp::ZERO);
	}

	#[test]
	pub fn generate_two_vs_in_sequence() {
		let vs = VersionStamp::from_bytes([0, 0, 0, 0, 0, 0, 0, 1, 0, 0]).iter().flat_map(|vs| {
			let skip_because_first_is_equal = 1;
			vs.iter().skip(skip_because_first_is_equal).map(move |vs2| (vs, vs2))
		});
		let versionstamps = vs.take(4).collect::<Vec<(VersionStamp, VersionStamp)>>();

		assert_eq!(
			versionstamps.len(),
			4,
			"We expect the combinations to be 2x2 matrix, but was {:?}",
			versionstamps
		);

		let acceptable_values = [65536u128, 131072, 196608, 262144, 327680, 393216];
		for (first, second) in versionstamps {
			assert!(first < second, "First: {:?}, Second: {:?}", first, second);
			let first = first.into_u128();
			let second = second.into_u128();
			assert!(acceptable_values.contains(&first));
			assert!(acceptable_values.contains(&second));
		}
	}

	#[test]
	pub fn iteration_stops_past_end() {
		let mut iter = VersionStamp::from_bytes([255; 10]).iter();
		assert!(iter.next().is_some());
		assert!(iter.next().is_none());
	}

	#[test]
	fn try_to_u64_be() {
		use super::*;
		// Overflow
		let v = VersionStamp::from_bytes([255, 255, 255, 255, 255, 255, 255, 255, 0, 1]);
		let res = v.try_into_u64();
		assert!(res.is_err());
		// No overflow
		let v = VersionStamp::from_bytes([255, 255, 255, 255, 255, 255, 255, 255, 0, 0]);
		let res = v.try_into_u64().unwrap();
		assert_eq!(res, u64::MAX);
	}

	#[test]
	fn try_u128_to_versionstamp() {
		use super::*;
		// Overflow
		let v = u128::MAX;
		let res = VersionStamp::try_from_u128(v);
		assert!(res.is_err());
		// No overflow
		let v = u128::MAX >> 48;
		let res = VersionStamp::try_from_u128(v).unwrap();
		assert_eq!(res.as_bytes(), [255, 255, 255, 255, 255, 255, 255, 255, 255, 255]);
	}

	#[test]
	fn can_add_u64_conversion() {
		let start = 5u64;
		let vs = VersionStamp::from_u64(start);
		// The last 2 bytes are empty
		assert_eq!("00000000000000050000", hex::encode(vs.as_bytes()));
		let mid = vs.try_into_u64().unwrap();
		assert_eq!(start, mid);
		let mid = mid + 1;
		let vs = VersionStamp::from_u64(mid);
		// The last 2 bytes are empty
		assert_eq!("00000000000000060000", hex::encode(vs.as_bytes()));
		let end = vs.try_into_u64().unwrap();
		assert_eq!(end, 6);
	}
}