4
4
*/
5
5
6
6
use crate :: ColumnName ;
7
+ use crate :: Embeddings ;
7
8
use crate :: IndexMetadata ;
8
9
use crate :: Key ;
9
10
use crate :: TableName ;
@@ -25,12 +26,23 @@ pub(crate) enum DbIndex {
25
26
GetProcessedIds {
26
27
tx : oneshot:: Sender < anyhow:: Result < BoxStream < ' static , Result < Key , NextRowError > > > > ,
27
28
} ,
29
+
30
+ GetItems {
31
+ #[ allow( clippy:: type_complexity) ]
32
+ tx : oneshot:: Sender <
33
+ anyhow:: Result < BoxStream < ' static , Result < ( Key , Embeddings ) , NextRowError > > > ,
34
+ > ,
35
+ } ,
28
36
}
29
37
30
38
pub ( crate ) trait DbIndexExt {
31
39
async fn get_processed_ids (
32
40
& self ,
33
41
) -> anyhow:: Result < BoxStream < ' static , Result < Key , NextRowError > > > ;
42
+
43
+ async fn get_items (
44
+ & self ,
45
+ ) -> anyhow:: Result < BoxStream < ' static , Result < ( Key , Embeddings ) , NextRowError > > > ;
34
46
}
35
47
36
48
impl DbIndexExt for mpsc:: Sender < DbIndex > {
@@ -41,6 +53,14 @@ impl DbIndexExt for mpsc::Sender<DbIndex> {
41
53
self . send ( DbIndex :: GetProcessedIds { tx } ) . await ?;
42
54
rx. await ?
43
55
}
56
+
57
+ async fn get_items (
58
+ & self ,
59
+ ) -> anyhow:: Result < BoxStream < ' static , Result < ( Key , Embeddings ) , NextRowError > > > {
60
+ let ( tx, rx) = oneshot:: channel ( ) ;
61
+ self . send ( DbIndex :: GetItems { tx } ) . await ?;
62
+ rx. await ?
63
+ }
44
64
}
45
65
46
66
pub ( crate ) async fn new (
@@ -67,12 +87,17 @@ async fn process(statements: Arc<Statements>, msg: DbIndex) {
67
87
. unwrap_or_else ( |_| {
68
88
warn ! ( "db_index::process: Db::GetProcessedIds: unable to send response" )
69
89
} ) ,
90
+
91
+ DbIndex :: GetItems { tx } => tx
92
+ . send ( statements. get_items ( ) . await )
93
+ . unwrap_or_else ( |_| warn ! ( "db_index::process: Db::GetItems: unable to send response" ) ) ,
70
94
}
71
95
}
72
96
73
97
struct Statements {
74
98
session : Arc < Session > ,
75
99
st_get_processed_ids : PreparedStatement ,
100
+ st_get_items : PreparedStatement ,
76
101
}
77
102
78
103
impl Statements {
@@ -86,6 +111,15 @@ impl Statements {
86
111
. await
87
112
. context ( "get_processed_ids_query" ) ?,
88
113
114
+ st_get_items : session
115
+ . prepare ( Self :: get_items_query (
116
+ & metadata. table_name ,
117
+ & metadata. key_name ,
118
+ & metadata. target_name ,
119
+ ) )
120
+ . await
121
+ . context ( "get_items_query" ) ?,
122
+
89
123
session,
90
124
} )
91
125
}
@@ -112,4 +146,26 @@ impl Statements {
112
146
. map_ok ( |( key, ) | ( key as u64 ) . into ( ) )
113
147
. boxed ( ) )
114
148
}
149
+
150
+ fn get_items_query ( table : & TableName , col_id : & ColumnName , col_emb : & ColumnName ) -> String {
151
+ format ! (
152
+ "
153
+ SELECT {col_id}, {col_emb}
154
+ FROM {table}
155
+ WHERE processed = FALSE
156
+ "
157
+ )
158
+ }
159
+
160
+ async fn get_items (
161
+ & self ,
162
+ ) -> anyhow:: Result < BoxStream < ' static , Result < ( Key , Embeddings ) , NextRowError > > > {
163
+ Ok ( self
164
+ . session
165
+ . execute_iter ( self . st_get_items . clone ( ) , ( ) )
166
+ . await ?
167
+ . rows_stream :: < ( i64 , Vec < f32 > ) > ( ) ?
168
+ . map_ok ( |( key, embeddings) | ( ( key as u64 ) . into ( ) , embeddings. into ( ) ) )
169
+ . boxed ( ) )
170
+ }
115
171
}
0 commit comments