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 all commits
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
28 changes: 27 additions & 1 deletion crates/corro-agent/src/api/public/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,25 @@ async fn build_query_rows_response(

pub async fn api_v1_queries(
Extension(agent): Extension<Agent>,
headers: axum::headers::HeaderMap,
axum::extract::Json(stmt): axum::extract::Json<Statement>,
) -> impl IntoResponse {
// https://github.com/ndjson/ndjson-spec#33-mediatype-and-file-extensions
let ndjson = headers.get("accept").map(|a| a == "application/x-ndjson").unwrap_or(false);

let (mut tx, body) = hyper::Body::channel();

// TODO: timeout on data send instead of infinitely waiting for channel space.
let (data_tx, mut data_rx) = channel(512);

tokio::spawn(async move {
if !ndjson {
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 +504,23 @@ pub async fn api_v1_queries(
}
}

buf.extend_from_slice(b"\n");
if !matches!(row_res, QueryEvent::EndOfQuery { .. }) {
buf.extend_from_slice(if 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 !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 @@ -613,6 +634,7 @@ pub async fn api_v1_db_schema(

#[cfg(test)]
mod tests {
use axum::http::HeaderValue;
use bytes::Bytes;
use corro_types::{api::RowId, config::Config, schema::SqliteType, base::Version};
use futures::Stream;
Expand Down Expand Up @@ -781,8 +803,12 @@ mod tests {

println!("transaction body: {body:?}");

let mut headers = axum::headers::HeaderMap::new();
headers.insert("Accept", HeaderValue::from_static("application/x-ndjson"));

let res = api_v1_queries(
Extension(agent.clone()),
headers,
axum::Json(Statement::Simple("select * from tests".into())),
)
.await
Expand Down
Loading