Skip to content

Commit 482a77d

Browse files
Refactor responses
1 parent 2ad8aca commit 482a77d

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

src/server.rs

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ enum ResponseError {
315315
StartIndexError(#[from] StartIndexError),
316316
#[error("{0:?}")]
317317
SearchError(#[from] SearchError),
318+
#[error("Missing id in index {0}")]
319+
IdMissing(String),
318320
}
319321

320322
fn add_to_duplicates(duplicates: &mut HashMap<usize, usize>, id1: usize, id2: usize) {
@@ -526,7 +528,7 @@ impl Service {
526528
previous,
527529
}) => {
528530
let result = self.get_start_index(req, domain, commit, previous).await;
529-
fun_name(result)
531+
string_response_or_error(result)
530532
}
531533
Ok(ResourceSpec::AssignIndex {
532534
domain,
@@ -569,45 +571,16 @@ impl Service {
569571
let result = self
570572
.get_duplicate_candidates(domain, commit, threshold)
571573
.await;
572-
match result {
573-
Ok(result) => todo!(),
574-
Err(e) => todo!(),
575-
}
574+
string_response_or_error(result)
576575
}
577576
Ok(ResourceSpec::Similar {
578577
domain,
579578
commit,
580579
count,
581580
id,
582581
}) => {
583-
let index_id = create_index_name(&domain, &commit);
584-
// if None, then return 404
585-
let hnsw = self.get_index(&index_id).await.unwrap();
586-
let elts = hnsw.layer_len(0);
587-
let mut qp = None;
588-
for i in 0..elts {
589-
if *hnsw.feature(i).id() == id {
590-
qp = Some(hnsw.feature(i))
591-
}
592-
}
593-
match qp {
594-
Some(qp) => {
595-
let res = search(qp, count, &hnsw).unwrap();
596-
let ids: Vec<QueryResult> = res
597-
.iter()
598-
.map(|p| QueryResult {
599-
id: p.id().to_string(),
600-
distance: f32::from_bits(p.distance()),
601-
})
602-
.collect();
603-
let s = serde_json::to_string(&ids).unwrap();
604-
Ok(Response::builder().body(s.into()).unwrap())
605-
}
606-
None => Ok(Response::builder()
607-
.status(StatusCode::NOT_FOUND)
608-
.body("id not found".into())
609-
.unwrap()),
610-
}
582+
let result = self.get_similar_documents(domain, commit, id, count).await;
583+
string_response_or_error(result)
611584
}
612585
Ok(_) => todo!(),
613586
Err(e) => Ok(Response::builder()
@@ -617,6 +590,40 @@ impl Service {
617590
}
618591
}
619592

593+
async fn get_similar_documents(
594+
self: Arc<Self>,
595+
domain: String,
596+
commit: String,
597+
id: String,
598+
count: usize,
599+
) -> Result<String, ResponseError> {
600+
let index_id = create_index_name(&domain, &commit);
601+
// if None, then return 404
602+
let hnsw = self.get_index(&index_id).await?;
603+
let elts = hnsw.layer_len(0);
604+
let mut qp = None;
605+
for i in 0..elts {
606+
if *hnsw.feature(i).id() == id {
607+
qp = Some(hnsw.feature(i))
608+
}
609+
}
610+
match qp {
611+
Some(qp) => {
612+
let res = search(qp, count, &hnsw)?;
613+
let ids: Vec<QueryResult> = res
614+
.iter()
615+
.map(|p| QueryResult {
616+
id: p.id().to_string(),
617+
distance: f32::from_bits(p.distance()),
618+
})
619+
.collect();
620+
let s = serde_json::to_string(&ids)?;
621+
Ok(s)
622+
}
623+
None => Err(ResponseError::IdMissing(id)),
624+
}
625+
}
626+
620627
async fn get_duplicate_candidates(
621628
self: Arc<Self>,
622629
domain: String,
@@ -702,7 +709,9 @@ impl Service {
702709
}
703710
}
704711

705-
fn fun_name(result: Result<String, ResponseError>) -> Result<Response<Body>, Infallible> {
712+
fn string_response_or_error(
713+
result: Result<String, ResponseError>,
714+
) -> Result<Response<Body>, Infallible> {
706715
match result {
707716
Ok(task_id) => Ok(Response::builder().body(task_id.into()).unwrap()),
708717
Err(e) => Ok(Response::builder()

0 commit comments

Comments
 (0)