Skip to content

Commit 9873a3d

Browse files
committed
#786 Make Cobol processor return the number of records processed.
1 parent a58fdc8 commit 9873a3d

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/processor/CobolProcessor.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ trait CobolProcessor {
3838
* @param inputStream the input stream containing raw COBOL records.
3939
* @param outputStream the output stream where processed records will be written.
4040
* @param rawRecordProcessor the processor that processes each raw record.
41+
* @return The number of records processed.
4142
*/
4243
def process(inputStream: SimpleStream,
4344
outputStream: OutputStream)
44-
(rawRecordProcessor: RawRecordProcessor): Unit
45+
(rawRecordProcessor: RawRecordProcessor): Long
4546

4647
}
4748

@@ -56,7 +57,7 @@ object CobolProcessor {
5657
new CobolProcessor {
5758
override def process(inputStream: SimpleStream,
5859
outputStream: OutputStream)
59-
(rawRecordProcessor: RawRecordProcessor): Unit = {
60+
(rawRecordProcessor: RawRecordProcessor): Long = {
6061
val recordExtractor = getRecordExtractor(readerParameters, inputStream)
6162

6263
val dataStream = inputStream.copyStream()
@@ -116,7 +117,7 @@ object CobolProcessor {
116117

117118
reader.recordExtractor(0, dataStream, headerStream) match {
118119
case Some(extractor) => extractor
119-
case None =>
120+
case None =>
120121
throw new IllegalArgumentException(s"Cannot create a record extractor for the given reader parameters. " +
121122
"Please check the copybook and the reader parameters."
122123
)

cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/processor/impl/StreamProcessor.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ object StreamProcessor {
3434
* @param recordExtractor the extractor that extracts raw records from the input stream.
3535
* @param recordProcessor the per-record processing logic implementation.
3636
* @param outputStream the output stream where the processed records will be written.
37+
* @return The number of records processed.
3738
*/
3839
def processStream(copybook: Copybook,
3940
options: Map[String, String],
4041
inputStream: SimpleStream,
4142
recordExtractor: RawRecordExtractor,
4243
recordProcessor: RawRecordProcessor,
43-
outputStream: OutputStream): Unit = {
44+
outputStream: OutputStream): Long = {
45+
var recordCount = 0L
4446
while (recordExtractor.hasNext) {
47+
recordCount += 1
4548
val record = recordExtractor.next()
4649
val recordSize = record.length
4750

@@ -61,5 +64,6 @@ object StreamProcessor {
6164
val footer = inputStream.next(footerSize.toInt)
6265
outputStream.write(footer)
6366
}
67+
recordCount
6468
}
6569
}

cobol-parser/src/test/scala/za/co/absa/cobrix/cobol/processor/CobolProcessorBuilderSuite.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ class CobolProcessorBuilderSuite extends AnyWordSpec {
4242
}
4343
}
4444

45-
builder.build().process(is, os)(processor)
45+
val count = builder.build().process(is, os)(processor)
4646

4747
val outputArray = os.toByteArray
4848

49+
assert(count == 4)
4950
assert(outputArray.head == -16)
5051
assert(outputArray(1) == -15)
5152
assert(outputArray(2) == -14)

0 commit comments

Comments
 (0)