From f3549cabc95113412aa6d7e2e82066f0d836cd1d Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Fri, 9 Mar 2018 18:33:50 +0000 Subject: [PATCH] JENA-1504: Fix batch handling. Fix new line output. --- .../riot/writer/WriterStreamRDFBatched.java | 67 +++++++++---------- .../riot/writer/WriterStreamRDFBlocks.java | 30 ++++----- 2 files changed, 44 insertions(+), 53 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java index 87573d5770b..3572643c4bb 100644 --- a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java +++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBatched.java @@ -68,75 +68,68 @@ protected final void reset() { batchQuads = null ; } - @Override - protected final void print(Quad quad) { - if ( false ) { - // Merge to a triple stream. - triple(quad.asTriple()) ; - return ; - } - - Node g = quad.getGraph() ; - Node s = quad.getSubject() ; - + private void batch(Node g, Node s, boolean forTriples) { if ( !Objects.equals(g, currentGraph) || !Objects.equals(s, currentSubject) ) { - if ( currentSubject != null ) { - if ( currentGraph == null ) - finishBatchTriples(currentSubject) ; - else - finishBatchQuad(currentGraph, currentSubject) ; - } - startBatchQuad(g, s) ; + finishBatchTriples(currentSubject) ; + finishBatchQuad(currentGraph, currentSubject) ; + if ( forTriples ) + startBatchTriple(s); + else + startBatchQuad(g, s); currentGraph = g ; currentSubject = s ; } - processQuad(quad) ; } @Override protected final void print(Triple triple) { Node s = triple.getSubject() ; - if ( !Objects.equals(s, currentSubject) ) { - if ( currentSubject != null ) - finishBatchTriples(currentSubject) ; - startBatchTriple(s) ; - - currentGraph = null ; - currentSubject = s ; - } + batch(null, s, true); processTriple(triple) ; } private void startBatchTriple(Node subject) { batchTriples = new ArrayList<>() ; - } - - private void processTriple(Triple triple) { - batchTriples.add(triple) ; + this.currentGraph = null; + this.currentSubject = subject; } private void finishBatchTriples(Node subject) { - if ( batchTriples != null && batchTriples.size() > 0 ) { + if ( batchTriples != null && !batchTriples.isEmpty() ) { printBatchTriples(currentSubject, batchTriples) ; batchTriples.clear() ; } } - private void startBatchQuad(Node graph, Node subject) { - batchQuads = new ArrayList<>() ; + private void processTriple(Triple triple) { + batchTriples.add(triple) ; } - private void processQuad(Quad Quad) { - batchQuads.add(Quad) ; + @Override + protected final void print(Quad quad) { + Node g = quad.getGraph() ; + Node s = quad.getSubject() ; + batch(g, s, false); + processQuad(quad) ; + } + + private void startBatchQuad(Node graph, Node subject) { + batchQuads = new ArrayList<>() ; + this.currentGraph = graph; + this.currentSubject = subject; } private void finishBatchQuad(Node graph, Node subject) { - if ( batchQuads != null && batchQuads.size() > 0 ) { + if ( batchQuads != null && !batchQuads.isEmpty() ) { printBatchQuads(currentGraph, currentSubject, batchQuads) ; batchQuads.clear() ; } } + private void processQuad(Quad quad) { + batchQuads.add(quad) ; + } + protected abstract void printBatchQuads(Node g, Node s, List batch) ; protected abstract void printBatchTriples(Node s, List batch) ; diff --git a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java index 64b4297add4..97e179f6ad5 100644 --- a/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java +++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/WriterStreamRDFBlocks.java @@ -64,7 +64,6 @@ public class WriterStreamRDFBlocks extends WriterStreamRDFBatched // Quad output protected Node lastGraph = null ; protected Node lastSubject = null ; - protected boolean firstGraph = true ; protected int currentGraphIndent = 0; public WriterStreamRDFBlocks(OutputStream output) { @@ -82,13 +81,13 @@ public WriterStreamRDFBlocks(IndentedWriter output) { @Override protected void printBatchQuads(Node g, Node s, List quads) { if ( g == null ) + // print as Triples has currentGraph == null. g = Quad.defaultGraphNodeGenerated ; if ( Objects.equals(g, lastGraph) ) { // Same graph, different subject. out.println(" .") ; - out.println() ; } else { - // Start graph + // Different graph endGraph(g) ; startGraph(g) ; lastGraph = g ; @@ -99,21 +98,24 @@ protected void printBatchQuads(Node g, Node s, List quads) { lastSubject = s ; } + private void startBatch() { + // Any output so far? prefixes or a previous graph. + if ( out.getRow() > 1 ) + out.println() ; + out.flush(); + } + private void gap(int gap) { out.print(' ', gap) ; } @Override protected void printBatchTriples(Node s, List triples) { - // Blank line? - // Not if not prefixes and first batch. - if ( out.getRow() > 1 ) - out.println() ; - + startBatch(); printBatch(s, triples) ; // End of cluster. - out.print(" .") ; - out.println() ; + out.println(" .") ; + lastGraph = null; } private void printBatch(Node s, List triples) { @@ -158,14 +160,11 @@ protected void finalizeRun() { protected boolean dftGraph(Node g) { return g == Quad.defaultGraphNodeGenerated ; } protected void startGraph(Node g) { + startBatch(); // Start graph if ( lastGraph == null ) { boolean NL_START = (dftGraph(g) ? NL_GDFT_START : NL_GNMD_START) ; - - if ( !firstGraph ) - out.println() ; - firstGraph = false ; - + lastSubject = null ; if ( !dftGraph(g) ) { outputNode(g) ; @@ -218,5 +217,4 @@ protected void endGraph(Node g) { protected void setGraphIndent(int x) { currentGraphIndent = x ; } protected int graphIndent() { return currentGraphIndent ; } - }