Skip to main content

qdrant_edge/shard/retrieve/
record_internal.rs

1use crate::segment::data_types::order_by::OrderValue;
2use crate::segment::data_types::segment_record::SegmentRecord;
3use crate::segment::data_types::vectors::{DEFAULT_VECTOR_NAME, VectorRef, VectorStructInternal};
4use crate::segment::types::{Payload, PointIdType, ShardKey, VectorName};
5
6use crate::shard::operations::point_ops::{PointStructPersisted, VectorStructPersisted};
7
8/// Point data
9#[derive(Clone, Debug, PartialEq)]
10pub struct RecordInternal {
11    /// Id of the point
12    pub id: PointIdType,
13    /// Payload - values assigned to the point
14    pub payload: Option<Payload>,
15    /// Vector of the point
16    pub vector: Option<VectorStructInternal>,
17    /// Shard Key
18    pub shard_key: Option<ShardKey>,
19    /// Order value, if used for order_by
20    pub order_value: Option<OrderValue>,
21}
22
23impl RecordInternal {
24    pub fn new_empty(id: PointIdType) -> Self {
25        Self {
26            id,
27            payload: None,
28            vector: None,
29            shard_key: None,
30            order_value: None,
31        }
32    }
33
34    pub fn get_vector_by_name(&self, name: &VectorName) -> Option<VectorRef<'_>> {
35        match &self.vector {
36            Some(VectorStructInternal::Single(vector)) => {
37                (name == DEFAULT_VECTOR_NAME).then_some(VectorRef::from(vector))
38            }
39            Some(VectorStructInternal::MultiDense(vectors)) => {
40                (name == DEFAULT_VECTOR_NAME).then_some(VectorRef::from(vectors))
41            }
42            Some(VectorStructInternal::Named(vectors)) => vectors.get(name).map(VectorRef::from),
43            None => None,
44        }
45    }
46}
47
48impl From<SegmentRecord> for RecordInternal {
49    fn from(record: SegmentRecord) -> Self {
50        let SegmentRecord {
51            id,
52            payload,
53            vectors,
54        } = record;
55        Self {
56            id,
57            payload,
58            vector: vectors.map(VectorStructInternal::from),
59            shard_key: None,
60            order_value: None,
61        }
62    }
63}
64
65/// Warn: panics if the vector is empty
66impl TryFrom<RecordInternal> for PointStructPersisted {
67    type Error = String;
68
69    fn try_from(record: RecordInternal) -> Result<Self, Self::Error> {
70        let RecordInternal {
71            id,
72            payload,
73            vector,
74            shard_key: _,
75            order_value: _,
76        } = record;
77
78        if vector.is_none() {
79            return Err("Vector is empty".to_string());
80        }
81
82        Ok(Self {
83            id,
84            payload,
85            vector: VectorStructPersisted::from(vector.unwrap()),
86        })
87    }
88}
89
90#[cfg(feature = "api")]
91impl From<RecordInternal> for api::grpc::qdrant::RetrievedPoint {
92    fn from(record: RecordInternal) -> Self {
93        use api::conversions::json::payload_to_proto;
94        use api::grpc::conversions::convert_shard_key_to_grpc;
95
96        let RecordInternal {
97            id,
98            payload,
99            vector,
100            shard_key,
101            order_value,
102        } = record;
103        Self {
104            id: Some(id.into()),
105            payload: payload.map(payload_to_proto).unwrap_or_default(),
106            vectors: vector.map(api::grpc::qdrant::VectorsOutput::from),
107            shard_key: shard_key.map(convert_shard_key_to_grpc),
108            order_value: order_value.map(From::from),
109        }
110    }
111}
112
113#[cfg(feature = "api")]
114impl From<RecordInternal> for api::rest::Record {
115    fn from(value: RecordInternal) -> Self {
116        let RecordInternal {
117            id,
118            payload,
119            vector,
120            shard_key,
121            order_value,
122        } = value;
123        Self {
124            id,
125            payload,
126            vector: vector.map(api::rest::VectorStructOutput::from),
127            shard_key,
128            order_value,
129        }
130    }
131}