Repository navigation
fix(import): a bulk JSON-LD import writes @graph scoping as insert does - #2148
Conversation
A bulk JSON-LD import refused every document that scoped data to a named
graph: a node's graph selector ("@graph": "ex:g") and a JSON-LD named graph
({"@id": "ex:g", "@graph": [...]}) both stopped the import. The adapter now
reads graph scoping the way the transaction parser does (a node's selector
scopes it and everything nested in it, a named graph keeps its own
properties in the enclosing graph and its content in the named one, and
embedded nodes, list members, @included and @reverse share their node's
graph) and writes named-graph statements as quads.
The import sink takes those quads like TriG's named-graph blocks: the flake
carries its graph, the spool record its g_id, and the commit that first
writes a graph records it in graph_delta, so an index built by the import
and a rebuild from its commits read the same graphs. Graph names are
classified by the shared classify_written_graph_name; "default" is the
default graph, and the ledger's txn-meta and config graphs are refused, as
a TriG import refuses them (one guard now serves both).
What the import cannot place it now refuses instead of skipping: a keyword
on a node object other than the ones it reads, and a top-level bare "graph"
key, which a transaction reads as its routing key. A list member carrying
its own properties keeps them; they were dropped before.
A load that replays the commits finds each named graph through the commit's graph_delta; the index the import builds and a reindex do not need it, so neither read could tell it was missing.
…does The test pinned the refusal bulk import used to give; it now checks the selector and the named-graph forms against the same TriG data.
Bulk import and the transaction parser now place a JSON-LD node and resolve its graph name through the same functions instead of matching copies: node_graph (fluree-graph-json-ld) decides what a node's @graph opens (its own selector, a named graph's content under its @id, or nothing), and resolve_written_graph (moved out of the template parser) resolves a selector, so import reads "default", aliases, keywords and relative names exactly as insert's data does. Import adds only its own guard on top: the ledger's txn-meta and config graphs are refused, as TriG import refuses them. Each distinct name is resolved once per document. A top-level bare "graph" key on a single node is now dropped, as insert drops it (it is a transaction's routing key there), rather than refused. The twins cover every probed form, including the envelope and @included controls, an empty named graph and an annotated edge under a selector, and pin the config graph as the one place import and insert differ.
A document that scopes its nodes with a selector names the same few graphs on every node. The adapter now resolves and creates each graph's term once per name, and the import sink looks a run of statements' graph up once, instead of per node and per statement.
…ph-scope # Conflicts: # fluree-db-transact/src/import.rs # fluree-db-transact/src/parse/jsonld.rs
Since main reads every graph position of a ledger through one table, the ledger's own address names its default graph and L#<g> the graph <g>, for insert and for a TriG block alike. The twins now cover the same names in a JSON-LD import, and both lanes use one ledger id so the address means the same on each; the txn-meta IRI is then refused by both.
bplatz
left a comment
There was a problem hiding this comment.
Approving. Sharing node_graph/resolve_written_graph with insert is the right shape. The graph-id handling (shared allocator with a spool, finalizer counter without one, graph_delta on first use) and the three-way read in the tests are solid.
There's one bug inline (a .jsonl line with its own @context can't resolve a compact selector). Please fix it before merging. Also, this delivers the second item of #2081 and half the third, so the body should say Partially addresses #2081 and name what remains (TriG on the single-parse driver; @odata.etag, which import still drops while insert keeps it as a predicate). @aaj3f please go through the feedback before merging.
| fluree_graph_json_ld::adapter::to_graph_events_scoped(&expanded, &mut sink, |name| { | ||
| let resolved = match graph_context.take() { | ||
| Some(context) => Ok(context), | ||
| None => crate::parse::edge_annotations::top_level_context(&doc), |
There was a problem hiding this comment.
Bug: for .jsonl, doc is the splitter's {"@context": <shared>, "@graph": [lines]} wrapper, so a line that carries its own @context never has it consulted here. {"@context": {"ex": …}, "@id": "ex:a", "@graph": "ex:g", …} as a .jsonl line fails the import with Unresolved compact IRI 'ex:g'. The same node imports into g as a .jsonld file or under a shared first-line context, and insert accepts it. Resolving a selector against the node's own context, not the document's top level, would fix it. Failing probe:
#[tokio::test]
async fn a_jsonl_line_with_its_own_context_resolves_its_selector() {
let line = json!({"@context": ctx(), "@id": "ex:a", "@graph": "ex:g", "ex:q": "v"});
assert_eq!(
import("data.jsonl", &format!("{line}\n")).await.unwrap(),
["g: ex:a ex:q v"]
);
}There was a problem hiding this comment.
Right, thanks for the probe. Fixed in 842c914b8, and I went a little wider than the selector, because it's really about what a .jsonl line is. Each line is now read as a document of its own under the shared context, which is what the reader's docs describe ("as if the files were imported separately"). The line is expanded under the shared context beneath its own @context, and its graph names resolve against that. So your line imports into g, as it does as a .jsonld file and as insert takes it. parse_jsonld_chunk gets the chunk's shape (JsonLdChunk::Lines for the ndjson stream, Document otherwise) from the import, and the .jsonld path is unchanged. Most lines read the same either way, those with no @context of their own that aren't envelopes. So those still expand together and are written in one walk (5fa6e9371), which resolves each graph name once per chunk. Only the other lines expand on their own. For a 1.05M-statement .jsonl import that keeps allocations identical to before, with or without a selector on every line, and instructions within noise (+0.6–0.7%). Reading every line on its own had cost 9% more allocations when every line names a graph.
bulk_import_of_jsonl_reads_each_line_as_a_document twins five cases against inserting each line as its own document: your line (as "a line whose own @context resolves its selector"), a line whose own context adds to a shared one, the bare "graph" key below, a line that is itself an envelope, and the shared-context case that was already there. Reading the chunk as one envelope again fails four of them. Your two come back as the Unresolved compact IRI 'ex:g' error, the bare key lands in g, and the envelope line is refused as an @id-less named graph.
| let Some(obj) = doc.as_object().filter(|obj| obj.contains_key("graph")) else { | ||
| return Ok(()); | ||
| }; | ||
| if fluree_graph_json_ld::envelope_graph_key(obj)?.is_some() { |
There was a problem hiding this comment.
The splitter's wrapper is an envelope, so this skips every .jsonl line: a bare "graph": "ex:g" on a line under a shared context writes to g, while the same node as a .jsonld doc drops the key. The splitter docs say segments read "as if the files were imported separately", which argues for dropping it there too. If not, worth documenting.
There was a problem hiding this comment.
Agreed, and that's in the same commit (842c914b8). Each line drops a top-level bare "graph" key now, just as the line imported or inserted alone does, and it's pinned as one of the jsonl twins. The ndjson caveat in docs/cli/create.md now says what's read per line, and that blank-node labels still unify across a context segment, as before.
| } | ||
| "@type" => {} | ||
| // Skipping a keyword would drop what it says. | ||
| k if is_keyword_form(k) => { |
There was a problem hiding this comment.
Matching insert here is reasonable, but JSON-LD 1.1 expansion drops keyword-like keys (with a warning) rather than erroring, so a file that imported before can now fail. Worth stating the departure from the spec somewhere a user will see it.
There was a problem hiding this comment.
Here's what I found. insert refuses an unknown keyword-form key with JSON-LD keyword "@foo" is not supported on a node object, and has since #2009. JSON-LD 1.1's IRI expansion says a key with the form of a keyword should get a warning and expand to null, which drops it. So insert already departs from the spec here, deliberately, and the compatibility reference lists that under "Deviations from the JSON-LD 1.1 expansion algorithm".
I'd keep import refusing, as insert does. #2081's third item asks for exactly that, one key classifier for both, and a drop that only shows up as a warning in a bulk import's log is easy to miss, while the refusal names the key so the fix is obvious. You're right that it's a change for a file that imported before, though, so I've made it visible (89597987c):
- The compatibility reference's deviation line now says a transaction and a bulk import alike refuse the key where the algorithm drops it with a warning.
docs/cli/create.mdsays it in the JSON-LD paragraph of import.- There's a behavior-change entry, "Bulk JSON-LD import writes graph-scoped data, and refuses what it cannot place". It says a document with an unknown
@-keyword that imported before now fails, naming the key.
I thought about switching import to the spec's drop-with-warning, but I think keeping both lanes refusing is the better call for now. If we ever do want the spec behavior there, it should change for insert and import together so they don't drift apart again.
…ph-scope # Conflicts: # fluree-db-transact/src/import.rs # fluree-db-transact/src/import_sink.rs # fluree-db-transact/src/namespace.rs # fluree-db-transact/src/parse/jsonld.rs # fluree-graph-json-ld/src/adapter.rs
The ndjson reader hands a chunk over as one {"@context": <shared>,
"@graph": [lines]} wrapper, and import read it as one envelope, so a
line's own @context never resolved its graph selector (the import failed
with an unresolved compact IRI), and a top-level bare "graph" key on a line
was read as a selector, where the same line imported or inserted alone
drops it. Each line is now read as the document it is: expanded under the
shared context beneath its own, its graph names resolved against that, a
bare "graph" key dropped, and a line that is an envelope unwrapped.
parse_jsonld_chunk takes the chunk's shape (JsonLdChunk) from the import.
The jsonl twins insert each line as its own document and compare.
…does The compatibility reference listed the refusal as a deviation from JSON-LD 1.1 expansion (which drops such a key with a warning) for transactions; it now names bulk import too, and the create docs say it where a user reads about importing JSON-LD. The stale note that bulk import refuses @graph scoping becomes a behavior-change entry, and the ndjson caveat says what is now read per line.
A line with no @context of its own that is not an envelope reads the same as a node of the chunk's envelope as it does alone, so those lines expand together and are written in one walk, which resolves each graph name once per chunk rather than once per line. Only the other lines expand on their own.
|
Thanks! The body now says |
Brings #2217 (SPARQL reads list members as values; CONSTRUCT writes lists), #2218 (schema writes judged by modify policy), #2208, #2169, #2168, #2153, #2149 and #2148 into next. Resolutions: - Nameservice: branch_records is the one branch listing (strict, retracted branches kept for GC) on every backend, resolved through next's bindings; Fluree::list_branches filters retracted ones. The lifecycle conformance suite reads live branches through a helper. DynamoDB's narrow listing is wired into its trait impl. - JSON-LD adapter: main's graph scoping (Walk, Scope, node_graph, selectors) with next's triple terms, @annotation, @reifies, blank-label and number datatype handling. to_graph_events writes a JSON-LD named graph under its @id and refuses a graph selector. main's f:reifiesGraph anchor is dropped: next writes an annotation as an rdf:reifies link in its graph. emit_quad_list_item takes (graph, index). - Bulk import: an annotation's link is written to the graph its slots name, and a triple-term statement in a named graph is spooled under that graph (both landed in the default graph); emit_quad_reified_triple is implemented. resolve_object_value keeps main's vector refusals and next's term-table path. - Ledger manager: a store that fails to load fails the load or keeps the reloaded ledger's state (main), over next's load_decoding_root and activation swap rule. - Novelty keeps main's KeptEffects without the retired arena's reifies capture. The pool's resolve_events_at and compaction's split_lifecycles follow fluree_db_core::same_t. - Retired annotation arena files stay deleted. - Tests from main adapted to next: one Fluree per storage directory, async genesis_ledger, storage-namespace content stores, u32 graph ids, a store broken by hiding a dictionary branch (next has no disk cache), a refused import leaving no ledger. next's tests that pinned bulk import refusing @graph and SPARQL's RDF list view now check what main changed them to.
A bulk JSON-LD import (
fluree create --from x.jsonld,fluree.create(id).import(path), and the.jsonl/.ndjsonstream) now writes@graphscoping the wayinsertdoes. Onmainit refused every document that scoped data to a named graph, so a JSON-LD named graph ({"@id": "ex:g", "@graph": [...]}) or a node with a graph selector ("@graph": "ex:g") stopped the whole import with "graph scoping is not supported by bulk import". (On v4.2.4, before that refusal went in, the same documents imported with the named-graph content dropped.)Behavior change. Documents
mainrefuses now import:main"@graph": ...)insertwrites it"graph"keyinsertdrops it (it'sinsert's routing key), node in the default graph@listmember carrying its own propertiesinsertkeeps them"@foo") on a nodeinsertrefuses it.jsonlline with its own@contextand a graph selector.jsonlline with a top-level bare"graph"keySo a file that failed to import on
maincan now succeed, and one with a stray@-keyword that imported (minus that key) now fails, naming the key. JSON-LD 1.1 expansion would drop such a key with a warning;insertrefuses it since #2009, and the compatibility reference already listed that as a deviation, so it now names bulk import too (anddocs/cli/create.mdsays it where import is documented). Everything else imports exactly as before.The two paths now place a node and resolve its graph through the same code rather than matching copies.
node_graph(fluree-graph-json-ld/src/graph_shape.rs) decides what a node's@graphopens: its own selector, a named graph's content under its@id, or nothing.resolve_written_graph, moved out of the transaction parser's template context, turns a selector into a graph, so"default", keywords, compact and relative names all read as they do forinsert's data. The adapter (fluree-graph-json-ld/src/adapter.rs) carries the scope down as the transaction parser does: a selector scopes its node and everything nested in it, a named graph keeps its own properties in the enclosing graph, and embedded nodes, list members,@includedand@reverseshare their node's graph. Statements in a named graph go out as quads, andImportSinktakes them the way TriG's named-graph blocks are written: the flake carries its graph, the spool record itsg_id, and the commit that first writes a graph records it ingraph_delta. An annotated edge in a named graph gets itsf:reifiesGraphanchor, asinsertwrites it, so deleting the edge still takes the bundle with it.A
.jsonl/.ndjsonfile reaches the import as the reader's{"@context": <shared>, "@graph": [lines]}chunks, and each line is read as a document of its own under that shared context, the way the reader's docs describe a file "as if imported separately": the line's own@contextresolves its graph names, a top-level bare"graph"key is dropped, and a line that is an envelope is unwrapped (blank-node labels still unify across a context segment, as before).parse_jsonld_chunktakes the chunk's shape (JsonLdChunk::DocumentorLines) from the import.Every graph name an import writes then goes through the ledger's graph-position table, the same one a TriG block and
insertread since #2007, so the ledger's own address is its default graph andL#<g>is the graph<g>in a JSON-LD import too.The one place import and
insertdiffer on purpose is the ledger's config graph:"@graph": "config"(or its IRI) is refused, as a TriG import already refuses it, since the import pipeline has no index pass that fills it.txn-metais refused on both. The reserved-graph guard is one function for TriG and JSON-LD import now.insertmain@id+@graph), with or without a property of its own, or emptyg(property in default)insertg+ defaultinsert{"@id"}, relative to@base,"default")insert@idthat is this ledger's address (urn:fluree:L,L,urn:fluree:L#<g>)<g>insert@list,@included,@reverse, an annotated edgeginsert.jsonlnamed-graph line + plain line + selector lineg+ default +hinsert"graph"key on a node@includedwith no selector"config""txn-meta",urn:default,?g, a relative name with no@base, a blank-node or@id-less named graphTests are in
it_import_jsonld_graph_scope.rs. Every form in that table is written byinsertand by import and checked against the expected statements per graph, and each import is read three ways that have to agree: from the index it built, after a reindex, and from its commits alone (build_index(false)). That last one is the only read that goes throughgraph_delta, fwiw; the first two find the graphs without it, so a version withgraph_deltaleft empty passed both until the third read went in. There's also a TriG import of the same data as a second oracle, a three-file import where two files write the same graph, the annotation anchor and its delete cascade, and the refusals, which check the refused import wrote nothing (and thatinsertrefuses the same documents, or writesconfig). All six fail onmain. The adapter and sink have unit tests of their own (the sink's sit behind fluree-db-transact'simportfeature, so--features importto run them).On perf, the default-graph path every existing JSON-LD import takes is flat. For a 1.05M-statement
.jsonlimport, allocations are identical and instructions are +0.2% to +0.7% over several builds, against 0.4% between two builds of the samemainsources. That holds after the per-line reading too, since lines that read the same either way still expand and resolve together. Named graphs are new, so there's no before for them. The same 1.05M statements spread over 4 named graphs cost +21% instructions over the default graph (+25% when every node carries its own selector), with the same allocations, and the difference is mostly the per-graph commit and index work. For comparison, TriG's existing named-graph path costs +63% over its own default graph on the same data.Partially addresses #2081: this is its second item (graph-scoped JSON-LD on bulk import) and half the third (an unknown
@-keyword now refused on import as oninsert). What remains there is the first item, TriG import on the single-parse driver, and@odata.etag-style keys (@plus non-letters), which import still drops whileinsertstores them as a property.