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.hfile; 019 020import java.io.DataOutputStream; 021import java.io.IOException; 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.hbase.ExtendedCell; 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; 031import org.apache.yetus.audience.InterfaceAudience; 032 033/** 034 * Does not perform any kind of encoding/decoding. 035 */ 036@InterfaceAudience.Private 037public class NoOpDataBlockEncoder implements HFileDataBlockEncoder { 038 039 public static final NoOpDataBlockEncoder INSTANCE = 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(ExtendedCell cell, HFileBlockEncodingContext encodingCtx, DataOutputStream out) 051 throws IOException { 052 NoneEncodingState state = (NoneEncodingState) encodingCtx.getEncodingState(); 053 NoneEncoder encoder = state.encoder; 054 int size = encoder.write(cell); 055 state.postCellEncode(size, size); 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(Configuration conf, 084 byte[] dummyHeader, HFileContext meta) { 085 return new HFileBlockDefaultEncodingContext(conf, null, dummyHeader, meta); 086 } 087 088 @Override 089 public HFileBlockDecodingContext newDataBlockDecodingContext(Configuration conf, 090 HFileContext meta) { 091 return new HFileBlockDefaultDecodingContext(conf, meta); 092 } 093 094 @Override 095 public void startBlockEncoding(HFileBlockEncodingContext blkEncodingCtx, DataOutputStream out) 096 throws IOException { 097 if (blkEncodingCtx.getClass() != HFileBlockDefaultEncodingContext.class) { 098 throw new IOException(this.getClass().getName() + " only accepts " 099 + HFileBlockDefaultEncodingContext.class.getName() + " as the " + "encoding context."); 100 } 101 102 HFileBlockDefaultEncodingContext encodingCtx = 103 (HFileBlockDefaultEncodingContext) blkEncodingCtx; 104 encodingCtx.prepareEncoding(out); 105 106 NoneEncoder encoder = new NoneEncoder(out, encodingCtx); 107 NoneEncodingState state = new NoneEncodingState(); 108 state.encoder = encoder; 109 blkEncodingCtx.setEncodingState(state); 110 } 111 112 @Override 113 public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out, 114 byte[] uncompressedBytesWithHeader, BlockType blockType) throws IOException { 115 encodingCtx.postEncoding(BlockType.DATA); 116 } 117}