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 package org.apache.hadoop.fs;
019
020 import org.apache.commons.codec.binary.Base64;
021 import org.apache.commons.lang.builder.EqualsBuilder;
022 import org.apache.commons.lang.builder.HashCodeBuilder;
023 import org.apache.hadoop.classification.InterfaceAudience;
024 import org.apache.hadoop.classification.InterfaceStability;
025
026 /**
027 * HDFS-specific volume identifier which implements {@link VolumeId}. Can be
028 * used to differentiate between the data directories on a single datanode. This
029 * identifier is only unique on a per-datanode basis.
030 *
031 * Note that invalid IDs are represented by {@link VolumeId#INVALID_VOLUME_ID}.
032 */
033 @InterfaceStability.Unstable
034 @InterfaceAudience.Public
035 public class HdfsVolumeId implements VolumeId {
036
037 private final byte[] id;
038
039 public HdfsVolumeId(byte[] id) {
040 if (id == null) {
041 throw new NullPointerException("A valid Id can only be constructed " +
042 "with a non-null byte array.");
043 }
044 this.id = id;
045 }
046
047 @Override
048 public final boolean isValid() {
049 return true;
050 }
051
052 @Override
053 public int compareTo(VolumeId arg0) {
054 if (arg0 == null) {
055 return 1;
056 }
057 if (!arg0.isValid()) {
058 // any valid ID is greater
059 // than any invalid ID:
060 return 1;
061 }
062 return hashCode() - arg0.hashCode();
063 }
064
065 @Override
066 public int hashCode() {
067 return new HashCodeBuilder().append(id).toHashCode();
068 }
069
070 @Override
071 public boolean equals(Object obj) {
072 if (obj == null || obj.getClass() != getClass()) {
073 return false;
074 }
075 if (obj == this) {
076 return true;
077 }
078 HdfsVolumeId that = (HdfsVolumeId) obj;
079 // NB: if (!obj.isValid()) { return false; } check is not necessary
080 // because we have class identity checking above, and for this class
081 // isValid() is always true.
082 return new EqualsBuilder().append(this.id, that.id).isEquals();
083 }
084
085 @Override
086 public String toString() {
087 return Base64.encodeBase64String(id);
088 }
089 }