File size: 4,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
use api::rest::schema::ShardKeySelector;
use segment::data_types::vectors::DEFAULT_VECTOR_NAME;
use segment::types::PointIdType;

use crate::operations::types::{DiscoverRequestInternal, RecommendRequestInternal, UsingVector};
use crate::operations::universal_query::collection_query::{
    CollectionQueryResolveRequest, VectorInputInternal, VectorQuery,
};

const EMPTY_SHARD_KEY_SELECTOR: Option<ShardKeySelector> = None;

pub trait RetrieveRequest {
    fn get_lookup_collection(&self) -> Option<&String>;

    fn get_referenced_point_ids(&self) -> Vec<PointIdType>;

    fn get_lookup_vector_name(&self) -> String;

    fn get_lookup_shard_key(&self) -> &Option<ShardKeySelector>;
}

impl RetrieveRequest for RecommendRequestInternal {
    fn get_lookup_collection(&self) -> Option<&String> {
        self.lookup_from.as_ref().map(|x| &x.collection)
    }

    fn get_referenced_point_ids(&self) -> Vec<PointIdType> {
        self.positive
            .iter()
            .chain(self.negative.iter())
            .filter_map(|example| example.as_point_id())
            .collect()
    }

    fn get_lookup_vector_name(&self) -> String {
        match &self.lookup_from {
            None => match &self.using {
                None => DEFAULT_VECTOR_NAME.to_owned(),
                Some(UsingVector::Name(vector_name)) => vector_name.clone(),
            },
            Some(lookup_from) => match &lookup_from.vector {
                None => DEFAULT_VECTOR_NAME.to_owned(),
                Some(vector_name) => vector_name.clone(),
            },
        }
    }

    fn get_lookup_shard_key(&self) -> &Option<ShardKeySelector> {
        self.lookup_from
            .as_ref()
            .map(|x| &x.shard_key)
            .unwrap_or(&EMPTY_SHARD_KEY_SELECTOR)
    }
}

impl RetrieveRequest for DiscoverRequestInternal {
    fn get_lookup_collection(&self) -> Option<&String> {
        self.lookup_from.as_ref().map(|x| &x.collection)
    }

    fn get_referenced_point_ids(&self) -> Vec<PointIdType> {
        let mut res = Vec::new();

        match &self.target {
            None => {}
            Some(example) => {
                if let Some(point_id) = example.as_point_id() {
                    res.push(point_id);
                }
            }
        }

        if let Some(context) = &self.context {
            for pair in context {
                if let Some(pos_id) = pair.positive.as_point_id() {
                    res.push(pos_id);
                }
                if let Some(neg_id) = pair.negative.as_point_id() {
                    res.push(neg_id);
                }
            }
        }

        res
    }

    fn get_lookup_vector_name(&self) -> String {
        match &self.lookup_from {
            None => match &self.using {
                None => DEFAULT_VECTOR_NAME.to_owned(),
                Some(UsingVector::Name(vector_name)) => vector_name.clone(),
            },
            Some(lookup_from) => match &lookup_from.vector {
                None => DEFAULT_VECTOR_NAME.to_owned(),
                Some(vector_name) => vector_name.clone(),
            },
        }
    }

    fn get_lookup_shard_key(&self) -> &Option<ShardKeySelector> {
        self.lookup_from
            .as_ref()
            .map(|x| &x.shard_key)
            .unwrap_or(&EMPTY_SHARD_KEY_SELECTOR)
    }
}

impl<'a> RetrieveRequest for CollectionQueryResolveRequest<'a> {
    fn get_lookup_collection(&self) -> Option<&String> {
        self.lookup_from.as_ref().map(|x| &x.collection)
    }

    fn get_referenced_point_ids(&self) -> Vec<PointIdType> {
        self.vector_query
            .get_referenced_ids()
            .iter()
            .map(|x| **x)
            .collect()
    }

    fn get_lookup_vector_name(&self) -> String {
        match &self.lookup_from {
            None => self.using.clone(),
            Some(lookup_from) => match &lookup_from.vector {
                None => self.using.clone(),
                Some(vector_name) => vector_name.clone(),
            },
        }
    }

    fn get_lookup_shard_key(&self) -> &Option<ShardKeySelector> {
        self.lookup_from
            .as_ref()
            .map(|x| &x.shard_key)
            .unwrap_or(&EMPTY_SHARD_KEY_SELECTOR)
    }
}
impl VectorQuery<VectorInputInternal> {
    pub fn get_referenced_ids(&self) -> Vec<&PointIdType> {
        self.flat_iter()
            .filter_map(VectorInputInternal::as_id)
            .collect()
    }
}