下面列出了怎么用org.apache.hadoop.hbase.io.TimeRange的API类实例代码及写法,或者点击链接到github查看源代码。
private static List<Cell> toKeyValue(byte[] row, NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> rowdata, TimeRange timeRange, int maxVersions) {
List<Cell> ret = new ArrayList<>();
for (byte[] family : rowdata.keySet()) {
for (byte[] qualifier : rowdata.get(family).keySet()) {
int versionsAdded = 0;
for (Map.Entry<Long, byte[]> tsToVal : rowdata.get(family).get(qualifier).descendingMap().entrySet()) {
if (versionsAdded == maxVersions)
break;
Long timestamp = tsToVal.getKey();
if (!timeRange.withinTimeRange(timestamp))
continue;
byte[] value = tsToVal.getValue();
ret.add(new KeyValue(row, family, qualifier, timestamp, value));
versionsAdded++;
}
}
}
return ret;
}
protected UserScanQueryMatcher(Scan scan, ScanInfo scanInfo, ColumnTracker columns,
boolean hasNullColumn, long oldestUnexpiredTS, long now) {
super(createStartKey(scan, scanInfo), scanInfo, columns, oldestUnexpiredTS, now);
this.hasNullColumn = hasNullColumn;
this.filter = scan.getFilter();
if (this.filter != null) {
this.versionsAfterFilter =
scan.isRaw() ? scan.getMaxVersions() : Math.min(scan.getMaxVersions(),
scanInfo.getMaxVersions());
} else {
this.versionsAfterFilter = 0;
}
this.stopRow = scan.getStopRow();
TimeRange timeRange = scan.getColumnFamilyTimeRange().get(scanInfo.getFamily());
if (timeRange == null) {
this.tr = scan.getTimeRange();
} else {
this.tr = timeRange;
}
}
@Test
public void testPreStoreScannerOpen() throws IOException {
RegionCoprocessorHost host = new RegionCoprocessorHost(region, rsServices, conf);
Scan scan = new Scan();
scan.setTimeRange(TimeRange.INITIAL_MIN_TIMESTAMP, TimeRange.INITIAL_MAX_TIMESTAMP);
assertTrue("Scan is not for all time", scan.getTimeRange().isAllTime());
//SimpleRegionObserver is set to update the ScanInfo parameters if the passed-in scan
//is for all time. this lets us exercise both that the Scan is wired up properly in the coproc
//and that we can customize the metadata
ScanInfo oldScanInfo = getScanInfo();
HStore store = mock(HStore.class);
when(store.getScanInfo()).thenReturn(oldScanInfo);
ScanInfo newScanInfo = host.preStoreScannerOpen(store, scan);
verifyScanInfo(newScanInfo);
}
/**
* Create a protocol buffer MutateRequest for a conditioned put/delete
*
* @return a mutate request
* @throws IOException
*/
public static MutateRequest buildMutateRequest(final byte[] regionName, final byte[] row,
final byte[] family, final byte[] qualifier, final CompareOperator op, final byte[] value,
final Filter filter, final TimeRange timeRange, final Mutation mutation) throws IOException {
MutationType type;
if (mutation instanceof Put) {
type = MutationType.PUT;
} else {
type = MutationType.DELETE;
}
return MutateRequest.newBuilder()
.setRegion(buildRegionSpecifier(RegionSpecifierType.REGION_NAME, regionName))
.setMutation(ProtobufUtil.toMutation(type, mutation))
.setCondition(buildCondition(row, family, qualifier, op, value, filter, timeRange))
.build();
}
/**
* Create a protocol buffer Condition
*
* @return a Condition
* @throws IOException
*/
public static Condition buildCondition(final byte[] row, final byte[] family,
final byte[] qualifier, final CompareOperator op, final byte[] value, final Filter filter,
final TimeRange timeRange) throws IOException {
Condition.Builder builder = Condition.newBuilder().setRow(UnsafeByteOperations.unsafeWrap(row));
if (filter != null) {
builder.setFilter(ProtobufUtil.toFilter(filter));
} else {
builder.setFamily(UnsafeByteOperations.unsafeWrap(family))
.setQualifier(UnsafeByteOperations.unsafeWrap(
qualifier == null ? HConstants.EMPTY_BYTE_ARRAY : qualifier))
.setComparator(ProtobufUtil.toComparator(new BinaryComparator(value)))
.setCompareType(CompareType.valueOf(op.name()));
}
return builder.setTimeRange(ProtobufUtil.toTimeRange(timeRange)).build();
}
private ScanRanges (RowKeySchema schema, int[] slotSpan, List<List<KeyRange>> ranges, KeyRange scanRange, boolean useSkipScanFilter, boolean isPointLookup, Integer bucketNum, TimeRange rowTimestampRange) {
this.isPointLookup = isPointLookup;
this.isSalted = bucketNum != null;
this.useSkipScanFilter = useSkipScanFilter;
this.scanRange = scanRange;
this.rowTimestampRange = rowTimestampRange;
if (isSalted && !isPointLookup) {
ranges.set(0, SaltingUtil.generateAllSaltingRanges(bucketNum));
}
this.ranges = ImmutableList.copyOf(ranges);
this.slotSpan = slotSpan;
this.schema = schema;
if (schema != null && !ranges.isEmpty()) {
if (!this.useSkipScanFilter) {
int boundSlotCount = this.getBoundSlotCount();
ranges = ranges.subList(0, boundSlotCount);
slotSpan = Arrays.copyOf(slotSpan, boundSlotCount);
}
this.filter = new SkipScanFilter(ranges, slotSpan, this.schema);
}
}
private static TimeRange getRowTimestampColumnRange(List<List<KeyRange>> ranges, RowKeySchema schema, int rowTimestampColPos) {
try {
if (rowTimestampColPos != -1) {
if (ranges != null && ranges.size() > rowTimestampColPos) {
List<KeyRange> rowTimestampColRange = ranges.get(rowTimestampColPos);
List<KeyRange> sortedRange = new ArrayList<>(rowTimestampColRange);
Field f = schema.getField(rowTimestampColPos);
Collections.sort(sortedRange, f.getSortOrder() == SortOrder.ASC ? KeyRange.COMPARATOR : KeyRange.DESC_COMPARATOR);
SortOrder order = f.getSortOrder();
KeyRange lowestRange = sortedRange.get(0);
KeyRange highestRange = sortedRange.get(rowTimestampColRange.size() - 1);
if (order == SortOrder.DESC) {
return getDescTimeRange(lowestRange, highestRange, f);
}
return getAscTimeRange( lowestRange, highestRange, f);
}
}
} catch (IOException e) {
Throwables.propagate(e);
}
return null;
}
private static TimeRange getAscTimeRange(KeyRange lowestRange, KeyRange highestRange, Field f)
throws IOException {
long low;
long high;
PDataCodec codec = PLong.INSTANCE.getCodec();
if (lowestRange.lowerUnbound()) {
low = 0;
} else {
long lowerRange = codec.decodeLong(lowestRange.getLowerRange(), 0, SortOrder.ASC);
low = lowestRange.isLowerInclusive() ? lowerRange : safelyIncrement(lowerRange);
}
if (highestRange.upperUnbound()) {
high = HConstants.LATEST_TIMESTAMP;
} else {
long upperRange = codec.decodeLong(highestRange.getUpperRange(), 0, SortOrder.ASC);
if (highestRange.isUpperInclusive()) {
high = safelyIncrement(upperRange);
} else {
high = upperRange;
}
}
return new TimeRange(low, high);
}
@Override
public void preScannerOpen(org.apache.hadoop.hbase.coprocessor.ObserverContext<RegionCoprocessorEnvironment> c,
Scan scan) throws IOException {
byte[] txnScn = scan.getAttribute(TX_SCN);
if (txnScn!=null) {
TimeRange timeRange = scan.getTimeRange();
scan.setTimeRange(timeRange.getMin(), Bytes.toLong(txnScn));
}
if (isRegionObserverFor(scan)) {
// For local indexes, we need to throw if out of region as we'll get inconsistent
// results otherwise while in other cases, it may just mean out client-side data
// on region boundaries is out of date and can safely be ignored.
if (!skipRegionBoundaryCheck(scan) || ScanUtil.isLocalIndex(scan)) {
throwIfScanOutOfRegion(scan, c.getEnvironment().getRegion());
}
// Muck with the start/stop row of the scan and set as reversed at the
// last possible moment. You need to swap the start/stop and make the
// start exclusive and the stop inclusive.
ScanUtil.setupReverseScan(scan);
}
}
public static void setTimeRange(Scan scan, TimeRange range) {
try {
scan.setTimeRange(range.getMin(), range.getMax());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* @return Make a TimeRange from current state of <code>this</code>.
*/
TimeRange toTimeRange() {
long min = getMin();
long max = getMax();
// Initial TimeRangeTracker timestamps are the opposite of what you want for a TimeRange. Fix!
if (min == INITIAL_MIN_TIMESTAMP) {
min = TimeRange.INITIAL_MIN_TIMESTAMP;
}
if (max == INITIAL_MAX_TIMESTAMP) {
max = TimeRange.INITIAL_MAX_TIMESTAMP;
}
return new TimeRange(min, max);
}
@Override
public boolean shouldUseScanner(Scan scan, HStore store, long oldestUnexpiredTS) {
// if the file has no entries, no need to validate or create a scanner.
byte[] cf = store.getColumnFamilyDescriptor().getName();
TimeRange timeRange = scan.getColumnFamilyTimeRange().get(cf);
if (timeRange == null) {
timeRange = scan.getTimeRange();
}
return reader.passesTimerangeFilter(timeRange, oldestUnexpiredTS) && reader
.passesKeyRangeFilter(scan) && reader.passesBloomFilter(scan, scan.getFamilyMap().get(cf));
}
@Test
@Deprecated
public void testCheckAndMutateWithTimeRangeForOldApi() throws Exception {
AsyncTable<?> table = getTable.get();
final long ts = System.currentTimeMillis() / 2;
Put put = new Put(row);
put.addColumn(FAMILY, QUALIFIER, ts, VALUE);
boolean ok =
table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).ifNotExists().thenPut(put).get();
assertTrue(ok);
ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts + 10000))
.ifEquals(VALUE).thenPut(put).get();
assertFalse(ok);
ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts))
.ifEquals(VALUE).thenPut(put).get();
assertTrue(ok);
RowMutations rm = new RowMutations(row).add((Mutation) put);
ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts + 10000))
.ifEquals(VALUE).thenMutate(rm).get();
assertFalse(ok);
ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts))
.ifEquals(VALUE).thenMutate(rm).get();
assertTrue(ok);
Delete delete = new Delete(row).addColumn(FAMILY, QUALIFIER);
ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts + 10000))
.ifEquals(VALUE).thenDelete(delete).get();
assertFalse(ok);
ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts))
.ifEquals(VALUE).thenDelete(delete).get();
assertTrue(ok);
}
@Test
@Deprecated
public void testCheckAndMutateWithFilterAndTimeRangeForOldApi() throws Throwable {
AsyncTable<?> table = getTable.get();
// Put with specifying the timestamp
table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")))
.get();
// Put with success
boolean ok = table.checkAndMutate(row, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 101))
.thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))
.get();
assertTrue(ok);
Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(row, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 100))
.thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")))
.get();
assertFalse(ok);
assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
@Test
public void testCheckAndMutateWithFilterAndTimeRange() throws Throwable {
AsyncTable<?> table = getTable.get();
// Put with specifying the timestamp
table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")))
.get();
// Put with success
boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
.ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 101))
.build(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))).get();
assertTrue(ok);
Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
.ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 100))
.build(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))))
.get();
assertFalse(ok);
assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
@Test
@Deprecated
public void testCheckAndMutateWithFilterAndTimeRangeForOldApi() throws Throwable {
try (Table table = createTable()) {
// Put with specifying the timestamp
table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
// Put with success
boolean ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY,
Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 101))
.thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")));
assertTrue(ok);
Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 100))
.thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
assertFalse(ok);
assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
}
}
@Test
public void testCheckAndMutateWithFilterAndTimeRange() throws Throwable {
try (Table table = createTable()) {
// Put with specifying the timestamp
table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
// Put with success
boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
.ifMatches(new SingleColumnValueFilter(FAMILY,
Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 101))
.build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
assertTrue(ok);
Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
.ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
CompareOperator.EQUAL, Bytes.toBytes("a")))
.timeRange(TimeRange.between(0, 100))
.build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))));
assertFalse(ok);
assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
}
}
private void checkHTableInterfaceMethods() throws Exception {
long time = EnvironmentEdgeManager.currentTime();
mee.setValue(time);
hTableInterface.put(new Put(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, Bytes.toBytes(1L)));
checkRowValue(ROW_A, Bytes.toBytes(1L));
time = EnvironmentEdgeManager.currentTime();
mee.setValue(time);
TimeRange range10 = new TimeRange(1, time+10);
hTableInterface.increment(new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 10L)
.setTimeRange(range10.getMin(), range10.getMax()));
checkRowValue(ROW_A, Bytes.toBytes(11L));
assertEquals(MyObserver.tr10.getMin(), range10.getMin());
assertEquals(MyObserver.tr10.getMax(), range10.getMax());
time = EnvironmentEdgeManager.currentTime();
mee.setValue(time);
TimeRange range2 = new TimeRange(1, time+20);
List<Row> actions =
Arrays.asList(new Row[] { new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 2L)
.setTimeRange(range2.getMin(), range2.getMax()),
new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 2L)
.setTimeRange(range2.getMin(), range2.getMax()) });
Object[] results3 = new Object[actions.size()];
Object[] results1 = results3;
hTableInterface.batch(actions, results1);
assertEquals(MyObserver.tr2.getMin(), range2.getMin());
assertEquals(MyObserver.tr2.getMax(), range2.getMax());
for (Object r2 : results1) {
assertTrue(r2 instanceof Result);
}
checkRowValue(ROW_A, Bytes.toBytes(15L));
hTableInterface.close();
}
@Test
public void testHTableInterfaceMethods() throws Exception {
try (Table table = util.createTable(TableName.valueOf(name.getMethodName()), TEST_FAMILY)) {
table.put(new Put(ROW).addColumn(TEST_FAMILY, QUAL, VALUE));
long time = EnvironmentEdgeManager.currentTime();
mee.setValue(time);
table.put(new Put(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("a")));
checkRowValue(table, ROW, Bytes.toBytes("a"));
time = EnvironmentEdgeManager.currentTime();
mee.setValue(time);
TimeRange range10 = new TimeRange(1, time + 10);
Result r = table.append(new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("b"))
.setTimeRange(range10.getMin(), range10.getMax()));
checkRowValue(table, ROW, Bytes.toBytes("ab"));
assertEquals(MyObserver.tr10.getMin(), range10.getMin());
assertEquals(MyObserver.tr10.getMax(), range10.getMax());
time = EnvironmentEdgeManager.currentTime();
mee.setValue(time);
TimeRange range2 = new TimeRange(1, time+20);
List<Row> actions =
Arrays.asList(new Row[] {
new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("c"))
.setTimeRange(range2.getMin(), range2.getMax()),
new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("c"))
.setTimeRange(range2.getMin(), range2.getMax()) });
Object[] results1 = new Object[actions.size()];
table.batch(actions, results1);
assertEquals(MyObserver.tr2.getMin(), range2.getMin());
assertEquals(MyObserver.tr2.getMax(), range2.getMax());
for (Object r2 : results1) {
assertTrue(r2 instanceof Result);
}
checkRowValue(table, ROW, Bytes.toBytes("abcc"));
}
}
@Test
public void testExtreme() {
TimeRange tr = TimeRange.allTime();
assertTrue(tr.includesTimeRange(TimeRange.allTime()));
TimeRangeTracker trt = getTimeRangeTracker();
assertFalse(trt.includesTimeRange(TimeRange.allTime()));
trt.includeTimestamp(1);
trt.includeTimestamp(10);
assertTrue(trt.includesTimeRange(TimeRange.allTime()));
}
@Test
public void testTimeRangeTrackerNullIsSameAsTimeRangeNull() throws IOException {
TimeRangeTracker src = getTimeRangeTracker(1, 2);
byte[] bytes = TimeRangeTracker.toByteArray(src);
TimeRange tgt = TimeRangeTracker.parseFrom(bytes).toTimeRange();
assertEquals(src.getMin(), tgt.getMin());
assertEquals(src.getMax(), tgt.getMax());
}
@Test
public void testSimpleInRange() {
TimeRangeTracker trr = getTimeRangeTracker();
trr.includeTimestamp(0);
trr.includeTimestamp(2);
assertTrue(trr.includesTimeRange(new TimeRange(1)));
}
@Test
public void testRangeConstruction() throws IOException {
TimeRange defaultRange = TimeRange.allTime();
assertEquals(0L, defaultRange.getMin());
assertEquals(Long.MAX_VALUE, defaultRange.getMax());
assertTrue(defaultRange.isAllTime());
TimeRange oneArgRange = new TimeRange(0L);
assertEquals(0L, oneArgRange.getMin());
assertEquals(Long.MAX_VALUE, oneArgRange.getMax());
assertTrue(oneArgRange.isAllTime());
TimeRange oneArgRange2 = new TimeRange(1);
assertEquals(1, oneArgRange2.getMin());
assertEquals(Long.MAX_VALUE, oneArgRange2.getMax());
assertFalse(oneArgRange2.isAllTime());
TimeRange twoArgRange = new TimeRange(0L, Long.MAX_VALUE);
assertEquals(0L, twoArgRange.getMin());
assertEquals(Long.MAX_VALUE, twoArgRange.getMax());
assertTrue(twoArgRange.isAllTime());
TimeRange twoArgRange2 = new TimeRange(0L, Long.MAX_VALUE - 1);
assertEquals(0L, twoArgRange2.getMin());
assertEquals(Long.MAX_VALUE - 1, twoArgRange2.getMax());
assertFalse(twoArgRange2.isAllTime());
TimeRange twoArgRange3 = new TimeRange(1, Long.MAX_VALUE);
assertEquals(1, twoArgRange3.getMin());
assertEquals(Long.MAX_VALUE, twoArgRange3.getMax());
assertFalse(twoArgRange3.isAllTime());
}
@Test
public void testGet() throws Exception {
byte[] row = Bytes.toBytes("row");
byte[] fam = Bytes.toBytes("fam");
byte[] qf1 = Bytes.toBytes("qf1");
long ts = System.currentTimeMillis();
int maxVersions = 2;
Get get = new Get(row);
get.addColumn(fam, qf1);
get.setTimeRange(ts, ts + 1);
get.readVersions(maxVersions);
ClientProtos.Get getProto = ProtobufUtil.toGet(get);
Get desGet = ProtobufUtil.toGet(getProto);
assertTrue(Bytes.equals(get.getRow(), desGet.getRow()));
Set<byte[]> set = null;
Set<byte[]> desSet = null;
for (Map.Entry<byte[], NavigableSet<byte[]>> entry : get.getFamilyMap().entrySet()) {
assertTrue(desGet.getFamilyMap().containsKey(entry.getKey()));
set = entry.getValue();
desSet = desGet.getFamilyMap().get(entry.getKey());
for (byte[] qualifier : set) {
assertTrue(desSet.contains(qualifier));
}
}
assertEquals(get.getMaxVersions(), desGet.getMaxVersions());
TimeRange tr = get.getTimeRange();
TimeRange desTr = desGet.getTimeRange();
assertEquals(tr.getMax(), desTr.getMax());
assertEquals(tr.getMin(), desTr.getMin());
}
/**
* Create a protocol buffer MutateRequest for conditioned row mutations
*
* @return a mutate request
* @throws IOException
*/
public static ClientProtos.MultiRequest buildMutateRequest(final byte[] regionName,
final byte[] row, final byte[] family, final byte[] qualifier,
final CompareOperator op, final byte[] value, final Filter filter, final TimeRange timeRange,
final RowMutations rowMutations) throws IOException {
RegionAction.Builder builder =
getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName);
builder.setAtomic(true);
ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
for (Mutation mutation: rowMutations.getMutations()) {
MutationType mutateType;
if (mutation instanceof Put) {
mutateType = MutationType.PUT;
} else if (mutation instanceof Delete) {
mutateType = MutationType.DELETE;
} else {
throw new DoNotRetryIOException("RowMutations supports only put and delete, not " +
mutation.getClass().getName());
}
mutationBuilder.clear();
MutationProto mp = ProtobufUtil.toMutation(mutateType, mutation, mutationBuilder);
actionBuilder.clear();
actionBuilder.setMutation(mp);
builder.addAction(actionBuilder.build());
}
return ClientProtos.MultiRequest.newBuilder().addRegionAction(builder.setCondition(
buildCondition(row, family, qualifier, op, value, filter, timeRange)).build()).build();
}
public static HBaseProtos.TimeRange toTimeRange(TimeRange timeRange) {
if (timeRange == null) {
timeRange = TimeRange.allTime();
}
return HBaseProtos.TimeRange.newBuilder().setFrom(timeRange.getMin())
.setTo(timeRange.getMax())
.build();
}
private CheckAndMutate(byte[] row, byte[] family, byte[] qualifier,final CompareOperator op,
byte[] value, TimeRange timeRange, Row action) {
super(row, HConstants.LATEST_TIMESTAMP, Collections.emptyNavigableMap());
this.family = family;
this.qualifier = qualifier;
this.op = op;
this.value = value;
this.filter = null;
this.timeRange = timeRange;
this.action = action;
}
private CheckAndMutate(byte[] row, Filter filter, TimeRange timeRange, Row action) {
super(row, HConstants.LATEST_TIMESTAMP, Collections.emptyNavigableMap());
this.family = null;
this.qualifier = null;
this.op = null;
this.value = null;
this.filter = filter;
this.timeRange = timeRange;
this.action = action;
}
/**
* Get versions of columns with the specified timestamp. Note, default maximum
* versions to return is 1. If your time range spans more than one version
* and you want all versions returned, up the number of versions beyond the
* defaut.
* @param timestamp version timestamp
* @see #readAllVersions()
* @see #readVersions(int)
* @return this
*/
public Scan setTimestamp(long timestamp) {
try {
tr = new TimeRange(timestamp, timestamp + 1);
} catch(Exception e) {
// This should never happen, unless integer overflow or something extremely wrong...
LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
throw e;
}
return this;
}
/**
* Get versions of columns with the specified timestamp.
* @param timestamp version timestamp
* @return this for invocation chaining
*/
public Get setTimestamp(long timestamp) {
try {
tr = new TimeRange(timestamp, timestamp + 1);
} catch(Exception e) {
// This should never happen, unless integer overflow or something extremely wrong...
LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
throw e;
}
return this;
}