View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver.wal;
19  
20  import java.io.DataInputStream;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.codec.Codec;
28  import org.apache.hadoop.hbase.codec.Decoder;
29  import org.apache.hadoop.hbase.codec.Encoder;
30  import org.apache.hadoop.hbase.codec.KeyValueCodec;
31  import org.apache.hadoop.hbase.regionserver.wal.KeyValueCompression.CompressedKvEncoder;
32  
33  public class WALEditCodec implements Codec {
34    /** Configuration key for a custom class to use when serializing the WALEdits to the HLog */
35    public static final String WAL_EDIT_CODEC_CLASS_KEY = "hbase.regionserver.wal.codec";
36  
37    private CompressionContext compression;
38  
39    /**
40     * Nullary Constructor - all subclass must support this to load from configuration. Setup can be
41     * completed in the {@link #init} method.
42     * <p>
43     * This implementation defaults to having no compression on the resulting {@link Encoder}/
44     * {@link Decoder}, though it can be added via {@link #setCompression(CompressionContext)}
45     */
46    public WALEditCodec() {
47    }
48  
49    /**
50     * Initialize <tt>this</tt> - called exactly once after the object is instantiated and before any
51     * other method in this class. By default, does nothing.
52     * @param conf {@link Configuration} from which to configure <tt>this</tt>
53     */
54    public void init(Configuration conf) {
55    }
56  
57    public void setCompression(CompressionContext compression) {
58      this.compression = compression;
59    }
60  
61    @Override
62    public Decoder getDecoder(InputStream is) {
63      return
64          (compression == null) ? new KeyValueCodec.KeyValueDecoder((DataInputStream) is)
65              : new KeyValueCompression.CompressedKvDecoder((DataInputStream) is, compression);
66    }
67  
68    @Override
69    public Encoder getEncoder(OutputStream os) {
70      return
71          (compression == null) ? new KeyValueCodec.KeyValueEncoder((DataOutputStream) os)
72          : new CompressedKvEncoder((DataOutputStream) os, compression);
73    }
74  
75    /**
76     * Create and setup a {@link WALEditCodec} from the {@link Configuration}, if one has been
77     * specified. Fully prepares the codec for use in serialization.
78     * @param conf {@link Configuration} to read for the user-specified codec. If none is specified,
79     *          uses a {@link WALEditCodec}.
80     * @param compressionContext compression to setup on the codec.
81     * @return a {@link WALEditCodec} ready for use.
82     * @throws IOException if the codec cannot be created
83     */
84    public static WALEditCodec create(Configuration conf, CompressionContext compressionContext)
85        throws IOException {
86      Class<? extends WALEditCodec> codecClazz = conf.getClass(WALEditCodec.WAL_EDIT_CODEC_CLASS_KEY,
87        WALEditCodec.class, WALEditCodec.class);
88      try {
89        WALEditCodec codec = codecClazz.newInstance();
90        codec.init(conf);
91        codec.setCompression(compressionContext);
92        return codec;
93      } catch (InstantiationException e) {
94        throw new IOException("Couldn't instantiate the configured WALEditCodec!", e);
95      } catch (IllegalAccessException e) {
96        throw new IOException("Couldn't instantiate the configured WALEditCodec!", e);
97      }
98    }
99  }