@@ -227,6 +227,7 @@ pub struct QueryResult {
227
227
}
228
228
229
229
pub struct Service {
230
+ content_endpoint : Option < String > ,
230
231
api_key : String ,
231
232
path : PathBuf ,
232
233
vector_store : VectorStore ,
@@ -235,14 +236,20 @@ pub struct Service {
235
236
indexes : RwLock < HashMap < String , Arc < HnswIndex > > > ,
236
237
}
237
238
239
+ #[ derive( Debug , Error ) ]
240
+ enum StartIndexError {
241
+ #[ error( "No content endpoint found: specify at server startup or supply indexing data from the command line" ) ]
242
+ NoContentEndpoint ,
243
+ }
244
+
238
245
async fn extract_body ( req : Request < Body > ) -> Bytes {
239
246
hyper:: body:: to_bytes ( req. into_body ( ) ) . await . unwrap ( )
240
247
}
241
248
242
249
enum TerminusIndexOperationError { }
243
250
244
- const TERMINUSDB_INDEX_ENDPOINT : & str = "http://localhost:6363/api/index" ;
245
- async fn get_operations_from_terminusdb (
251
+ async fn get_operations_from_content_endpoint (
252
+ content_endpoint : String ,
246
253
domain : String ,
247
254
commit : String ,
248
255
previous : Option < String > ,
@@ -251,7 +258,7 @@ async fn get_operations_from_terminusdb(
251
258
if let Some ( previous) = previous {
252
259
params. push ( ( "previous" , previous) )
253
260
}
254
- let endpoint = format ! ( "{}/{}" , TERMINUSDB_INDEX_ENDPOINT , & domain) ;
261
+ let endpoint = format ! ( "{}/{}" , content_endpoint , & domain) ;
255
262
let url = reqwest:: Url :: parse_with_params ( & endpoint, & params) . unwrap ( ) ;
256
263
let res = reqwest:: get ( url)
257
264
. await
@@ -322,9 +329,15 @@ impl Service {
322
329
s
323
330
}
324
331
325
- fn new < P : Into < PathBuf > > ( path : P , num_bufs : usize , key : String ) -> Self {
332
+ fn new < P : Into < PathBuf > > (
333
+ path : P ,
334
+ num_bufs : usize ,
335
+ key : String ,
336
+ content_endpoint : Option < String > ,
337
+ ) -> Self {
326
338
let path = path. into ( ) ;
327
339
Service {
340
+ content_endpoint,
328
341
api_key : key,
329
342
path : path. clone ( ) ,
330
343
vector_store : VectorStore :: new ( path, num_bufs) ,
@@ -360,26 +373,33 @@ impl Service {
360
373
commit : String ,
361
374
previous : Option < String > ,
362
375
task_id : String ,
363
- ) {
364
- tokio:: spawn ( async move {
365
- let index_id = create_index_name ( & domain, & commit) ;
366
- if self . test_and_set_pending ( index_id. clone ( ) ) . await {
367
- let opstream = get_operations_from_terminusdb (
368
- domain. clone ( ) ,
369
- commit. clone ( ) ,
370
- previous. clone ( ) ,
371
- )
372
- . await
373
- . unwrap ( )
374
- . chunks ( 100 ) ;
375
- let ( id, hnsw) = self
376
- . process_operation_chunks ( opstream, domain, commit, previous, & index_id)
377
- . await ;
378
- self . set_index ( id, hnsw. into ( ) ) . await ;
379
- self . clear_pending ( & index_id) . await ;
380
- }
381
- self . set_task_status ( task_id, TaskStatus :: Completed ) . await ;
382
- } ) ;
376
+ ) -> Result < ( ) , StartIndexError > {
377
+ let content_endpoint = self . content_endpoint . clone ( ) ;
378
+ if let Some ( content_endpoint) = content_endpoint {
379
+ tokio:: spawn ( async move {
380
+ let index_id = create_index_name ( & domain, & commit) ;
381
+ if self . test_and_set_pending ( index_id. clone ( ) ) . await {
382
+ let opstream = get_operations_from_content_endpoint (
383
+ content_endpoint. to_string ( ) ,
384
+ domain. clone ( ) ,
385
+ commit. clone ( ) ,
386
+ previous. clone ( ) ,
387
+ )
388
+ . await
389
+ . unwrap ( )
390
+ . chunks ( 100 ) ;
391
+ let ( id, hnsw) = self
392
+ . process_operation_chunks ( opstream, domain, commit, previous, & index_id)
393
+ . await ;
394
+ self . set_index ( id, hnsw. into ( ) ) . await ;
395
+ self . clear_pending ( & index_id) . await ;
396
+ }
397
+ self . set_task_status ( task_id, TaskStatus :: Completed ) . await ;
398
+ } ) ;
399
+ Ok ( ( ) )
400
+ } else {
401
+ Err ( StartIndexError :: NoContentEndpoint )
402
+ }
383
403
}
384
404
385
405
async fn assign_index (
@@ -454,8 +474,13 @@ impl Service {
454
474
let task_id = Service :: generate_task ( ) ;
455
475
self . set_task_status ( task_id. clone ( ) , TaskStatus :: Pending )
456
476
. await ;
457
- self . start_indexing ( domain, commit, previous, task_id. clone ( ) ) ;
458
- Ok ( Response :: builder ( ) . body ( task_id. into ( ) ) . unwrap ( ) )
477
+ match self . start_indexing ( domain, commit, previous, task_id. clone ( ) ) {
478
+ Ok ( ( ) ) => Ok ( Response :: builder ( ) . body ( task_id. into ( ) ) . unwrap ( ) ) ,
479
+ Err ( e) => Ok ( Response :: builder ( )
480
+ . status ( 400 )
481
+ . body ( e. to_string ( ) . into ( ) )
482
+ . unwrap ( ) ) ,
483
+ }
459
484
}
460
485
Ok ( ResourceSpec :: AssignIndex {
461
486
domain,
@@ -598,9 +623,10 @@ pub async fn serve<P: Into<PathBuf>>(
598
623
port : u16 ,
599
624
num_bufs : usize ,
600
625
key : String ,
626
+ content_endpoint : Option < String > ,
601
627
) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > {
602
628
let addr = SocketAddr :: new ( IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) , port) ;
603
- let service = Arc :: new ( Service :: new ( directory, num_bufs, key) ) ;
629
+ let service = Arc :: new ( Service :: new ( directory, num_bufs, key, content_endpoint ) ) ;
604
630
let make_svc = make_service_fn ( move |_conn| {
605
631
let s = service. clone ( ) ;
606
632
async {
0 commit comments