Skip to content

feat: restore ldb_query_raw shared-library query entry point - #60

Merged
mscasso-scanoss merged 2 commits into
crc64from
feat/ldb-query-raw-wrapper
Sep 18, 2026
Merged

mscasso-scanoss merged 2 commits into
crc64from
feat/ldb-query-raw-wrapper

Conversation

@scanoss-qg

Copy link
Copy Markdown

Why

The new_hash refactor removed src/ldb_wrapper.c, which defined ldb_query_raw — the single-key shared-library entry point that third-party consumers link libldb.so against (e.g. the Go cgo binding used by scanoss.api). On this branch the only remaining query surface is the interactive CLI (shell/command), so a process embedding libldb.so has no programmatic way to read a table. This restores that entry point, adapted to the current architecture.

What

Two additive files (src/ldb_wrapper.{c,h}), picked up automatically by the Makefile's src/*.c wildcard; libldb.so then exports ldb_query_raw and ldb_dump_row.

  • ldb_query_raw(dbtable, key) — validates the table, reads its .cfg, checks the hex key against the table's key_ln (or the 32-bit main key), and collects every matching record via ldb_fetch_recordset.
  • ldb_dump_row(...) — record handler matching the current ldb_record_handler_t signature (struct ldb_table * first arg, no int subkey_ln). Frames each record as [uint32 size][payload] into a growable T_RawRes.

Because key length is .cfg-driven, both MD5 (16-byte) and CRC64 (8-byte) tables work with no code change.

Hardening over the historical wrapper

The version that existed pre-new_hash had a few rough edges; this restores it fixed:

  • Ownership: no longer free()s the caller-owned dbtable argument (ldb_read_cfg copies it internally — it was never the callee's to free).
  • No exit() in a library: a failed realloc now stops iteration and returns the partial result instead of exit(EXIT_FAILURE) killing the host process.
  • Buffer growth: the grow check now accounts for the 4-byte size prefix (the old check compared payload size only while writing size + 4 — a potential 4-byte overrun at the boundary).
  • Diagnostics: routed through logger (log_info) instead of printf; dropped an unused allocation.

Testing

  • Builds clean with make / make lib (-Wall, no new warnings); objdump -T libldb.so shows ldb_query_raw / ldb_dump_row exported, and the ldb CLI links unchanged.
  • Validated end-to-end against a real CRC64 knowledge base (the ossv2 pivot → file → path chain) through the downstream scanoss.api LDB read path.

Follow-ups (out of scope here)

  • A shared-library-level test for ldb_query_raw (the current test/ suite exercises the CLI via bash_unit; there's precedent in test/null_sector_test.c for a C test).
  • Downstream consumers that relied on the old wrapper freeing dbtable must free their own argument after re-vendoring (the correct ownership).
  • Release builds of libldb.so should target the oldest supported glibc for portability across consumer hosts.

The new_hash refactor dropped src/ldb_wrapper.c, which provided ldb_query_raw
— the single-key shared-library entry point third-party consumers link against
(e.g. the Go cgo binding in scanoss.api). The CLI (shell/command) is the only
query surface left on this branch, so programmatic consumers of libldb.so have
no entry point.

Restore the wrapper, adapted to the new_hash / crc64 architecture:
- ldb_dump_row matches the current ldb_record_handler_t signature (takes
  struct ldb_table *, no int subkey_ln) and frames each record as
  [uint32 size][payload] into a growable T_RawRes.
- Key length is validated against the table's .cfg key_ln (or the 32-bit main
  key), so both MD5 (16-byte) and CRC64 (8-byte) tables work unchanged.

Hardening over the historical version:
- Do not free the caller-owned dbtable argument (ldb_read_cfg copies it; it was
  never ours to free).
- Replace exit(EXIT_FAILURE) on a failed realloc with a graceful stop that
  returns the partial result — a library must not abort the host process.
- Fix the growth check to account for the 4-byte size prefix (the old check
  compared payload size only but wrote size+4, a potential 4-byte overrun).
- Route diagnostics through logger (log_info) instead of printf, and drop an
  unused buffer allocation.

Picked up automatically by the Makefile's src/*.c wildcard; libldb.so exports
ldb_query_raw and ldb_dump_row. Verified end-to-end against a real CRC64 KB
(ossv2 pivot/file/path chain) via the downstream scanoss.api LDB read path.
@mscasso-scanoss mscasso-scanoss self-assigned this Sep 18, 2026
@mscasso-scanoss
mscasso-scanoss merged commit 696bcb1 into crc64 Sep 18, 2026
1 check passed
@mscasso-scanoss
mscasso-scanoss deleted the feat/ldb-query-raw-wrapper branch September 18, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants