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