001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import java.io.IOException;
025import java.util.Arrays;
026import java.util.Set;
027import org.apache.commons.lang3.builder.EqualsBuilder;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.client.Scan.ReadType;
031import org.apache.hadoop.hbase.filter.FilterList;
032import org.apache.hadoop.hbase.security.access.Permission;
033import org.apache.hadoop.hbase.security.visibility.Authorizations;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.Assert;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
043import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
044
045// TODO: cover more test cases
046@Category({ ClientTests.class, SmallTests.class })
047public class TestScan {
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestScan.class);
050
051  @Test
052  public void testAttributesSerialization() throws IOException {
053    Scan scan = new Scan();
054    scan.setAttribute("attribute1", Bytes.toBytes("value1"));
055    scan.setAttribute("attribute2", Bytes.toBytes("value2"));
056    scan.setAttribute("attribute3", Bytes.toBytes("value3"));
057
058    ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
059
060    Scan scan2 = ProtobufUtil.toScan(scanProto);
061
062    Assert.assertNull(scan2.getAttribute("absent"));
063    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
064    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
065    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
066    Assert.assertEquals(3, scan2.getAttributesMap().size());
067  }
068
069  @Test
070  public void testGetToScan() throws Exception {
071    Get get = new Get(Bytes.toBytes(1));
072    get.setCacheBlocks(true).setConsistency(Consistency.TIMELINE).setFilter(new FilterList())
073      .setId("get").setIsolationLevel(IsolationLevel.READ_COMMITTED)
074      .setLoadColumnFamiliesOnDemand(false).setMaxResultsPerColumnFamily(1000).readVersions(9999)
075      .setRowOffsetPerColumnFamily(5).setTimeRange(0, 13)
076      .setAttribute("att_v0", Bytes.toBytes("att_v0"))
077      .setColumnFamilyTimeRange(Bytes.toBytes("cf"), 0, 123).setReplicaId(3)
078      .setACL("test_user", new Permission(Permission.Action.READ))
079      .setAuthorizations(new Authorizations("test_label")).setQueryMetricsEnabled(true)
080      .setPriority(3);
081
082    Scan scan = new Scan(get);
083    assertEquals(get.getCacheBlocks(), scan.getCacheBlocks());
084    assertEquals(get.getConsistency(), scan.getConsistency());
085    assertEquals(get.getFilter(), scan.getFilter());
086    assertEquals(get.getId(), scan.getId());
087    assertEquals(get.getIsolationLevel(), scan.getIsolationLevel());
088    assertEquals(get.getLoadColumnFamiliesOnDemandValue(),
089      scan.getLoadColumnFamiliesOnDemandValue());
090    assertEquals(get.getMaxResultsPerColumnFamily(), scan.getMaxResultsPerColumnFamily());
091    assertEquals(get.getMaxVersions(), scan.getMaxVersions());
092    assertEquals(get.getRowOffsetPerColumnFamily(), scan.getRowOffsetPerColumnFamily());
093    assertEquals(get.getTimeRange().getMin(), scan.getTimeRange().getMin());
094    assertEquals(get.getTimeRange().getMax(), scan.getTimeRange().getMax());
095    assertTrue(Bytes.equals(get.getAttribute("att_v0"), scan.getAttribute("att_v0")));
096    assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin(),
097      scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMin());
098    assertEquals(get.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax(),
099      scan.getColumnFamilyTimeRange().get(Bytes.toBytes("cf")).getMax());
100    assertEquals(get.getReplicaId(), scan.getReplicaId());
101    assertEquals(get.getACL(), scan.getACL());
102    assertEquals(get.getAuthorizations().getLabels(), scan.getAuthorizations().getLabels());
103    assertEquals(get.getPriority(), scan.getPriority());
104    assertEquals(get.isQueryMetricsEnabled(), scan.isQueryMetricsEnabled());
105  }
106
107  @Test
108  public void testScanAttributes() {
109    Scan scan = new Scan();
110    Assert.assertTrue(scan.getAttributesMap().isEmpty());
111    Assert.assertNull(scan.getAttribute("absent"));
112
113    scan.setAttribute("absent", null);
114    Assert.assertTrue(scan.getAttributesMap().isEmpty());
115    Assert.assertNull(scan.getAttribute("absent"));
116
117    // adding attribute
118    scan.setAttribute("attribute1", Bytes.toBytes("value1"));
119    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
120    Assert.assertEquals(1, scan.getAttributesMap().size());
121    Assert.assertTrue(
122      Arrays.equals(Bytes.toBytes("value1"), scan.getAttributesMap().get("attribute1")));
123
124    // overriding attribute value
125    scan.setAttribute("attribute1", Bytes.toBytes("value12"));
126    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1")));
127    Assert.assertEquals(1, scan.getAttributesMap().size());
128    Assert.assertTrue(
129      Arrays.equals(Bytes.toBytes("value12"), scan.getAttributesMap().get("attribute1")));
130
131    // adding another attribute
132    scan.setAttribute("attribute2", Bytes.toBytes("value2"));
133    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
134    Assert.assertEquals(2, scan.getAttributesMap().size());
135    Assert.assertTrue(
136      Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2")));
137
138    // removing attribute
139    scan.setAttribute("attribute2", null);
140    Assert.assertNull(scan.getAttribute("attribute2"));
141    Assert.assertEquals(1, scan.getAttributesMap().size());
142    Assert.assertNull(scan.getAttributesMap().get("attribute2"));
143
144    // removing non-existed attribute
145    scan.setAttribute("attribute2", null);
146    Assert.assertNull(scan.getAttribute("attribute2"));
147    Assert.assertEquals(1, scan.getAttributesMap().size());
148    Assert.assertNull(scan.getAttributesMap().get("attribute2"));
149
150    // removing another attribute
151    scan.setAttribute("attribute1", null);
152    Assert.assertNull(scan.getAttribute("attribute1"));
153    Assert.assertTrue(scan.getAttributesMap().isEmpty());
154    Assert.assertNull(scan.getAttributesMap().get("attribute1"));
155  }
156
157  @Test
158  public void testNullQualifier() {
159    Scan scan = new Scan();
160    byte[] family = Bytes.toBytes("family");
161    scan.addColumn(family, null);
162    Set<byte[]> qualifiers = scan.getFamilyMap().get(family);
163    Assert.assertEquals(1, qualifiers.size());
164  }
165
166  @Test
167  public void testSetAuthorizations() {
168    Scan scan = new Scan();
169    scan.setAuthorizations(new Authorizations("A", "B", "0123", "A0", "1A1", "_a"));
170    scan.setAuthorizations(new Authorizations("A|B"));
171    scan.setAuthorizations(new Authorizations("A&B"));
172    scan.setAuthorizations(new Authorizations("!B"));
173    scan.setAuthorizations(new Authorizations("A", "(A)"));
174    scan.setAuthorizations(new Authorizations("A", "{A"));
175    scan.setAuthorizations(new Authorizations(" "));
176    scan.setAuthorizations(new Authorizations(":B"));
177    scan.setAuthorizations(new Authorizations("-B"));
178    scan.setAuthorizations(new Authorizations(".B"));
179    scan.setAuthorizations(new Authorizations("/B"));
180  }
181
182  @Test
183  public void testSetStartRowAndSetStopRow() {
184    Scan scan = new Scan();
185    scan.withStartRow(null);
186    scan.withStartRow(new byte[1]);
187    scan.withStartRow(new byte[HConstants.MAX_ROW_LENGTH]);
188    try {
189      scan.withStartRow(new byte[HConstants.MAX_ROW_LENGTH + 1]);
190      fail("should've thrown exception");
191    } catch (IllegalArgumentException iae) {
192      // Expected
193    }
194
195    scan.withStopRow(null);
196    scan.withStopRow(new byte[1]);
197    scan.withStopRow(new byte[HConstants.MAX_ROW_LENGTH]);
198    try {
199      scan.withStopRow(new byte[HConstants.MAX_ROW_LENGTH + 1]);
200      fail("should've thrown exception");
201    } catch (IllegalArgumentException iae) {
202      // Expected
203    }
204  }
205
206  @Test
207  public void testScanCopyConstructor() throws Exception {
208    Scan scan = new Scan();
209
210    scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("q"))
211      .setACL("test_user", new Permission(Permission.Action.READ)).setAllowPartialResults(true)
212      .setAsyncPrefetch(false).setAttribute("test_key", Bytes.toBytes("test_value"))
213      .setAuthorizations(new Authorizations("test_label")).setBatch(10).setCacheBlocks(false)
214      .setCaching(10).setConsistency(Consistency.TIMELINE).setFilter(new FilterList())
215      .setId("scan_copy_constructor").setIsolationLevel(IsolationLevel.READ_COMMITTED).setLimit(100)
216      .setLoadColumnFamiliesOnDemand(false).setMaxResultSize(100).setMaxResultsPerColumnFamily(1000)
217      .readVersions(9999).setMvccReadPoint(5).setNeedCursorResult(true).setPriority(1).setRaw(true)
218      .setReplicaId(3).setReversed(true).setRowOffsetPerColumnFamily(5)
219      .setStartStopRowForPrefixScan(Bytes.toBytes("row_")).setScanMetricsEnabled(true)
220      .setReadType(ReadType.STREAM).withStartRow(Bytes.toBytes("row_1"))
221      .withStopRow(Bytes.toBytes("row_2")).setTimeRange(0, 13).setQueryMetricsEnabled(true);
222
223    // create a copy of existing scan object
224    Scan scanCopy = new Scan(scan);
225
226    // validate fields of copied scan object match with the original scan object
227    assertEquals(scan.getACL(), scanCopy.getACL());
228    assertEquals(scan.getAllowPartialResults(), scanCopy.getAllowPartialResults());
229    assertEquals(scan.getAttribute("test_key"), scanCopy.getAttribute("test_key"));
230    assertEquals(scan.getAttributeSize(), scanCopy.getAttributeSize());
231    assertEquals(scan.getAttributesMap(), scanCopy.getAttributesMap());
232    assertEquals(scan.getAuthorizations().getLabels(), scanCopy.getAuthorizations().getLabels());
233    assertEquals(scan.getBatch(), scanCopy.getBatch());
234    assertEquals(scan.getCacheBlocks(), scanCopy.getCacheBlocks());
235    assertEquals(scan.getCaching(), scanCopy.getCaching());
236    assertEquals(scan.getConsistency(), scanCopy.getConsistency());
237    assertEquals(scan.getFamilies().length, scanCopy.getFamilies().length);
238    assertEquals(scan.getFamilies()[0], scanCopy.getFamilies()[0]);
239    assertEquals(scan.getFamilyMap(), scanCopy.getFamilyMap());
240    assertEquals(scan.getFilter(), scanCopy.getFilter());
241    assertEquals(scan.getId(), scanCopy.getId());
242    assertEquals(scan.getIsolationLevel(), scanCopy.getIsolationLevel());
243    assertEquals(scan.getLimit(), scanCopy.getLimit());
244    assertEquals(scan.getLoadColumnFamiliesOnDemandValue(),
245      scanCopy.getLoadColumnFamiliesOnDemandValue());
246    assertEquals(scan.getMaxResultSize(), scanCopy.getMaxResultSize());
247    assertEquals(scan.getMaxResultsPerColumnFamily(), scanCopy.getMaxResultsPerColumnFamily());
248    assertEquals(scan.getMaxVersions(), scanCopy.getMaxVersions());
249    assertEquals(scan.getMvccReadPoint(), scanCopy.getMvccReadPoint());
250    assertEquals(scan.getPriority(), scanCopy.getPriority());
251    assertEquals(scan.getReadType(), scanCopy.getReadType());
252    assertEquals(scan.getReplicaId(), scanCopy.getReplicaId());
253    assertEquals(scan.getRowOffsetPerColumnFamily(), scanCopy.getRowOffsetPerColumnFamily());
254    assertEquals(scan.getStartRow(), scanCopy.getStartRow());
255    assertEquals(scan.getStopRow(), scanCopy.getStopRow());
256    assertEquals(scan.getTimeRange(), scanCopy.getTimeRange());
257    assertEquals(scan.isQueryMetricsEnabled(), scanCopy.isQueryMetricsEnabled());
258
259    assertTrue("Make sure copy constructor adds all the fields in the copied object",
260      EqualsBuilder.reflectionEquals(scan, scanCopy));
261  }
262
263  @Test
264  public void testScanReadType() throws Exception {
265    Scan scan = new Scan();
266    assertEquals(ReadType.DEFAULT, scan.getReadType());
267    Scan copyScan = new Scan(scan);
268    assertEquals(ReadType.DEFAULT, copyScan.getReadType());
269  }
270}