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;
021
022import org.apache.hadoop.hbase.ByteBufferKeyOnlyKeyValue;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellComparator;
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(
035      DataBlockEncoding encoding, byte[] header, HFileContext meta) {
036    return new HFileBlockDefaultEncodingContext(encoding, header, meta);
037  }
038
039  @Override
040  public HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext meta) {
041    return new HFileBlockDefaultDecodingContext(meta);
042  }
043
044  protected void postEncoding(HFileBlockEncodingContext encodingCtx)
045      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 Cell createFirstKeyCell(ByteBuffer key, int keyLength) {
054    if (key.hasArray()) {
055      return new KeyValue.KeyOnlyKeyValue(key.array(), key.arrayOffset()
056          + key.position(), keyLength);
057    } else {
058      return new ByteBufferKeyOnlyKeyValue(key, key.position(), keyLength);
059    }
060  }
061
062  protected abstract static class AbstractEncodedSeeker implements
063      EncodedSeeker {
064    protected HFileBlockDecodingContext decodingCtx;
065    protected final CellComparator comparator;
066
067    public AbstractEncodedSeeker(CellComparator comparator,
068        HFileBlockDecodingContext decodingCtx) {
069      this.comparator = comparator;
070      this.decodingCtx = decodingCtx;
071    }
072
073    protected boolean includesMvcc() {
074      return this.decodingCtx.getHFileContext().isIncludesMvcc();
075    }
076
077    protected boolean includesTags() {
078      return this.decodingCtx.getHFileContext().isIncludesTags();
079    }
080
081  }
082
083}