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.hfile;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021
022import org.apache.hadoop.hbase.Cell;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
025import org.apache.hadoop.hbase.io.encoding.EncodingState;
026import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
027import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultDecodingContext;
028import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext;
029import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
030import org.apache.hadoop.hbase.io.encoding.NoneEncoder;
031
032/**
033 * Does not perform any kind of encoding/decoding.
034 */
035@InterfaceAudience.Private
036public class NoOpDataBlockEncoder implements HFileDataBlockEncoder {
037
038  public static final NoOpDataBlockEncoder INSTANCE =
039      new NoOpDataBlockEncoder();
040
041  private static class NoneEncodingState extends EncodingState {
042    NoneEncoder encoder = null;
043  }
044
045  /** Cannot be instantiated. Use {@link #INSTANCE} instead. */
046  private NoOpDataBlockEncoder() {
047  }
048
049  @Override
050  public int encode(Cell cell, HFileBlockEncodingContext encodingCtx,
051      DataOutputStream out) throws IOException {
052    NoneEncodingState state = (NoneEncodingState) encodingCtx
053        .getEncodingState();
054    NoneEncoder encoder = state.encoder;
055    return encoder.write(cell);
056  }
057
058  @Override
059  public boolean useEncodedScanner() {
060    return false;
061  }
062
063  @Override
064  public void saveMetadata(HFile.Writer writer) {
065  }
066
067  @Override
068  public DataBlockEncoding getDataBlockEncoding() {
069    return DataBlockEncoding.NONE;
070  }
071
072  @Override
073  public DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction) {
074    return DataBlockEncoding.NONE;
075  }
076  
077  @Override
078  public String toString() {
079    return getClass().getSimpleName();
080  }
081
082  @Override
083  public HFileBlockEncodingContext newDataBlockEncodingContext(
084      byte[] dummyHeader, HFileContext meta) {
085    return new HFileBlockDefaultEncodingContext(null, dummyHeader, meta);
086  }
087
088  @Override
089  public HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext meta) {
090    return new HFileBlockDefaultDecodingContext(meta);
091  }
092
093  @Override
094  public void startBlockEncoding(HFileBlockEncodingContext blkEncodingCtx,
095      DataOutputStream out) throws IOException {
096    if (blkEncodingCtx.getClass() != HFileBlockDefaultEncodingContext.class) {
097      throw new IOException(this.getClass().getName() + " only accepts "
098          + HFileBlockDefaultEncodingContext.class.getName() + " as the "
099          + "encoding context.");
100    }
101
102    HFileBlockDefaultEncodingContext encodingCtx = (HFileBlockDefaultEncodingContext) blkEncodingCtx;
103    encodingCtx.prepareEncoding(out);
104
105    NoneEncoder encoder = new NoneEncoder(out, encodingCtx);
106    NoneEncodingState state = new NoneEncodingState();
107    state.encoder = encoder;
108    blkEncodingCtx.setEncodingState(state);
109  }
110
111  @Override
112  public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out,
113      byte[] uncompressedBytesWithHeader, BlockType blockType) throws IOException {
114    encodingCtx.postEncoding(BlockType.DATA);
115  }
116}