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