Repository navigation
Conversation
Counting the quads matching a pattern currently requires iterating over `quads_for_pattern`, which decodes every match into a `Quad` even though the caller only wants a number. In pyoxigraph the cost is higher still, because each match additionally becomes a Python object. `count_quads_for_pattern` counts at the `StorageReader` level, on the encoded quads, so no decoding, allocation or Python conversion happens. The method is added to `Store`, `Snapshot` and `Transaction` for consistency with `quads_for_pattern`, and exposed in pyoxigraph. The memory backend already keeps per-term counts for index selection, which could make single-bound-term patterns O(1). That is left out here because those counts are not obviously correct for a reader pinned to an older version. Closes oxigraph#1262 Signed-off-by: Carlo van Driesten <[email protected]>
CI resultsActions are now enabled on this fork, so upstream's full 39 pass, 3 fail — all 3 failures are external-service uploads this fork cannot Highlights, in particular the things that cannot be checked on a single Linux box:
The three failures
Both services authenticate via OIDC ( These will pass on the upstream PR, where the service registrations exist. |
CI is greenRun https://github.com/ASCS-eV/oxigraph/actions/runs/44049773533: 39 pass, 3 skipped, 0 fail. The three earlier red jobs ( Reminder for the upstream PR: branch from |
Summary
Adds
count_quads_for_patterntoStore,SnapshotandTransaction, and exposesit in pyoxigraph.
Counting the quads matching a pattern currently requires iterating
quads_for_pattern,which decodes every match into a
Quadeven though the caller only wants a number.In pyoxigraph each match additionally becomes a Python object. The new method counts
at the
StorageReaderlevel, on encoded quads, so no decoding, allocation or Pythonconversion happens.
This implements the API requested in upstream issue
oxigraph#1262 ("[Feature] Store -
CountForPattern"), for exactly the reason stated there.
This is the fork-first PR. It is opened against
ASCS-eV:mainso the change can bereviewed and exercised here, following the same flow already used for
ASCS-eV/linkml. The upstream PR tooxigraph/oxigraphfollows only after this review.Why we need it
oxrdflib— the bridge that lets rdflib use oxigraph as a backend — implementslen(graph)as:It cannot call
len(store)instead, becauselen(store)counts quads across allgraphs while rdflib needs distinct triples within one context. pyoxigraph offers
quads_for_pattern(decoding iterator) and__len__(everything, no pattern) — but nocounting primitive that takes a pattern. That gap is in oxigraph, which is why the fix
belongs here rather than in
oxrdflib.This path is hot for us: our SHACL validation pipeline counts pattern matches
constantly, and
perf/rdfs-inference-indexed(merged inontology-management-baseas oxigraph#119) worked around it rather than fixing the cause.
Benchmark
10,100 quads (10,000 in a named graph, 100 in the default graph), release build,
best of 7 runs, counting the 10,000 quads of the named graph:
SELECT (COUNT(DISTINCT TRIPLE(?s,?p,?o)))— what oxrdflib runssum(1 for _ in store.quads_for_pattern(None, None, None, g))SELECT (COUNT(*))store.count_quads_for_pattern(None, None, None, g)len(store)(whole store, native, for reference)The new method lands within ~2× of
len(store), which is the practical floor for ascan-based count.
Implementation
lib/oxigraph/src/storage/mod.rsStorageReader::count_quads_for_pattern— the actual worklib/oxigraph/src/store.rsStore/Snapshot/Transactionmethods + rustdoc with a doctestpython/src/store.rsStore.count_quads_for_patternbindinglib/oxigraph/tests/store.rspython/tests/test_store.pyCHANGELOG.md313 insertions, 0 deletions, no public API changed.
Both backends are handled: the RocksDB arm iterates and propagates per-item
Resultswith
quad?;so an I/O error mid-scan cannot silently truncate the count; the memoryarm uses
.count().Counting the iterator is exact: RocksDB's
quads_for_patterndispatches eachbound/unbound combination to its own index with no post-filtering, and the
default-graph and named-graph indexes are disjoint.
Deliberately not done
The memory backend already keeps per-term counts (
DashMap<EncodedTerm, (Weak<QuadListNode>, u64)>)to choose the smallest index. For a pattern with exactly one bound term, that count
would give O(1) instead of O(matches). It is not used here because it is not
obvious that those counts are correct for a reader pinned to an older version or after
deletions, and a wrong count is far worse than a slow one. Worth a follow-up if a
maintainer can confirm the semantics.
Also out of scope, both plausible follow-ups:
Dataset.count_quads_for_patterninpyoxigraph and the equivalent JS binding.
Testing
Every Rust check from upstream's
.github/workflows/tests.yml, run locally:cargo fmt -- --checkcargo clippy -p oxigraph --all-targets --no-default-features -- -D warnings -D clippy::allcargo clippy -p oxigraph --all-targets -- -D warnings -D clippy::allcargo clippy -p pyoxigraph --all-targets -- -D warnings -D clippy::allcargo test -p oxigraph --no-default-featurescargo test -p oxigraph(RocksDB)cargo doc -p oxigraph --no-depsPython job:
uv run --locked -m unittestuv run --locked sphinx-build -M html . builduv run --locked python generate_stubs.py pyoxigraph pyoxigraph.pyi --ruffuv run --locked ty check generate_stubs.py testsuv run --locked ruff format --check ./ruff check .uv run --locked -m mypy.stubtest pyoxigraph --allowlist=mypy_allowlist.txtuv run --locked sphinx-lint docsOne pre-existing test,
test_store.TestStore.test_update_load, was skipped locally:it runs
LOAD <https://www.w3.org/1999/02/22-rdf-syntax-ns>and this machine has nodirect internet egress. It is unrelated to this change and runs normally in CI.
Test design
test_count_quads_for_pattern(memory) andtest_count_quads_for_pattern_on_disk(RocksDB, feature-gated) both run a 192-combination cross-check: 4 subjects ×
3 predicates × 4 objects × 4 graph names — unbound, matching, and deliberately
non-existent terms in each position — asserting
count_quads_for_pattern(...) == quads_for_pattern(...).count(). The fixture puts thesame triple in both the default graph and a named graph, which is where a naive
implementation double-counts.
Plus
test_count_quads_for_pattern_on_empty_storeandtest_count_quads_for_pattern_isolation(a transaction sees its own uncommitted write;a snapshot taken earlier stays pinned).
Mutation testing
A green suite proves nothing unless it goes red when the code is wrong, so the
implementation was deliberately broken three ways:
graph_namefiltercount - 1)All four new tests were also confirmed to execute by name, including the
feature-gated on-disk one.
Honest limitation: removing the
quad?;error propagation in the RocksDB arm is notcaught by any test — provoking a mid-scan I/O error needs fault injection that oxigraph
does not have. It is in the diff because it is correct, not because a test forced it.
Review checklist
count_quads_for_patternthe name upstream wants? [Feature] Store - CountForPattern oxigraph/oxigraph#1262 saysCountForPattern; this name was chosen to mirrorquads_for_pattern.Transactionvariant stay (symmetry withquads_for_pattern/len) or be dropped for minimalism?Datasetand the JS binding get the same method before going upstream?