File size: 5,448 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use common::types::PointOffsetType;
use itertools::Itertools;

use super::binary_index::BinaryIndex;
use super::map_index::MapIndex;
use crate::data_types::facets::{FacetHit, FacetValueRef};
use crate::index::struct_filter_context::StructFilterContext;
use crate::payload_storage::FilterContext;
use crate::types::{IntPayloadType, UuidIntType};

pub enum FacetIndex<'a> {
    Keyword(&'a MapIndex<str>),
    Int(&'a MapIndex<IntPayloadType>),
    Uuid(&'a MapIndex<UuidIntType>),
    Bool(&'a BinaryIndex),
}

impl<'a> FacetIndex<'a> {
    pub fn get_values(
        &self,
        point_id: PointOffsetType,
    ) -> Box<dyn Iterator<Item = FacetValueRef<'a>> + 'a> {
        match self {
            FacetIndex::Keyword(index) => {
                let iter = index
                    .get_values(point_id)
                    .into_iter()
                    .flatten() // flatten the Option
                    .map(FacetValueRef::Keyword);

                Box::new(iter)
            }
            FacetIndex::Int(index) => {
                let iter = index
                    .get_values(point_id)
                    .into_iter()
                    .flatten() // flatten the Option
                    .map(FacetValueRef::Int);

                Box::new(iter)
            }
            FacetIndex::Uuid(index) => {
                let iter = index
                    .get_values(point_id)
                    .into_iter()
                    .flatten() // flatten the Option
                    .map(FacetValueRef::Uuid);

                Box::new(iter)
            }
            FacetIndex::Bool(index) => {
                let iter = vec![
                    index.values_has_true(point_id).then_some(true),
                    index.values_has_false(point_id).then_some(false),
                ]
                .into_iter()
                .flatten()
                .map(FacetValueRef::Bool);

                Box::new(iter)
            }
        }
    }

    pub fn iter_values(&self) -> Box<dyn Iterator<Item = FacetValueRef<'a>> + 'a> {
        match self {
            FacetIndex::Keyword(index) => {
                let iter = index.iter_values().map(FacetValueRef::Keyword);
                Box::new(iter)
            }
            FacetIndex::Int(index) => {
                let iter = index.iter_values().map(FacetValueRef::Int);
                Box::new(iter)
            }
            FacetIndex::Uuid(index) => {
                let iter = index.iter_values().map(FacetValueRef::Uuid);
                Box::new(iter)
            }
            FacetIndex::Bool(_index) => {
                let iter = vec![true, false].into_iter().map(FacetValueRef::Bool);
                Box::new(iter)
            }
        }
    }

    pub fn iter_filtered_counts_per_value(
        &self,
        context: &'a StructFilterContext,
    ) -> impl Iterator<Item = FacetHit<FacetValueRef<'a>>> + 'a {
        let iter: Box<dyn Iterator<Item = _>> = match self {
            FacetIndex::Keyword(index) => {
                let iter = index
                    .iter_values_map()
                    .map(|(value, ids_iter)| (FacetValueRef::Keyword(value), ids_iter));
                Box::new(iter)
            }
            FacetIndex::Int(index) => {
                let iter = index
                    .iter_values_map()
                    .map(|(value, ids_iter)| (FacetValueRef::Int(value), ids_iter));
                Box::new(iter)
            }
            FacetIndex::Uuid(index) => {
                let iter = index
                    .iter_values_map()
                    .map(|(value, ids_iter)| (FacetValueRef::Uuid(value), ids_iter));
                Box::new(iter)
            }
            FacetIndex::Bool(index) => {
                let iter = index
                    .iter_values_map()
                    .map(|(value, ids_iter)| (FacetValueRef::Bool(value), ids_iter));
                Box::new(iter)
            }
        };

        iter.map(|(value, internal_ids_iter)| FacetHit {
            value,
            count: internal_ids_iter
                .unique()
                .filter(|&point_id| context.check(point_id))
                .count(),
        })
    }
    pub fn iter_counts_per_value(
        &'a self,
    ) -> impl Iterator<Item = FacetHit<FacetValueRef<'a>>> + 'a {
        let iter: Box<dyn Iterator<Item = _>> = match self {
            FacetIndex::Keyword(index) => {
                let iter = index
                    .iter_counts_per_value()
                    .map(|(value, count)| (FacetValueRef::Keyword(value), count));
                Box::new(iter)
            }
            FacetIndex::Int(index) => {
                let iter = index
                    .iter_counts_per_value()
                    .map(|(value, count)| (FacetValueRef::Int(value), count));
                Box::new(iter)
            }
            FacetIndex::Uuid(index) => {
                let iter = index
                    .iter_counts_per_value()
                    .map(|(value, count)| (FacetValueRef::Uuid(value), count));
                Box::new(iter)
            }
            FacetIndex::Bool(index) => {
                let iter = index
                    .iter_counts_per_value()
                    .map(|(value, count)| (FacetValueRef::Bool(value), count));
                Box::new(iter)
            }
        };

        iter.map(|(value, count)| FacetHit { value, count })
    }
}