-
Notifications
You must be signed in to change notification settings - Fork 78
/
VarLenNestedReader.scala
340 lines (309 loc) · 14.6 KB
/
VarLenNestedReader.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright 2018 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package za.co.absa.cobrix.cobol.reader
import za.co.absa.cobrix.cobol.internal.Logging
import za.co.absa.cobrix.cobol.parser.common.Constants
import za.co.absa.cobrix.cobol.parser.encoding.codepage.CodePage
import za.co.absa.cobrix.cobol.parser.encoding.{ASCII, EBCDIC}
import za.co.absa.cobrix.cobol.parser.headerparsers.{RecordHeaderParser, RecordHeaderParserFactory}
import za.co.absa.cobrix.cobol.parser.recordformats.RecordFormat.{FixedBlock, VariableBlock}
import za.co.absa.cobrix.cobol.parser.{Copybook, CopybookParser}
import za.co.absa.cobrix.cobol.reader.extractors.raw._
import za.co.absa.cobrix.cobol.reader.extractors.record.RecordHandler
import za.co.absa.cobrix.cobol.reader.index.IndexGenerator
import za.co.absa.cobrix.cobol.reader.index.entry.SparseIndexEntry
import za.co.absa.cobrix.cobol.reader.iterator.{VarLenHierarchicalIterator, VarLenNestedIterator}
import za.co.absa.cobrix.cobol.reader.parameters.ReaderParameters
import za.co.absa.cobrix.cobol.reader.recordheader.{RecordHeaderDecoderBdw, RecordHeaderDecoderRdw, RecordHeaderParameters}
import za.co.absa.cobrix.cobol.reader.schema.CobolSchema
import za.co.absa.cobrix.cobol.reader.stream.SimpleStream
import za.co.absa.cobrix.cobol.reader.validator.ReaderParametersValidator
import java.nio.charset.{Charset, StandardCharsets}
import scala.collection.immutable.HashMap
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
/**
* The Cobol data reader for variable length records that gets input binary data as a stream and produces nested structure schema
*
* @param copybookContents The contents of a copybook.
* @param readerProperties Additional properties for customizing the reader.
*/
class VarLenNestedReader[T: ClassTag](copybookContents: Seq[String],
readerProperties: ReaderParameters,
handler: RecordHandler[T]) extends VarLenReader with Logging with Serializable {
protected val cobolSchema: CobolSchema = loadCopyBook(copybookContents)
val recordHeaderParser: RecordHeaderParser = {
getRecordHeaderParser
}
checkInputArgumentsValidity()
def recordExtractor(startingRecordNumber: Long,
dataStream: SimpleStream,
headerStream: SimpleStream,
copybook: Copybook
): Option[RawRecordExtractor] = {
val rdwParams = RecordHeaderParameters(readerProperties.isRdwBigEndian, readerProperties.rdwAdjustment)
val rdwDecoder = new RecordHeaderDecoderRdw(rdwParams)
val bdwOpt = readerProperties.bdw
val bdwParamsOpt = bdwOpt.map(bdw => RecordHeaderParameters(bdw.isBigEndian, bdw.adjustment))
val bdwDecoderOpt = bdwParamsOpt.map(bdwParams => new RecordHeaderDecoderBdw(bdwParams))
val reParams = RawRecordContext(startingRecordNumber, dataStream, headerStream, copybook, rdwDecoder, bdwDecoderOpt.getOrElse(rdwDecoder), readerProperties.reAdditionalInfo)
readerProperties.recordExtractor match {
case Some(recordExtractorClass) =>
Some(RawRecordExtractorFactory.createRecordHeaderParser(recordExtractorClass, reParams))
case None if readerProperties.isText && readerProperties.allowPartialRecords =>
Some(new TextRecordExtractor(reParams))
case None if readerProperties.isText =>
Some(new TextFullRecordExtractor(reParams))
case None if readerProperties.recordFormat == FixedBlock =>
val fbParams = FixedBlockParameters(readerProperties.recordLength, bdwOpt.get.blockLength, bdwOpt.get.recordsPerBlock)
FixedBlockParameters.validate(fbParams)
Some(new FixedBlockRawRecordExtractor(reParams, fbParams))
case None if readerProperties.recordFormat == VariableBlock =>
Some(new VariableBlockVariableRecordExtractor(reParams))
case None if readerProperties.variableSizeOccurs &&
readerProperties.recordHeaderParser.isEmpty &&
!readerProperties.isRecordSequence &&
readerProperties.lengthFieldExpression.isEmpty =>
Some(new VarOccursRecordExtractor(reParams))
case None =>
None
}
}
override def getCobolSchema: CobolSchema = cobolSchema
override def getRecordSize: Int = cobolSchema.copybook.getRecordSize
override def isIndexGenerationNeeded: Boolean = (readerProperties.lengthFieldExpression.isEmpty || readerProperties.isRecordSequence) && readerProperties.isIndexGenerationNeeded
override def isRdwBigEndian: Boolean = readerProperties.isRdwBigEndian
override def getRecordIterator(dataStream: SimpleStream,
headerStream: SimpleStream,
startingFileOffset: Long,
fileNumber: Int,
startingRecordIndex: Long): Iterator[Seq[Any]] =
if (cobolSchema.copybook.isHierarchical) {
new VarLenHierarchicalIterator(cobolSchema.copybook,
dataStream,
readerProperties,
recordHeaderParser,
recordExtractor(startingRecordIndex, dataStream, headerStream, cobolSchema.copybook),
fileNumber,
startingRecordIndex,
startingFileOffset,
handler)
} else {
new VarLenNestedIterator(cobolSchema.copybook,
dataStream,
readerProperties,
recordHeaderParser,
recordExtractor(startingRecordIndex, dataStream, headerStream, cobolSchema.copybook),
fileNumber,
startingRecordIndex,
startingFileOffset,
cobolSchema.segmentIdPrefix,
handler)
}
/**
* Traverses the data sequentially as fast as possible to generate record index.
* This index will be used to distribute workload of the conversion.
*
* @param dataStream A stream of input binary data
* @param headerStream A stream pointing to the beginning of the file, even if inputStream is pointing
* to a record in the middle.
* @param fileNumber A file number uniquely identified a particular file of the data set
* @return An index of the file
*/
override def generateIndex(dataStream: SimpleStream,
headerStream: SimpleStream,
fileNumber: Int,
isRdwBigEndian: Boolean): ArrayBuffer[SparseIndexEntry] = {
val inputSplitSizeRecords: Option[Int] = readerProperties.inputSplitRecords
val inputSplitSizeMB: Option[Int] = getSplitSizeMB
if (inputSplitSizeRecords.isDefined) {
if (inputSplitSizeRecords.get < 1 || inputSplitSizeRecords.get > 1000000000) {
throw new IllegalArgumentException(s"Invalid input split size. The requested number of records is ${inputSplitSizeRecords.get}.")
}
logger.info(s"Input split size = ${inputSplitSizeRecords.get} records")
} else {
if (inputSplitSizeMB.nonEmpty) {
if (inputSplitSizeMB.get < 1 || inputSplitSizeMB.get > 2000) {
throw new IllegalArgumentException(s"Invalid input split size of ${inputSplitSizeMB.get} MB.")
}
logger.info(s"Input split size = ${inputSplitSizeMB.get} MB")
}
}
val copybook = cobolSchema.copybook
val segmentIdField = ReaderParametersValidator.getSegmentIdField(readerProperties.multisegment, copybook)
val segmentIdValue = getRootSegmentId
// It makes sense to parse data hierarchically only if hierarchical id generation is requested
val isHierarchical = readerProperties.multisegment match {
case Some(params) => params.segmentLevelIds.nonEmpty || params.fieldParentMap.nonEmpty
case None => false
}
segmentIdField match {
case Some(field) => IndexGenerator.sparseIndexGenerator(fileNumber,
dataStream,
readerProperties.fileStartOffset,
recordHeaderParser,
recordExtractor(0L, dataStream, headerStream, copybook),
inputSplitSizeRecords,
inputSplitSizeMB,
Some(copybook),
Some(field),
isHierarchical,
segmentIdValue)
case None => IndexGenerator.sparseIndexGenerator(fileNumber,
dataStream,
readerProperties.fileStartOffset,
recordHeaderParser,
recordExtractor(0L, dataStream, headerStream, copybook),
inputSplitSizeRecords,
inputSplitSizeMB,
None,
None,
isHierarchical)
}
}
private def loadCopyBook(copyBookContents: Seq[String]): CobolSchema = {
val encoding = if (readerProperties.isEbcdic) EBCDIC else ASCII
val segmentRedefines = readerProperties.multisegment.map(r => r.segmentIdRedefineMap.values.toList.distinct).getOrElse(Nil)
val fieldParentMap = readerProperties.multisegment.map(r => r.fieldParentMap).getOrElse(HashMap[String, String]())
val codePage = getCodePage(readerProperties.ebcdicCodePage, readerProperties.ebcdicCodePageClass)
val asciiCharset = if (readerProperties.asciiCharset.isEmpty) StandardCharsets.US_ASCII else Charset.forName(readerProperties.asciiCharset)
val schema = if (copyBookContents.size == 1)
CopybookParser.parseTree(encoding,
copyBookContents.head,
readerProperties.dropGroupFillers,
readerProperties.dropValueFillers,
readerProperties.fillerNamingPolicy,
segmentRedefines,
fieldParentMap,
readerProperties.stringTrimmingPolicy,
readerProperties.commentPolicy,
readerProperties.strictSignOverpunch,
readerProperties.improvedNullDetection,
readerProperties.decodeBinaryAsHex,
codePage,
asciiCharset,
readerProperties.isUtf16BigEndian,
readerProperties.floatingPointFormat,
readerProperties.nonTerminals,
readerProperties.occursMappings,
readerProperties.debugFieldsPolicy,
readerProperties.fieldCodePage)
else
Copybook.merge(copyBookContents.map(cpb =>
CopybookParser.parseTree(encoding,
cpb,
readerProperties.dropGroupFillers,
readerProperties.dropValueFillers,
readerProperties.fillerNamingPolicy,
segmentRedefines,
fieldParentMap,
readerProperties.stringTrimmingPolicy,
readerProperties.commentPolicy,
readerProperties.strictSignOverpunch,
readerProperties.improvedNullDetection,
readerProperties.decodeBinaryAsHex,
codePage,
asciiCharset,
readerProperties.isUtf16BigEndian,
readerProperties.floatingPointFormat,
nonTerminals = readerProperties.nonTerminals,
readerProperties.occursMappings,
readerProperties.debugFieldsPolicy,
readerProperties.fieldCodePage)
))
val segIdFieldCount = readerProperties.multisegment.map(p => p.segmentLevelIds.size).getOrElse(0)
val segmentIdPrefix = readerProperties.multisegment.map(p => p.segmentIdPrefix).getOrElse("")
new CobolSchema(schema, readerProperties.schemaPolicy, readerProperties.inputFileNameColumn, readerProperties.generateRecordId, readerProperties.generateRecordBytes, segIdFieldCount, segmentIdPrefix)
}
private def checkInputArgumentsValidity(): Unit = {
if (readerProperties.startOffset < 0) {
throw new IllegalArgumentException(s"Invalid record start offset = ${readerProperties.startOffset}. A record start offset cannot be negative.")
}
if (readerProperties.endOffset < 0) {
throw new IllegalArgumentException(s"Invalid record end offset = ${readerProperties.endOffset}. A record end offset cannot be negative.")
}
}
private def getSplitSizeMB: Option[Int] = {
if (readerProperties.inputSplitSizeMB.isDefined) {
readerProperties.inputSplitSizeMB
} else {
readerProperties.hdfsDefaultBlockSize
}
}
private def getCodePage(codePageName: String, codePageClass: Option[String]): CodePage = {
codePageClass match {
case Some(c) => CodePage.getCodePageByClass(c)
case None => CodePage.getCodePageByName(codePageName)
}
}
private def getRecordHeaderParser: RecordHeaderParser = {
val adjustment1 = if (readerProperties.isRdwPartRecLength) -4 else 0
val adjustment2 = readerProperties.rdwAdjustment
val rhp = readerProperties.recordHeaderParser match {
case Some(customRecordParser) => RecordHeaderParserFactory.createRecordHeaderParser(customRecordParser,
cobolSchema.getRecordSize,
readerProperties.fileStartOffset,
readerProperties.fileEndOffset,
adjustment1 + adjustment2)
case None => getDefaultRecordHeaderParser
}
readerProperties.rhpAdditionalInfo.foreach(rhp.onReceiveAdditionalInfo)
rhp
}
private def getDefaultRecordHeaderParser: RecordHeaderParser = {
val adjustment1 = if (readerProperties.isRdwPartRecLength) -4 else 0
val adjustment2 = readerProperties.rdwAdjustment
if (readerProperties.isRecordSequence) {
// RDW record parsers
if (isRdwBigEndian) {
RecordHeaderParserFactory.createRecordHeaderParser(Constants.RhRdwBigEndian,
cobolSchema.getRecordSize,
readerProperties.fileStartOffset,
readerProperties.fileEndOffset,
adjustment1 + adjustment2
)
} else {
RecordHeaderParserFactory.createRecordHeaderParser(Constants.RhRdwLittleEndian,
cobolSchema.getRecordSize,
readerProperties.fileStartOffset,
readerProperties.fileEndOffset,
adjustment1 + adjustment2
)
}
} else {
// Fixed record length record parser
val recordSize = readerProperties.recordLength.getOrElse(cobolSchema.getRecordSize)
RecordHeaderParserFactory.createRecordHeaderParser(Constants.RhRdwFixedLength,
recordSize,
readerProperties.fileStartOffset,
readerProperties.fileEndOffset,
0
)
}
}
private def getRootSegmentId: String = {
readerProperties.multisegment match {
case Some(m) =>
if (m.fieldParentMap.nonEmpty && m.segmentIdRedefineMap.nonEmpty) {
cobolSchema.copybook.getRootSegmentIds(m.segmentIdRedefineMap, m.fieldParentMap).headOption.getOrElse("")
} else {
m.segmentLevelIds.headOption.getOrElse("")
}
case None =>
""
}
}
}