use of com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange in project java-bigtable-hbase by googleapis.
the class RowRangeAdapter method rangeSetToByteStringRange.
/**
* Convert guava's {@link RangeSet} to Bigtable's {@link ByteStringRange}. Please note that this
* will convert boundless ranges into unset key cases.
*/
@VisibleForTesting
void rangeSetToByteStringRange(RangeSet<RowKeyWrapper> guavaRangeSet, Query query) {
for (Range<RowKeyWrapper> guavaRange : guavaRangeSet.asRanges()) {
// Is it a point?
if (guavaRange.hasLowerBound() && guavaRange.lowerBoundType() == BoundType.CLOSED && guavaRange.hasUpperBound() && guavaRange.upperBoundType() == BoundType.CLOSED && guavaRange.lowerEndpoint().equals(guavaRange.upperEndpoint())) {
query.rowKey(guavaRange.lowerEndpoint().getKey());
} else {
ByteStringRange byteRange = ByteStringRange.unbounded();
// Handle start key
if (guavaRange.hasLowerBound()) {
switch(guavaRange.lowerBoundType()) {
case CLOSED:
byteRange.startClosed(guavaRange.lowerEndpoint().getKey());
break;
case OPEN:
byteRange.startOpen(guavaRange.lowerEndpoint().getKey());
break;
default:
throw new IllegalArgumentException("Unexpected lower bound type: " + guavaRange.lowerBoundType());
}
}
// handle end key
if (guavaRange.hasUpperBound()) {
switch(guavaRange.upperBoundType()) {
case CLOSED:
byteRange.endClosed(guavaRange.upperEndpoint().getKey());
break;
case OPEN:
byteRange.endOpen(guavaRange.upperEndpoint().getKey());
break;
default:
throw new IllegalArgumentException("Unexpected upper bound type: " + guavaRange.upperBoundType());
}
}
query.range(byteRange);
}
}
}
use of com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange in project java-bigtable by googleapis.
the class RowSetUtil method getBound.
/**
* Get the bounding range of a {@link RowSet}.
*/
public static ByteStringRange getBound(RowSet rowSet) {
// Find min & max keys
ByteString minKey = null;
ByteString maxKey = null;
for (ByteString key : rowSet.getRowKeysList()) {
if (minKey == null || ByteStringComparator.INSTANCE.compare(minKey, key) > 0) {
minKey = key;
}
if (maxKey == null || ByteStringComparator.INSTANCE.compare(maxKey, key) < 0) {
maxKey = key;
}
}
// Convert min & max keys in start & end points for a range
StartPoint minStartPoint = null;
EndPoint maxEndPoint = null;
if (minKey != null) {
minStartPoint = new StartPoint(minKey, true);
}
if (maxKey != null) {
maxEndPoint = new EndPoint(maxKey, true);
}
// Expand the range using the RowSet ranges
for (RowRange rowRange : rowSet.getRowRangesList()) {
StartPoint currentStartPoint = StartPoint.extract(rowRange);
if (minStartPoint == null || minStartPoint.compareTo(currentStartPoint) > 0) {
minStartPoint = currentStartPoint;
}
EndPoint currentEndpoint = EndPoint.extract(rowRange);
if (maxEndPoint == null || maxEndPoint.compareTo(currentEndpoint) < 0) {
maxEndPoint = currentEndpoint;
}
}
// Build a range using the endpoints
ByteStringRange boundingRange = ByteStringRange.unbounded();
if (minStartPoint != null) {
if (minStartPoint.isClosed) {
boundingRange.startClosed(minStartPoint.value);
} else {
boundingRange.startOpen(minStartPoint.value);
}
}
if (maxEndPoint != null) {
if (maxEndPoint.isClosed) {
boundingRange.endClosed(maxEndPoint.value);
} else {
boundingRange.endOpen(maxEndPoint.value);
}
}
return boundingRange;
}
use of com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange in project java-bigtable by googleapis.
the class RowSetUtilTest method emptyBoundTest.
@Test
public void emptyBoundTest() {
RowSet rowSet = RowSet.getDefaultInstance();
ByteStringRange actual = RowSetUtil.getBound(rowSet);
assertThat(actual).isEqualTo(ByteStringRange.unbounded());
}
use of com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange in project java-bigtable by googleapis.
the class RowSetUtilTest method singleRangeUnbounded2BoundTest.
@Test
public void singleRangeUnbounded2BoundTest() {
RowSet rowSet = RowSet.newBuilder().addRowRanges(RowRange.newBuilder().setEndKeyClosed(ByteString.copyFromUtf8("z"))).build();
ByteStringRange actual = RowSetUtil.getBound(rowSet);
assertThat(actual).isEqualTo(ByteStringRange.unbounded().endClosed("z"));
}
use of com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange in project java-bigtable by googleapis.
the class RowSetUtilTest method singleKeyBoundTest.
@Test
public void singleKeyBoundTest() {
RowSet rowSet = RowSet.newBuilder().addRowKeys(ByteString.copyFromUtf8("a")).build();
ByteStringRange actual = RowSetUtil.getBound(rowSet);
assertThat(actual).isEqualTo(ByteStringRange.unbounded().startClosed("a").endClosed("a"));
}
Aggregations