@Test public void mergesMatchingRecords() throws Exception { String leftInputStr = "<http: "<http: "<http: "<http: "<http: "<http: "<http: String rightInputStr = "<http: "<http: "<http: "<http: "<http: "<http: "<http: ByteArrayOutputStream output = new ByteArrayOutputStream(); InputStream leftInput = new ByteArrayInputStream(leftInputStr.getBytes()); InputStream rightInput = new ByteArrayInputStream(rightInputStr.getBytes()); NTuplesFileMerger merger = new NTuplesFileMerger(transform, new ParserConfig()); merger.merge(new InputStreamReader(leftInput), new InputStreamReader(rightInput), new OutputStreamWriter(output)); String expectedOutput = "<http: "<http: "<http: "<http: "<http: "<http: "<http: "<http: "<http: NTuplesParser outputParser = new NTuplesParser(new InputStreamReader(new ByteArrayInputStream(output.toByteArray())), new ParserConfig()); NTuplesParser expectedOutputParser = new NTuplesParser(new InputStreamReader(new ByteArrayInputStream(expectedOutput.getBytes())), new ParserConfig()); while (outputParser.hasNext() || expectedOutputParser.hasNext()) { assertThat(outputParser.hasNext(), is(expectedOutputParser.hasNext())); assertThat(outputParser.next(), is(expectedOutputParser.next())); } }
public void merge(Reader leftReader, Reader rightReader, Writer outputWriter) throws IOException, NTupleMergeTransformException { NTuplesParser leftParser = new NTuplesParser(leftReader, parserConfig); NTuplesParser rightParser = new NTuplesParser(rightReader, parserConfig); NTuplesWriter output = new NTuplesWriter(outputWriter); List<List<Value>> rightBuffer = new ArrayList<List<Value>>(); try { while (leftParser.hasNext() && rightParser.hasNext()) { boolean wasEqual = NTuplesParserUtils.skipLessThan(leftParser, rightParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); if (!wasEqual && leftParser.hasNext()) { wasEqual = NTuplesParserUtils.skipLessThan(rightParser, leftParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); } if (wasEqual) { rightBuffer.clear(); readToBuffer(rightParser, rightBuffer); mergeWithBuffer(leftParser.next(), rightBuffer, output); while (leftParser.hasNext() && !rightBuffer.isEmpty() && compare(leftParser.peek(), rightBuffer.get(0)) == 0) { mergeWithBuffer(leftParser.next(), rightBuffer, output); } } } } finally { output.close(); leftParser.close(); rightParser.close(); } }
NTuplesFileMerger { public void merge(Reader leftReader, Reader rightReader, Writer outputWriter) throws IOException, NTupleMergeTransformException { NTuplesParser leftParser = new NTuplesParser(leftReader, parserConfig); NTuplesParser rightParser = new NTuplesParser(rightReader, parserConfig); NTuplesWriter output = new NTuplesWriter(outputWriter); List<List<Value>> rightBuffer = new ArrayList<List<Value>>(); try { while (leftParser.hasNext() && rightParser.hasNext()) { boolean wasEqual = NTuplesParserUtils.skipLessThan(leftParser, rightParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); if (!wasEqual && leftParser.hasNext()) { wasEqual = NTuplesParserUtils.skipLessThan(rightParser, leftParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); } if (wasEqual) { rightBuffer.clear(); readToBuffer(rightParser, rightBuffer); mergeWithBuffer(leftParser.next(), rightBuffer, output); while (leftParser.hasNext() && !rightBuffer.isEmpty() && compare(leftParser.peek(), rightBuffer.get(0)) == 0) { mergeWithBuffer(leftParser.next(), rightBuffer, output); } } } } finally { output.close(); leftParser.close(); rightParser.close(); } } }
NTuplesFileMerger { public void merge(Reader leftReader, Reader rightReader, Writer outputWriter) throws IOException, NTupleMergeTransformException { NTuplesParser leftParser = new NTuplesParser(leftReader, parserConfig); NTuplesParser rightParser = new NTuplesParser(rightReader, parserConfig); NTuplesWriter output = new NTuplesWriter(outputWriter); List<List<Value>> rightBuffer = new ArrayList<List<Value>>(); try { while (leftParser.hasNext() && rightParser.hasNext()) { boolean wasEqual = NTuplesParserUtils.skipLessThan(leftParser, rightParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); if (!wasEqual && leftParser.hasNext()) { wasEqual = NTuplesParserUtils.skipLessThan(rightParser, leftParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); } if (wasEqual) { rightBuffer.clear(); readToBuffer(rightParser, rightBuffer); mergeWithBuffer(leftParser.next(), rightBuffer, output); while (leftParser.hasNext() && !rightBuffer.isEmpty() && compare(leftParser.peek(), rightBuffer.get(0)) == 0) { mergeWithBuffer(leftParser.next(), rightBuffer, output); } } } } finally { output.close(); leftParser.close(); rightParser.close(); } } NTuplesFileMerger(NTupleMergeTransform transform, ParserConfig parserConfig); }
NTuplesFileMerger { public void merge(Reader leftReader, Reader rightReader, Writer outputWriter) throws IOException, NTupleMergeTransformException { NTuplesParser leftParser = new NTuplesParser(leftReader, parserConfig); NTuplesParser rightParser = new NTuplesParser(rightReader, parserConfig); NTuplesWriter output = new NTuplesWriter(outputWriter); List<List<Value>> rightBuffer = new ArrayList<List<Value>>(); try { while (leftParser.hasNext() && rightParser.hasNext()) { boolean wasEqual = NTuplesParserUtils.skipLessThan(leftParser, rightParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); if (!wasEqual && leftParser.hasNext()) { wasEqual = NTuplesParserUtils.skipLessThan(rightParser, leftParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); } if (wasEqual) { rightBuffer.clear(); readToBuffer(rightParser, rightBuffer); mergeWithBuffer(leftParser.next(), rightBuffer, output); while (leftParser.hasNext() && !rightBuffer.isEmpty() && compare(leftParser.peek(), rightBuffer.get(0)) == 0) { mergeWithBuffer(leftParser.next(), rightBuffer, output); } } } } finally { output.close(); leftParser.close(); rightParser.close(); } } NTuplesFileMerger(NTupleMergeTransform transform, ParserConfig parserConfig); void merge(Reader leftReader, Reader rightReader, Writer outputWriter); }
NTuplesFileMerger { public void merge(Reader leftReader, Reader rightReader, Writer outputWriter) throws IOException, NTupleMergeTransformException { NTuplesParser leftParser = new NTuplesParser(leftReader, parserConfig); NTuplesParser rightParser = new NTuplesParser(rightReader, parserConfig); NTuplesWriter output = new NTuplesWriter(outputWriter); List<List<Value>> rightBuffer = new ArrayList<List<Value>>(); try { while (leftParser.hasNext() && rightParser.hasNext()) { boolean wasEqual = NTuplesParserUtils.skipLessThan(leftParser, rightParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); if (!wasEqual && leftParser.hasNext()) { wasEqual = NTuplesParserUtils.skipLessThan(rightParser, leftParser.peek().get(0), NTuplesParserUtils.VALUE_COMPARATOR); } if (wasEqual) { rightBuffer.clear(); readToBuffer(rightParser, rightBuffer); mergeWithBuffer(leftParser.next(), rightBuffer, output); while (leftParser.hasNext() && !rightBuffer.isEmpty() && compare(leftParser.peek(), rightBuffer.get(0)) == 0) { mergeWithBuffer(leftParser.next(), rightBuffer, output); } } } } finally { output.close(); leftParser.close(); rightParser.close(); } } NTuplesFileMerger(NTupleMergeTransform transform, ParserConfig parserConfig); void merge(Reader leftReader, Reader rightReader, Writer outputWriter);  }