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.hdfs.server.protocol;
019    
020    import org.apache.hadoop.hdfs.protocol.HdfsConstants;
021    
022    import com.google.common.base.Function;
023    import com.google.common.collect.ComparisonChain;
024    
025    public class RemoteEditLog implements Comparable<RemoteEditLog> {
026      private long startTxId = HdfsConstants.INVALID_TXID;
027      private long endTxId = HdfsConstants.INVALID_TXID;
028      private boolean isInProgress = false;
029      
030      public RemoteEditLog() {
031      }
032    
033      public RemoteEditLog(long startTxId, long endTxId) {
034        this.startTxId = startTxId;
035        this.endTxId = endTxId;
036        this.isInProgress = (endTxId == HdfsConstants.INVALID_TXID);
037      }
038      
039      public RemoteEditLog(long startTxId, long endTxId, boolean inProgress) {
040        this.startTxId = startTxId;
041        this.endTxId = endTxId;
042        this.isInProgress = inProgress;
043      }
044    
045      public long getStartTxId() {
046        return startTxId;
047      }
048    
049      public long getEndTxId() {
050        return endTxId;
051      }
052    
053      public boolean isInProgress() {
054        return isInProgress;
055      }
056    
057      @Override
058      public String toString() {
059        if (!isInProgress) {
060          return "[" + startTxId + "," + endTxId + "]";
061        } else {
062          return "[" + startTxId + "-? (in-progress)]";
063        }
064      }
065      
066      @Override
067      public int compareTo(RemoteEditLog log) {
068        return ComparisonChain.start()
069          .compare(startTxId, log.startTxId)
070          .compare(endTxId, log.endTxId)
071          .result();
072      }
073    
074      @Override
075      public boolean equals(Object o) {
076        if (!(o instanceof RemoteEditLog)) return false;
077        return this.compareTo((RemoteEditLog)o) == 0;
078      }
079      
080      @Override
081      public int hashCode() {
082        return (int) (startTxId * endTxId);
083      }
084      
085      /**
086       * Guava <code>Function</code> which applies {@link #getStartTxId()} 
087       */
088      public static final Function<RemoteEditLog, Long> GET_START_TXID =
089        new Function<RemoteEditLog, Long>() {
090          @Override
091          public Long apply(RemoteEditLog log) {
092            if (null == log) {
093              return HdfsConstants.INVALID_TXID;
094            }
095            return log.getStartTxId();
096          }
097        };
098    }