Skip to content

Commit 8059dcd

Browse files
authored
fix: only upload chunks to orama if there are more than 0 chunks (#901)
1 parent 8a56fb0 commit 8059dcd

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

api/src/orama.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,34 +186,37 @@ impl OramaClient {
186186

187187
let chunks = {
188188
let str_data = serde_json::to_string(&search).unwrap();
189-
(str_data.len() as f64 / MAX_ORAMA_INSERT_SIZE).ceil() as usize
189+
((str_data.len() as f64 / MAX_ORAMA_INSERT_SIZE).ceil() as usize).max(1)
190190
};
191191

192-
for chunk in search.chunks(search.len() / chunks) {
193-
let body = serde_json::json!({ "upsert": &chunk });
194-
let package = format!("{scope_name}/{package_name}");
195-
let span = Span::current();
196-
let client = self.clone();
197-
let path = format!("/webhooks/{}/notify", self.symbols_index_id);
198-
tokio::spawn(
199-
async move {
200-
let res = match client.request(&path, body).await {
201-
Ok(res) => res,
202-
Err(err) => {
203-
error!("failed to insert on OramaClient::upsert_symbols: {err}");
204-
return;
205-
}
206-
};
207-
let status = res.status();
208-
if !status.is_success() {
209-
let response = res.text().await.unwrap_or_default();
210-
error!(
192+
let chunks_size = search.len() / chunks;
193+
if chunks_size != 0 {
194+
for chunk in search.chunks(chunks_size) {
195+
let body = serde_json::json!({ "upsert": &chunk });
196+
let package = format!("{scope_name}/{package_name}");
197+
let span = Span::current();
198+
let client = self.clone();
199+
let path = format!("/webhooks/{}/notify", self.symbols_index_id);
200+
tokio::spawn(
201+
async move {
202+
let res = match client.request(&path, body).await {
203+
Ok(res) => res,
204+
Err(err) => {
205+
error!("failed to insert on OramaClient::upsert_symbols: {err}");
206+
return;
207+
}
208+
};
209+
let status = res.status();
210+
if !status.is_success() {
211+
let response = res.text().await.unwrap_or_default();
212+
error!(
211213
"failed to insert on OramaClient::upsert_symbols for {package} (status {status}): {response}"
212214
);
215+
}
213216
}
214-
}
215-
.instrument(span),
216-
);
217+
.instrument(span),
218+
);
219+
}
217220
}
218221
}
219222
}

0 commit comments

Comments
 (0)