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.namenode.snapshot;
019
020import org.apache.hadoop.hdfs.server.namenode.INode;
021import org.apache.hadoop.hdfs.server.namenode.INode.BlocksMapUpdateInfo;
022import org.apache.hadoop.hdfs.server.namenode.INodeFile;
023import org.apache.hadoop.hdfs.server.namenode.INodeFileAttributes;
024import org.apache.hadoop.hdfs.server.namenode.Quota;
025
026import java.util.List;
027
028/**
029 * The difference of an {@link INodeFile} between two snapshots.
030 */
031public class FileDiff extends
032    AbstractINodeDiff<INodeFile, INodeFileAttributes, FileDiff> {
033
034  /** The file size at snapshot creation time. */
035  private final long fileSize;
036
037  FileDiff(int snapshotId, INodeFile file) {
038    super(snapshotId, null, null);
039    fileSize = file.computeFileSize();
040  }
041
042  /** Constructor used by FSImage loading */
043  FileDiff(int snapshotId, INodeFileAttributes snapshotINode,
044      FileDiff posteriorDiff, long fileSize) {
045    super(snapshotId, snapshotINode, posteriorDiff);
046    this.fileSize = fileSize;
047  }
048
049  /** @return the file size in the snapshot. */
050  public long getFileSize() {
051    return fileSize;
052  }
053  
054  @Override
055  Quota.Counts combinePosteriorAndCollectBlocks(INodeFile currentINode,
056      FileDiff posterior, BlocksMapUpdateInfo collectedBlocks,
057      final List<INode> removedINodes) {
058    return currentINode.getFileWithSnapshotFeature()
059        .updateQuotaAndCollectBlocks(currentINode, posterior, collectedBlocks,
060            removedINodes);
061  }
062  
063  @Override
064  public String toString() {
065    return super.toString() + " fileSize=" + fileSize + ", rep="
066        + (snapshotINode == null? "?": snapshotINode.getFileReplication());
067  }
068
069  @Override
070  Quota.Counts destroyDiffAndCollectBlocks(INodeFile currentINode,
071      BlocksMapUpdateInfo collectedBlocks, final List<INode> removedINodes) {
072    return currentINode.getFileWithSnapshotFeature()
073        .updateQuotaAndCollectBlocks(currentINode, this, collectedBlocks,
074            removedINodes);
075  }
076}