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 java.io.DataInput;
021import java.io.DataOutput;
022import java.io.IOException;
023import org.apache.hadoop.hdfs.protocol.HdfsConstants;
024import org.apache.hadoop.io.Writable;
025
026import com.google.common.base.Function;
027import com.google.common.collect.ComparisonChain;
028
029public class RemoteEditLog implements Writable, Comparable<RemoteEditLog> {
030  private long startTxId = HdfsConstants.INVALID_TXID;
031  private long endTxId = HdfsConstants.INVALID_TXID;
032  
033  public RemoteEditLog() {
034  }
035
036  public RemoteEditLog(long startTxId, long endTxId) {
037    this.startTxId = startTxId;
038    this.endTxId = endTxId;
039  }
040
041  public long getStartTxId() {
042    return startTxId;
043  }
044
045  public long getEndTxId() {
046    return endTxId;
047  }
048    
049  @Override
050  public String toString() {
051    return "[" + startTxId + "," + endTxId + "]";
052  }
053
054  @Override
055  public void write(DataOutput out) throws IOException {
056    out.writeLong(startTxId);
057    out.writeLong(endTxId);
058  }
059
060  @Override
061  public void readFields(DataInput in) throws IOException {
062    startTxId = in.readLong();
063    endTxId = in.readLong();
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        return log.getStartTxId();
093      }
094    };
095}