Browse Source

feat(searcher): a little query optimization

iwanhae 1 year ago
parent
commit
4bc303aa09
2 changed files with 12 additions and 1 deletions
  1. 1 0
      searcher/src/main.rs
  2. 11 1
      searcher/src/server/handler.rs

+ 1 - 0
searcher/src/main.rs

@@ -50,6 +50,7 @@ async fn main() -> std::io::Result<()> {
             .app_data(app_data_encoder.clone())
             .app_data(app_data_database.clone())
             .app_data(app_data_vector.clone())
+            .service(server::handler::healthz)
             .service(server::handler::hello)
             .service(server::handler::search)
     })

+ 11 - 1
searcher/src/server/handler.rs

@@ -1,3 +1,5 @@
+use std::sync::atomic::{AtomicU64, Ordering};
+
 use super::types;
 use actix::Addr;
 use actix_web::{get, web::Data, web::Path, HttpResponse, Responder};
@@ -11,6 +13,11 @@ use crate::{
     vector_db::{self, VectorDB},
 };
 
+#[get("/healthz")]
+async fn healthz() -> impl Responder {
+    HttpResponse::Ok().body("ok")
+}
+
 #[get("/")]
 async fn hello(pool: Data<Pool<SqliteConnectionManager>>) -> impl Responder {
     let conn = pool.get().unwrap();
@@ -29,7 +36,10 @@ async fn hello(pool: Data<Pool<SqliteConnectionManager>>) -> impl Responder {
 
     let mut samples: Vec<String> = vec![];
     for row in conn
-        .prepare_cached("SELECT summary FROM function_analyses ORDER BY RANDOM() LIMIT 10;")
+        .prepare_cached(
+            r"SELECT summary FROM function_analyses WHERE id 
+            IN (SELECT id FROM function_analyses ORDER BY RANDOM() LIMIT 10);",
+        )
         .unwrap()
         .query_map([], |row| row.get::<usize, String>(0))
         .unwrap()