View Javadoc
1   /*
2    * Copyright 2021 TiKV Project Authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package org.tikv.common.meta;
19  
20  import java.io.Serializable;
21  import java.util.Objects;
22  
23  /** TiTimestamp is the timestamp returned by timestamp oracle inside placement driver */
24  public class TiTimestamp implements Serializable {
25    private static final int PHYSICAL_SHIFT_BITS = 18;
26  
27    private final long physical;
28    private final long logical;
29  
30    public TiTimestamp(long p, long l) {
31      this.physical = p;
32      this.logical = l;
33    }
34  
35    public static long extractPhysical(long ts) {
36      return ts >> PHYSICAL_SHIFT_BITS;
37    }
38  
39    public long getVersion() {
40      return (physical << PHYSICAL_SHIFT_BITS) + logical;
41    }
42  
43    public long getPhysical() {
44      return this.physical;
45    }
46  
47    public long getLogical() {
48      return this.logical;
49    }
50  
51    public TiTimestamp getPrevious() {
52      return new TiTimestamp(physical, logical - 1);
53    }
54  
55    @Override
56    public boolean equals(Object other) {
57      if (other == this) {
58        return true;
59      }
60      if (!(other instanceof TiTimestamp)) {
61        return false;
62      }
63      return this.getVersion() == ((TiTimestamp) other).getVersion();
64    }
65  
66    @Override
67    public int hashCode() {
68      return Objects.hash(getVersion());
69    }
70  
71    @Override
72    public String toString() {
73      return "TiTimestamp(" + getVersion() + ")";
74    }
75  }