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