reifydb_value/value/container/
identity_id.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, identity::IdentityId, value_type::ValueType},
17};
18
19pub struct IdentityIdContainer<S: Storage = Cow> {
20 data: S::Vec<IdentityId>,
21}
22
23impl<S: Storage> Clone for IdentityIdContainer<S> {
24 fn clone(&self) -> Self {
25 Self {
26 data: self.data.clone(),
27 }
28 }
29}
30
31impl<S: Storage> Debug for IdentityIdContainer<S>
32where
33 S::Vec<IdentityId>: Debug,
34{
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.debug_struct("IdentityIdContainer").field("data", &self.data).finish()
37 }
38}
39
40impl<S: Storage> PartialEq for IdentityIdContainer<S>
41where
42 S::Vec<IdentityId>: PartialEq,
43{
44 fn eq(&self, other: &Self) -> bool {
45 self.data == other.data
46 }
47}
48
49impl Serialize for IdentityIdContainer<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<IdentityId>,
54 }
55 Helper {
56 data: &self.data,
57 }
58 .serialize(serializer)
59 }
60}
61
62impl<'de> Deserialize<'de> for IdentityIdContainer<Cow> {
63 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
64 #[derive(Deserialize)]
65 struct Helper {
66 data: CowVec<IdentityId>,
67 }
68 let h = Helper::deserialize(deserializer)?;
69 Ok(IdentityIdContainer {
70 data: h.data,
71 })
72 }
73}
74
75impl<S: Storage> Deref for IdentityIdContainer<S> {
76 type Target = [IdentityId];
77
78 fn deref(&self) -> &Self::Target {
79 self.data.as_slice()
80 }
81}
82
83impl IdentityIdContainer<Cow> {
84 pub fn new(data: Vec<IdentityId>) -> Self {
85 Self {
86 data: CowVec::new(data),
87 }
88 }
89
90 pub fn from_vec(data: Vec<IdentityId>) -> 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
103impl<S: Storage> IdentityIdContainer<S> {
104 pub fn from_parts(data: S::Vec<IdentityId>) -> Self {
105 Self {
106 data,
107 }
108 }
109
110 pub fn len(&self) -> usize {
111 DataVec::len(&self.data)
112 }
113
114 pub fn is_empty(&self) -> bool {
115 DataVec::is_empty(&self.data)
116 }
117
118 pub fn clear(&mut self) {
119 DataVec::clear(&mut self.data);
120 }
121
122 pub fn push(&mut self, value: impl Into<Option<IdentityId>>) {
123 let value = value.into();
124 match value {
125 Some(id) => {
126 DataVec::push(&mut self.data, id);
127 }
128 None => {
129 DataVec::push(&mut self.data, IdentityId::default());
130 }
131 }
132 }
133
134 pub fn push_default(&mut self) {
135 self.push(None);
136 }
137
138 pub fn get(&self, index: usize) -> Option<IdentityId> {
139 if index < self.len() {
140 Some(self.data[index])
141 } else {
142 None
143 }
144 }
145
146 pub fn iter(&self) -> impl Iterator<Item = Option<IdentityId>> + '_ {
147 self.data.iter().map(|&id| Some(id))
148 }
149
150 pub fn data(&self) -> &S::Vec<IdentityId> {
151 &self.data
152 }
153
154 pub fn data_mut(&mut self) -> &mut S::Vec<IdentityId> {
155 &mut self.data
156 }
157
158 pub fn is_defined(&self, idx: usize) -> bool {
159 idx < self.len()
160 }
161
162 pub fn extend(&mut self, other: &Self) -> Result<()> {
163 DataVec::extend_from_slice(&mut self.data, DataVec::as_slice(&other.data));
164 Ok(())
165 }
166
167 pub fn get_value(&self, index: usize) -> Value {
168 self.get(index).map(Value::IdentityId).unwrap_or(Value::none_of(ValueType::IdentityId))
169 }
170
171 pub fn filter(&mut self, mask: &S::BitVec) {
172 let mut new_data = DataVec::spawn(&self.data, DataBitVec::count_ones(mask));
173
174 for (i, keep) in DataBitVec::iter(mask).enumerate() {
175 if keep && i < DataVec::len(&self.data) {
176 DataVec::push(&mut new_data, self.data[i]);
177 }
178 }
179
180 self.data = new_data;
181 }
182
183 pub fn reorder(&mut self, indices: &[usize]) {
184 let mut new_data = DataVec::spawn(&self.data, indices.len());
185
186 for &index in indices {
187 if index < DataVec::len(&self.data) {
188 DataVec::push(&mut new_data, self.data[index]);
189 } else {
190 DataVec::push(&mut new_data, IdentityId::default());
191 }
192 }
193
194 self.data = new_data;
195 }
196
197 pub fn take(&self, num: usize) -> Self {
198 Self {
199 data: DataVec::take(&self.data, num),
200 }
201 }
202
203 pub fn slice(&self, start: usize, end: usize) -> Self {
204 let count = (end - start).min(self.len().saturating_sub(start));
205 let mut new_data = DataVec::spawn(&self.data, count);
206 for i in start..(start + count) {
207 DataVec::push(&mut new_data, self.data[i]);
208 }
209 Self {
210 data: new_data,
211 }
212 }
213
214 pub fn as_string(&self, index: usize) -> String {
215 self.get(index).map(|id| id.to_string()).unwrap_or_else(|| "none".to_string())
216 }
217
218 pub fn capacity(&self) -> usize {
219 DataVec::capacity(&self.data)
220 }
221}
222
223impl From<Vec<IdentityId>> for IdentityIdContainer<Cow> {
224 fn from(data: Vec<IdentityId>) -> Self {
225 Self::from_vec(data)
226 }
227}
228
229impl FromIterator<Option<IdentityId>> for IdentityIdContainer<Cow> {
230 fn from_iter<T: IntoIterator<Item = Option<IdentityId>>>(iter: T) -> Self {
231 let mut container = Self::with_capacity(0);
232 for item in iter {
233 container.push(item);
234 }
235 container
236 }
237}