File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
google-cloud-bigtable/src
main/java/com/google/cloud/bigtable/data/v2/models
test/java/com/google/cloud/bigtable/data/v2/models Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 1717
1818import com .google .api .core .InternalApi ;
1919import com .google .bigtable .v2 .ReadRowsRequest ;
20+ import com .google .bigtable .v2 .RowFilter ;
2021import com .google .bigtable .v2 .RowRange ;
2122import com .google .bigtable .v2 .RowSet ;
2223import com .google .cloud .bigtable .data .v2 .internal .ByteStringComparator ;
4243public final class Query implements Serializable {
4344 private static final long serialVersionUID = -316972783499434755L ;
4445
46+ // bigtable can server the largest filter size of 20MB.
47+ private static final int MAX_FILTER_SIZE = 20 * 1024 * 1024 ;
48+
4549 private final String tableId ;
4650 private transient ReadRowsRequest .Builder builder = ReadRowsRequest .newBuilder ();
4751
@@ -162,7 +166,13 @@ public Query range(ByteStringRange range) {
162166 * filters, please use {@link Filters#interleave()} or {@link Filters#chain()}.
163167 */
164168 public Query filter (Filters .Filter filter ) {
165- builder .setFilter (filter .toProto ());
169+ Preconditions .checkNotNull (filter , "filter can't be null" );
170+
171+ RowFilter rowFilter = filter .toProto ();
172+ Preconditions .checkArgument (
173+ rowFilter .getSerializedSize () < MAX_FILTER_SIZE , "filter size can't be more than 20MB" );
174+
175+ builder .setFilter (rowFilter );
166176 return this ;
167177 }
168178
Original file line number Diff line number Diff line change @@ -112,6 +112,28 @@ public void rowRangeTest() {
112112 assertThat (actualProto ).isEqualTo (expectedProto .build ());
113113 }
114114
115+ @ Test
116+ public void filterTestWithExceptions () {
117+ Exception actualException = null ;
118+ try {
119+ Query .create (TABLE_ID ).filter (null );
120+ } catch (Exception ex ) {
121+ actualException = ex ;
122+ }
123+ assertThat (actualException ).isInstanceOf (NullPointerException .class );
124+
125+ actualException = null ;
126+ int maxFilterSize = 20 * 1024 * 1024 ;
127+ ByteString largeValue = ByteString .copyFrom (new byte [maxFilterSize + 1 ]);
128+
129+ try {
130+ Query .create (TABLE_ID ).filter (FILTERS .value ().exactMatch (largeValue ));
131+ } catch (Exception ex ) {
132+ actualException = ex ;
133+ }
134+ assertThat (actualException ).hasMessageThat ().contains ("filter size can't be more than 20MB" );
135+ }
136+
115137 @ Test
116138 public void filterTest () {
117139 Query query = Query .create (TABLE_ID ).filter (FILTERS .key ().regex (".*" ));
You can’t perform that action at this time.
0 commit comments