Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic implementation of comma-separated json #110

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion crates/corro-agent/src/api/public/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use hyper::StatusCode;
use itertools::Itertools;
use metrics::counter;
use rusqlite::{named_params, params_from_iter, ToSql, Transaction};
use serde::Deserialize;
use spawn::spawn_counted;
use tokio::{
sync::{
Expand Down Expand Up @@ -464,8 +465,14 @@ async fn build_query_rows_response(
}
}

#[derive(Deserialize)]
pub struct V1QueriesParams {
ndjson: bool,
}

pub async fn api_v1_queries(
Extension(agent): Extension<Agent>,
axum::extract::Query(pms): axum::extract::Query<V1QueriesParams>,
axum::extract::Json(stmt): axum::extract::Json<Statement>,
) -> impl IntoResponse {
let (mut tx, body) = hyper::Body::channel();
Expand All @@ -474,6 +481,13 @@ pub async fn api_v1_queries(
let (data_tx, mut data_rx) = channel(512);

tokio::spawn(async move {
if !pms.ndjson {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could do Accept: application/ndjson as a header. At least it's generally how content type is negotiated :)

I just invented that, but maybe there's a better mime type for that.

if let Err(e) = tx.send_data(bytes::Bytes::from_static(b"[")).await {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cleaner to implement a tokio_util::codec::{Encoder,Decoder} for this format!

I wouldn't hold back this PR for that though!

error!("could not send data through body's channel: {e}");
return;
}
}

let mut buf = BytesMut::new();

while let Some(row_res) = data_rx.recv().await {
Expand All @@ -493,13 +507,23 @@ pub async fn api_v1_queries(
}
}

buf.extend_from_slice(b"\n");
if !matches!(row_res, QueryEvent::EndOfQuery { .. }) {
buf.extend_from_slice(if pms.ndjson { b"\n" } else { b"," });
}

if let Err(e) = tx.send_data(buf.split().freeze()).await {
error!("could not send data through body's channel: {e}");
return;
}
}

if !pms.ndjson {
if let Err(e) = tx.send_data(bytes::Bytes::from_static(b"]")).await {
error!("could not send data through body's channel: {e}");
return;
}
}

debug!("query body channel done");
});

Expand Down Expand Up @@ -783,6 +807,7 @@ mod tests {

let res = api_v1_queries(
Extension(agent.clone()),
axum::extract::Query(V1QueriesParams { ndjson: true }),
axum::Json(Statement::Simple("select * from tests".into())),
)
.await
Expand Down
Loading