# Batches

> UCall for C

Batch callbacks receive a counted array of retained request handles, followed by the registered context.
The array is borrowed only during the callback; copy its handles into application-owned storage to defer work without copying request bytes.
Every loop deposits into one queue per batch endpoint, so batches mix requests from all loops, and partial batches flush at their configured deadline.

The batch endpoint's polling owner can also manage the engines the batch feeds, with no thread of its own per engine.
Each USearch, UStore and UForm executor runs one call at a time, and a deferred call on a caller-exclusive executor finishes on that executor's threads, so the owner submits one deferred call per executor and replies from the status words:

```c
ucall_server_take_batch(&take);                     // its callback copies the request handles out
search.completion = usearch_completion_deferred_k;  // every query of the batch, in one call
usearch_dense_search(&search);
read.completion = ustore_completion_deferred_k;     // every document of the batch, in one call
ustore_docs_read(&read);
while (usearch_outcome_status(search.outcome) == usearch_status_deferred_k ||
       ustore_outcome_status(read.outcome) == ustore_status_deferred_k)
    ucall_server_take_calls(&calls);                // keep serving the plain endpoints meanwhile
reply_to_each(requests, &search, &read);            // ucall_server_reply_content per request
```

`#include <ucall/types.h>`

`#include <ucall/server.h>`

## ucall_body_delivery_t

When an endpoint's callback runs: once the body is buffered, or at headers time with the body pulled through ucall_server_request_body_chunk().

```c
enum ucall_body_delivery_t
```

- `ucall_body_delivery_buffered_k` = `0`: The callback runs once the whole body is buffered, within `max_request_bytes`.
- `ucall_body_delivery_streamed_k` = `1`: The callback runs at headers time and pulls the body in chunks.

## ucall_received_http_batch_callback_t

Callback type for handling batched requests on the server side.

```c
typedef void(*) ucall_received_http_batch_callback_t(ucall_received_http_request_t const *requests, size_t count, void *context);
```

Notes: The array is valid only during the callback. Each request stays valid until a reply consumes it or the server is freed, independently of the array and the other requests.

## ucall_server_take_batch

Invokes one batch endpoint's callback if a batch is due, with requests from every event loop, on the calling thread.

```c
void ucall_server_take_batch(ucall_server_take_batch_t *arguments);
```

UCall creates no thread; a bounded wait sleeps until a deposit, the partial batch's deadline, or the timeout, without polling any event loop.

- `server`: Initialized server that registered `endpoint`.
- `outcome`: Outcome of this round.
- `endpoint`: Endpoint from `ucall_server_add_http_batch_endpoint()`; each has one polling owner.
- `timeout_microseconds`: Maximum wait for a due batch; zero performs one nonblocking round.

Statuses: - [`ucall_status_uninitialized_k`](https://www.unum.cloud/docs/ucall/c/errors.md#ucall_status_uninitialized_k) The `server` is `NULL`.
- [`ucall_status_invalid_argument_k`](https://www.unum.cloud/docs/ucall/c/errors.md#ucall_status_invalid_argument_k) The `endpoint` is `NULL`.

## ucall_server_take_batches

Invokes one batch endpoint's due callbacks on the calling thread, sleeping in between, until `ucall_server_stop()`.

```c
void ucall_server_take_batches(ucall_server_take_batches_t *arguments);
```

Requests still queued then are released without a callback.

- `server`: Initialized server that registered `endpoint`.
- `outcome`: Final outcome after polling stops.
- `endpoint`: Endpoint from `ucall_server_add_http_batch_endpoint()`; each has one polling owner.

Statuses: - [`ucall_status_uninitialized_k`](https://www.unum.cloud/docs/ucall/c/errors.md#ucall_status_uninitialized_k) The `server` is `NULL`.
- [`ucall_status_invalid_argument_k`](https://www.unum.cloud/docs/ucall/c/errors.md#ucall_status_invalid_argument_k) The `endpoint` is `NULL`.

