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 <openinx@gmail.com>
This commit is contained in:
Huon Wilson 2019-08-13 15:03:56 +10:00 committed by huzheng
parent 69debb69e1
commit a4e3136614
2 changed files with 70 additions and 5 deletions

View File

@ -26,12 +26,13 @@ import org.apache.yetus.audience.InterfaceAudience;
* {@link #INITIAL_MIN_TIMESTAMP} and {@link #INITIAL_MAX_TIMESTAMP} only. Gets freaked out if * {@link #INITIAL_MIN_TIMESTAMP} and {@link #INITIAL_MAX_TIMESTAMP} only. Gets freaked out if
* passed a timestamp that is < {@link #INITIAL_MIN_TIMESTAMP}, * passed a timestamp that is < {@link #INITIAL_MIN_TIMESTAMP},
* <p> * <p>
* Evaluated according to minStamp &lt;= timestamp &lt; maxStamp * Evaluated according to minStamp &lt;= timestamp &lt; maxStamp or [minStamp,maxStamp) in interval
* or [minStamp,maxStamp) in interval notation. * notation.
* <p> * <p>
* Can be returned and read by clients. Should not be directly created by clients. * Can be returned and read by clients. Should not be directly created by clients. Thus, all
* Thus, all constructors are purposely @InterfaceAudience.Private. * constructors are purposely @InterfaceAudience.Private.
*<p>Immutable. Thread-safe. * <p>
* Immutable. Thread-safe.
*/ */
@InterfaceAudience.Public @InterfaceAudience.Public
public class TimeRange { public class TimeRange {
@ -51,6 +52,34 @@ public class TimeRange {
return new TimeRange(ts, ts + 1); return new TimeRange(ts, ts + 1);
} }
/**
* Represents the time interval [minStamp, Long.MAX_VALUE)
* @param minStamp the minimum timestamp value, inclusive
*/
public static TimeRange from(long minStamp) {
check(minStamp, INITIAL_MAX_TIMESTAMP);
return new TimeRange(minStamp, INITIAL_MAX_TIMESTAMP);
}
/**
* Represents the time interval [0, maxStamp)
* @param maxStamp the minimum timestamp value, exclusive
*/
public static TimeRange until(long maxStamp) {
check(INITIAL_MIN_TIMESTAMP, maxStamp);
return new TimeRange(INITIAL_MIN_TIMESTAMP, maxStamp);
}
/**
* Represents the time interval [minStamp, maxStamp)
* @param minStamp the minimum timestamp, inclusive
* @param maxStamp the maximum timestamp, exclusive
*/
public static TimeRange between(long minStamp, long maxStamp) {
check(minStamp, maxStamp);
return new TimeRange(minStamp, maxStamp);
}
private final long minStamp; private final long minStamp;
private final long maxStamp; private final long maxStamp;
private final boolean allTime; private final boolean allTime;

View File

@ -4929,12 +4929,48 @@ public class TestFromClientSide {
.thenPut(put); .thenPut(put);
assertFalse(ok); assertFalse(ok);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
.timeRange(TimeRange.from(ts + 10000))
.ifEquals(VALUE)
.thenPut(put);
assertFalse(ok);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
.timeRange(TimeRange.between(ts + 10000, ts + 20000))
.ifEquals(VALUE)
.thenPut(put);
assertFalse(ok);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
.timeRange(TimeRange.until(ts))
.ifEquals(VALUE)
.thenPut(put);
assertFalse(ok);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER) ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
.timeRange(TimeRange.at(ts)) .timeRange(TimeRange.at(ts))
.ifEquals(VALUE) .ifEquals(VALUE)
.thenPut(put); .thenPut(put);
assertTrue(ok); assertTrue(ok);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
.timeRange(TimeRange.from(ts))
.ifEquals(VALUE)
.thenPut(put);
assertTrue(ok);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
.timeRange(TimeRange.between(ts, ts + 20000))
.ifEquals(VALUE)
.thenPut(put);
assertTrue(ok);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)
.timeRange(TimeRange.until(ts + 10000))
.ifEquals(VALUE)
.thenPut(put);
assertTrue(ok);
RowMutations rm = new RowMutations(ROW) RowMutations rm = new RowMutations(ROW)
.add((Mutation) put); .add((Mutation) put);
ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER) ok = table.checkAndMutate(ROW, FAMILY).qualifier(QUALIFIER)