Search in sources :

Example 1 with ByteStringRange

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);
        }
    }
}
Also used : ByteStringRange(com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange) RowKeyWrapper(com.google.cloud.bigtable.hbase.util.RowKeyWrapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ByteStringRange

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;
}
Also used : RowRange(com.google.bigtable.v2.RowRange) ByteString(com.google.protobuf.ByteString) ByteStringRange(com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange)

Example 3 with ByteStringRange

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());
}
Also used : RowSet(com.google.bigtable.v2.RowSet) ByteStringRange(com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange) Test(org.junit.Test)

Example 4 with ByteStringRange

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"));
}
Also used : RowSet(com.google.bigtable.v2.RowSet) ByteStringRange(com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange) Test(org.junit.Test)

Example 5 with ByteStringRange

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"));
}
Also used : RowSet(com.google.bigtable.v2.RowSet) ByteStringRange(com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange) Test(org.junit.Test)

Aggregations

ByteStringRange (com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange)22 Test (org.junit.Test)20 RowSet (com.google.bigtable.v2.RowSet)10 RowRange (com.google.bigtable.v2.RowRange)1 RowKeyWrapper (com.google.cloud.bigtable.hbase.util.RowKeyWrapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteString (com.google.protobuf.ByteString)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1