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.assertFalse;
022import static org.junit.Assert.assertTrue;
023import static org.junit.Assert.fail;
024
025import java.io.IOException;
026import java.util.Arrays;
027import java.util.Set;
028import org.apache.commons.lang3.builder.EqualsBuilder;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.client.Scan.ReadType;
032import org.apache.hadoop.hbase.filter.FilterList;
033import org.apache.hadoop.hbase.security.access.Permission;
034import org.apache.hadoop.hbase.security.visibility.Authorizations;
035import org.apache.hadoop.hbase.testclassification.ClientTests;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.Assert;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
044import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
045
046// TODO: cover more test cases
047@Category({ ClientTests.class, SmallTests.class })
048public class TestScan {
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestScan.class);
051
052  @Test
053  public void testAttributesSerialization() throws IOException {
054    Scan scan = new Scan();
055    scan.setAttribute("attribute1", Bytes.toBytes("value1"));
056    scan.setAttribute("attribute2", Bytes.toBytes("value2"));
057    scan.setAttribute("attribute3", Bytes.toBytes("value3"));
058
059    ClientProtos.Scan scanProto = ProtobufUtil.toScan(scan);
060
061    Scan scan2 = ProtobufUtil.toScan(scanProto);
062
063    Assert.assertNull(scan2.getAttribute("absent"));
064    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan2.getAttribute("attribute1")));
065    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan2.getAttribute("attribute2")));
066    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), scan2.getAttribute("attribute3")));
067    Assert.assertEquals(3, scan2.getAttributesMap().size());
068  }
069
070  @Test
071  public void testGetToScan() throws Exception {
072    Get get = new Get(Bytes.toBytes(1));
073    get.setCacheBlocks(true).setConsistency(Consistency.TIMELINE).setFilter(new FilterList())
074      .setId("get").setIsolationLevel(IsolationLevel.READ_COMMITTED)
075      .setLoadColumnFamiliesOnDemand(false).setMaxResultsPerColumnFamily(1000).setMaxVersions(9999)
076      .setRowOffsetPerColumnFamily(5).setTimeRange(0, 13)
077      .setAttribute("att_v0", Bytes.toBytes("att_v0"))
078      .setColumnFamilyTimeRange(Bytes.toBytes("cf"), 0, 123).setReplicaId(3)
079      .setACL("test_user", new Permission(Permission.Action.READ))
080      .setAuthorizations(new Authorizations("test_label")).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  }
105
106  @Test
107  public void testScanAttributes() {
108    Scan scan = new Scan();
109    Assert.assertTrue(scan.getAttributesMap().isEmpty());
110    Assert.assertNull(scan.getAttribute("absent"));
111
112    scan.setAttribute("absent", null);
113    Assert.assertTrue(scan.getAttributesMap().isEmpty());
114    Assert.assertNull(scan.getAttribute("absent"));
115
116    // adding attribute
117    scan.setAttribute("attribute1", Bytes.toBytes("value1"));
118    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), scan.getAttribute("attribute1")));
119    Assert.assertEquals(1, scan.getAttributesMap().size());
120    Assert.assertTrue(
121      Arrays.equals(Bytes.toBytes("value1"), scan.getAttributesMap().get("attribute1")));
122
123    // overriding attribute value
124    scan.setAttribute("attribute1", Bytes.toBytes("value12"));
125    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), scan.getAttribute("attribute1")));
126    Assert.assertEquals(1, scan.getAttributesMap().size());
127    Assert.assertTrue(
128      Arrays.equals(Bytes.toBytes("value12"), scan.getAttributesMap().get("attribute1")));
129
130    // adding another attribute
131    scan.setAttribute("attribute2", Bytes.toBytes("value2"));
132    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), scan.getAttribute("attribute2")));
133    Assert.assertEquals(2, scan.getAttributesMap().size());
134    Assert.assertTrue(
135      Arrays.equals(Bytes.toBytes("value2"), scan.getAttributesMap().get("attribute2")));
136
137    // removing attribute
138    scan.setAttribute("attribute2", null);
139    Assert.assertNull(scan.getAttribute("attribute2"));
140    Assert.assertEquals(1, scan.getAttributesMap().size());
141    Assert.assertNull(scan.getAttributesMap().get("attribute2"));
142
143    // removing non-existed attribute
144    scan.setAttribute("attribute2", null);
145    Assert.assertNull(scan.getAttribute("attribute2"));
146    Assert.assertEquals(1, scan.getAttributesMap().size());
147    Assert.assertNull(scan.getAttributesMap().get("attribute2"));
148
149    // removing another attribute
150    scan.setAttribute("attribute1", null);
151    Assert.assertNull(scan.getAttribute("attribute1"));
152    Assert.assertTrue(scan.getAttributesMap().isEmpty());
153    Assert.assertNull(scan.getAttributesMap().get("attribute1"));
154  }
155
156  @Test
157  public void testNullQualifier() {
158    Scan scan = new Scan();
159    byte[] family = Bytes.toBytes("family");
160    scan.addColumn(family, null);
161    Set<byte[]> qualifiers = scan.getFamilyMap().get(family);
162    Assert.assertEquals(1, qualifiers.size());
163  }
164
165  @Test
166  public void testSetAuthorizations() {
167    Scan scan = new Scan();
168    scan.setAuthorizations(new Authorizations("A", "B", "0123", "A0", "1A1", "_a"));
169    scan.setAuthorizations(new Authorizations("A|B"));
170    scan.setAuthorizations(new Authorizations("A&B"));
171    scan.setAuthorizations(new Authorizations("!B"));
172    scan.setAuthorizations(new Authorizations("A", "(A)"));
173    scan.setAuthorizations(new Authorizations("A", "{A"));
174    scan.setAuthorizations(new Authorizations(" "));
175    scan.setAuthorizations(new Authorizations(":B"));
176    scan.setAuthorizations(new Authorizations("-B"));
177    scan.setAuthorizations(new Authorizations(".B"));
178    scan.setAuthorizations(new Authorizations("/B"));
179  }
180
181  @Test
182  public void testSetStartRowAndSetStopRow() {
183    Scan scan = new Scan();
184    scan.setStartRow(null);
185    scan.setStartRow(new byte[1]);
186    scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH]);
187    try {
188      scan.setStartRow(new byte[HConstants.MAX_ROW_LENGTH + 1]);
189      fail("should've thrown exception");
190    } catch (IllegalArgumentException iae) {
191      // Expected
192    }
193
194    scan.setStopRow(null);
195    scan.setStopRow(new byte[1]);
196    scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH]);
197    try {
198      scan.setStopRow(new byte[HConstants.MAX_ROW_LENGTH + 1]);
199      fail("should've thrown exception");
200    } catch (IllegalArgumentException iae) {
201      // Expected
202    }
203  }
204
205  @Test
206  public void testScanCopyConstructor() throws Exception {
207    Scan scan = new Scan();
208
209    scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("q"))
210      .setACL("test_user", new Permission(Permission.Action.READ)).setAllowPartialResults(true)
211      .setAsyncPrefetch(false).setAttribute("test_key", Bytes.toBytes("test_value"))
212      .setAuthorizations(new Authorizations("test_label")).setBatch(10).setCacheBlocks(false)
213      .setCaching(10).setConsistency(Consistency.TIMELINE).setFilter(new FilterList())
214      .setId("scan_copy_constructor").setIsolationLevel(IsolationLevel.READ_COMMITTED).setLimit(100)
215      .setLoadColumnFamiliesOnDemand(false).setMaxResultSize(100).setMaxResultsPerColumnFamily(1000)
216      .readVersions(9999).setMvccReadPoint(5).setNeedCursorResult(true).setPriority(1).setRaw(true)
217      .setReplicaId(3).setReversed(true).setRowOffsetPerColumnFamily(5)
218      .setStartStopRowForPrefixScan(Bytes.toBytes("row_")).setScanMetricsEnabled(true)
219      .setSmall(true).setReadType(ReadType.STREAM).withStartRow(Bytes.toBytes("row_1"))
220      .withStopRow(Bytes.toBytes("row_2")).setTimeRange(0, 13);
221
222    // create a copy of existing scan object
223    Scan scanCopy = new Scan(scan);
224
225    // validate fields of copied scan object match with the original scan object
226    assertEquals(scan.getACL(), scanCopy.getACL());
227    assertEquals(scan.getAllowPartialResults(), scanCopy.getAllowPartialResults());
228    assertEquals(scan.getAttribute("test_key"), scanCopy.getAttribute("test_key"));
229    assertEquals(scan.getAttributeSize(), scanCopy.getAttributeSize());
230    assertEquals(scan.getAttributesMap(), scanCopy.getAttributesMap());
231    assertEquals(scan.getAuthorizations().getLabels(), scanCopy.getAuthorizations().getLabels());
232    assertEquals(scan.getBatch(), scanCopy.getBatch());
233    assertEquals(scan.getCacheBlocks(), scanCopy.getCacheBlocks());
234    assertEquals(scan.getCaching(), scanCopy.getCaching());
235    assertEquals(scan.getConsistency(), scanCopy.getConsistency());
236    assertEquals(scan.getFamilies().length, scanCopy.getFamilies().length);
237    assertEquals(scan.getFamilies()[0], scanCopy.getFamilies()[0]);
238    assertEquals(scan.getFamilyMap(), scanCopy.getFamilyMap());
239    assertEquals(scan.getFilter(), scanCopy.getFilter());
240    assertEquals(scan.getId(), scanCopy.getId());
241    assertEquals(scan.getIsolationLevel(), scanCopy.getIsolationLevel());
242    assertEquals(scan.getLimit(), scanCopy.getLimit());
243    assertEquals(scan.getLoadColumnFamiliesOnDemandValue(),
244      scanCopy.getLoadColumnFamiliesOnDemandValue());
245    assertEquals(scan.getMaxResultSize(), scanCopy.getMaxResultSize());
246    assertEquals(scan.getMaxResultsPerColumnFamily(), scanCopy.getMaxResultsPerColumnFamily());
247    assertEquals(scan.getMaxVersions(), scanCopy.getMaxVersions());
248    assertEquals(scan.getMvccReadPoint(), scanCopy.getMvccReadPoint());
249    assertEquals(scan.getPriority(), scanCopy.getPriority());
250    assertEquals(scan.getReadType(), scanCopy.getReadType());
251    assertEquals(scan.getReplicaId(), scanCopy.getReplicaId());
252    assertEquals(scan.getRowOffsetPerColumnFamily(), scanCopy.getRowOffsetPerColumnFamily());
253    assertEquals(scan.getStartRow(), scanCopy.getStartRow());
254    assertEquals(scan.getStopRow(), scanCopy.getStopRow());
255    assertEquals(scan.getTimeRange(), scanCopy.getTimeRange());
256
257    assertTrue("Make sure copy constructor adds all the fields in the copied object",
258      EqualsBuilder.reflectionEquals(scan, scanCopy));
259  }
260
261  @Test
262  public void testScanReadType() throws Exception {
263    Scan scan = new Scan();
264    assertFalse(scan.isSmall());
265    assertEquals(ReadType.DEFAULT, scan.getReadType());
266    Scan copyScan = new Scan(scan);
267    copyScan.setSmall(scan.isSmall());
268    assertFalse(copyScan.isSmall());
269    assertEquals(ReadType.DEFAULT, copyScan.getReadType());
270  }
271}