All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.orc.impl.ZeroCopyShims Maven / Gradle / Ivy

/**
 * 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.orc.impl;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.EnumSet;

import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.ReadOption;
import org.apache.hadoop.io.ByteBufferPool;

class ZeroCopyShims {
  private static final class ByteBufferPoolAdapter implements ByteBufferPool {
    private HadoopShims.ByteBufferPoolShim pool;

    public ByteBufferPoolAdapter(HadoopShims.ByteBufferPoolShim pool) {
      this.pool = pool;
    }

    @Override
    public final ByteBuffer getBuffer(boolean direct, int length) {
      return this.pool.getBuffer(direct, length);
    }

    @Override
    public final void putBuffer(ByteBuffer buffer) {
      this.pool.putBuffer(buffer);
    }
  }

  private static final class ZeroCopyAdapter implements HadoopShims.ZeroCopyReaderShim {
    private final FSDataInputStream in;
    private final ByteBufferPoolAdapter pool;
    private final static EnumSet CHECK_SUM = EnumSet
        .noneOf(ReadOption.class);
    private final static EnumSet NO_CHECK_SUM = EnumSet
        .of(ReadOption.SKIP_CHECKSUMS);

    public ZeroCopyAdapter(FSDataInputStream in,
                           HadoopShims.ByteBufferPoolShim poolshim) {
      this.in = in;
      if (poolshim != null) {
        pool = new ByteBufferPoolAdapter(poolshim);
      } else {
        pool = null;
      }
    }

    public final ByteBuffer readBuffer(int maxLength, boolean verifyChecksums)
        throws IOException {
      EnumSet options = NO_CHECK_SUM;
      if (verifyChecksums) {
        options = CHECK_SUM;
      }
      return this.in.read(this.pool, maxLength, options);
    }

    public final void releaseBuffer(ByteBuffer buffer) {
      this.in.releaseBuffer(buffer);
    }

    @Override
    public final void close() throws IOException {
      this.in.close();
    }
  }

  public static HadoopShims.ZeroCopyReaderShim getZeroCopyReader(FSDataInputStream in,
                                                                 HadoopShims.ByteBufferPoolShim pool) throws IOException {
    return new ZeroCopyAdapter(in, pool);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy