Github user ppadma commented on a diff in the pull request:

    https://github.com/apache/drill/pull/611#discussion_r85153296
  
    --- Diff: 
exec/java-exec/src/main/java/org/apache/drill/exec/util/filereader/DirectBufInputStream.java
 ---
    @@ -0,0 +1,183 @@
    +/**
    + * 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
    + * <p/>
    + * http://www.apache.org/licenses/LICENSE-2.0
    + * <p/>
    + * 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.util.filereader;
    +
    +import com.google.common.base.Preconditions;
    +import io.netty.buffer.DrillBuf;
    +import org.apache.drill.exec.memory.BufferAllocator;
    +import org.apache.hadoop.fs.ByteBufferReadable;
    +import org.apache.hadoop.fs.FSDataInputStream;
    +import org.apache.parquet.hadoop.util.CompatibilityUtil;
    +
    +import java.io.FilterInputStream;
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.lang.reflect.InvocationTargetException;
    +import java.lang.reflect.Method;
    +import java.nio.ByteBuffer;
    +
    +public class DirectBufInputStream extends FilterInputStream {
    +
    +  private static final org.slf4j.Logger logger =
    +      org.slf4j.LoggerFactory.getLogger(DirectBufInputStream.class);
    +
    +  protected boolean enableHints = true;
    +  protected String streamId; // a name for logging purposes only
    +  protected BufferAllocator allocator;
    +  /**
    +   * The length of the data we expect to read. The caller may, in fact,
    +   * ask for more or less bytes. However this is useful for providing 
hints where
    +   * the underlying InputStream supports hints (e.g. fadvise)
    +   */
    +  protected final long totalByteSize;
    +
    +  /**
    +   * The offset in the underlying stream to start reading from
    +   */
    +  protected final long startOffset;
    +
    +  public DirectBufInputStream(InputStream in, BufferAllocator allocator, 
String id, long startOffset,
    +      long totalByteSize, boolean enableHints) {
    +    super(in);
    +    Preconditions.checkArgument(startOffset >= 0);
    +    Preconditions.checkArgument(totalByteSize >= 0);
    +    this.streamId = id;
    +    this.allocator = allocator;
    +    this.startOffset = startOffset;
    +    this.totalByteSize = totalByteSize;
    +    this.enableHints = enableHints;
    +  }
    +
    +  public void init() throws IOException, UnsupportedOperationException {
    +    checkStreamSupportsByteBuffer();
    +    if (enableHints) {
    +      fadviseIfAvailable(getInputStream(), this.startOffset, 
this.totalByteSize);
    +    }
    +    getInputStream().seek(this.startOffset);
    +    return;
    +  }
    +
    +  public int read() throws IOException {
    +    return getInputStream().read();
    +  }
    +
    +  public synchronized int read(DrillBuf buf, int off, int len) throws 
IOException {
    +    buf.clear();
    +    ByteBuffer directBuffer = buf.nioBuffer(0, len);
    +    int lengthLeftToRead = len;
    +    while (lengthLeftToRead > 0) {
    +      lengthLeftToRead -= CompatibilityUtil.getBuf(getInputStream(), 
directBuffer, lengthLeftToRead);
    +    }
    +    buf.writerIndex(len);
    +    return len;
    +  }
    +
    +  public synchronized DrillBuf getNext(int bytes) throws IOException {
    +    DrillBuf b = allocator.buffer(bytes);
    +    int bytesRead = -1;
    +    try {
    +    bytesRead = read(b, 0, bytes);
    +    } catch (IOException e){
    +      b.release();
    +      throw e;
    +    }
    +    if (bytesRead <= -1) {
    --- End diff --
    
    bytesRead = 0 ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to