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