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 org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.util.DataChecksum;
23  
24  /**
25   * Checksum types. The Checksum type is a one byte number
26   * that stores a representation of the checksum algorithm
27   * used to encode a hfile. The ordinal of these cannot
28   * change or else you risk breaking all existing HFiles out there.
29   */
30  @InterfaceAudience.Private
31  public enum ChecksumType {
32  
33    NULL((byte)0) {
34      @Override
35      public String getName() {
36        return "NULL";
37      }
38  
39      @Override public DataChecksum.Type getDataChecksumType() {
40        return DataChecksum.Type.NULL;
41      }
42    },
43  
44    CRC32((byte)1) {
45      @Override
46      public String getName() {
47        return "CRC32";
48      }
49  
50      @Override public DataChecksum.Type getDataChecksumType() {
51        return DataChecksum.Type.CRC32;
52      }
53    },
54  
55    CRC32C((byte)2) {
56      @Override
57      public String getName() {
58        return "CRC32C";
59      }
60  
61      @Override public DataChecksum.Type getDataChecksumType() {
62        return DataChecksum.Type.CRC32C;
63      }
64    };
65  
66    private final byte code;
67  
68    public static ChecksumType getDefaultChecksumType() {
69      return ChecksumType.CRC32C;
70    }
71  
72    /** returns the name of this checksum type */
73    public abstract String getName();
74  
75    /** Function to get corresponding {@link org.apache.hadoop.util.DataChecksum.Type}. */
76    public abstract DataChecksum.Type getDataChecksumType();
77  
78    private ChecksumType(final byte c) {
79      this.code = c;
80    }
81  
82    public byte getCode() {
83      return this.code;
84    }
85  
86    /**
87     * Cannot rely on enum ordinals . They change if item is removed or moved.
88     * Do our own codes.
89     * @param b
90     * @return Type associated with passed code.
91     */
92    public static ChecksumType codeToType(final byte b) {
93      for (ChecksumType t : ChecksumType.values()) {
94        if (t.getCode() == b) {
95          return t;
96        }
97      }
98      throw new RuntimeException("Unknown checksum type code " + b);
99    }
100 
101   /**
102    * Map a checksum name to a specific type.
103    * Do our own names.
104    * @param name
105    * @return Type associated with passed code.
106    */
107   public static ChecksumType nameToType(final String name) {
108     for (ChecksumType t : ChecksumType.values()) {
109       if (t.getName().equals(name)) {
110         return t;
111       }
112     }
113     throw new RuntimeException("Unknown checksum type name " + name);
114   }
115 }