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