Skip to content

Commit 43a0ec8

Browse files
huonwopeninx
authored andcommitted
HBASE-22841 Add more factory functions to TimeRange
These functions make it easier to possible to use `org.apache.hadoop.hbase.client.Table.CheckAndMutateBuilder#timeRange` with more interesting ranges, without being forced to use the deprecated constructors. Signed-off-by: huzheng <[email protected]>
1 parent 3eb602c commit 43a0ec8

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

hbase-common/src/main/java/org/apache/hadoop/hbase/io/TimeRange.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
* {@link #INITIAL_MIN_TIMESTAMP} and {@link #INITIAL_MAX_TIMESTAMP} only. Gets freaked out if
2727
* passed a timestamp that is < {@link #INITIAL_MIN_TIMESTAMP},
2828
* <p>
29-
* Evaluated according to minStamp &lt;= timestamp &lt; maxStamp
30-
* or [minStamp,maxStamp) in interval notation.
29+
* Evaluated according to minStamp &lt;= timestamp &lt; maxStamp or [minStamp,maxStamp) in interval
30+
* notation.
3131
* <p>
32-
* Can be returned and read by clients. Should not be directly created by clients.
33-
* Thus, all constructors are purposely @InterfaceAudience.Private.
34-
*<p>Immutable. Thread-safe.
32+
* Can be returned and read by clients. Should not be directly created by clients. Thus, all
33+
* constructors are purposely @InterfaceAudience.Private.
34+
* <p>
35+
* Immutable. Thread-safe.
3536
*/
3637
@InterfaceAudience.Public
3738
public class TimeRange {
@@ -51,6 +52,34 @@ public static TimeRange at(long ts) {
5152
return new TimeRange(ts, ts + 1);
5253
}
5354

55+
/**
56+
* Represents the time interval [minStamp, Long.MAX_VALUE)
57+
* @param minStamp the minimum timestamp value, inclusive
58+
*/
59+
public static TimeRange from(long minStamp) {
60+
check(minStamp, INITIAL_MAX_TIMESTAMP);
61+
return new TimeRange(minStamp, INITIAL_MAX_TIMESTAMP);
62+
}
63+
64+
/**
65+
* Represents the time interval [0, maxStamp)
66+
* @param maxStamp the minimum timestamp value, exclusive
67+
*/
68+
public static TimeRange until(long maxStamp) {
69+
check(INITIAL_MIN_TIMESTAMP, maxStamp);
70+
return new TimeRange(INITIAL_MIN_TIMESTAMP, maxStamp);
71+
}
72+
73+
/**
74+
* Represents the time interval [minStamp, maxStamp)
75+
* @param minStamp the minimum timestamp, inclusive
76+
* @param maxStamp the maximum timestamp, exclusive
77+
*/
78+
public static TimeRange between(long minStamp, long maxStamp) {
79+
check(minStamp, maxStamp);
80+
return new TimeRange(minStamp, maxStamp);
81+
}
82+
5483
private final long minStamp;
5584
private final long maxStamp;
5685
private final boolean allTime;

hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,12 +4866,48 @@ public void testCheckAndMutateWithTimeRange() throws IOException {
48664866
.thenPut(put);
48674867
assertFalse(ok);
48684868

4869+
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
4870+
.timeRange(TimeRange.from(ts + 10000))
4871+
.ifEquals(VALUE)
4872+
.thenPut(put);
4873+
assertFalse(ok);
4874+
4875+
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
4876+
.timeRange(TimeRange.between(ts + 10000, ts + 20000))
4877+
.ifEquals(VALUE)
4878+
.thenPut(put);
4879+
assertFalse(ok);
4880+
4881+
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
4882+
.timeRange(TimeRange.until(ts))
4883+
.ifEquals(VALUE)
4884+
.thenPut(put);
4885+
assertFalse(ok);
4886+
48694887
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
48704888
.timeRange(TimeRange.at(ts))
48714889
.ifEquals(VALUE)
48724890
.thenPut(put);
48734891
assertTrue(ok);
48744892

4893+
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
4894+
.timeRange(TimeRange.from(ts))
4895+
.ifEquals(VALUE)
4896+
.thenPut(put);
4897+
assertTrue(ok);
4898+
4899+
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
4900+
.timeRange(TimeRange.between(ts, ts + 20000))
4901+
.ifEquals(VALUE)
4902+
.thenPut(put);
4903+
assertTrue(ok);
4904+
4905+
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
4906+
.timeRange(TimeRange.until(ts + 10000))
4907+
.ifEquals(VALUE)
4908+
.thenPut(put);
4909+
assertTrue(ok);
4910+
48754911
RowMutations rm = new RowMutations(ROW)
48764912
.add((Mutation) put);
48774913
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)

0 commit comments

Comments
 (0)