surrealdb_core/kvs/
key.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
/// A trait for types which can be encoded as a kv-store key.
pub trait KeyEncode {
	fn encode(&self) -> Result<Vec<u8>, crate::err::Error> {
		let mut buf = Vec::new();
		self.encode_into(&mut buf)?;
		Ok(buf)
	}

	fn encode_owned(self) -> Result<Vec<u8>, crate::err::Error>
	where
		Self: Sized,
	{
		self.encode()
	}

	/// Push the bytes this key would encode into the buffer.
	///
	/// Implementation can make no assumption about the contents of the buffer.
	/// The buffer should not be cleared and if there are bytes present in the buffer they should
	/// also be present when this function returns.
	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error>;

	fn encode_owned_into(self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error>
	where
		Self: Sized,
	{
		self.encode_into(buffer)
	}
}

/// A trait for types which can be decoded from a kv-store key bytes.
pub trait KeyDecode<'a> {
	fn decode(bytes: &'a [u8]) -> Result<Self, crate::err::Error>
	where
		Self: Sized;
}

pub trait KeyDecodeOwned: for<'a> KeyDecode<'a> {
	/// Decode the key from an owned vector.
	///
	/// A lot of kv query methods return vectors for keys, which some key types might be able to
	/// use to more effeciently decode the data.
	///
	/// The default implementation just calls decode
	fn decode_from_vec(bytes: Vec<u8>) -> Result<Self, crate::err::Error>
	where
		Self: Sized,
	{
		Self::decode(&bytes)
	}
}

impl KeyEncode for Vec<u8> {
	fn encode(&self) -> Result<Vec<u8>, crate::err::Error> {
		Ok(self.clone())
	}

	fn encode_owned(self) -> Result<Vec<u8>, crate::err::Error> {
		Ok(self)
	}

	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
		buffer.extend_from_slice(self);
		Ok(())
	}

	fn encode_owned_into(self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
		if buffer.is_empty() {
			// we can just move self into the buffer since there is no data.
			*buffer = self;
		} else {
			// we can't overwrite the buffer so instead copy self into it.
			buffer.extend_from_slice(&self);
		}
		Ok(())
	}
}

impl<K: KeyEncode> KeyEncode for &K {
	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
		(*self).encode_into(buffer)
	}
}

impl KeyEncode for &str {
	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
		buffer.extend_from_slice(self.as_bytes());
		Ok(())
	}
}

impl KeyEncode for &[u8] {
	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
		buffer.extend_from_slice(self);
		Ok(())
	}
}

impl KeyDecode<'_> for Vec<u8> {
	fn decode(bytes: &[u8]) -> Result<Self, crate::err::Error>
	where
		Self: Sized,
	{
		Ok(bytes.to_vec())
	}
}

impl KeyDecodeOwned for Vec<u8> {
	fn decode_from_vec(bytes: Vec<u8>) -> Result<Self, crate::err::Error> {
		Ok(bytes)
	}
}

impl<'a> KeyDecode<'a> for () {
	fn decode(_: &'a [u8]) -> Result<Self, crate::err::Error>
	where
		Self: Sized,
	{
		Ok(())
	}
}

impl KeyDecodeOwned for () {
	fn decode_from_vec(_: Vec<u8>) -> Result<Self, crate::err::Error>
	where
		Self: Sized,
	{
		Ok(())
	}
}

/// Implements KeyEncode and KeyDecode uusing storekey and deserialize and serialize
/// implementations.
macro_rules! impl_key {
	($name:ident$(<$l:lifetime>)?) => {
		impl$(<$l>)? crate::kvs::KeyEncode for $name $(<$l>)?{
			fn encode(&self) -> Result<Vec<u8>, crate::err::Error> {
				Ok(storekey::serialize(self)?)
			}

			fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
				Ok(storekey::serialize_into(buffer, self)?)
			}
		}

		impl_key!(@decode $name $(,$l)?);
	};

	(@decode $name:ident, $l:lifetime) => {
		impl<$l> crate::kvs::KeyDecode<$l> for $name<$l>{
			fn decode(bytes: &$l[u8]) -> Result<Self, crate::err::Error> {
				Ok(storekey::deserialize(bytes)?)
			}
		}
	};

	(@decode $name:ident) => {
		impl<'a> crate::kvs::KeyDecode<'a> for $name{
			fn decode(bytes: &'a[u8]) -> Result<Self, crate::err::Error> {
				Ok(storekey::deserialize(bytes)?)
			}
		}

		impl crate::kvs::KeyDecodeOwned for $name {
			fn decode_from_vec(bytes: Vec<u8>) -> Result<Self, crate::err::Error> {
				Ok(storekey::deserialize(bytes.as_slice())?)
			}
		}
	};
}
pub(crate) use impl_key;