Skip to content

Commit

Permalink
toggle on web interface, cached query
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Jan 26, 2024
1 parent 9b97327 commit d9926d4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion search/src/index/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Documents {
let mut prev = "";

writer.write_vbyte(documents.len() as u32);
for l in documents.iter() {
for l in documents {
let p_len = utils::get_matching_prefix_len(prev, &l.path);
writer.write_gamma(p_len as u32);
let remaining: String = l.path.chars().skip(p_len).collect();
Expand Down
29 changes: 24 additions & 5 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ use serde::{Deserialize, Serialize};
use std::{
env,
fs::read_to_string,
mem::replace,
sync::{Arc, Mutex},
};

struct AppState {
index_path: String,
query_processor: Mutex<Processor>,
cached_query: Mutex<String>,
cached_result: Mutex<Option<QueryResponse>>,
}

#[tokio::main]
Expand All @@ -40,6 +43,8 @@ async fn main() {
let state = Arc::new(AppState {
index_path: base_path.clone(),
query_processor: Mutex::new(Processor::build_query_processor(&index_path)),
cached_query: Mutex::new(String::new()),
cached_result: Mutex::new(None),
});

let app = Router::new()
Expand Down Expand Up @@ -91,15 +96,15 @@ struct QueryRequest {
query: String,
}

#[derive(Template)]
#[derive(Template, Clone)]
#[template(path = "query.html")]
struct QueryResponse {
tokens: Vec<String>,
time_ms: u128,
documents: Vec<Document>,
}

#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Clone)]
struct Document {
id: u32,
score: f64,
Expand All @@ -112,7 +117,15 @@ async fn post_query(
State(state): State<Arc<AppState>>,
Json(payload): Json<QueryRequest>,
) -> impl IntoResponse {
info!("Query request: {:?}", payload);
info!("Query request: {}", payload.query);

let mut cq = state.cached_query.lock().unwrap();
let mut cqr = state.cached_result.lock().unwrap();

if *cq == payload.query {
info!("Cache hit for query: {}", payload.query);
return HtmlTemplate(cqr.clone().unwrap());
}

let mut q = state.query_processor.lock().unwrap();

Expand All @@ -129,11 +142,17 @@ async fn post_query(
})
.collect();

HtmlTemplate(QueryResponse {
let response = QueryResponse {
tokens: query_result.tokens,
documents,
time_ms: query_result.time_ms,
})
};

info!("Replacing cache with query: {}", payload.query);
let _ = replace(&mut *cq, payload.query);
let _ = replace(&mut *cqr, Some(response.clone()));

HtmlTemplate(response)
}

fn read_file_content(path: String) -> String {
Expand Down
21 changes: 21 additions & 0 deletions server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@
}
</style>

<script>
document.addEventListener('htmx:afterSwap', function () {
var containers = document.querySelectorAll('.toggle-container');
containers.forEach(function (container) {
container.addEventListener('click', function (event) {
var containedId = container.id.replace('_open', '').replace('_closed', ''); // Extract the base ID
var op = document.getElementById(containedId + '_open');
var cl = document.getElementById(containedId + '_closed');

if (op.style.display !== "none") {
op.style.display = "none";
cl.style.display = "block";
} else {
op.style.display = "block";
cl.style.display = "none";
}
});
});
});
</script>

<title>search-rs</title>
</head>

Expand Down
14 changes: 11 additions & 3 deletions server/templates/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ <h1 class=" font-light text-md mb-6">

{% for doc in documents %}

<div>
<div class="bg-neutral-800 p-6 rounded-md shadow-md mb-6">

<div id="{{doc.path}}" class="toggle-container">
<div id="{{doc.path}}_closed" class="bg-neutral-800 p-6 rounded-md shadow-md mb-6">
<h2 class="text-xl font-semibold mb-4">
{{ doc.path }}
</h2>
<p class="text-neutral-300">
{{ doc.content|truncate(150) }}
</p>
</div>
<div id="{{doc.path}}_open" class="bg-neutral-800 p-6 rounded-md shadow-md mb-6" style="display:none;">
<h2 class="text-xl font-semibold mb-4">
{{ doc.path }}
</h2>
<p class="text-neutral-300">
<!-- {{ doc.content|truncate(300) }} -->
{{ doc.content }}
</p>
</div>
Expand Down

0 comments on commit d9926d4

Please sign in to comment.