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