View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.io.hfile;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  import org.apache.hadoop.hbase.Cell;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
25  import org.apache.hadoop.hbase.io.encoding.EncodingState;
26  import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
27  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultDecodingContext;
28  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext;
29  import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
30  import org.apache.hadoop.hbase.io.encoding.NoneEncoder;
31
32  /**
33   * Does not perform any kind of encoding/decoding.
34   */
35  @InterfaceAudience.Private
36  public class NoOpDataBlockEncoder implements HFileDataBlockEncoder {
37
38    public static final NoOpDataBlockEncoder INSTANCE =
39        new NoOpDataBlockEncoder();
40
41    private static class NoneEncodingState extends EncodingState {
42      NoneEncoder encoder = null;
43    }
44
45    /** Cannot be instantiated. Use {@link #INSTANCE} instead. */
46    private NoOpDataBlockEncoder() {
47    }
48
49    @Override
50    public int encode(Cell cell, HFileBlockEncodingContext encodingCtx,
51        DataOutputStream out) throws IOException {
52      NoneEncodingState state = (NoneEncodingState) encodingCtx
53          .getEncodingState();
54      NoneEncoder encoder = state.encoder;
55      return encoder.write(cell);
56    }
57
58    @Override
59    public boolean useEncodedScanner() {
60      return false;
61    }
62
63    @Override
64    public void saveMetadata(HFile.Writer writer) {
65    }
66
67    @Override
68    public DataBlockEncoding getDataBlockEncoding() {
69      return DataBlockEncoding.NONE;
70    }
71
72    @Override
73    public DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction) {
74      return DataBlockEncoding.NONE;
75    }
76
77    @Override
78    public String toString() {
79      return getClass().getSimpleName();
80    }
81
82    @Override
83    public HFileBlockEncodingContext newDataBlockEncodingContext(
84        byte[] dummyHeader, HFileContext meta) {
85      return new HFileBlockDefaultEncodingContext(null, dummyHeader, meta);
86    }
87  
88    @Override
89    public HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext meta) {
90      return new HFileBlockDefaultDecodingContext(meta);
91    }
92
93    @Override
94    public void startBlockEncoding(HFileBlockEncodingContext blkEncodingCtx,
95        DataOutputStream out) throws IOException {
96      if (blkEncodingCtx.getClass() != HFileBlockDefaultEncodingContext.class) {
97        throw new IOException(this.getClass().getName() + " only accepts "
98            + HFileBlockDefaultEncodingContext.class.getName() + " as the "
99            + "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 }