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