Skip to content

kv: add flb_kv_get_all_key_values function#11788

Open
iypetrov wants to merge 5 commits intofluent:masterfrom
iypetrov:iypetrov-add-kv-get-all-key-values
Open

kv: add flb_kv_get_all_key_values function#11788
iypetrov wants to merge 5 commits intofluent:masterfrom
iypetrov:iypetrov-add-kv-get-all-key-values

Conversation

@iypetrov
Copy link
Copy Markdown

@iypetrov iypetrov commented May 8, 2026

Add flb_kv_get_all_key_values function for mk_list.

Addresses #11776 (1/3 sub-task)


Enter [N/A] in the box, if an item is not applicable to your change.

Testing
Before we can approve your change; please submit the following in a comment:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **New Features**
  * Added an API to retrieve all key/value entries from a collection as an allocated array, returning NULL for empty/invalid input and reporting the item count; includes allocation and error handling.

* **Tests**
  * Added unit tests validating empty and populated collection behavior, returned count, and returned entries.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

iypetrov added 2 commits May 8, 2026 09:59
The goal of this enhancement is to extend the key-value store API to support
retrieving all elements from the linked list in a single call. Currently, the
only way to retrieve all elements is to guess the keys and repeatedly call
`flb_kv_get_key_value`, which is especially problematic for plugin writers.

Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 8, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This pull request adds a public API, flb_kv_get_all_key_values, that returns all flb_kv entries from a mk_list as a newly allocated array and reports the element count; it includes implementation, header declaration, and a unit test with cleanup.

Changes

Key-Value Array Retrieval API

Layer / File(s) Summary
Public API Contract
include/fluent-bit/flb_kv.h
Function declaration for flb_kv_get_all_key_values added, returning struct flb_kv** and accepting int *out_count.
Core Implementation
src/flb_kv.c
Function validates the input list, allocates an array sized to mk_list_size(list), populates it with existing flb_kv pointers, sets *out_count, and returns the array or NULL on failure.
Tests & Support
tests/internal/kv.c
Added #include <fluent-bit/flb_mem.h>; unit test test_kv_get_all_key_values verifies NULL/count==0 for empty list, correct array of three pairs for populated list, frees returned array, and registers the test in TEST_LIST.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

docs-required

Poem

🐰 A tiny hop, a clever trace,
Keys and values find their place,
I gather pairs in tidy rows,
Count them true before I close,
Tests applaud the tidy chase.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title directly and accurately summarizes the main change: adding a new function flb_kv_get_all_key_values to the kv API.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@include/fluent-bit/flb_kv.h`:
- Line 44: flb_kv_get_all_key_values currently returns a pointer array without
length metadata, making callers unsafe to iterate; modify its signature to
include an output count (e.g., add a size_t *out_count parameter) or change the
return to a small struct { struct flb_kv **items; size_t count; } so callers can
know the array length; update the function implementation and all call sites
that use flb_kv_get_all_key_values to set/read the count and to free/iterate
correctly (search for references to flb_kv_get_all_key_values to update usage).

In `@tests/internal/kv.c`:
- Around line 49-61: The test accesses pairs[0..2] without ensuring the
underlying list contains three items; change the check so after verifying pairs
is non-NULL you also assert mk_list_size(&list) == 3 (or equivalent size check
against the list used to build pairs) before indexing pairs[0], pairs[1], and
pairs[2]; reference the list used when calling flb_kv_item_set() and use that
size guard so the subsequent TEST_CHECK(strcmp(...)) validations only run when
the list actually has three entries.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 96ef314f-ede6-4b61-9703-2e217b4ad117

📥 Commits

Reviewing files that changed from the base of the PR and between 7299905 and 24549d4.

📒 Files selected for processing (3)
  • include/fluent-bit/flb_kv.h
  • src/flb_kv.c
  • tests/internal/kv.c

Comment thread include/fluent-bit/flb_kv.h Outdated
Comment thread tests/internal/kv.c Outdated
iypetrov added 3 commits May 8, 2026 10:22
This out_count parameter is needed so the callers can safely iterate without external assumptions.

Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
…t parameter

Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
Signed-off-by: Ilia Petrov <ilia.yavorov.petrov@gmail.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
tests/internal/kv.c (1)

44-46: ⚡ Quick win

Check each flb_kv_item_set result explicitly

Please assert each insertion succeeds, so failures are reported at the source rather than only via downstream count/content checks.

Suggested patch
-    flb_kv_item_set(&list, "host", "localhost");
-    flb_kv_item_set(&list, "port", "8080");
-    flb_kv_item_set(&list, "path", "/api");
+    TEST_CHECK(flb_kv_item_set(&list, "host", "localhost") != NULL);
+    TEST_CHECK(flb_kv_item_set(&list, "port", "8080") != NULL);
+    TEST_CHECK(flb_kv_item_set(&list, "path", "/api") != NULL);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/internal/kv.c` around lines 44 - 46, Each flb_kv_item_set call should
have its return value checked and asserted immediately so insertion failures are
reported at the source; update the three calls to flb_kv_item_set(&list, "host",
"localhost"), flb_kv_item_set(&list, "port", "8080"), and flb_kv_item_set(&list,
"path", "/api") to capture their returns into distinct variables (e.g.,
ret_host/ret_port/ret_path) and add assertions that each return indicates
success (match the test framework's success condition) before proceeding to
later count/content checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/internal/kv.c`:
- Around line 44-46: Each flb_kv_item_set call should have its return value
checked and asserted immediately so insertion failures are reported at the
source; update the three calls to flb_kv_item_set(&list, "host", "localhost"),
flb_kv_item_set(&list, "port", "8080"), and flb_kv_item_set(&list, "path",
"/api") to capture their returns into distinct variables (e.g.,
ret_host/ret_port/ret_path) and add assertions that each return indicates
success (match the test framework's success condition) before proceeding to
later count/content checks.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2ccc1540-93cc-41e1-98ac-1dd74658090e

📥 Commits

Reviewing files that changed from the base of the PR and between dd48761 and ff54e1a.

📒 Files selected for processing (1)
  • tests/internal/kv.c

Copy link
Copy Markdown
Contributor

@cosmo0920 cosmo0920 left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants