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 void encode(Cell cell, HFileBlockEncodingContext encodingCtx,
051      DataOutputStream out) throws IOException {
052    NoneEncodingState state = (NoneEncodingState) encodingCtx
053        .getEncodingState();
054    NoneEncoder encoder = state.encoder;
055    int size = encoder.write(cell);
056    state.postCellEncode(size, size);
057  }
058
059  @Override
060  public boolean useEncodedScanner() {
061    return false;
062  }
063
064  @Override
065  public void saveMetadata(HFile.Writer writer) {
066  }
067
068  @Override
069  public DataBlockEncoding getDataBlockEncoding() {
070    return DataBlockEncoding.NONE;
071  }
072
073  @Override
074  public DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction) {
075    return DataBlockEncoding.NONE;
076  }
077  
078  @Override
079  public String toString() {
080    return getClass().getSimpleName();
081  }
082
083  @Override
084  public HFileBlockEncodingContext newDataBlockEncodingContext(
085      byte[] dummyHeader, HFileContext meta) {
086    return new HFileBlockDefaultEncodingContext(null, dummyHeader, meta);
087  }
088
089  @Override
090  public HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext meta) {
091    return new HFileBlockDefaultDecodingContext(meta);
092  }
093
094  @Override
095  public void startBlockEncoding(HFileBlockEncodingContext blkEncodingCtx,
096      DataOutputStream out) throws IOException {
097    if (blkEncodingCtx.getClass() != HFileBlockDefaultEncodingContext.class) {
098      throw new IOException(this.getClass().getName() + " only accepts "
099          + HFileBlockDefaultEncodingContext.class.getName() + " as the "
100          + "encoding context.");
101    }
102
103    HFileBlockDefaultEncodingContext encodingCtx =
104      (HFileBlockDefaultEncodingContext) blkEncodingCtx;
105    encodingCtx.prepareEncoding(out);
106
107    NoneEncoder encoder = new NoneEncoder(out, encodingCtx);
108    NoneEncodingState state = new NoneEncodingState();
109    state.encoder = encoder;
110    blkEncodingCtx.setEncodingState(state);
111  }
112
113  @Override
114  public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out,
115      byte[] uncompressedBytesWithHeader, BlockType blockType) throws IOException {
116    encodingCtx.postEncoding(BlockType.DATA);
117  }
118}