reifydb_type/value/container/
uuid.rs1use std::{
5 fmt::{self, Debug},
6 ops::Deref,
7 result::Result as StdResult,
8};
9
10use serde::{Deserialize, Deserializer, Serialize, Serializer};
11
12use crate::{
13 Result,
14 storage::{Cow, DataBitVec, DataVec, Storage},
15 util::cowvec::CowVec,
16 value::{Value, is::IsUuid},
17};
18
19pub struct UuidContainer<T, S: Storage = Cow>
20where
21 T: IsUuid,
22{
23 data: S::Vec<T>,
24}
25
26impl<T: IsUuid, S: Storage> Clone for UuidContainer<T, S> {
27 fn clone(&self) -> Self {
28 Self {
29 data: self.data.clone(),
30 }
31 }
32}
33
34impl<T: IsUuid + Debug, S: Storage> Debug for UuidContainer<T, S>
35where
36 S::Vec<T>: Debug,
37{
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.debug_struct("UuidContainer").field("data", &self.data).finish()
40 }
41}
42
43impl<T: IsUuid, S: Storage> PartialEq for UuidContainer<T, S>
44where
45 S::Vec<T>: PartialEq,
46{
47 fn eq(&self, other: &Self) -> bool {
48 self.data == other.data
49 }
50}
51
52impl<T: IsUuid + Serialize> Serialize for UuidContainer<T, Cow> {
53 fn serialize<Ser: Serializer>(&self, serializer: Ser) -> StdResult<Ser::Ok, Ser::Error> {
54 #[derive(Serialize)]
55 struct Helper<'a, T: Clone + PartialEq + Serialize> {
56 data: &'a CowVec<T>,
57 }
58 Helper {
59 data: &self.data,
60 }
61 .serialize(serializer)
62 }
63}
64
65impl<'de, T: IsUuid + Deserialize<'de>> Deserialize<'de> for UuidContainer<T, Cow> {
66 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
67 #[derive(Deserialize)]
68 struct Helper<T: Clone + PartialEq> {
69 data: CowVec<T>,
70 }
71 let h = Helper::deserialize(deserializer)?;
72 Ok(UuidContainer {
73 data: h.data,
74 })
75 }
76}
77
78impl<T: IsUuid, S: Storage> Deref for UuidContainer<T, S> {
79 type Target = [T];
80
81 fn deref(&self) -> &Self::Target {
82 self.data.as_slice()
83 }
84}
85
86impl<T> UuidContainer<T, Cow>
87where
88 T: IsUuid + Clone + Debug + Default,
89{
90 pub fn new(data: Vec<T>) -> Self {
91 Self {
92 data: CowVec::new(data),
93 }
94 }
95
96 pub fn with_capacity(capacity: usize) -> Self {
97 Self {
98 data: CowVec::with_capacity(capacity),
99 }
100 }
101
102 pub fn from_vec(data: Vec<T>) -> Self {
103 Self {
104 data: CowVec::new(data),
105 }
106 }
107}
108
109impl<T, S: Storage> UuidContainer<T, S>
110where
111 T: IsUuid + Clone + Debug + Default,
112{
113 pub fn from_parts(data: S::Vec<T>) -> Self {
114 Self {
115 data,
116 }
117 }
118
119 pub fn len(&self) -> usize {
120 DataVec::len(&self.data)
121 }
122
123 pub fn capacity(&self) -> usize {
124 DataVec::capacity(&self.data)
125 }
126
127 pub fn is_empty(&self) -> bool {
128 DataVec::is_empty(&self.data)
129 }
130
131 pub fn clear(&mut self) {
132 DataVec::clear(&mut self.data);
133 }
134
135 pub fn push(&mut self, value: T) {
136 DataVec::push(&mut self.data, value);
137 }
138
139 pub fn push_default(&mut self) {
140 DataVec::push(&mut self.data, T::default());
141 }
142
143 pub fn get(&self, index: usize) -> Option<&T> {
144 if index < self.len() {
145 DataVec::get(&self.data, index)
146 } else {
147 None
148 }
149 }
150
151 pub fn is_defined(&self, idx: usize) -> bool {
152 idx < self.len()
153 }
154
155 pub fn data(&self) -> &S::Vec<T> {
156 &self.data
157 }
158
159 pub fn data_mut(&mut self) -> &mut S::Vec<T> {
160 &mut self.data
161 }
162
163 pub fn as_string(&self, index: usize) -> String {
164 if index < self.len() {
165 self.data[index].to_string()
166 } else {
167 "none".to_string()
168 }
169 }
170
171 pub fn get_value(&self, index: usize) -> Value {
172 if index < self.len() {
173 self.data[index].to_value()
174 } else {
175 Value::none()
176 }
177 }
178
179 pub fn extend(&mut self, other: &Self) -> Result<()> {
180 DataVec::extend_iter(&mut self.data, other.data.iter().cloned());
181 Ok(())
182 }
183
184 pub fn iter(&self) -> impl Iterator<Item = Option<T>> + '_
185 where
186 T: Copy,
187 {
188 self.data.iter().map(|&v| Some(v))
189 }
190
191 pub fn slice(&self, start: usize, end: usize) -> Self {
192 let count = (end - start).min(self.len().saturating_sub(start));
193 let mut new_data = DataVec::spawn(&self.data, count);
194 for i in start..(start + count) {
195 DataVec::push(&mut new_data, self.data[i].clone());
196 }
197 Self {
198 data: new_data,
199 }
200 }
201
202 pub fn filter(&mut self, mask: &S::BitVec) {
203 let mut new_data = DataVec::spawn(&self.data, DataBitVec::count_ones(mask));
204
205 for (i, keep) in DataBitVec::iter(mask).enumerate() {
206 if keep && i < self.len() {
207 DataVec::push(&mut new_data, self.data[i].clone());
208 }
209 }
210
211 self.data = new_data;
212 }
213
214 pub fn reorder(&mut self, indices: &[usize]) {
215 let mut new_data = DataVec::spawn(&self.data, indices.len());
216
217 for &idx in indices {
218 if idx < self.len() {
219 DataVec::push(&mut new_data, self.data[idx].clone());
220 } else {
221 DataVec::push(&mut new_data, T::default());
222 }
223 }
224
225 self.data = new_data;
226 }
227
228 pub fn take(&self, num: usize) -> Self {
229 Self {
230 data: DataVec::take(&self.data, num),
231 }
232 }
233}
234
235impl<T> Default for UuidContainer<T, Cow>
236where
237 T: IsUuid + Clone + Debug + Default,
238{
239 fn default() -> Self {
240 Self::with_capacity(0)
241 }
242}
243
244#[cfg(test)]
245pub mod tests {
246 use super::*;
247 use crate::value::uuid::{Uuid4, Uuid7};
248
249 #[test]
250 fn test_uuid4_container() {
251 let uuid1 = Uuid4::generate();
252 let uuid2 = Uuid4::generate();
253 let uuids = vec![uuid1, uuid2];
254 let container = UuidContainer::from_vec(uuids.clone());
255
256 assert_eq!(container.len(), 2);
257 assert_eq!(container.get(0), Some(&uuids[0]));
258 assert_eq!(container.get(1), Some(&uuids[1]));
259
260 for i in 0..2 {
261 assert!(container.is_defined(i));
262 }
263 }
264
265 #[test]
266 fn test_uuid7_container() {
267 let uuid1 = Uuid7::generate();
268 let uuid2 = Uuid7::generate();
269 let uuids = vec![uuid1, uuid2];
270 let container = UuidContainer::from_vec(uuids.clone());
271
272 assert_eq!(container.len(), 2);
273 assert_eq!(container.get(0), Some(&uuids[0]));
274 assert_eq!(container.get(1), Some(&uuids[1]));
275 }
276
277 #[test]
278 fn test_with_capacity() {
279 let container: UuidContainer<Uuid4> = UuidContainer::with_capacity(10);
280 assert_eq!(container.len(), 0);
281 assert!(container.is_empty());
282 assert!(container.capacity() >= 10);
283 }
284
285 #[test]
286 fn test_push_with_default() {
287 let mut container: UuidContainer<Uuid4> = UuidContainer::with_capacity(3);
288 let uuid1 = Uuid4::generate();
289 let uuid2 = Uuid4::generate();
290
291 container.push(uuid1);
292 container.push_default();
293 container.push(uuid2);
294
295 assert_eq!(container.len(), 3);
296 assert_eq!(container.get(0), Some(&uuid1));
297 assert_eq!(container.get(1), Some(&Uuid4::default())); assert_eq!(container.get(2), Some(&uuid2));
299
300 assert!(container.is_defined(0));
301 assert!(container.is_defined(1));
302 assert!(container.is_defined(2));
303 }
304
305 #[test]
306 fn testault() {
307 let container: UuidContainer<Uuid4> = UuidContainer::default();
308 assert_eq!(container.len(), 0);
309 assert!(container.is_empty());
310 }
311}