PageRenderTime 6ms CodeModel.GetById 636ms app.highlight 397ms RepoModel.GetById 640ms app.codeStats 0ms

/secondaryindex/src/test/java/org/apache/hadoop/hbase/index/TestValuePartitionInScan.java

https://github.com/jy4618272/hindex
Java | 586 lines | 524 code | 43 blank | 19 comment | 40 complexity | 287b657882f13c8c0ee6be08f90f7fe9 MD5 | raw file
  1/**
  2 * Copyright 2011 The Apache Software Foundation
  3 *
  4 * Licensed to the Apache Software Foundation (ASF) under one
  5 * or more contributor license agreements.  See the NOTICE file
  6 * distributed with this work for additional information
  7 * regarding copyright ownership.  The ASF licenses this file
  8 * to you under the Apache License, Version 2.0 (the
  9 * "License"); you may not use this file except in compliance
 10 * with the License.  You may obtain a copy of the License at
 11 *
 12 *     http://www.apache.org/licenses/LICENSE-2.0
 13 *
 14 * Unless required by applicable law or agreed to in writing, software
 15 * distributed under the License is distributed on an "AS IS" BASIS,
 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 17 * See the License for the specific language governing permissions and
 18 * limitations under the License.
 19 */
 20package org.apache.hadoop.hbase.index;
 21
 22import java.util.ArrayList;
 23import java.util.List;
 24
 25import junit.framework.Assert;
 26
 27import org.apache.hadoop.conf.Configuration;
 28import org.apache.hadoop.hbase.HBaseTestingUtility;
 29import org.apache.hadoop.hbase.HColumnDescriptor;
 30import org.apache.hadoop.hbase.LargeTests;
 31import org.apache.hadoop.hbase.client.HBaseAdmin;
 32import org.apache.hadoop.hbase.client.HTable;
 33import org.apache.hadoop.hbase.client.Put;
 34import org.apache.hadoop.hbase.client.Result;
 35import org.apache.hadoop.hbase.client.ResultScanner;
 36import org.apache.hadoop.hbase.client.Scan;
 37import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
 38import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
 39import org.apache.hadoop.hbase.filter.FilterList;
 40import org.apache.hadoop.hbase.filter.FilterList.Operator;
 41import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
 42import org.apache.hadoop.hbase.index.ColumnQualifier.ValueType;
 43import org.apache.hadoop.hbase.index.client.EqualsExpression;
 44import org.apache.hadoop.hbase.index.client.IndexUtils;
 45import org.apache.hadoop.hbase.index.client.SingleIndexExpression;
 46import org.apache.hadoop.hbase.index.coprocessor.master.IndexMasterObserver;
 47import org.apache.hadoop.hbase.index.coprocessor.regionserver.IndexRegionObserver;
 48import org.apache.hadoop.hbase.index.coprocessor.wal.IndexWALObserver;
 49import org.apache.hadoop.hbase.index.filter.SingleColumnValuePartitionFilter;
 50import org.junit.After;
 51import org.junit.AfterClass;
 52import org.junit.Before;
 53import org.junit.BeforeClass;
 54import org.junit.Test;
 55import org.junit.experimental.categories.Category;
 56
 57@Category(LargeTests.class)
 58public class TestValuePartitionInScan {
 59
 60  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
 61
 62  @BeforeClass
 63  public static void setupBeforeClass() throws Exception {
 64    Configuration conf = UTIL.getConfiguration();
 65    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, IndexMasterObserver.class.getName());
 66    conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, IndexRegionObserver.class.getName());
 67    conf.set(CoprocessorHost.WAL_COPROCESSOR_CONF_KEY, IndexWALObserver.class.getName());
 68    conf.setInt("hbase.regionserver.lease.period", 10 * 60 * 1000);
 69    conf.setInt("hbase.rpc.timeout", 10 * 60 * 1000);
 70    UTIL.startMiniCluster(1);
 71  }
 72
 73  @AfterClass
 74  public static void tearDownAfterClass() throws Exception {
 75    UTIL.shutdownMiniCluster();
 76  }
 77
 78  @After
 79  public void tearDown() throws Exception {
 80    IndexRegionObserver.setIsTestingEnabled(false);
 81  }
 82
 83  @Before
 84  public void setUp() throws Exception {
 85    IndexRegionObserver.setIndexedFlowUsed(false);
 86    IndexRegionObserver.setSeekpointAdded(false);
 87    IndexRegionObserver.setSeekPoints(null);
 88    IndexRegionObserver.setIsTestingEnabled(true);
 89    IndexRegionObserver.addSeekPoints(null);
 90  }
 91
 92  @Test(timeout = 180000)
 93  public void testSeparatorPartition() throws Exception {
 94    HBaseAdmin admin = UTIL.getHBaseAdmin();
 95    Configuration conf = UTIL.getConfiguration();
 96    String userTableName = "testSeparatorPartition";
 97    IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
 98    HColumnDescriptor hcd = new HColumnDescriptor("cf1");
 99    ihtd.addFamily(hcd);
100    ValuePartition vp = new SeparatorPartition("_", 3);
101    IndexSpecification iSpec = new IndexSpecification("idx1");
102    iSpec.addIndexColumn(hcd, "cq", vp, ValueType.String, 200);
103    ihtd.addIndex(iSpec);
104    admin.createTable(ihtd);
105    HTable table = new HTable(conf, "testSeparatorPartition");
106    byte[] value1 = "2ndFloor_solitaire_huawei_bangalore_karnataka".getBytes();
107    Put p = new Put("row".getBytes());
108    p.add("cf1".getBytes(), "cq".getBytes(), value1);
109    table.put(p);
110    p = new Put("row2".getBytes());
111    p.add("cf1".getBytes(), "cq".getBytes(),
112      "7thFloor_solitaire_huawei_bangalore_karnataka".getBytes());
113    table.put(p);
114
115    p = new Put("row3".getBytes());
116    p.add("cf1".getBytes(), "cq".getBytes(), "rrr_sss_hhh_bangalore_karnataka".getBytes());
117    table.put(p);
118
119    Scan scan = new Scan();
120    scan.setFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
121        CompareOp.EQUAL, "huawei".getBytes(), vp));
122    int i = 0;
123    ResultScanner scanner = table.getScanner(scan);
124    List<Result> testRes = new ArrayList<Result>();
125    Result[] result = scanner.next(1);
126    while (result != null && result.length > 0) {
127      testRes.add(result[0]);
128      i++;
129      result = scanner.next(1);
130    }
131    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
132    Assert.assertTrue("Seekpoints should get added by index scanner",
133      IndexRegionObserver.getSeekpointAdded());
134    Assert.assertEquals("It should get two seek points from index scanner.", 2, IndexRegionObserver
135        .getMultipleSeekPoints().size());
136    Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
137  }
138
139  @Test(timeout = 180000)
140  public void testSpatialPartition() throws Exception {
141    HBaseAdmin admin = UTIL.getHBaseAdmin();
142    Configuration conf = UTIL.getConfiguration();
143    String userTableName = "testSpatialPartition";
144    IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
145    HColumnDescriptor hcd = new HColumnDescriptor("cf1");
146    ihtd.addFamily(hcd);
147    ValuePartition vp = new SpatialPartition(2, 3);
148    IndexSpecification iSpec = new IndexSpecification("idx1");
149    iSpec.addIndexColumn(hcd, "cq", vp, ValueType.String, 200);
150    ihtd.addIndex(iSpec);
151    admin.createTable(ihtd);
152    HTable table = new HTable(conf, "testSpatialPartition");
153    byte[] value1 = "helloworld".getBytes();
154    Put p = new Put("row".getBytes());
155    p.add("cf1".getBytes(), "cq".getBytes(), value1);
156    table.put(p);
157    p = new Put("row2".getBytes());
158    p.add("cf1".getBytes(), "cq".getBytes(), "spatial".getBytes());
159    table.put(p);
160
161    p = new Put("row3".getBytes());
162    p.add("cf1".getBytes(), "cq".getBytes(), "partition".getBytes());
163    table.put(p);
164
165    Scan scan = new Scan();
166    scan.setFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
167        CompareOp.LESS_OR_EQUAL, "rti".getBytes(), vp));
168    int i = 0;
169    ResultScanner scanner = table.getScanner(scan);
170    List<Result> testRes = new ArrayList<Result>();
171    Result[] result = scanner.next(1);
172    while (result != null && result.length > 0) {
173      testRes.add(result[0]);
174      i++;
175      result = scanner.next(1);
176    }
177    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
178    Assert.assertTrue("Seekpoints should get added by index scanner",
179      IndexRegionObserver.getSeekpointAdded());
180    Assert.assertEquals("It should get two seek points from index scanner.", 3, IndexRegionObserver
181        .getMultipleSeekPoints().size());
182    Assert.assertTrue("Overall result should have only 1 rows", testRes.size() == 3);
183  }
184
185  @Test(timeout = 180000)
186  public void testSpatialPartitionIfMulitplePartsOfValueAreIndexedByDifferentIndicesOnSameColumn()
187      throws Exception {
188    HBaseAdmin admin = UTIL.getHBaseAdmin();
189    Configuration conf = UTIL.getConfiguration();
190    String userTableName =
191        "testSpatialPartitionIfMulitplePartsOfValueAreIndexedByDifferentIndicesOnSameColumn";
192    IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
193    HColumnDescriptor hcd = new HColumnDescriptor("cf1");
194    ihtd.addFamily(hcd);
195    ValuePartition vp = new SpatialPartition(2, 3);
196    IndexSpecification iSpec = new IndexSpecification("idx1");
197    iSpec.addIndexColumn(hcd, "cq", vp, ValueType.String, 200);
198    ihtd.addIndex(iSpec);
199    ValuePartition vp2 = new SpatialPartition(5, 2);
200    iSpec = new IndexSpecification("idx2");
201    iSpec.addIndexColumn(hcd, "cq", vp2, ValueType.String, 200);
202    ihtd.addIndex(iSpec);
203    admin.createTable(ihtd);
204    HTable table = new HTable(conf, userTableName);
205    byte[] value1 = "helloworldmultiple".getBytes();
206    Put p = new Put("row".getBytes());
207    p.add("cf1".getBytes(), "cq".getBytes(), value1);
208    table.put(p);
209    p = new Put("row2".getBytes());
210    p.add("cf1".getBytes(), "cq".getBytes(), "spatialmultiple".getBytes());
211    table.put(p);
212
213    p = new Put("row3".getBytes());
214    p.add("cf1".getBytes(), "cq".getBytes(), "partitionmultiple".getBytes());
215    table.put(p);
216    FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
217    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
218        CompareOp.EQUAL, "rti".getBytes(), vp));
219    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
220        CompareOp.GREATER_OR_EQUAL, "ti".getBytes(), vp2));
221    Scan scan = new Scan();
222    scan.setFilter(masterFilter);
223    int i = 0;
224    ResultScanner scanner = table.getScanner(scan);
225    List<Result> testRes = new ArrayList<Result>();
226    Result[] result = scanner.next(1);
227    while (result != null && result.length > 0) {
228      testRes.add(result[0]);
229      i++;
230      result = scanner.next(1);
231    }
232    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
233    Assert.assertTrue("Seekpoints should get added by index scanner",
234      IndexRegionObserver.getSeekpointAdded());
235    Assert.assertEquals("It should get two seek points from index scanner.", 1, IndexRegionObserver
236        .getMultipleSeekPoints().size());
237    Assert.assertTrue("Overall result should have only 1 rows", testRes.size() == 1);
238
239    masterFilter = new FilterList(Operator.MUST_PASS_ONE);
240    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
241        CompareOp.LESS, "rti".getBytes(), vp));
242    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
243        CompareOp.GREATER, "ti".getBytes(), vp2));
244    scan = new Scan();
245    scan.setFilter(masterFilter);
246    i = 0;
247    scanner = table.getScanner(scan);
248    testRes = new ArrayList<Result>();
249    result = scanner.next(1);
250    while (result != null && result.length > 0) {
251      testRes.add(result[0]);
252      i++;
253      result = scanner.next(1);
254    }
255    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
256    Assert.assertTrue("Seekpoints should get added by index scanner",
257      IndexRegionObserver.getSeekpointAdded());
258    Assert.assertEquals("It should get two seek points from index scanner.", 3, IndexRegionObserver
259        .getMultipleSeekPoints().size());
260    Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
261
262  }
263
264  @Test(timeout = 180000)
265  public void
266      testSeparatorPartitionIfMulitplePartsOfValueAreIndexedByDifferentIndicesOnSameColumn()
267          throws Exception {
268    HBaseAdmin admin = UTIL.getHBaseAdmin();
269    Configuration conf = UTIL.getConfiguration();
270    String userTableName =
271        "testSeparatorPartitionIfMulitplePartsOfValueAreIndexedByDifferentIndicesOnSameColumn";
272    IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
273    HColumnDescriptor hcd = new HColumnDescriptor("cf1");
274    ihtd.addFamily(hcd);
275    ValuePartition vp = new SeparatorPartition("--", 3);
276    IndexSpecification iSpec = new IndexSpecification("idx1");
277    iSpec.addIndexColumn(hcd, "cq", vp, ValueType.String, 200);
278    ihtd.addIndex(iSpec);
279    ValuePartition vp2 = new SeparatorPartition("--", 2);
280    iSpec = new IndexSpecification("idx2");
281    iSpec.addIndexColumn(hcd, "cq", vp2, ValueType.String, 200);
282    ihtd.addIndex(iSpec);
283    admin.createTable(ihtd);
284    HTable table = new HTable(conf, userTableName);
285    byte[] value1 = "hello--world--multiple--1".getBytes();
286    Put p = new Put("row".getBytes());
287    p.add("cf1".getBytes(), "cq".getBytes(), value1);
288    table.put(p);
289    p = new Put("row2".getBytes());
290    p.add("cf1".getBytes(), "cq".getBytes(), "spatial--partition--multiple".getBytes());
291    table.put(p);
292
293    p = new Put("row3".getBytes());
294    p.add("cf1".getBytes(), "cq".getBytes(), "partition--by--separator--multiple".getBytes());
295    table.put(p);
296    FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
297    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
298        CompareOp.EQUAL, "multiple".getBytes(), vp));
299    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
300        CompareOp.GREATER_OR_EQUAL, "by".getBytes(), vp2));
301    Scan scan = new Scan();
302    scan.setFilter(masterFilter);
303    int i = 0;
304    ResultScanner scanner = table.getScanner(scan);
305    List<Result> testRes = new ArrayList<Result>();
306    Result[] result = scanner.next(1);
307    while (result != null && result.length > 0) {
308      testRes.add(result[0]);
309      i++;
310      result = scanner.next(1);
311    }
312    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
313    Assert.assertTrue("Seekpoints should get added by index scanner",
314      IndexRegionObserver.getSeekpointAdded());
315    Assert.assertEquals("It should get two seek points from index scanner.", 2, IndexRegionObserver
316        .getMultipleSeekPoints().size());
317    Assert.assertTrue("Overall result should have only 1 rows", testRes.size() == 2);
318
319    masterFilter = new FilterList(Operator.MUST_PASS_ONE);
320    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
321        CompareOp.GREATER_OR_EQUAL, "person".getBytes(), vp));
322    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
323        CompareOp.LESS, "multiple".getBytes(), vp2));
324    scan = new Scan();
325    scan.setFilter(masterFilter);
326    i = 0;
327    scanner = table.getScanner(scan);
328    testRes = new ArrayList<Result>();
329    result = scanner.next(1);
330    while (result != null && result.length > 0) {
331      testRes.add(result[0]);
332      i++;
333      result = scanner.next(1);
334    }
335    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
336    Assert.assertTrue("Seekpoints should get added by index scanner",
337      IndexRegionObserver.getSeekpointAdded());
338    Assert.assertEquals("It should get two seek points from index scanner.", 3, IndexRegionObserver
339        .getMultipleSeekPoints().size());
340    Assert.assertTrue("Overall result should have only 1 rows", testRes.size() == 1);
341
342  }
343
344  @Test(timeout = 180000)
345  public void testCombinationOfPartitionFiltersWithSCVF() throws Exception {
346    HBaseAdmin admin = UTIL.getHBaseAdmin();
347    Configuration conf = UTIL.getConfiguration();
348    String userTableName = "testCombinationOfPartitionFiltersWithSCVF";
349    IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
350    HColumnDescriptor hcd = new HColumnDescriptor("cf1");
351    ihtd.addFamily(hcd);
352    ValuePartition vp = new SeparatorPartition("--", 3);
353    IndexSpecification iSpec = new IndexSpecification("idx1");
354    iSpec.addIndexColumn(hcd, "cq", vp, ValueType.String, 200);
355    ihtd.addIndex(iSpec);
356    ValuePartition vp2 = new SeparatorPartition("--", 2);
357    iSpec = new IndexSpecification("idx2");
358    iSpec.addIndexColumn(hcd, "cq", vp2, ValueType.String, 200);
359    ihtd.addIndex(iSpec);
360    iSpec = new IndexSpecification("idx3");
361    iSpec.addIndexColumn(hcd, "cq", ValueType.String, 200);
362    ihtd.addIndex(iSpec);
363
364    admin.createTable(ihtd);
365    HTable table = new HTable(conf, userTableName);
366    byte[] value1 = "hello--world--multiple--1".getBytes();
367    Put p = new Put("row".getBytes());
368    p.add("cf1".getBytes(), "cq".getBytes(), value1);
369    table.put(p);
370    p = new Put("row2".getBytes());
371    p.add("cf1".getBytes(), "cq".getBytes(), "spatial--partition--multiple".getBytes());
372    table.put(p);
373
374    p = new Put("row3".getBytes());
375    p.add("cf1".getBytes(), "cq".getBytes(), "partition--by--separator--multiple".getBytes());
376    table.put(p);
377    FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
378    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
379        CompareOp.EQUAL, "multiple".getBytes(), vp));
380    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
381        CompareOp.GREATER_OR_EQUAL, "by".getBytes(), vp2));
382    masterFilter.addFilter(new SingleColumnValueFilter(hcd.getName(), "cq".getBytes(),
383        CompareOp.EQUAL, "spatial--partition--multiple".getBytes()));
384
385    Scan scan = new Scan();
386    scan.setFilter(masterFilter);
387    int i = 0;
388    ResultScanner scanner = table.getScanner(scan);
389    List<Result> testRes = new ArrayList<Result>();
390    Result[] result = scanner.next(1);
391    while (result != null && result.length > 0) {
392      testRes.add(result[0]);
393      i++;
394      result = scanner.next(1);
395    }
396    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
397    Assert.assertTrue("Seekpoints should get added by index scanner",
398      IndexRegionObserver.getSeekpointAdded());
399    Assert.assertEquals("It should get two seek points from index scanner.", 1, IndexRegionObserver
400        .getMultipleSeekPoints().size());
401    Assert.assertTrue("Overall result should have only 1 rows", testRes.size() == 1);
402
403    masterFilter = new FilterList(Operator.MUST_PASS_ONE);
404    masterFilter.addFilter(new SingleColumnValueFilter(hcd.getName(), "cq".getBytes(),
405        CompareOp.GREATER_OR_EQUAL, "partition--by--separator--multiple".getBytes()));
406    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
407        CompareOp.GREATER_OR_EQUAL, "person".getBytes(), vp));
408    masterFilter.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
409        CompareOp.LESS, "multiple".getBytes(), vp2));
410    scan = new Scan();
411    scan.setFilter(masterFilter);
412    i = 0;
413    scanner = table.getScanner(scan);
414    testRes = new ArrayList<Result>();
415    result = scanner.next(1);
416    while (result != null && result.length > 0) {
417      testRes.add(result[0]);
418      i++;
419      result = scanner.next(1);
420    }
421    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
422    Assert.assertTrue("Seekpoints should get added by index scanner",
423      IndexRegionObserver.getSeekpointAdded());
424    Assert.assertEquals("It should get two seek points from index scanner.", 3, IndexRegionObserver
425        .getMultipleSeekPoints().size());
426    Assert.assertTrue("Overall result should have only 1 rows", testRes.size() == 2);
427
428  }
429
430  @Test(timeout = 180000)
431  public void testCombinationOfPartitionFiltersWithSCVFPart2() throws Exception {
432    HBaseAdmin admin = UTIL.getHBaseAdmin();
433    Configuration conf = UTIL.getConfiguration();
434    String userTableName = "testCombinationOfPartitionFiltersWithSCVFPart2";
435    IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
436    HColumnDescriptor hcd = new HColumnDescriptor("cf1");
437    ihtd.addFamily(hcd);
438    ValuePartition vp = new SeparatorPartition("--", 3);
439    IndexSpecification iSpec = new IndexSpecification("idx1");
440    iSpec.addIndexColumn(hcd, "cq", vp, ValueType.String, 100);
441    iSpec.addIndexColumn(hcd, "cq1", ValueType.String, 100);
442    ihtd.addIndex(iSpec);
443    ValuePartition vp2 = new SeparatorPartition("--", 2);
444    iSpec = new IndexSpecification("idx2");
445    iSpec.addIndexColumn(hcd, "cq", vp2, ValueType.String, 100);
446    ihtd.addIndex(iSpec);
447    iSpec = new IndexSpecification("idx3");
448    iSpec.addIndexColumn(hcd, "cq1", ValueType.String, 100);
449    ihtd.addIndex(iSpec);
450    iSpec = new IndexSpecification("idx4");
451    iSpec.addIndexColumn(hcd, "cq", ValueType.String, 100);
452    iSpec.addIndexColumn(hcd, "cq1", ValueType.String, 100);
453    ihtd.addIndex(iSpec);
454
455    admin.createTable(ihtd);
456    HTable table = new HTable(conf, userTableName);
457    byte[] value1 = "hello--world--multiple--1".getBytes();
458    Put p = new Put("row".getBytes());
459    p.add("cf1".getBytes(), "cq".getBytes(), value1);
460    table.put(p);
461    p = new Put("row2".getBytes());
462    p.add("cf1".getBytes(), "cq".getBytes(), "spatial--partition--multiple".getBytes());
463    p.add("cf1".getBytes(), "cq1".getBytes(), "spatialPartition".getBytes());
464    table.put(p);
465    p = new Put("row3".getBytes());
466    p.add("cf1".getBytes(), "cq".getBytes(), "partition--by--multiple--multiple".getBytes());
467    p.add("cf1".getBytes(), "cq1".getBytes(), "partitionValue".getBytes());
468    table.put(p);
469    p = new Put("row4".getBytes());
470    p.add("cf1".getBytes(), "cq".getBytes(), "partition--multiple--multiple--multiple".getBytes());
471    p.add("cf1".getBytes(), "cq1".getBytes(), "multiple".getBytes());
472    table.put(p);
473    p = new Put("row5".getBytes());
474    p.add("cf1".getBytes(), "cq1".getBytes(), "abcd".getBytes());
475    table.put(p);
476    p = new Put("row6".getBytes());
477    p.add("cf1".getBytes(), "cq".getBytes(), "1234".getBytes());
478    table.put(p);
479
480    FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
481
482    FilterList filter1 = new FilterList(Operator.MUST_PASS_ALL);
483    filter1.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
484        CompareOp.EQUAL, "multiple".getBytes(), vp));
485    filter1.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
486        CompareOp.GREATER_OR_EQUAL, "by".getBytes(), vp2));
487    filter1.addFilter(new SingleColumnValueFilter(hcd.getName(), "cq".getBytes(), CompareOp.EQUAL,
488        "partition--multiple--multiple--multiple".getBytes()));
489
490    FilterList filter2 = new FilterList(Operator.MUST_PASS_ONE);
491    filter2.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
492        CompareOp.EQUAL, "multiple".getBytes(), vp));
493    filter2.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
494        CompareOp.EQUAL, "multiple".getBytes(), vp2));
495
496    FilterList filter3 = new FilterList(Operator.MUST_PASS_ALL);
497    filter3.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
498        CompareOp.EQUAL, "multiple".getBytes(), vp));
499    filter3.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
500        CompareOp.EQUAL, "multiple".getBytes(), vp2));
501
502    FilterList filter4 = new FilterList(Operator.MUST_PASS_ALL);
503    filter3.addFilter(new SingleColumnValueFilter(hcd.getName(), "cq1".getBytes(),
504        CompareOp.GREATER_OR_EQUAL, "1234".getBytes()));
505    filter3.addFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
506        CompareOp.GREATER_OR_EQUAL, "multiple".getBytes(), vp2));
507    masterFilter.addFilter(filter1);
508    masterFilter.addFilter(filter2);
509    masterFilter.addFilter(filter3);
510    masterFilter.addFilter(filter4);
511
512    Scan scan = new Scan();
513    scan.setFilter(masterFilter);
514    int i = 0;
515    ResultScanner scanner = table.getScanner(scan);
516    List<Result> testRes = new ArrayList<Result>();
517    Result[] result = scanner.next(1);
518    while (result != null && result.length > 0) {
519      testRes.add(result[0]);
520      i++;
521      result = scanner.next(1);
522    }
523    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
524    Assert.assertTrue("Seekpoints should get added by index scanner",
525      IndexRegionObserver.getSeekpointAdded());
526    Assert.assertEquals("It should get two seek points from index scanner.", 1, IndexRegionObserver
527        .getMultipleSeekPoints().size());
528    Assert.assertTrue("Overall result should have only 1 rows", testRes.size() == 1);
529  }
530
531  @Test(timeout = 180000)
532  public void testSingleColumnValuePartitionFilterBySettingAsAttributeToScan() throws Exception {
533
534    HBaseAdmin admin = UTIL.getHBaseAdmin();
535    Configuration conf = UTIL.getConfiguration();
536    String userTableName = "testSingleColumnValuePartitionFilterBySettingAsAttributeToScan";
537    IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
538    HColumnDescriptor hcd = new HColumnDescriptor("cf1");
539    ihtd.addFamily(hcd);
540    ValuePartition vp = new SeparatorPartition("_", 3);
541    IndexSpecification iSpec = new IndexSpecification("idx1");
542    iSpec.addIndexColumn(hcd, "cq", vp, ValueType.String, 200);
543    ihtd.addIndex(iSpec);
544    admin.createTable(ihtd);
545    HTable table = new HTable(conf, userTableName);
546    byte[] value1 = "2ndFloor_solitaire_huawei_bangalore_karnataka".getBytes();
547    Put p = new Put("row".getBytes());
548    p.add("cf1".getBytes(), "cq".getBytes(), value1);
549    table.put(p);
550    p = new Put("row2".getBytes());
551    p.add("cf1".getBytes(), "cq".getBytes(),
552      "7thFloor_solitaire_huawei_bangalore_karnataka".getBytes());
553    table.put(p);
554
555    p = new Put("row3".getBytes());
556    p.add("cf1".getBytes(), "cq".getBytes(), "rrr_sss_hhh_bangalore_karnataka".getBytes());
557    table.put(p);
558
559    Scan scan = new Scan();
560    SingleIndexExpression singleIndexExpression = new SingleIndexExpression("idx1");
561    byte[] value = "huawei".getBytes();
562    Column column = new Column("cf1".getBytes(), "cq".getBytes(), vp);
563    EqualsExpression equalsExpression = new EqualsExpression(column, value);
564    singleIndexExpression.addEqualsExpression(equalsExpression);
565    scan.setAttribute(Constants.INDEX_EXPRESSION, IndexUtils.toBytes(singleIndexExpression));
566    scan.setFilter(new SingleColumnValuePartitionFilter(hcd.getName(), "cq".getBytes(),
567        CompareOp.EQUAL, "huawei".getBytes(), vp));
568    int i = 0;
569    ResultScanner scanner = table.getScanner(scan);
570    List<Result> testRes = new ArrayList<Result>();
571    Result[] result = scanner.next(1);
572    while (result != null && result.length > 0) {
573      testRes.add(result[0]);
574      i++;
575      result = scanner.next(1);
576    }
577    Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
578    Assert.assertTrue("Seekpoints should get added by index scanner",
579      IndexRegionObserver.getSeekpointAdded());
580    Assert.assertEquals("It should get two seek points from index scanner.", 2, IndexRegionObserver
581        .getMultipleSeekPoints().size());
582    Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
583
584  }
585
586}