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 */
018
019package org.apache.hadoop.hbase.util;
020
021import org.apache.hadoop.util.DataChecksum;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Checksum types. The Checksum type is a one byte number
026 * that stores a representation of the checksum algorithm
027 * used to encode a hfile. The ordinal of these cannot
028 * change or else you risk breaking all existing HFiles out there.
029 */
030@InterfaceAudience.Private
031public enum ChecksumType {
032
033  NULL((byte)0) {
034    @Override
035    public String getName() {
036      return "NULL";
037    }
038
039    @Override public DataChecksum.Type getDataChecksumType() {
040      return DataChecksum.Type.NULL;
041    }
042  },
043
044  CRC32((byte)1) {
045    @Override
046    public String getName() {
047      return "CRC32";
048    }
049
050    @Override public DataChecksum.Type getDataChecksumType() {
051      return DataChecksum.Type.CRC32;
052    }
053  },
054
055  CRC32C((byte)2) {
056    @Override
057    public String getName() {
058      return "CRC32C";
059    }
060
061    @Override public DataChecksum.Type getDataChecksumType() {
062      return DataChecksum.Type.CRC32C;
063    }
064  };
065
066  private final byte code;
067
068  public static ChecksumType getDefaultChecksumType() {
069    return ChecksumType.CRC32C;
070  }
071
072  /** returns the name of this checksum type */
073  public abstract String getName();
074
075  /** Function to get corresponding {@link org.apache.hadoop.util.DataChecksum.Type}. */
076  public abstract DataChecksum.Type getDataChecksumType();
077
078  private ChecksumType(final byte c) {
079    this.code = c;
080  }
081
082  public byte getCode() {
083    return this.code;
084  }
085
086  /**
087   * Cannot rely on enum ordinals . They change if item is removed or moved.
088   * Do our own codes.
089   * @param b
090   * @return Type associated with passed code.
091   */
092  public static ChecksumType codeToType(final byte b) {
093    for (ChecksumType t : ChecksumType.values()) {
094      if (t.getCode() == b) {
095        return t;
096      }
097    }
098    throw new RuntimeException("Unknown checksum type code " + b);
099  }
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}