-
Notifications
You must be signed in to change notification settings - Fork 2
rdf to-jelly: implement the GRAPHS stream type #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ostrzyciel
merged 4 commits into
main
from
101-rdf-to-jelly-add-physical_stream_type_graphs-support
May 2, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/scala/eu/neverblink/jelly/cli/util/jena/riot/JellyStreamWriterGraphs.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package eu.neverblink.jelly.cli.util.jena.riot | ||
|
|
||
| import eu.ostrzyciel.jelly.convert.jena.JenaConverterFactory | ||
| import eu.ostrzyciel.jelly.convert.jena.riot.JellyFormatVariant | ||
| import eu.ostrzyciel.jelly.core.ProtoEncoder | ||
| import eu.ostrzyciel.jelly.core.proto.v1.{RdfStreamFrame, RdfStreamRow} | ||
| import org.apache.jena.graph.{Node, Triple} | ||
| import org.apache.jena.riot.system.StreamRDF | ||
| import org.apache.jena.sparql.core.Quad | ||
|
|
||
| import java.io.OutputStream | ||
| import scala.collection.mutable.ListBuffer | ||
|
|
||
| /** A stream writer that writes RDF data in Jelly format, using the GRAPHS physical stream type. | ||
| * | ||
| * JellyStreamWriter in jelly-jena only supports TRIPLES and QUADS physical stream types, so this | ||
| * is needed to support the remaining GRAPHS physical stream type. | ||
| */ | ||
| final class JellyStreamWriterGraphs(opt: JellyFormatVariant, out: OutputStream) extends StreamRDF: | ||
| private val buffer: ListBuffer[RdfStreamRow] = new ListBuffer[RdfStreamRow]() | ||
| // We don't set any options here – it is the responsibility of the caller to set | ||
| // a valid stream type (GRAPHS). | ||
| private val encoder = JenaConverterFactory.encoder( | ||
| ProtoEncoder.Params( | ||
| opt.opt, | ||
| opt.enableNamespaceDeclarations, | ||
| Some(buffer), | ||
| ), | ||
| ) | ||
| private var currentGraph: Node = null | ||
|
|
||
| // No need to handle this, the encoder will emit the header automatically anyway | ||
| override def start(): Unit = () | ||
|
|
||
| override def triple(triple: Triple): Unit = | ||
| handleGraph(Quad.defaultGraphIRI) | ||
| encoder.addTripleStatement(triple) | ||
| if opt.delimited && buffer.size >= opt.frameSize then flushBuffer() | ||
|
|
||
| override def quad(quad: Quad): Unit = | ||
| handleGraph(quad.getGraph) | ||
| encoder.addTripleStatement( | ||
| quad.getSubject, | ||
| quad.getPredicate, | ||
| quad.getObject, | ||
| ) | ||
| if opt.delimited && buffer.size >= opt.frameSize then flushBuffer() | ||
|
|
||
| // Not supported | ||
| override def base(base: String): Unit = () | ||
|
|
||
| override def prefix(prefix: String, iri: String): Unit = | ||
| if opt.enableNamespaceDeclarations then | ||
| encoder.declareNamespace(prefix, iri) | ||
| if opt.delimited && buffer.size >= opt.frameSize then flushBuffer() | ||
|
|
||
| private def handleGraph(graph: Node): Unit = | ||
| if currentGraph == null then | ||
| // First graph in the stream | ||
| encoder.startGraph(graph) | ||
| currentGraph = graph | ||
| else if Quad.isDefaultGraph(currentGraph) then | ||
| if !Quad.isDefaultGraph(graph) then | ||
| // We are switching default -> named | ||
| encoder.endGraph() | ||
| encoder.startGraph(graph) | ||
| currentGraph = graph | ||
| else if Quad.isDefaultGraph(graph) || graph != currentGraph then | ||
| // We are switching named -> named or named -> default | ||
| encoder.endGraph() | ||
| encoder.startGraph(graph) | ||
| currentGraph = graph | ||
|
|
||
| // Flush the buffer and finish the stream | ||
| override def finish(): Unit = | ||
| if currentGraph != null then | ||
| encoder.endGraph() | ||
| currentGraph = null | ||
| if !opt.delimited then | ||
| // Non-delimited variant – whole stream in one frame | ||
| val frame = RdfStreamFrame(rows = buffer.toList) | ||
| frame.writeTo(out) | ||
| else if buffer.nonEmpty then flushBuffer() | ||
| out.flush() | ||
|
|
||
| private def flushBuffer(): Unit = | ||
| val frame = RdfStreamFrame(rows = buffer.toList) | ||
| frame.writeDelimitedTo(out) | ||
| buffer.clear() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this logic could be moved to a separate method (to make sure both conditions are always correctly checked in the if)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh, that's how it is in the Jena original. Let's keep it as-is.