surrealdb/key/database/
ts.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
//! Stores database timestamps
use crate::key::error::KeyCategory;
use crate::key::key_req::KeyRequirements;
use derive::Key;
use serde::{Deserialize, Serialize};

// Ts stands for Database Timestamps that corresponds to Versionstamps.
// Each Ts key is suffixed by a timestamp.
// The value is the versionstamp that corresponds to the timestamp.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
pub struct Ts<'a> {
	__: u8,
	_a: u8,
	pub ns: &'a str,
	_b: u8,
	pub db: &'a str,
	_c: u8,
	_d: u8,
	_e: u8,
	pub ts: u64,
}

pub fn new<'a>(ns: &'a str, db: &'a str, ts: u64) -> Ts<'a> {
	Ts::new(ns, db, ts)
}

/// Returns the prefix for the whole database timestamps
pub fn prefix(ns: &str, db: &str) -> Vec<u8> {
	let mut k = crate::key::database::all::new(ns, db).encode().unwrap();
	k.extend_from_slice(&[b'!', b't', b's']);
	k
}

/// Returns the prefix for the whole database timestamps
pub fn suffix(ns: &str, db: &str) -> Vec<u8> {
	let mut k = prefix(ns, db);
	k.extend_from_slice(&[0xff]);
	k
}

impl KeyRequirements for Ts<'_> {
	fn key_category(&self) -> KeyCategory {
		KeyCategory::DatabaseTimestamp
	}
}

impl<'a> Ts<'a> {
	pub fn new(ns: &'a str, db: &'a str, ts: u64) -> Self {
		Ts {
			__: b'/',
			_a: b'*',
			ns,
			_b: b'*',
			db,
			_c: b'!',
			_d: b't',
			_e: b's',
			ts,
		}
	}
}

#[cfg(test)]
mod tests {
	#[test]
	fn key() {
		use super::*;
		#[rustfmt::skip]
		let val = Ts::new(
			"test",
			"test",
			123,
		);
		let enc = Ts::encode(&val).unwrap();
		let dec = Ts::decode(&enc).unwrap();
		assert_eq!(val, dec);
	}
}