001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.encoding;
019
020import java.io.IOException;
021import java.nio.ByteBuffer;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.ByteBufferKeyOnlyKeyValue;
024import org.apache.hadoop.hbase.ExtendedCell;
025import org.apache.hadoop.hbase.KeyValue;
026import org.apache.hadoop.hbase.io.hfile.BlockType;
027import org.apache.hadoop.hbase.io.hfile.HFileContext;
028import org.apache.yetus.audience.InterfaceAudience;
029
030@InterfaceAudience.Private
031public abstract class AbstractDataBlockEncoder implements DataBlockEncoder {
032
033  @Override
034  public HFileBlockEncodingContext newDataBlockEncodingContext(Configuration conf,
035    DataBlockEncoding encoding, byte[] header, HFileContext meta) {
036    return new HFileBlockDefaultEncodingContext(conf, encoding, header, meta);
037  }
038
039  @Override
040  public HFileBlockDecodingContext newDataBlockDecodingContext(Configuration conf,
041    HFileContext meta) {
042    return new HFileBlockDefaultDecodingContext(conf, meta);
043  }
044
045  protected void postEncoding(HFileBlockEncodingContext encodingCtx) throws IOException {
046    if (encodingCtx.getDataBlockEncoding() != DataBlockEncoding.NONE) {
047      encodingCtx.postEncoding(BlockType.ENCODED_DATA);
048    } else {
049      encodingCtx.postEncoding(BlockType.DATA);
050    }
051  }
052
053  protected ExtendedCell createFirstKeyCell(ByteBuffer key, int keyLength) {
054    if (key.hasArray()) {
055      return new KeyValue.KeyOnlyKeyValue(key.array(), key.arrayOffset() + key.position(),
056        keyLength);
057    } else {
058      return new ByteBufferKeyOnlyKeyValue(key, key.position(), keyLength);
059    }
060  }
061
062  /**
063   * Decorates EncodedSeeker with a {@link HFileBlockDecodingContext}
064   */
065  protected abstract static class AbstractEncodedSeeker implements EncodedSeeker {
066    protected HFileBlockDecodingContext decodingCtx;
067
068    public AbstractEncodedSeeker(HFileBlockDecodingContext decodingCtx) {
069      this.decodingCtx = decodingCtx;
070    }
071
072    protected boolean includesMvcc() {
073      return this.decodingCtx.getHFileContext().isIncludesMvcc();
074    }
075
076    protected boolean includesTags() {
077      return this.decodingCtx.getHFileContext().isIncludesTags();
078    }
079  }
080}