arina-ielchiieva commented on a change in pull request #1807: DRILL-7293: Convert the regex ("log") plugin to use EVF URL: https://github.com/apache/drill/pull/1807#discussion_r293326805
########## File path: exec/java-exec/src/main/java/org/apache/drill/exec/store/log/LogBatchReader.java ########## @@ -0,0 +1,210 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.drill.exec.store.log; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +import org.apache.drill.common.exceptions.UserException; +import org.apache.drill.exec.physical.impl.scan.file.FileScanFramework.FileSchemaNegotiator; +import org.apache.drill.exec.physical.impl.scan.framework.ManagedReader; +import org.apache.drill.exec.physical.rowSet.ResultSetLoader; +import org.apache.drill.exec.physical.rowSet.RowSetLoader; +import org.apache.drill.exec.record.metadata.TupleMetadata; +import org.apache.drill.exec.vector.accessor.ScalarWriter; +import org.apache.drill.shaded.guava.com.google.common.base.Charsets; +import org.apache.hadoop.mapred.FileSplit; + +public class LogBatchReader implements ManagedReader<FileSchemaNegotiator> { + + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LogBatchReader.class); + public static final String RAW_LINE_COL_NAME = "_raw"; + public static final String UNMATCHED_LINE_COL_NAME = "_unmatched_rows"; + + private FileSplit split; + private final LogFormatConfig formatConfig; + private final Pattern pattern; + private final TupleMetadata schema; + private BufferedReader reader; + private int capturingGroups; + private ResultSetLoader loader; + private ScalarWriter rawColWriter; + private ScalarWriter unmatchedColWriter; + private boolean saveMatchedRows; + private int maxErrors; + private int lineNumber; + private int errorCount; + + public LogBatchReader(LogFormatConfig formatConfig, Pattern pattern, TupleMetadata schema) { + this.formatConfig = formatConfig; + this.maxErrors = Math.max(0, formatConfig.getMaxErrors()); + this.pattern = pattern; + this.schema = schema; + } + + @Override + public boolean open(FileSchemaNegotiator negotiator) { + split = negotiator.split(); + setupPattern(); + negotiator.setTableSchema(schema, true); + loader = negotiator.build(); + bindColumns(loader.writer()); + openFile(negotiator); + return true; + } + + private void setupPattern() { + try { + Matcher m = pattern.matcher("test"); + capturingGroups = m.groupCount(); + } catch (PatternSyntaxException e) { + throw UserException + .validationError(e) + .message("Failed to parse regex: \"%s\"", formatConfig.getRegex()) + .build(logger); + } + } + + private void bindColumns(RowSetLoader writer) { + for (int i = 0; i < capturingGroups; i++) { + saveMatchedRows |= writer.scalar(i).isProjected(); + } + rawColWriter = writer.scalar(RAW_LINE_COL_NAME); + saveMatchedRows |= rawColWriter.isProjected(); + unmatchedColWriter = writer.scalar(UNMATCHED_LINE_COL_NAME); + + // If no match-case columns are projected, and the unmatched + // columns is unprojected, then we want to count (matched) + // rows. + + saveMatchedRows |= !unmatchedColWriter.isProjected(); + } + + private void openFile(FileSchemaNegotiator negotiator) { + InputStream in; + try { + in = negotiator.fileSystem().open(split.getPath()); + } catch (Exception e) { + throw UserException + .dataReadError(e) + .message("Failed to open open input file: %s", split.getPath()) + .addContext("User name", negotiator.userName()) + .build(logger); + } + reader = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8)); + } + + @Override + public boolean next() { + RowSetLoader rowWriter = loader.writer(); + while (! rowWriter.isFull()) { + if (! nextLine(rowWriter)) { + return false; + } + } + return true; + } + + private boolean nextLine(RowSetLoader rowWriter) { + String line; + try { + line = reader.readLine(); + } catch (IOException e) { + throw UserException + .dataReadError(e) + .message("Error reading file:") Review comment: ```suggestion .message("Error reading file") ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services