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.hdfs.server.datanode;
019
020import java.io.File;
021import java.io.IOException;
022
023import org.apache.hadoop.hdfs.protocol.Block;
024import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.ReplicaState;
025import org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeSpi;
026
027/**
028 * This class describes a replica that has been finalized.
029 */
030public class FinalizedReplica extends ReplicaInfo {
031  private boolean unlinked;      // copy-on-write done for block
032  private byte[] lastPartialChunkChecksum;
033
034  /**
035   * Constructor
036   * @param blockId block id
037   * @param len replica length
038   * @param genStamp replica generation stamp
039   * @param vol volume where replica is located
040   * @param dir directory path where block and meta files are located
041   */
042  public FinalizedReplica(long blockId, long len, long genStamp,
043      FsVolumeSpi vol, File dir) {
044    this(blockId, len, genStamp, vol, dir, null);
045  }
046
047  /**
048   * Constructor.
049   * @param blockId block id
050   * @param len replica length
051   * @param genStamp replica generation stamp
052   * @param vol volume where replica is located
053   * @param dir directory path where block and meta files are located
054   * @param checksum the last partial chunk checksum
055   */
056  public FinalizedReplica(long blockId, long len, long genStamp,
057      FsVolumeSpi vol, File dir, byte[] checksum) {
058    super(blockId, len, genStamp, vol, dir);
059    this.setLastPartialChunkChecksum(checksum);
060  }
061
062  /**
063   * Constructor
064   * @param block a block
065   * @param vol volume where replica is located
066   * @param dir directory path where block and meta files are located
067   */
068  public FinalizedReplica(Block block, FsVolumeSpi vol, File dir) {
069    this(block, vol, dir, null);
070  }
071
072  /**
073   * Constructor.
074   * @param block a block
075   * @param vol volume where replica is located
076   * @param dir directory path where block and meta files are located
077   * @param checksum the last partial chunk checksum
078   */
079  public FinalizedReplica(Block block, FsVolumeSpi vol, File dir,
080      byte[] checksum) {
081    super(block, vol, dir);
082    this.setLastPartialChunkChecksum(checksum);
083  }
084
085  /**
086   * Copy constructor.
087   * @param from where to copy construct from
088   */
089  public FinalizedReplica(FinalizedReplica from) {
090    super(from);
091    this.unlinked = from.isUnlinked();
092    this.setLastPartialChunkChecksum(from.getLastPartialChunkChecksum());
093  }
094
095  @Override  // ReplicaInfo
096  public ReplicaState getState() {
097    return ReplicaState.FINALIZED;
098  }
099  
100  @Override // ReplicaInfo
101  public boolean isUnlinked() {
102    return unlinked;
103  }
104
105  @Override  // ReplicaInfo
106  public void setUnlinked() {
107    unlinked = true;
108  }
109  
110  @Override
111  public long getVisibleLength() {
112    return getNumBytes();       // all bytes are visible
113  }
114
115  @Override
116  public long getBytesOnDisk() {
117    return getNumBytes();
118  }
119
120  @Override  // Object
121  public boolean equals(Object o) {
122    return super.equals(o);
123  }
124  
125  @Override  // Object
126  public int hashCode() {
127    return super.hashCode();
128  }
129  
130  @Override
131  public String toString() {
132    return super.toString()
133        + "\n  unlinked          =" + unlinked;
134  }
135
136  public byte[] getLastPartialChunkChecksum() {
137    return lastPartialChunkChecksum;
138  }
139
140  public void setLastPartialChunkChecksum(byte[] checksum) {
141    lastPartialChunkChecksum = checksum;
142  }
143
144  public void loadLastPartialChunkChecksum()
145      throws IOException {
146    byte[] lastChecksum = getVolume().loadLastPartialChunkChecksum(
147        getBlockFile(), getMetaFile());
148    setLastPartialChunkChecksum(lastChecksum);
149  }
150}