Skip to main content

ledb_types/
index.rs

1use std::{
2    borrow::Cow,
3    hash::BuildHasher,
4    collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
5    iter::IntoIterator,
6    ops::{Deref, DerefMut},
7    rc::{Rc, Weak as RcWeak},
8    sync::{Arc, Mutex, RwLock, Weak as ArcWeak},
9    vec::IntoIter as VecIntoIter,
10};
11
12use serde::{Serialize, Deserialize};
13
14#[cfg(feature = "bytes")]
15use bytes::{Bytes, BytesMut};
16
17/// Indexed field definition
18#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub struct KeyField {
20    pub path: String,
21    #[serde(default)]
22    pub key: KeyType,
23    #[serde(default)]
24    pub kind: IndexKind,
25}
26
27impl KeyField {
28    /// Create key field from field name
29    #[inline]
30    pub fn new<S: ToString>(path: S) -> Self {
31        Self {
32            path: path.to_string(),
33            key: KeyType::default(),
34            kind: IndexKind::default(),
35        }
36    }
37
38    /// Add key type
39    #[inline]
40    pub fn with_type(mut self, key: KeyType) -> Self {
41        self.key = key;
42        self
43    }
44
45    /// Add index kind
46    #[inline]
47    pub fn with_kind(mut self, kind: IndexKind) -> Self {
48        self.kind = kind;
49        self
50    }
51
52    /// Set parent path
53    ///
54    /// This makes key field to be child for parent path
55    #[inline]
56    pub fn set_parent<S: AsRef<str>>(&mut self, parent: S) {
57        self.path.insert_str(0, ".");
58        self.path.insert_str(0, parent.as_ref());
59    }
60
61    /// Add parent path
62    ///
63    /// This makes key field to be child for parent path
64    #[inline]
65    pub fn with_parent<S: AsRef<str>>(mut self, parent: S) -> Self {
66        self.set_parent(parent);
67        self
68    }
69}
70
71impl<S: ToString> From<(S,)> for KeyField {
72    fn from((path,): (S,)) -> Self {
73        Self::new(path)
74    }
75}
76
77impl<'a, S: ToString> From<&'a (S,)> for KeyField {
78    fn from((path,): &(S,)) -> Self {
79        Self::new(path.to_string())
80    }
81}
82
83impl<S: ToString> From<(S, KeyType)> for KeyField {
84    fn from((path, key): (S, KeyType)) -> Self {
85        Self::new(path).with_type(key)
86    }
87}
88
89impl<'a, S: ToString> From<&'a (S, KeyType)> for KeyField {
90    fn from((path, key): &(S, KeyType)) -> Self {
91        Self::new(path.to_string()).with_type(*key)
92    }
93}
94
95impl<S: ToString> From<(S, IndexKind)> for KeyField {
96    fn from((path, kind): (S, IndexKind)) -> Self {
97        Self::new(path).with_kind(kind)
98    }
99}
100
101impl<'a, S: ToString> From<&'a (S, IndexKind)> for KeyField {
102    fn from((path, kind): &(S, IndexKind)) -> Self {
103        Self::new(path.to_string()).with_kind(*kind)
104    }
105}
106
107impl<S: ToString> From<(S, KeyType, IndexKind)> for KeyField {
108    fn from((path, key, kind): (S, KeyType, IndexKind)) -> Self {
109        Self {
110            path: path.to_string(),
111            key,
112            kind,
113        }
114    }
115}
116
117impl<'a, S: ToString> From<&'a (S, KeyType, IndexKind)> for KeyField {
118    fn from((path, key, kind): &(S, KeyType, IndexKind)) -> Self {
119        Self {
120            path: path.to_string(),
121            key: *key,
122            kind: *kind,
123        }
124    }
125}
126
127impl<S: ToString> From<(S, IndexKind, KeyType)> for KeyField {
128    fn from((path, kind, key): (S, IndexKind, KeyType)) -> Self {
129        Self {
130            path: path.to_string(),
131            key,
132            kind,
133        }
134    }
135}
136
137impl<'a, S: ToString> From<&'a (S, IndexKind, KeyType)> for KeyField {
138    fn from((path, kind, key): &(S, IndexKind, KeyType)) -> Self {
139        Self {
140            path: path.to_string(),
141            key: *key,
142            kind: *kind,
143        }
144    }
145}
146
147impl Into<(String, KeyType, IndexKind)> for KeyField {
148    fn into(self) -> (String, KeyType, IndexKind) {
149        let KeyField { path, key, kind } = self;
150        (path, key, kind)
151    }
152}
153
154impl Into<(String, IndexKind, KeyType)> for KeyField {
155    fn into(self) -> (String, IndexKind, KeyType) {
156        let KeyField { path, key, kind } = self;
157        (path, kind, key)
158    }
159}
160
161/// Indexed fields definition
162#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
163pub struct KeyFields(Vec<KeyField>);
164
165impl Default for KeyFields {
166    fn default() -> Self {
167        KeyFields(Vec::default())
168    }
169}
170
171impl KeyFields {
172    /// Create new key fields set
173    #[inline]
174    pub fn new() -> Self {
175        KeyFields::default()
176    }
177
178    /// Add key field to set
179    #[inline]
180    pub fn with_field<T>(mut self, field: T) -> Self
181    where
182        KeyField: From<T>,
183    {
184        self.push(KeyField::from(field));
185        self
186    }
187
188    /// Add key fields to set
189    #[inline]
190    pub fn with_fields(mut self, mut fields: KeyFields) -> Self {
191        self.append(&mut *fields);
192        self
193    }
194
195    /// Set parent path
196    ///
197    /// This makes key fields in set to be children for parent path
198    pub fn set_parent<S: AsRef<str>>(&mut self, parent: S) {
199        for field in &mut self.0 {
200            field.set_parent(&parent);
201        }
202    }
203
204    /// Add parent path
205    ///
206    /// This makes key fields in set to be children for parent path
207    pub fn with_parent<S: AsRef<str>>(mut self, parent: S) -> Self {
208        self.set_parent(&parent);
209        self
210    }
211}
212
213/*
214impl From<Vec<KeyField>> for KeyFields {
215    fn from(vec: Vec<KeyField>) -> Self {
216        KeyFields(vec)
217    }
218}
219
220impl<'a> From<&'a [KeyField]> for KeyFields {
221    fn from(arr: &[KeyField]) -> Self {
222        KeyFields(arr.into())
223    }
224}
225 */
226
227impl<'a, T> From<&'a [T]> for KeyFields
228where
229    T: Clone,
230    KeyField: From<T>,
231{
232    fn from(arr: &[T]) -> Self {
233        KeyFields(arr.iter().cloned().map(KeyField::from).collect())
234    }
235}
236
237impl<T> From<Vec<T>> for KeyFields
238where
239    KeyField: From<T>,
240{
241    fn from(vec: Vec<T>) -> Self {
242        KeyFields(vec.into_iter().map(KeyField::from).collect())
243    }
244}
245
246impl AsRef<[KeyField]> for KeyFields {
247    fn as_ref(&self) -> &[KeyField] {
248        self.0.as_ref()
249    }
250}
251
252impl AsMut<[KeyField]> for KeyFields {
253    fn as_mut(&mut self) -> &mut [KeyField] {
254        self.0.as_mut()
255    }
256}
257
258impl Deref for KeyFields {
259    type Target = Vec<KeyField>;
260
261    fn deref(&self) -> &Self::Target {
262        &self.0
263    }
264}
265
266impl DerefMut for KeyFields {
267    fn deref_mut(&mut self) -> &mut Self::Target {
268        &mut self.0
269    }
270}
271
272impl IntoIterator for KeyFields {
273    type Item = KeyField;
274    type IntoIter = VecIntoIter<Self::Item>;
275
276    fn into_iter(self) -> Self::IntoIter {
277        self.0.into_iter()
278    }
279}
280
281/// The type of key
282#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
283pub enum KeyType {
284    #[serde(rename = "int")]
285    Int,
286    #[serde(rename = "float")]
287    Float,
288    #[serde(rename = "string")]
289    String,
290    #[serde(rename = "binary")]
291    Binary,
292    #[serde(rename = "bool")]
293    Bool,
294}
295
296impl Default for KeyType {
297    fn default() -> Self {
298        KeyType::Binary
299    }
300}
301
302/// The kind of index
303#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
304pub enum IndexKind {
305    /// Index which may contains duplicates
306    #[serde(rename = "index")]
307    Index,
308    /// Index which contains unique keys only
309    #[serde(rename = "unique")]
310    Unique,
311}
312
313impl Default for IndexKind {
314    fn default() -> Self {
315        IndexKind::Index
316    }
317}
318
319/// Field key type inference
320pub trait DocumentKeyType {
321    /// Get type of field key by field type
322    fn key_type() -> KeyType {
323        KeyType::default()
324    }
325}
326
327impl DocumentKeyType for bool {
328    fn key_type() -> KeyType {
329        KeyType::Bool
330    }
331}
332
333impl DocumentKeyType for u8 {
334    fn key_type() -> KeyType {
335        KeyType::Int
336    }
337}
338
339impl DocumentKeyType for i8 {
340    fn key_type() -> KeyType {
341        KeyType::Int
342    }
343}
344
345impl DocumentKeyType for u16 {
346    fn key_type() -> KeyType {
347        KeyType::Int
348    }
349}
350
351impl DocumentKeyType for i16 {
352    fn key_type() -> KeyType {
353        KeyType::Int
354    }
355}
356
357impl DocumentKeyType for u32 {
358    fn key_type() -> KeyType {
359        KeyType::Int
360    }
361}
362
363impl DocumentKeyType for i32 {
364    fn key_type() -> KeyType {
365        KeyType::Int
366    }
367}
368
369impl DocumentKeyType for u64 {
370    fn key_type() -> KeyType {
371        KeyType::Int
372    }
373}
374
375impl DocumentKeyType for i64 {
376    fn key_type() -> KeyType {
377        KeyType::Int
378    }
379}
380
381impl DocumentKeyType for f32 {
382    fn key_type() -> KeyType {
383        KeyType::Float
384    }
385}
386
387impl DocumentKeyType for f64 {
388    fn key_type() -> KeyType {
389        KeyType::Float
390    }
391}
392
393impl DocumentKeyType for String {
394    fn key_type() -> KeyType {
395        KeyType::String
396    }
397}
398
399#[cfg(feature = "bytes")]
400impl DocumentKeyType for Bytes {
401    fn key_type() -> KeyType {
402        KeyType::Binary
403    }
404}
405
406#[cfg(feature = "bytes")]
407impl DocumentKeyType for BytesMut {
408    fn key_type() -> KeyType {
409        KeyType::Binary
410    }
411}
412
413impl<'a, T: DocumentKeyType> DocumentKeyType for &'a T {
414    fn key_type() -> KeyType {
415        T::key_type()
416    }
417}
418
419impl<'a, T: DocumentKeyType> DocumentKeyType for &'a mut T {
420    fn key_type() -> KeyType {
421        T::key_type()
422    }
423}
424
425impl<T: DocumentKeyType> DocumentKeyType for Box<T> {
426    fn key_type() -> KeyType {
427        T::key_type()
428    }
429}
430
431impl<T: DocumentKeyType> DocumentKeyType for Rc<T> {
432    fn key_type() -> KeyType {
433        T::key_type()
434    }
435}
436
437impl<T: DocumentKeyType> DocumentKeyType for RcWeak<T> {
438    fn key_type() -> KeyType {
439        T::key_type()
440    }
441}
442
443impl<T: DocumentKeyType> DocumentKeyType for Arc<T> {
444    fn key_type() -> KeyType {
445        T::key_type()
446    }
447}
448
449impl<T: DocumentKeyType> DocumentKeyType for ArcWeak<T> {
450    fn key_type() -> KeyType {
451        T::key_type()
452    }
453}
454
455impl<T: DocumentKeyType> DocumentKeyType for Mutex<T> {
456    fn key_type() -> KeyType {
457        T::key_type()
458    }
459}
460
461impl<T: DocumentKeyType> DocumentKeyType for RwLock<T> {
462    fn key_type() -> KeyType {
463        T::key_type()
464    }
465}
466
467impl<'a, T: DocumentKeyType> DocumentKeyType for &'a [T] {
468    fn key_type() -> KeyType {
469        T::key_type()
470    }
471}
472
473impl<'a, T: DocumentKeyType> DocumentKeyType for &'a mut [T] {
474    fn key_type() -> KeyType {
475        T::key_type()
476    }
477}
478
479impl<'a, T: DocumentKeyType + Clone> DocumentKeyType for Cow<'a, T> {
480    fn key_type() -> KeyType {
481        T::key_type()
482    }
483}
484
485impl<T: DocumentKeyType> DocumentKeyType for [T] {
486    fn key_type() -> KeyType {
487        T::key_type()
488    }
489}
490
491impl<T: DocumentKeyType> DocumentKeyType for Vec<T> {
492    fn key_type() -> KeyType {
493        T::key_type()
494    }
495}
496
497impl<T: DocumentKeyType> DocumentKeyType for VecDeque<T> {
498    fn key_type() -> KeyType {
499        T::key_type()
500    }
501}
502
503impl<T: DocumentKeyType, S: BuildHasher> DocumentKeyType for HashSet<T, S> {
504    fn key_type() -> KeyType {
505        T::key_type()
506    }
507}
508
509impl<K, T: DocumentKeyType, S: BuildHasher> DocumentKeyType for HashMap<K, T, S> {
510    fn key_type() -> KeyType {
511        T::key_type()
512    }
513}
514
515impl<T: DocumentKeyType> DocumentKeyType for BTreeSet<T> {
516    fn key_type() -> KeyType {
517        T::key_type()
518    }
519}
520
521impl<K, T: DocumentKeyType> DocumentKeyType for BTreeMap<K, T> {
522    fn key_type() -> KeyType {
523        T::key_type()
524    }
525}
526
527impl<T: DocumentKeyType> DocumentKeyType for Option<T> {
528    fn key_type() -> KeyType {
529        T::key_type()
530    }
531}