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