bool dict_iterate(struct dict_iterate_context *ctx,
const char **key_r, const char **value_r)
{
- return ctx == &dict_iter_unsupported ? FALSE :
- ctx->dict->v.iterate(ctx, key_r, value_r);
+ if (ctx == &dict_iter_unsupported)
+ return FALSE;
+ if (ctx->max_rows > 0 && ctx->row_count >= ctx->max_rows) {
+ /* row count was limited */
+ return FALSE;
+ }
+ if (!ctx->dict->v.iterate(ctx, key_r, value_r))
+ return FALSE;
+ ctx->row_count++;
+ return TRUE;
}
void dict_iterate_set_async_callback(struct dict_iterate_context *ctx,
ctx->async_context = context;
}
+void dict_iterate_set_limit(struct dict_iterate_context *ctx,
+ uint64_t max_rows)
+{
+ ctx->max_rows = max_rows;
+}
+
bool dict_iterate_has_more(struct dict_iterate_context *ctx)
{
return ctx->has_more;
void dict_iterate_set_async_callback(struct dict_iterate_context *ctx,
dict_iterate_callback_t *callback,
void *context);
+/* Limit how many rows will be returned by the iteration (0 = unlimited).
+ This allows backends to optimize the query (e.g. use LIMIT 1 with SQL). */
+void dict_iterate_set_limit(struct dict_iterate_context *ctx,
+ uint64_t max_rows);
/* If dict_iterate() returns FALSE, the iteration may be finished or if this
is an async iteration it may be waiting for more data. If this function
returns TRUE, the dict callback is called again with more data. */