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  
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.IOException;
22  import java.lang.reflect.Constructor;
23  import java.util.zip.Checksum;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * Checksum types. The Checksum type is a one byte number
30   * that stores a representation of the checksum algorithm
31   * used to encode a hfile. The ordinal of these cannot 
32   * change or else you risk breaking all existing HFiles out there.
33   */
34  public enum ChecksumType {
35  
36    NULL((byte)0) {
37      @Override
38      public String getName() {
39        return "NULL";
40      }
41      @Override
42      public void initialize() {
43        // do nothing
44      }
45      @Override
46      public Checksum getChecksumObject() throws IOException {
47        return null; // checksums not used
48      }
49    },
50  
51    CRC32((byte)1) {
52      private transient Constructor<?> ctor;
53  
54      @Override
55      public String getName() {
56        return "CRC32";
57      }
58  
59      @Override
60      public void initialize() {
61        final String PURECRC32 = "org.apache.hadoop.util.PureJavaCrc32";
62        final String JDKCRC = "java.util.zip.CRC32";
63        LOG = LogFactory.getLog(ChecksumType.class);
64  
65        // check if hadoop library is available
66        try {
67          ctor = ChecksumFactory.newConstructor(PURECRC32);
68          LOG.debug(PURECRC32 + " available");
69        } catch (Exception e) {
70          LOG.trace(PURECRC32 + " not available.");
71        }
72        try {
73          // The default checksum class name is java.util.zip.CRC32. 
74          // This is available on all JVMs.
75          if (ctor == null) {
76            ctor = ChecksumFactory.newConstructor(JDKCRC);
77            LOG.debug(JDKCRC + " available");
78          }
79        } catch (Exception e) {
80          LOG.trace(JDKCRC + " not available.");
81        }
82      }
83  
84      @Override
85      public Checksum getChecksumObject() throws IOException {
86        if (ctor == null) {
87          throw new IOException("Bad constructor for " + getName());
88        }
89        try {
90          return (Checksum)ctor.newInstance();
91        } catch (Exception e) {
92          throw new IOException(e);
93        }
94      }
95    },
96  
97    CRC32C((byte)2) {
98      private transient Constructor<?> ctor;
99  
100     @Override
101     public String getName() {
102       return "CRC32C";
103     }
104 
105     @Override
106     public void initialize() {
107       final String PURECRC32C = "org.apache.hadoop.util.PureJavaCrc32C";
108       LOG = LogFactory.getLog(ChecksumType.class);
109       try {
110         ctor = ChecksumFactory.newConstructor(PURECRC32C);
111         LOG.debug(PURECRC32C + " available");
112       } catch (Exception e) {
113         LOG.trace(PURECRC32C + " not available.");
114       }
115     }
116 
117     @Override
118     public Checksum getChecksumObject() throws IOException {
119       if (ctor == null) {
120         throw new IOException("Bad constructor for " + getName());
121       }
122       try {
123         return (Checksum)ctor.newInstance();
124       } catch (Exception e) {
125         throw new IOException(e);
126       }
127     }
128   };
129 
130   private final byte code;
131   protected Log LOG;
132 
133   /** initializes the relevant checksum class object */
134   abstract void initialize();
135 
136   /** returns the name of this checksum type */
137   public abstract String getName();
138 
139   private ChecksumType(final byte c) {
140     this.code = c;
141     initialize();
142   }
143 
144   /** returns a object that can be used to generate/validate checksums */
145   public abstract Checksum getChecksumObject() throws IOException;
146 
147   public byte getCode() {
148     return this.code;
149   }
150 
151   /**
152    * Cannot rely on enum ordinals . They change if item is removed or moved.
153    * Do our own codes.
154    * @param b
155    * @return Type associated with passed code.
156    */
157   public static ChecksumType codeToType(final byte b) {
158     for (ChecksumType t : ChecksumType.values()) {
159       if (t.getCode() == b) {
160         return t;
161       }
162     }
163     throw new RuntimeException("Unknown checksum type code " + b);
164   }
165 
166   /**
167    * Map a checksum name to a specific type.
168    * Do our own names.
169    * @param name
170    * @return Type associated with passed code.
171    */
172   public static ChecksumType nameToType(final String name) {
173     for (ChecksumType t : ChecksumType.values()) {
174       if (t.getName().equals(name)) {
175         return t;
176       }
177     }
178     throw new RuntimeException("Unknown checksum type name " + name);
179   }
180 }