View Javadoc

1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver.wal;
21  
22  import java.io.DataInput;
23  import java.io.DataOutput;
24  import java.io.EOFException;
25  import java.io.IOException;
26  
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.io.WritableComparable;
30  
31  /**
32   * A Key for an entry in the change log.
33   *
34   * The log intermingles edits to many tables and rows, so each log entry
35   * identifies the appropriate table and row.  Within a table and row, they're
36   * also sorted.
37   *
38   * <p>Some Transactional edits (START, COMMIT, ABORT) will not have an
39   * associated row.
40   */
41  public class HLogKey implements WritableComparable<HLogKey> {
42    private byte [] regionName;
43    private byte [] tablename;
44    private long logSeqNum;
45    // Time at which this edit was written.
46    private long writeTime;
47  
48    private byte clusterId;
49  
50    /** Writable Consructor -- Do not use. */
51    public HLogKey() {
52      this(null, null, 0L, HConstants.LATEST_TIMESTAMP);
53    }
54  
55    /**
56     * Create the log key!
57     * We maintain the tablename mainly for debugging purposes.
58     * A regionName is always a sub-table object.
59     *
60     * @param regionName  - name of region
61     * @param tablename   - name of table
62     * @param logSeqNum   - log sequence number
63     * @param now Time at which this edit was written.
64     */
65    public HLogKey(final byte [] regionName, final byte [] tablename,
66        long logSeqNum, final long now) {
67      this.regionName = regionName;
68      this.tablename = tablename;
69      this.logSeqNum = logSeqNum;
70      this.writeTime = now;
71      this.clusterId = HConstants.DEFAULT_CLUSTER_ID;
72    }
73  
74    //////////////////////////////////////////////////////////////////////////////
75    // A bunch of accessors
76    //////////////////////////////////////////////////////////////////////////////
77  
78    /** @return region name */
79    public byte [] getRegionName() {
80      return regionName;
81    }
82  
83    /** @return table name */
84    public byte [] getTablename() {
85      return tablename;
86    }
87  
88    /** @return log sequence number */
89    public long getLogSeqNum() {
90      return logSeqNum;
91    }
92  
93    void setLogSeqNum(long logSeqNum) {
94      this.logSeqNum = logSeqNum;
95    }
96  
97    /**
98     * @return the write time
99     */
100   public long getWriteTime() {
101     return this.writeTime;
102   }
103 
104   /**
105    * Get the id of the original cluster
106    * @return
107    */
108   public byte getClusterId() {
109     return clusterId;
110   }
111 
112   /**
113    * Set the cluster id of this key
114    * @param clusterId
115    */
116   public void setClusterId(byte clusterId) {
117     this.clusterId = clusterId;
118   }
119 
120   @Override
121   public String toString() {
122     return Bytes.toString(tablename) + "/" + Bytes.toString(regionName) + "/" +
123       logSeqNum;
124   }
125 
126   @Override
127   public boolean equals(Object obj) {
128     if (this == obj) {
129       return true;
130     }
131     if (obj == null || getClass() != obj.getClass()) {
132       return false;
133     }
134     return compareTo((HLogKey)obj) == 0;
135   }
136 
137   @Override
138   public int hashCode() {
139     int result = Bytes.hashCode(this.regionName);
140     result ^= this.logSeqNum;
141     result ^= this.writeTime;
142     result ^= this.clusterId;
143     return result;
144   }
145 
146   public int compareTo(HLogKey o) {
147     int result = Bytes.compareTo(this.regionName, o.regionName);
148     if (result == 0) {
149       if (this.logSeqNum < o.logSeqNum) {
150         result = -1;
151       } else if (this.logSeqNum > o.logSeqNum) {
152         result = 1;
153       }
154       if (result == 0) {
155         if (this.writeTime < o.writeTime) {
156           result = -1;
157         } else if (this.writeTime > o.writeTime) {
158           return 1;
159         }
160       }
161     }
162     return result;
163   }
164 
165   public void write(DataOutput out) throws IOException {
166     Bytes.writeByteArray(out, this.regionName);
167     Bytes.writeByteArray(out, this.tablename);
168     out.writeLong(this.logSeqNum);
169     out.writeLong(this.writeTime);
170     out.writeByte(this.clusterId);
171   }
172 
173   public void readFields(DataInput in) throws IOException {
174     this.regionName = Bytes.readByteArray(in);
175     this.tablename = Bytes.readByteArray(in);
176     this.logSeqNum = in.readLong();
177     this.writeTime = in.readLong();
178     try {
179       this.clusterId = in.readByte();
180     } catch(EOFException e) {
181       // Means it's an old key, just continue
182     }
183   }
184 
185 }