PageRenderTime 70ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/secondaryindex/src/test/java/org/apache/hadoop/hbase/index/coprocessor/regionserver/TestMultipleIndicesInScan.java

https://github.com/jy4618272/hindex
Java | 3493 lines | 3064 code | 329 blank | 100 comment | 180 complexity | 95f01d4f2d92d3ef1204bf8dd29772c6 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.coprocessor.regionserver;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertTrue;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import junit.framework.Assert;
  27. import org.apache.hadoop.conf.Configuration;
  28. import org.apache.hadoop.hbase.HBaseTestingUtility;
  29. import org.apache.hadoop.hbase.HColumnDescriptor;
  30. import org.apache.hadoop.hbase.HConstants;
  31. import org.apache.hadoop.hbase.HTableDescriptor;
  32. import org.apache.hadoop.hbase.LargeTests;
  33. import org.apache.hadoop.hbase.client.HBaseAdmin;
  34. import org.apache.hadoop.hbase.client.HTable;
  35. import org.apache.hadoop.hbase.client.Put;
  36. import org.apache.hadoop.hbase.client.Result;
  37. import org.apache.hadoop.hbase.client.ResultScanner;
  38. import org.apache.hadoop.hbase.client.Scan;
  39. import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
  40. import org.apache.hadoop.hbase.filter.BinaryComparator;
  41. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
  42. import org.apache.hadoop.hbase.filter.DoubleComparator;
  43. import org.apache.hadoop.hbase.filter.Filter;
  44. import org.apache.hadoop.hbase.filter.FilterList;
  45. import org.apache.hadoop.hbase.filter.FilterList.Operator;
  46. import org.apache.hadoop.hbase.filter.FloatComparator;
  47. import org.apache.hadoop.hbase.filter.IntComparator;
  48. import org.apache.hadoop.hbase.filter.RowFilter;
  49. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
  50. import org.apache.hadoop.hbase.index.ColumnQualifier.ValueType;
  51. import org.apache.hadoop.hbase.index.Constants;
  52. import org.apache.hadoop.hbase.index.IndexSpecification;
  53. import org.apache.hadoop.hbase.index.IndexedHTableDescriptor;
  54. import org.apache.hadoop.hbase.index.coprocessor.master.IndexMasterObserver;
  55. import org.apache.hadoop.hbase.index.coprocessor.wal.IndexWALObserver;
  56. import org.apache.hadoop.hbase.index.util.ByteArrayBuilder;
  57. import org.apache.hadoop.hbase.index.util.IndexUtils;
  58. import org.apache.hadoop.hbase.util.Bytes;
  59. import org.apache.hadoop.hbase.zookeeper.ZKAssign;
  60. import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
  61. import org.apache.zookeeper.KeeperException;
  62. import org.junit.After;
  63. import org.junit.AfterClass;
  64. import org.junit.Before;
  65. import org.junit.BeforeClass;
  66. import org.junit.Test;
  67. import org.junit.experimental.categories.Category;
  68. @Category(LargeTests.class)
  69. public class TestMultipleIndicesInScan {
  70. private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
  71. @BeforeClass
  72. public static void setupBeforeClass() throws Exception {
  73. Configuration conf = UTIL.getConfiguration();
  74. conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, IndexMasterObserver.class.getName());
  75. conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, IndexRegionObserver.class.getName());
  76. conf.set(CoprocessorHost.WAL_COPROCESSOR_CONF_KEY, IndexWALObserver.class.getName());
  77. conf.setInt("hbase.regionserver.lease.period", 10 * 60 * 1000);
  78. conf.setInt("hbase.rpc.timeout", 10 * 60 * 1000);
  79. UTIL.startMiniCluster(1);
  80. }
  81. @AfterClass
  82. public static void tearDownAfterClass() throws Exception {
  83. UTIL.shutdownMiniCluster();
  84. }
  85. @After
  86. public void tearDown() throws Exception {
  87. IndexRegionObserver.setIsTestingEnabled(false);
  88. }
  89. @Before
  90. public void setUp() throws Exception {
  91. IndexRegionObserver.setIndexedFlowUsed(false);
  92. IndexRegionObserver.setSeekpointAdded(false);
  93. IndexRegionObserver.setSeekPoints(null);
  94. IndexRegionObserver.setIsTestingEnabled(true);
  95. IndexRegionObserver.addSeekPoints(null);
  96. }
  97. @Test(timeout = 180000)
  98. public void testAndOrCombinationWithMultipleIndices() throws IOException, KeeperException,
  99. InterruptedException {
  100. HBaseAdmin admin = UTIL.getHBaseAdmin();
  101. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  102. Configuration conf = UTIL.getConfiguration();
  103. String userTableName = "testSimpleScenarioForMultipleIndices";
  104. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  105. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  106. ihtd.addFamily(hcd);
  107. IndexSpecification indexSpecification =
  108. createIndexSpecification(hcd, ValueType.String, 10,
  109. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  110. ihtd.addIndex(indexSpecification);
  111. indexSpecification =
  112. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  113. ihtd.addIndex(indexSpecification);
  114. indexSpecification =
  115. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  116. ihtd.addIndex(indexSpecification);
  117. indexSpecification =
  118. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2", "c1" }, "idx4");
  119. ihtd.addIndex(indexSpecification);
  120. admin.createTable(ihtd);
  121. ZKAssign.blockUntilNoRIT(zkw);
  122. HTable table = new HTable(conf, "testSimpleScenarioForMultipleIndices");
  123. putforIDX1(Bytes.toBytes("row0"), table);
  124. putforIDX1(Bytes.toBytes("row1"), table);
  125. putforIDX2(Bytes.toBytes("row2"), table);
  126. putforIDX3(Bytes.toBytes("row3"), table);
  127. putforIDX1(Bytes.toBytes("row4"), table);
  128. putforIDX2(Bytes.toBytes("row4"), table);
  129. putforIDX3(Bytes.toBytes("row4"), table);
  130. putforIDX1(Bytes.toBytes("row5"), table);
  131. putforIDX1(Bytes.toBytes("row6"), table);
  132. putforIDX2(Bytes.toBytes("row7"), table);
  133. putforIDX3(Bytes.toBytes("row8"), table);
  134. putforIDX1(Bytes.toBytes("row9"), table);
  135. putforIDX2(Bytes.toBytes("row9"), table);
  136. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  137. // create the filter
  138. FilterList filter = new FilterList(Operator.MUST_PASS_ONE);
  139. SingleColumnValueFilter iscvf1 =
  140. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  141. "apple".getBytes());
  142. SingleColumnValueFilter iscvf2 =
  143. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  144. "bat".getBytes());
  145. filter.addFilter(iscvf1);
  146. filter.addFilter(iscvf2);
  147. FilterList filter1 = new FilterList(Operator.MUST_PASS_ALL);
  148. iscvf1 =
  149. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  150. "cat".getBytes());
  151. iscvf2 =
  152. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  153. "dog".getBytes());
  154. filter1.addFilter(iscvf1);
  155. filter1.addFilter(iscvf2);
  156. FilterList filter2 = new FilterList(Operator.MUST_PASS_ALL);
  157. iscvf1 =
  158. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  159. "ele".getBytes());
  160. iscvf2 =
  161. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.EQUAL,
  162. "fan".getBytes());
  163. filter2.addFilter(iscvf1);
  164. filter2.addFilter(iscvf2);
  165. masterFilter.addFilter(filter);
  166. masterFilter.addFilter(filter1);
  167. masterFilter.addFilter(filter2);
  168. Scan scan = new Scan();
  169. scan.setFilter(masterFilter);
  170. int i = 0;
  171. ResultScanner scanner = table.getScanner(scan);
  172. List<Result> testRes = new ArrayList<Result>();
  173. Result[] result = scanner.next(1);
  174. while (result != null && result.length > 0) {
  175. testRes.add(result[0]);
  176. i++;
  177. result = scanner.next(1);
  178. }
  179. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  180. Assert.assertTrue("Seekpoints should get added by index scanner",
  181. IndexRegionObserver.getSeekpointAdded());
  182. Assert.assertEquals("It should get two seek points from index scanner.", 2, IndexRegionObserver
  183. .getMultipleSeekPoints().size());
  184. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
  185. }
  186. @Test(timeout = 180000)
  187. public void testReseekWhenSomeScannerAlreadyScannedGreaterValueThanSeekPoint()
  188. throws IOException, KeeperException, InterruptedException {
  189. HBaseAdmin admin = UTIL.getHBaseAdmin();
  190. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  191. Configuration conf = UTIL.getConfiguration();
  192. String userTableName = "testReseekWhenSomeScannerAlreadyScannedGreaterValueThanSeekPoint";
  193. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  194. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  195. ihtd.addFamily(hcd);
  196. IndexSpecification indexSpecification =
  197. createIndexSpecification(hcd, ValueType.String, 10,
  198. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  199. ihtd.addIndex(indexSpecification);
  200. indexSpecification =
  201. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  202. ihtd.addIndex(indexSpecification);
  203. indexSpecification =
  204. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  205. ihtd.addIndex(indexSpecification);
  206. indexSpecification =
  207. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2", "c1" }, "idx4");
  208. ihtd.addIndex(indexSpecification);
  209. admin.createTable(ihtd);
  210. ZKAssign.blockUntilNoRIT(zkw);
  211. HTable table =
  212. new HTable(conf, "testReseekWhenSomeScannerAlreadyScannedGreaterValueThanSeekPoint");
  213. putforIDX1(Bytes.toBytes("row0"), table);
  214. putforIDX1(Bytes.toBytes("row3"), table);
  215. putforIDX1(Bytes.toBytes("row5"), table);
  216. putforIDX2(Bytes.toBytes("row5"), table);
  217. putforIDX2(Bytes.toBytes("row6"), table);
  218. putforIDX3(Bytes.toBytes("row1"), table);
  219. putforIDX3(Bytes.toBytes("row3"), table);
  220. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  221. // create the filter
  222. FilterList filter = new FilterList(Operator.MUST_PASS_ONE);
  223. SingleColumnValueFilter iscvf1 =
  224. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  225. "apple".getBytes());
  226. SingleColumnValueFilter iscvf2 =
  227. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  228. "bat".getBytes());
  229. filter.addFilter(iscvf1);
  230. filter.addFilter(iscvf2);
  231. FilterList filter1 = new FilterList(Operator.MUST_PASS_ALL);
  232. iscvf1 =
  233. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  234. "cat".getBytes());
  235. iscvf2 =
  236. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  237. "dog".getBytes());
  238. filter1.addFilter(iscvf1);
  239. filter1.addFilter(iscvf2);
  240. FilterList filter2 = new FilterList(Operator.MUST_PASS_ALL);
  241. iscvf1 =
  242. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  243. "ele".getBytes());
  244. iscvf2 =
  245. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.EQUAL,
  246. "fan".getBytes());
  247. filter2.addFilter(iscvf1);
  248. filter2.addFilter(iscvf2);
  249. masterFilter.addFilter(filter);
  250. masterFilter.addFilter(filter1);
  251. masterFilter.addFilter(filter2);
  252. Scan scan = new Scan();
  253. scan.setFilter(masterFilter);
  254. int i = 0;
  255. ResultScanner scanner = table.getScanner(scan);
  256. List<Result> testRes = new ArrayList<Result>();
  257. Result[] result = scanner.next(1);
  258. while (result != null && result.length > 0) {
  259. testRes.add(result[0]);
  260. i++;
  261. result = scanner.next(1);
  262. }
  263. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  264. Assert.assertTrue("Seekpoints should get added by index scanner",
  265. IndexRegionObserver.getSeekpointAdded());
  266. Assert.assertEquals("It should get two seek points from index scanner.", 2, IndexRegionObserver
  267. .getMultipleSeekPoints().size());
  268. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
  269. }
  270. private void putforIDX3(byte[] row, HTable htable) throws IOException {
  271. Put p = new Put(row);
  272. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("bat"));
  273. htable.put(p);
  274. }
  275. private void putforIDX2(byte[] row, HTable htable) throws IOException {
  276. Put p = new Put(row);
  277. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("apple"));
  278. htable.put(p);
  279. }
  280. private void putforIDX1(byte[] row, HTable htable) throws IOException {
  281. Put p = new Put(row);
  282. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), Bytes.toBytes("cat"));
  283. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c4"), Bytes.toBytes("dog"));
  284. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c5"), Bytes.toBytes("ele"));
  285. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c6"), Bytes.toBytes("fan"));
  286. htable.put(p);
  287. }
  288. private IndexSpecification createIndexSpecification(HColumnDescriptor hcd, ValueType type,
  289. int maxValueLength, String[] qualifiers, String name) {
  290. IndexSpecification index = new IndexSpecification(name.getBytes());
  291. for (String qualifier : qualifiers) {
  292. index.addIndexColumn(hcd, qualifier, type, maxValueLength);
  293. }
  294. return index;
  295. }
  296. @Test(timeout = 180000)
  297. public void testWhenAppliedFilterGetsNoScanScheme() throws Exception {
  298. HBaseAdmin admin = UTIL.getHBaseAdmin();
  299. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  300. Configuration conf = UTIL.getConfiguration();
  301. String userTableName = "testWhenAppliedFilterGetsNoScanScheme";
  302. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  303. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  304. ihtd.addFamily(hcd);
  305. IndexSpecification indexSpecification =
  306. createIndexSpecification(hcd, ValueType.String, 10,
  307. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  308. ihtd.addIndex(indexSpecification);
  309. indexSpecification =
  310. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  311. ihtd.addIndex(indexSpecification);
  312. indexSpecification =
  313. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  314. ihtd.addIndex(indexSpecification);
  315. indexSpecification =
  316. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2", "c1" }, "idx4");
  317. ihtd.addIndex(indexSpecification);
  318. SingleColumnValueFilter filter =
  319. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  320. "ele".getBytes());
  321. admin.createTable(ihtd);
  322. ZKAssign.blockUntilNoRIT(zkw);
  323. HTable table = new HTable(conf, "testWhenAppliedFilterGetsNoScanScheme");
  324. putforIDX1(Bytes.toBytes("row1"), table);
  325. putforIDX1(Bytes.toBytes("row2"), table);
  326. putforIDX1(Bytes.toBytes("row3"), table);
  327. putforIDX1(Bytes.toBytes("row4"), table);
  328. Scan scan = new Scan();
  329. scan.setFilter(filter);
  330. int i = 0;
  331. ResultScanner scanner = table.getScanner(scan);
  332. List<Result> testRes = new ArrayList<Result>();
  333. Result[] result = scanner.next(1);
  334. while (result != null && result.length > 0) {
  335. testRes.add(result[0]);
  336. i++;
  337. result = scanner.next(1);
  338. }
  339. Assert.assertFalse("Index table should not be used", IndexRegionObserver.getIndexedFlowUsed());
  340. Assert.assertFalse("No seek points should get added from index flow",
  341. IndexRegionObserver.getSeekpointAdded());
  342. Assert.assertTrue(testRes.size() == 4);
  343. }
  344. @Test(timeout = 180000)
  345. public void testTheOROperation() throws Exception {
  346. HBaseAdmin admin = UTIL.getHBaseAdmin();
  347. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  348. Configuration conf = UTIL.getConfiguration();
  349. String userTableName = "testTheOROperation";
  350. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  351. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  352. ihtd.addFamily(hcd);
  353. IndexSpecification indexSpecification =
  354. createIndexSpecification(hcd, ValueType.String, 10,
  355. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  356. ihtd.addIndex(indexSpecification);
  357. indexSpecification =
  358. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  359. ihtd.addIndex(indexSpecification);
  360. indexSpecification =
  361. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  362. ihtd.addIndex(indexSpecification);
  363. indexSpecification =
  364. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2", "c1" }, "idx4");
  365. ihtd.addIndex(indexSpecification);
  366. SingleColumnValueFilter filter =
  367. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  368. "apple".getBytes());
  369. SingleColumnValueFilter filter1 =
  370. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  371. "bat".getBytes());
  372. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  373. masterFilter.addFilter(filter1);
  374. masterFilter.addFilter(filter);
  375. admin.createTable(ihtd);
  376. ZKAssign.blockUntilNoRIT(zkw);
  377. HTable table = new HTable(conf, "testTheOROperation");
  378. putforIDX2(Bytes.toBytes("row1"), table);
  379. putforIDX3(Bytes.toBytes("row1"), table);
  380. putforIDX2(Bytes.toBytes("row2"), table);
  381. putforIDX2(Bytes.toBytes("row3"), table);
  382. putforIDX3(Bytes.toBytes("row4"), table);
  383. putforIDX3(Bytes.toBytes("row5"), table);
  384. putforIDX2(Bytes.toBytes("row5"), table);
  385. putforIDX2(Bytes.toBytes("row6"), table);
  386. putforIDX3(Bytes.toBytes("row7"), table);
  387. Scan scan = new Scan();
  388. scan.setFilter(masterFilter);
  389. int i = 0;
  390. ResultScanner scanner = table.getScanner(scan);
  391. List<Result> testRes = new ArrayList<Result>();
  392. Result[] result = scanner.next(1);
  393. while (result != null && result.length > 0) {
  394. testRes.add(result[0]);
  395. i++;
  396. result = scanner.next(1);
  397. }
  398. Assert.assertTrue("Index flow should be used.", IndexRegionObserver.getIndexedFlowUsed());
  399. Assert.assertTrue("Index should fetch some seek points.",
  400. IndexRegionObserver.getSeekpointAdded());
  401. Assert.assertEquals("Index should fetch 7 seek points", 7, IndexRegionObserver
  402. .getMultipleSeekPoints().size());
  403. Assert.assertEquals("Final result should have 7 rows.", 7, testRes.size());
  404. }
  405. @Test(timeout = 180000)
  406. public void testTheANDOpeartion() throws Exception {
  407. HBaseAdmin admin = UTIL.getHBaseAdmin();
  408. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  409. Configuration conf = UTIL.getConfiguration();
  410. String userTableName = "testTheANDOpeartion";
  411. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  412. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  413. ihtd.addFamily(hcd);
  414. IndexSpecification indexSpecification =
  415. createIndexSpecification(hcd, ValueType.String, 10,
  416. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  417. ihtd.addIndex(indexSpecification);
  418. indexSpecification =
  419. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  420. ihtd.addIndex(indexSpecification);
  421. indexSpecification =
  422. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  423. ihtd.addIndex(indexSpecification);
  424. SingleColumnValueFilter filter =
  425. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  426. "apple".getBytes());
  427. SingleColumnValueFilter filter1 =
  428. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  429. "bat".getBytes());
  430. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  431. masterFilter.addFilter(filter1);
  432. masterFilter.addFilter(filter);
  433. admin.createTable(ihtd);
  434. ZKAssign.blockUntilNoRIT(zkw);
  435. HTable table = new HTable(conf, "testTheANDOpeartion");
  436. putforIDX2(Bytes.toBytes("row1"), table);
  437. putforIDX3(Bytes.toBytes("row1"), table);
  438. putforIDX2(Bytes.toBytes("row2"), table);
  439. putforIDX2(Bytes.toBytes("row3"), table);
  440. putforIDX3(Bytes.toBytes("row4"), table);
  441. putforIDX3(Bytes.toBytes("row5"), table);
  442. putforIDX2(Bytes.toBytes("row5"), table);
  443. putforIDX2(Bytes.toBytes("row6"), table);
  444. putforIDX3(Bytes.toBytes("row7"), table);
  445. Scan scan = new Scan();
  446. scan.setFilter(masterFilter);
  447. int i = 0;
  448. ResultScanner scanner = table.getScanner(scan);
  449. List<Result> testRes = new ArrayList<Result>();
  450. Result[] result = scanner.next(1);
  451. while (result != null && result.length > 0) {
  452. testRes.add(result[0]);
  453. i++;
  454. result = scanner.next(1);
  455. }
  456. Assert.assertTrue("Index flow should be used.", IndexRegionObserver.getIndexedFlowUsed());
  457. Assert.assertTrue("Index should fetch some seek points.",
  458. IndexRegionObserver.getSeekpointAdded());
  459. Assert.assertEquals("Index should fetch 2 seek points", 2, IndexRegionObserver
  460. .getMultipleSeekPoints().size());
  461. Assert.assertEquals("Final result should have 2 rows.", 2, testRes.size());
  462. }
  463. @Test(timeout = 180000)
  464. public void testAndOperationWithProperStartAndStopRow() throws Exception {
  465. HBaseAdmin admin = UTIL.getHBaseAdmin();
  466. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  467. Configuration conf = UTIL.getConfiguration();
  468. String userTableName = "testAndOperationWithProperStartAndStopRow";
  469. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  470. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  471. ihtd.addFamily(hcd);
  472. IndexSpecification indexSpecification =
  473. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx1");
  474. ihtd.addIndex(indexSpecification);
  475. admin.createTable(ihtd);
  476. HTable table = new HTable(conf, userTableName);
  477. rangePutForIdx2(table);
  478. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  479. SingleColumnValueFilter scvf =
  480. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  481. Bytes.toBytes("5"));
  482. masterFilter.addFilter(scvf);
  483. Scan scan = new Scan();
  484. scan.setFilter(masterFilter);
  485. ResultScanner scanner = table.getScanner(scan);
  486. List<Result> testRes = new ArrayList<Result>();
  487. Result[] result = scanner.next(1);
  488. while (result != null && result.length > 0) {
  489. testRes.add(result[0]);
  490. result = scanner.next(1);
  491. }
  492. assertTrue(testRes.size() == 1);
  493. }
  494. @Test(timeout = 180000)
  495. public void testAndOperationWithSimilarValuePattern() throws Exception {
  496. HBaseAdmin admin = UTIL.getHBaseAdmin();
  497. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  498. Configuration conf = UTIL.getConfiguration();
  499. String userTableName = "testAndOperationWithSimilarValuePattern";
  500. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  501. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  502. ihtd.addFamily(hcd);
  503. IndexSpecification indexSpecification =
  504. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx1");
  505. ihtd.addIndex(indexSpecification);
  506. admin.createTable(ihtd);
  507. HTable table = new HTable(conf, userTableName);
  508. Put p = new Put(Bytes.toBytes("row0"));
  509. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("aaa"));
  510. table.put(p);
  511. p = new Put(Bytes.toBytes("row9"));
  512. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("aaa1"));
  513. table.put(p);
  514. p = new Put(Bytes.toBytes("row1"));
  515. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("aaa3"));
  516. table.put(p);
  517. p = new Put(Bytes.toBytes("row3"));
  518. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("aaa4"));
  519. table.put(p);
  520. p = new Put(Bytes.toBytes("row7"));
  521. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("aaa5"));
  522. table.put(p);
  523. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  524. SingleColumnValueFilter scvf =
  525. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  526. Bytes.toBytes("aaa"));
  527. masterFilter.addFilter(scvf);
  528. Scan scan = new Scan();
  529. scan.setFilter(masterFilter);
  530. ResultScanner scanner = table.getScanner(scan);
  531. List<Result> testRes = new ArrayList<Result>();
  532. Result[] result = scanner.next(1);
  533. while (result != null && result.length > 0) {
  534. testRes.add(result[0]);
  535. result = scanner.next(1);
  536. }
  537. assertTrue(testRes.size() == 1);
  538. Assert.assertTrue("Index should fetch some seek points.",
  539. IndexRegionObserver.getSeekpointAdded());
  540. Assert.assertEquals("Index should fetch 2 seek points", 1, IndexRegionObserver
  541. .getMultipleSeekPoints().size());
  542. }
  543. @Test(timeout = 180000)
  544. public void testScanWithMutlipleIndicesOnTheSameColAndSimilarPattern() throws Exception {
  545. HBaseAdmin admin = UTIL.getHBaseAdmin();
  546. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  547. Configuration conf = UTIL.getConfiguration();
  548. String userTableName = "testScanWithMutlipleIndicesOnTheSameColAndSimilarPattern";
  549. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  550. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  551. ihtd.addFamily(hcd);
  552. IndexSpecification indexSpecification =
  553. createIndexSpecification(hcd, ValueType.String, 10,
  554. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  555. ihtd.addIndex(indexSpecification);
  556. admin.createTable(ihtd);
  557. HTable table = new HTable(conf, userTableName);
  558. byte[][] val =
  559. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  560. Bytes.toBytes("goat") };
  561. putsForIdx1WithDiffValues(Bytes.toBytes("row1"), val, table);
  562. val =
  563. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  564. Bytes.toBytes("goat1") };
  565. putsForIdx1WithDiffValues(Bytes.toBytes("row2"), val, table);
  566. val =
  567. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  568. Bytes.toBytes("goat1") };
  569. putsForIdx1WithDiffValues(Bytes.toBytes("row3"), val, table);
  570. val =
  571. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog1"), Bytes.toBytes("elef"),
  572. Bytes.toBytes("goat1") };
  573. putsForIdx1WithDiffValues(Bytes.toBytes("row4"), val, table);
  574. val =
  575. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  576. Bytes.toBytes("goat1"), Bytes.toBytes("ant") };
  577. putsForIdx1WithDiffValues(Bytes.toBytes("row5"), val, table);
  578. val =
  579. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elefe"),
  580. Bytes.toBytes("goat1") };
  581. putsForIdx1WithDiffValues(Bytes.toBytes("row6"), val, table);
  582. table.flushCommits();
  583. admin.flush(userTableName);
  584. admin.flush(userTableName + "_idx");
  585. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  586. SingleColumnValueFilter scvf =
  587. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  588. Bytes.toBytes("cat"));
  589. SingleColumnValueFilter scvf1 =
  590. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  591. Bytes.toBytes("dog"));
  592. SingleColumnValueFilter scvf2 =
  593. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  594. Bytes.toBytes("elef"));
  595. masterFilter.addFilter(scvf);
  596. masterFilter.addFilter(scvf1);
  597. masterFilter.addFilter(scvf2);
  598. Scan scan = new Scan();
  599. scan.setFilter(masterFilter);
  600. ResultScanner scanner = table.getScanner(scan);
  601. List<Result> testRes = new ArrayList<Result>();
  602. Result[] result = scanner.next(1);
  603. while (result != null && result.length > 0) {
  604. testRes.add(result[0]);
  605. result = scanner.next(1);
  606. }
  607. assertTrue(testRes.size() == 2);
  608. }
  609. @Test(timeout = 180000)
  610. public void testScanWithMutlipleIndicesWithGreaterthanEqualCondOnTheSameColAndSimilarPattern()
  611. throws Exception {
  612. HBaseAdmin admin = UTIL.getHBaseAdmin();
  613. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  614. Configuration conf = UTIL.getConfiguration();
  615. String userTableName =
  616. "testScanWithMutlipleIndicesWithGreaterthanEqualCondOnTheSameColAndSimilarPattern";
  617. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  618. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  619. ihtd.addFamily(hcd);
  620. IndexSpecification indexSpecification =
  621. createIndexSpecification(hcd, ValueType.String, 10,
  622. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  623. ihtd.addIndex(indexSpecification);
  624. admin.createTable(ihtd);
  625. HTable table = new HTable(conf, userTableName);
  626. byte[][] val =
  627. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  628. Bytes.toBytes("goat") };
  629. putsForIdx1WithDiffValues(Bytes.toBytes("row1"), val, table);
  630. val =
  631. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  632. Bytes.toBytes("goat1") };
  633. putsForIdx1WithDiffValues(Bytes.toBytes("row2"), val, table);
  634. val =
  635. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  636. Bytes.toBytes("goat1") };
  637. putsForIdx1WithDiffValues(Bytes.toBytes("row3"), val, table);
  638. val =
  639. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog1"), Bytes.toBytes("elef"),
  640. Bytes.toBytes("goat1") };
  641. putsForIdx1WithDiffValues(Bytes.toBytes("row4"), val, table);
  642. val =
  643. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  644. Bytes.toBytes("goat1"), Bytes.toBytes("ant") };
  645. putsForIdx1WithDiffValues(Bytes.toBytes("row5"), val, table);
  646. val =
  647. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elefe"),
  648. Bytes.toBytes("goat1") };
  649. putsForIdx1WithDiffValues(Bytes.toBytes("row6"), val, table);
  650. val =
  651. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  652. Bytes.toBytes("goat") };
  653. putsForIdx1WithDiffValues(Bytes.toBytes("row7"), val, table);
  654. table.flushCommits();
  655. admin.flush(userTableName);
  656. admin.flush(userTableName + "_idx");
  657. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  658. SingleColumnValueFilter scvf =
  659. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  660. Bytes.toBytes("cat"));
  661. SingleColumnValueFilter scvf1 =
  662. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  663. Bytes.toBytes("dog"));
  664. SingleColumnValueFilter scvf2 =
  665. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  666. Bytes.toBytes("elef"));
  667. SingleColumnValueFilter scvf3 =
  668. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.GREATER_OR_EQUAL,
  669. Bytes.toBytes("goat"));
  670. masterFilter.addFilter(scvf);
  671. masterFilter.addFilter(scvf1);
  672. masterFilter.addFilter(scvf2);
  673. masterFilter.addFilter(scvf3);
  674. Scan scan = new Scan();
  675. scan.setFilter(masterFilter);
  676. ResultScanner scanner = table.getScanner(scan);
  677. List<Result> testRes = new ArrayList<Result>();
  678. Result[] result = scanner.next(1);
  679. while (result != null && result.length > 0) {
  680. testRes.add(result[0]);
  681. result = scanner.next(1);
  682. }
  683. assertEquals(testRes.size(), 3);
  684. }
  685. @Test(timeout = 180000)
  686. public void testScanWithMutlipleIndicesWithGreaterCondOnTheSameColAndSimilarPattern()
  687. throws Exception {
  688. HBaseAdmin admin = UTIL.getHBaseAdmin();
  689. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  690. Configuration conf = UTIL.getConfiguration();
  691. String userTableName =
  692. "testScanWithMutlipleIndicesWithGreaterCondOnTheSameColAndSimilarPattern";
  693. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  694. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  695. ihtd.addFamily(hcd);
  696. IndexSpecification indexSpecification =
  697. createIndexSpecification(hcd, ValueType.String, 10,
  698. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  699. ihtd.addIndex(indexSpecification);
  700. admin.createTable(ihtd);
  701. HTable table = new HTable(conf, userTableName);
  702. byte[][] val =
  703. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  704. Bytes.toBytes("goat") };
  705. putsForIdx1WithDiffValues(Bytes.toBytes("row1"), val, table);
  706. val =
  707. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  708. Bytes.toBytes("goat1") };
  709. putsForIdx1WithDiffValues(Bytes.toBytes("row2"), val, table);
  710. val =
  711. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  712. Bytes.toBytes("goat1") };
  713. putsForIdx1WithDiffValues(Bytes.toBytes("row3"), val, table);
  714. val =
  715. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog1"), Bytes.toBytes("elef"),
  716. Bytes.toBytes("goat1") };
  717. putsForIdx1WithDiffValues(Bytes.toBytes("row4"), val, table);
  718. val =
  719. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  720. Bytes.toBytes("goat1"), Bytes.toBytes("ant") };
  721. putsForIdx1WithDiffValues(Bytes.toBytes("row5"), val, table);
  722. val =
  723. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elefe"),
  724. Bytes.toBytes("goat1") };
  725. putsForIdx1WithDiffValues(Bytes.toBytes("row6"), val, table);
  726. val =
  727. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  728. Bytes.toBytes("goat") };
  729. putsForIdx1WithDiffValues(Bytes.toBytes("row7"), val, table);
  730. table.flushCommits();
  731. admin.flush(userTableName);
  732. admin.flush(userTableName + "_idx");
  733. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  734. SingleColumnValueFilter scvf =
  735. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  736. Bytes.toBytes("cat"));
  737. SingleColumnValueFilter scvf1 =
  738. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  739. Bytes.toBytes("dog"));
  740. SingleColumnValueFilter scvf2 =
  741. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  742. Bytes.toBytes("elef"));
  743. SingleColumnValueFilter scvf3 =
  744. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.GREATER,
  745. Bytes.toBytes("goat"));
  746. masterFilter.addFilter(scvf);
  747. masterFilter.addFilter(scvf1);
  748. masterFilter.addFilter(scvf2);
  749. masterFilter.addFilter(scvf3);
  750. Scan scan = new Scan();
  751. scan.setFilter(masterFilter);
  752. ResultScanner scanner = table.getScanner(scan);
  753. List<Result> testRes = new ArrayList<Result>();
  754. Result[] result = scanner.next(1);
  755. while (result != null && result.length > 0) {
  756. testRes.add(result[0]);
  757. result = scanner.next(1);
  758. }
  759. assertEquals(testRes.size(), 2);
  760. }
  761. @Test(timeout = 180000)
  762. public void testScanWithMutlipleIndicesWithLesserCondOnTheSameColAndSimilarPattern()
  763. throws Exception {
  764. HBaseAdmin admin = UTIL.getHBaseAdmin();
  765. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  766. Configuration conf = UTIL.getConfiguration();
  767. String userTableName = "testScanWithMutlipleIndicesWithLesserCondOnTheSameColAndSimilarPattern";
  768. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  769. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  770. ihtd.addFamily(hcd);
  771. IndexSpecification indexSpecification =
  772. createIndexSpecification(hcd, ValueType.String, 10,
  773. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  774. ihtd.addIndex(indexSpecification);
  775. admin.createTable(ihtd);
  776. HTable table = new HTable(conf, userTableName);
  777. byte[][] val =
  778. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  779. Bytes.toBytes("goat") };
  780. putsForIdx1WithDiffValues(Bytes.toBytes("row1"), val, table);
  781. val =
  782. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  783. Bytes.toBytes("goat1") };
  784. putsForIdx1WithDiffValues(Bytes.toBytes("row2"), val, table);
  785. val =
  786. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  787. Bytes.toBytes("goat1") };
  788. putsForIdx1WithDiffValues(Bytes.toBytes("row3"), val, table);
  789. val =
  790. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog1"), Bytes.toBytes("elef"),
  791. Bytes.toBytes("goat1") };
  792. putsForIdx1WithDiffValues(Bytes.toBytes("row4"), val, table);
  793. val =
  794. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  795. Bytes.toBytes("goat1"), Bytes.toBytes("ant") };
  796. putsForIdx1WithDiffValues(Bytes.toBytes("row5"), val, table);
  797. val =
  798. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elefe"),
  799. Bytes.toBytes("goat1") };
  800. putsForIdx1WithDiffValues(Bytes.toBytes("row6"), val, table);
  801. val =
  802. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  803. Bytes.toBytes("goat") };
  804. putsForIdx1WithDiffValues(Bytes.toBytes("row7"), val, table);
  805. val =
  806. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  807. Bytes.toBytes("gda") };
  808. putsForIdx1WithDiffValues(Bytes.toBytes("row8"), val, table);
  809. val =
  810. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  811. Bytes.toBytes("goa") };
  812. putsForIdx1WithDiffValues(Bytes.toBytes("row9"), val, table);
  813. table.flushCommits();
  814. admin.flush(userTableName);
  815. admin.flush(userTableName + "_idx");
  816. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  817. SingleColumnValueFilter scvf =
  818. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  819. Bytes.toBytes("cat"));
  820. SingleColumnValueFilter scvf1 =
  821. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  822. Bytes.toBytes("dog"));
  823. SingleColumnValueFilter scvf2 =
  824. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  825. Bytes.toBytes("elef"));
  826. SingleColumnValueFilter scvf3 =
  827. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.LESS,
  828. Bytes.toBytes("goat"));
  829. masterFilter.addFilter(scvf);
  830. masterFilter.addFilter(scvf1);
  831. masterFilter.addFilter(scvf2);
  832. masterFilter.addFilter(scvf3);
  833. Scan scan = new Scan();
  834. scan.setFilter(masterFilter);
  835. ResultScanner scanner = table.getScanner(scan);
  836. List<Result> testRes = new ArrayList<Result>();
  837. Result[] result = scanner.next(1);
  838. while (result != null && result.length > 0) {
  839. testRes.add(result[0]);
  840. result = scanner.next(1);
  841. }
  842. assertEquals(testRes.size(), 2);
  843. }
  844. @Test(timeout = 180000)
  845. public void testScanWithMutlipleIndicesWithLesserEqualCondOnTheSameColAndSimilarPattern()
  846. throws Exception {
  847. HBaseAdmin admin = UTIL.getHBaseAdmin();
  848. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  849. Configuration conf = UTIL.getConfiguration();
  850. String userTableName =
  851. "testScanWithMutlipleIndicesWithLesserEqualCondOnTheSameColAndSimilarPattern";
  852. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  853. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  854. ihtd.addFamily(hcd);
  855. IndexSpecification indexSpecification =
  856. createIndexSpecification(hcd, ValueType.String, 10,
  857. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  858. ihtd.addIndex(indexSpecification);
  859. admin.createTable(ihtd);
  860. HTable table = new HTable(conf, userTableName);
  861. byte[][] val =
  862. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  863. Bytes.toBytes("goat") };
  864. putsForIdx1WithDiffValues(Bytes.toBytes("row1"), val, table);
  865. val =
  866. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("ele"),
  867. Bytes.toBytes("goat1") };
  868. putsForIdx1WithDiffValues(Bytes.toBytes("row2"), val, table);
  869. val =
  870. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  871. Bytes.toBytes("goat1") };
  872. putsForIdx1WithDiffValues(Bytes.toBytes("row3"), val, table);
  873. val =
  874. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog1"), Bytes.toBytes("elef"),
  875. Bytes.toBytes("goat1") };
  876. putsForIdx1WithDiffValues(Bytes.toBytes("row4"), val, table);
  877. val =
  878. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  879. Bytes.toBytes("goat1"), Bytes.toBytes("ant") };
  880. putsForIdx1WithDiffValues(Bytes.toBytes("row5"), val, table);
  881. val =
  882. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elefe"),
  883. Bytes.toBytes("goat1") };
  884. putsForIdx1WithDiffValues(Bytes.toBytes("row6"), val, table);
  885. val =
  886. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  887. Bytes.toBytes("goat") };
  888. putsForIdx1WithDiffValues(Bytes.toBytes("row7"), val, table);
  889. val =
  890. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  891. Bytes.toBytes("gda") };
  892. putsForIdx1WithDiffValues(Bytes.toBytes("row8"), val, table);
  893. val =
  894. new byte[][] { Bytes.toBytes("cat"), Bytes.toBytes("dog"), Bytes.toBytes("elef"),
  895. Bytes.toBytes("goa") };
  896. putsForIdx1WithDiffValues(Bytes.toBytes("row9"), val, table);
  897. table.flushCommits();
  898. admin.flush(userTableName);
  899. admin.flush(userTableName + "_idx");
  900. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  901. SingleColumnValueFilter scvf =
  902. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  903. Bytes.toBytes("cat"));
  904. SingleColumnValueFilter scvf1 =
  905. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  906. Bytes.toBytes("dog"));
  907. SingleColumnValueFilter scvf2 =
  908. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  909. Bytes.toBytes("elef"));
  910. SingleColumnValueFilter scvf3 =
  911. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.LESS_OR_EQUAL,
  912. Bytes.toBytes("goat"));
  913. masterFilter.addFilter(scvf);
  914. masterFilter.addFilter(scvf1);
  915. masterFilter.addFilter(scvf2);
  916. masterFilter.addFilter(scvf3);
  917. Scan scan = new Scan();
  918. scan.setFilter(masterFilter);
  919. ResultScanner scanner = table.getScanner(scan);
  920. List<Result> testRes = new ArrayList<Result>();
  921. Result[] result = scanner.next(1);
  922. while (result != null && result.length > 0) {
  923. testRes.add(result[0]);
  924. result = scanner.next(1);
  925. }
  926. assertEquals(testRes.size(), 3);
  927. }
  928. private void putsForIdx1WithDiffValues(byte[] row, byte[][] valList, HTable table)
  929. throws IOException {
  930. Put p = new Put(row);
  931. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), (valList[0]));
  932. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c4"), valList[1]);
  933. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c5"), valList[2]);
  934. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c6"), valList[3]);
  935. table.put(p);
  936. }
  937. @Test(timeout = 180000)
  938. public void testWhenSomePointsAreFetchedFromIndexButMainScanStillHasSomeFiltersToApply()
  939. throws Exception {
  940. HBaseAdmin admin = UTIL.getHBaseAdmin();
  941. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  942. Configuration conf = UTIL.getConfiguration();
  943. String userTableName = "MainScanStillHasSomeFiltersToApply";
  944. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  945. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  946. ihtd.addFamily(hcd);
  947. IndexSpecification indexSpecification =
  948. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  949. ihtd.addIndex(indexSpecification);
  950. indexSpecification =
  951. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  952. ihtd.addIndex(indexSpecification);
  953. SingleColumnValueFilter scvf =
  954. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  955. "apple".getBytes());
  956. scvf.setFilterIfMissing(true);
  957. SingleColumnValueFilter scvf1 =
  958. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  959. "bat".getBytes());
  960. scvf1.setFilterIfMissing(true);
  961. FilterList orFilter = new FilterList(Operator.MUST_PASS_ONE);
  962. orFilter.addFilter(scvf);
  963. orFilter.addFilter(scvf1);
  964. FilterList andFilter = new FilterList(Operator.MUST_PASS_ALL);
  965. SingleColumnValueFilter scvf2 =
  966. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  967. "cat".getBytes());
  968. scvf2.setFilterIfMissing(true);
  969. SingleColumnValueFilter scvf3 =
  970. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  971. "dog".getBytes());
  972. scvf3.setFilterIfMissing(true);
  973. SingleColumnValueFilter scvf4 =
  974. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  975. "ele".getBytes());
  976. scvf4.setFilterIfMissing(true);
  977. SingleColumnValueFilter scvf5 =
  978. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.EQUAL,
  979. "fan".getBytes());
  980. scvf5.setFilterIfMissing(true);
  981. andFilter.addFilter(scvf5);
  982. andFilter.addFilter(scvf4);
  983. andFilter.addFilter(scvf3);
  984. andFilter.addFilter(scvf2);
  985. FilterList master = new FilterList(Operator.MUST_PASS_ALL);
  986. master.addFilter(andFilter);
  987. master.addFilter(orFilter);
  988. admin.createTable(ihtd);
  989. ZKAssign.blockUntilNoRIT(zkw);
  990. HTable table = new HTable(conf, "MainScanStillHasSomeFiltersToApply");
  991. putforIDX1(Bytes.toBytes("row0"), table);
  992. putforIDX1(Bytes.toBytes("row1"), table);
  993. putforIDX2(Bytes.toBytes("row2"), table);
  994. putforIDX3(Bytes.toBytes("row3"), table);
  995. putforIDX1(Bytes.toBytes("row4"), table);
  996. putforIDX2(Bytes.toBytes("row4"), table);
  997. putforIDX3(Bytes.toBytes("row4"), table);
  998. putforIDX1(Bytes.toBytes("row5"), table);
  999. putforIDX1(Bytes.toBytes("row6"), table);
  1000. putforIDX2(Bytes.toBytes("row7"), table);
  1001. putforIDX3(Bytes.toBytes("row8"), table);
  1002. putforIDX1(Bytes.toBytes("row9"), table);
  1003. putforIDX2(Bytes.toBytes("row9"), table);
  1004. Scan scan = new Scan();
  1005. scan.setFilter(master);
  1006. // scan.setCaching(10);
  1007. int i = 0;
  1008. ResultScanner scanner = table.getScanner(scan);
  1009. List<Result> testRes = new ArrayList<Result>();
  1010. Result[] result = scanner.next(1);
  1011. while (result != null && result.length > 0) {
  1012. testRes.add(result[0]);
  1013. i++;
  1014. result = scanner.next(1);
  1015. }
  1016. Assert.assertTrue("Index flow should be used.", IndexRegionObserver.getIndexedFlowUsed());
  1017. Assert.assertTrue("Index should fetch some seek points.",
  1018. IndexRegionObserver.getSeekpointAdded());
  1019. Assert.assertEquals("Index should fetch 6 seek points", 6, IndexRegionObserver
  1020. .getMultipleSeekPoints().size());
  1021. Assert.assertEquals("Final result should have 2 rows.", 2, testRes.size());
  1022. }
  1023. @Test(timeout = 180000)
  1024. public void testWhenThereIsNoDataInIndexRegion() throws Exception {
  1025. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1026. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1027. Configuration conf = UTIL.getConfiguration();
  1028. String userTableName = "testWhenThereIsNoDataInIndexRegion";
  1029. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1030. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1031. ihtd.addFamily(hcd);
  1032. IndexSpecification indexSpecification =
  1033. createIndexSpecification(hcd, ValueType.String, 10,
  1034. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1035. ihtd.addIndex(indexSpecification);
  1036. indexSpecification =
  1037. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1038. ihtd.addIndex(indexSpecification);
  1039. indexSpecification =
  1040. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1041. ihtd.addIndex(indexSpecification);
  1042. SingleColumnValueFilter filter =
  1043. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  1044. "apple".getBytes());
  1045. SingleColumnValueFilter filter1 =
  1046. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  1047. "bat".getBytes());
  1048. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  1049. masterFilter.addFilter(filter1);
  1050. masterFilter.addFilter(filter);
  1051. admin.createTable(ihtd);
  1052. ZKAssign.blockUntilNoRIT(zkw);
  1053. HTable table = new HTable(conf, "testWhenThereIsNoDataInIndexRegion");
  1054. Scan scan = new Scan();
  1055. scan.setFilter(masterFilter);
  1056. int i = 0;
  1057. ResultScanner scanner = table.getScanner(scan);
  1058. List<Result> testRes = new ArrayList<Result>();
  1059. Result[] result = scanner.next(1);
  1060. while (result != null && result.length > 0) {
  1061. testRes.add(result[0]);
  1062. i++;
  1063. result = scanner.next(1);
  1064. }
  1065. Assert.assertTrue("Index flow should be used.", IndexRegionObserver.getIndexedFlowUsed());
  1066. Assert.assertFalse("Index should fetch some seek points.",
  1067. IndexRegionObserver.getSeekpointAdded());
  1068. }
  1069. @Test(timeout = 180000)
  1070. public void testMultipleScansOnTheIndexRegion() throws Exception {
  1071. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1072. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1073. final Configuration conf = UTIL.getConfiguration();
  1074. String userTableName = "testMultipleScansOnTheIndexRegion";
  1075. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1076. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1077. ihtd.addFamily(hcd);
  1078. IndexSpecification indexSpecification =
  1079. createIndexSpecification(hcd, ValueType.String, 10,
  1080. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1081. ihtd.addIndex(indexSpecification);
  1082. indexSpecification =
  1083. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1084. ihtd.addIndex(indexSpecification);
  1085. indexSpecification =
  1086. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1087. ihtd.addIndex(indexSpecification);
  1088. indexSpecification =
  1089. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2", "c1" }, "idx4");
  1090. ihtd.addIndex(indexSpecification);
  1091. admin.createTable(ihtd);
  1092. ZKAssign.blockUntilNoRIT(zkw);
  1093. HTable table = new HTable(conf, "testMultipleScansOnTheIndexRegion");
  1094. putforIDX1(Bytes.toBytes("row0"), table);
  1095. putforIDX1(Bytes.toBytes("row1"), table);
  1096. putforIDX2(Bytes.toBytes("row2"), table);
  1097. putforIDX3(Bytes.toBytes("row3"), table);
  1098. putforIDX1(Bytes.toBytes("row4"), table);
  1099. putforIDX2(Bytes.toBytes("row4"), table);
  1100. putforIDX3(Bytes.toBytes("row4"), table);
  1101. putforIDX1(Bytes.toBytes("row5"), table);
  1102. putforIDX1(Bytes.toBytes("row6"), table);
  1103. putforIDX2(Bytes.toBytes("row7"), table);
  1104. putforIDX3(Bytes.toBytes("row8"), table);
  1105. putforIDX1(Bytes.toBytes("row9"), table);
  1106. putforIDX2(Bytes.toBytes("row9"), table);
  1107. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  1108. // create the filter
  1109. FilterList filter = new FilterList(Operator.MUST_PASS_ONE);
  1110. SingleColumnValueFilter iscvf1 =
  1111. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  1112. "apple".getBytes());
  1113. SingleColumnValueFilter iscvf2 =
  1114. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  1115. "bat".getBytes());
  1116. filter.addFilter(iscvf1);
  1117. filter.addFilter(iscvf2);
  1118. FilterList filter1 = new FilterList(Operator.MUST_PASS_ALL);
  1119. iscvf1 =
  1120. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1121. "cat".getBytes());
  1122. iscvf2 =
  1123. new SingleColumnValueFilter("cf1".getBytes(), "c4".getBytes(), CompareOp.EQUAL,
  1124. "dog".getBytes());
  1125. filter1.addFilter(iscvf1);
  1126. filter1.addFilter(iscvf2);
  1127. FilterList filter2 = new FilterList(Operator.MUST_PASS_ALL);
  1128. iscvf1 =
  1129. new SingleColumnValueFilter("cf1".getBytes(), "c5".getBytes(), CompareOp.EQUAL,
  1130. "ele".getBytes());
  1131. iscvf2 =
  1132. new SingleColumnValueFilter("cf1".getBytes(), "c6".getBytes(), CompareOp.EQUAL,
  1133. "fan".getBytes());
  1134. filter2.addFilter(iscvf1);
  1135. filter2.addFilter(iscvf2);
  1136. masterFilter.addFilter(filter);
  1137. masterFilter.addFilter(filter1);
  1138. masterFilter.addFilter(filter2);
  1139. Scan scan = new Scan();
  1140. scan.setFilter(masterFilter);
  1141. int i = 0;
  1142. ResultScanner scanner = table.getScanner(scan);
  1143. List<Result> testRes = new ArrayList<Result>();
  1144. Result[] result = scanner.next(1);
  1145. Thread test = new testThread(masterFilter, "testMultipleScansOnTheIndexRegion");
  1146. test.start();
  1147. while (result != null && result.length > 0) {
  1148. testRes.add(result[0]);
  1149. i++;
  1150. result = scanner.next(1);
  1151. }
  1152. test.join();
  1153. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1154. Assert.assertTrue("Seekpoints should get added by index scanner",
  1155. IndexRegionObserver.getSeekpointAdded());
  1156. Assert.assertEquals("It should get two seek points from index scanner.", 4, IndexRegionObserver
  1157. .getMultipleSeekPoints().size());
  1158. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
  1159. }
  1160. private class testThread extends Thread {
  1161. Filter filter = null;
  1162. String tableName = null;
  1163. public testThread(Filter filter, String tableName) {
  1164. this.filter = filter;
  1165. this.tableName = tableName;
  1166. }
  1167. @Override
  1168. public synchronized void start() {
  1169. try {
  1170. HTable table = new HTable(UTIL.getConfiguration(), tableName);
  1171. Scan scan = new Scan();
  1172. scan.setFilter(filter);
  1173. int i = 0;
  1174. ResultScanner scanner = table.getScanner(scan);
  1175. List<Result> testRes = new ArrayList<Result>();
  1176. Result[] result = scanner.next(1);
  1177. while (result != null && result.length > 0) {
  1178. testRes.add(result[0]);
  1179. i++;
  1180. result = scanner.next(1);
  1181. }
  1182. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1183. Assert.assertTrue("Seekpoints should get added by index scanner",
  1184. IndexRegionObserver.getSeekpointAdded());
  1185. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
  1186. } catch (IOException e) {
  1187. }
  1188. }
  1189. }
  1190. @Test(timeout = 180000)
  1191. public void testFalsePositiveCases() throws Exception {
  1192. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1193. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1194. final Configuration conf = UTIL.getConfiguration();
  1195. String userTableName = "testFalsePositiveCases";
  1196. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1197. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1198. ihtd.addFamily(hcd);
  1199. IndexSpecification indexSpecification =
  1200. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1201. ihtd.addIndex(indexSpecification);
  1202. indexSpecification =
  1203. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1204. ihtd.addIndex(indexSpecification);
  1205. admin.createTable(ihtd);
  1206. ZKAssign.blockUntilNoRIT(zkw);
  1207. HTable table = new HTable(conf, "testFalsePositiveCases");
  1208. HTable idx_table = new HTable(conf, "testFalsePositiveCases_idx");
  1209. ByteArrayBuilder byteArray = new ByteArrayBuilder(33);
  1210. byteArray.put(new byte[1]);
  1211. byteArray.put(Bytes.toBytes("idx2"));
  1212. byteArray.put(new byte[14]);
  1213. byteArray.put(Bytes.toBytes("apple"));
  1214. byteArray.put(new byte[5]);
  1215. int offset = byteArray.position();
  1216. byteArray.put(Bytes.toBytes("row1"));
  1217. ByteArrayBuilder value = new ByteArrayBuilder(4);
  1218. value.put(Bytes.toBytes((short) byteArray.array().length));
  1219. value.put(Bytes.toBytes((short) offset));
  1220. Put p = new Put(byteArray.array());
  1221. p.add(Constants.IDX_COL_FAMILY, Constants.IDX_COL_QUAL, value.array());
  1222. idx_table.put(p);
  1223. SingleColumnValueFilter filter =
  1224. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  1225. "apple".getBytes());
  1226. SingleColumnValueFilter filter1 =
  1227. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  1228. "bat".getBytes());
  1229. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  1230. masterFilter.addFilter(filter1);
  1231. masterFilter.addFilter(filter);
  1232. Scan scan = new Scan();
  1233. scan.setFilter(masterFilter);
  1234. int i = 0;
  1235. ResultScanner scanner = table.getScanner(scan);
  1236. List<Result> testRes = new ArrayList<Result>();
  1237. Result[] result = scanner.next(1);
  1238. while (result != null && result.length > 0) {
  1239. testRes.add(result[0]);
  1240. i++;
  1241. result = scanner.next(1);
  1242. }
  1243. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1244. Assert.assertTrue("Seekpoints should get added by index scanner",
  1245. IndexRegionObserver.getSeekpointAdded());
  1246. Assert.assertEquals("It should get two seek points from index scanner.", 1, IndexRegionObserver
  1247. .getMultipleSeekPoints().size());
  1248. Assert.assertEquals("Overall result should have only 2 rows", 0, testRes.size());
  1249. }
  1250. @Test(timeout = 180000)
  1251. public void testSingleLevelRangeScanForAND() throws IOException, KeeperException,
  1252. InterruptedException {
  1253. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1254. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1255. Configuration conf = UTIL.getConfiguration();
  1256. String userTableName = "testSingleLevelRangeScanForAND";
  1257. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1258. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1259. ihtd.addFamily(hcd);
  1260. IndexSpecification indexSpecification =
  1261. createIndexSpecification(hcd, ValueType.String, 10,
  1262. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1263. ihtd.addIndex(indexSpecification);
  1264. indexSpecification =
  1265. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1266. ihtd.addIndex(indexSpecification);
  1267. indexSpecification =
  1268. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1269. ihtd.addIndex(indexSpecification);
  1270. indexSpecification =
  1271. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1272. ihtd.addIndex(indexSpecification);
  1273. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1274. // "c1" }, "idx4");
  1275. // ihtd.addIndex(indexSpecification);
  1276. admin.createTable(ihtd);
  1277. HTable table = new HTable(conf, userTableName);
  1278. rangePutForIdx2(table);
  1279. rangePutForIdx3(table);
  1280. rangePutForIdx4(table);
  1281. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  1282. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  1283. // SingleColumnValueFilter scvfsub = new SingleColumnValueFilter("cf1".getBytes(),
  1284. // "c1".getBytes(),
  1285. // CompareOp.EQUAL, "5".getBytes());
  1286. SingleColumnValueFilter scvf2sub =
  1287. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  1288. "6".getBytes());
  1289. SingleColumnValueFilter scvf3sub =
  1290. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1291. "2".getBytes());
  1292. // filterList1.addFilter(scvfsub);
  1293. // filterList1.addFilter(scvf2sub);
  1294. // filterList1.addFilter(scvf3sub);
  1295. // SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(),
  1296. // CompareOp.GREATER_OR_EQUAL, "5".getBytes());
  1297. // SingleColumnValueFilter scvf2 = new SingleColumnValueFilter("cf1".getBytes(),
  1298. // "c2".getBytes(),
  1299. // CompareOp.LESS_OR_EQUAL, "5".getBytes());
  1300. SingleColumnValueFilter scvf3 =
  1301. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1302. "5".getBytes());
  1303. // masterFilter.addFilter(scvf);
  1304. // masterFilter.addFilter(scvf2);
  1305. masterFilter.addFilter(scvf2sub);
  1306. masterFilter.addFilter(scvf3sub);
  1307. masterFilter.addFilter(scvf3);
  1308. // masterFilter.addFilter(filterList1);
  1309. Scan scan = new Scan();
  1310. scan.setFilter(masterFilter);
  1311. int i = 0;
  1312. ResultScanner scanner = table.getScanner(scan);
  1313. List<Result> testRes = new ArrayList<Result>();
  1314. Result[] result = scanner.next(1);
  1315. while (result != null && result.length > 0) {
  1316. testRes.add(result[0]);
  1317. i++;
  1318. result = scanner.next(1);
  1319. }
  1320. System.out.println("************* result count......*********** " + testRes.size()
  1321. + " ###### " + testRes);
  1322. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1323. Assert.assertTrue("Seekpoints should get added by index scanner",
  1324. IndexRegionObserver.getSeekpointAdded());
  1325. Assert.assertEquals("It should get two seek points from index scanner.", 2, IndexRegionObserver
  1326. .getMultipleSeekPoints().size());
  1327. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 2);
  1328. }
  1329. @Test(timeout = 180000)
  1330. public void testSingleLevelRangeScanForOR() throws IOException, KeeperException,
  1331. InterruptedException {
  1332. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1333. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1334. Configuration conf = UTIL.getConfiguration();
  1335. String userTableName = "testSingleLevelRangeScanForOR";
  1336. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1337. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1338. ihtd.addFamily(hcd);
  1339. IndexSpecification indexSpecification =
  1340. createIndexSpecification(hcd, ValueType.String, 10,
  1341. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1342. ihtd.addIndex(indexSpecification);
  1343. indexSpecification =
  1344. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1345. ihtd.addIndex(indexSpecification);
  1346. indexSpecification =
  1347. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1348. ihtd.addIndex(indexSpecification);
  1349. indexSpecification =
  1350. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1351. ihtd.addIndex(indexSpecification);
  1352. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1353. // "c1" }, "idx4");
  1354. // ihtd.addIndex(indexSpecification);
  1355. admin.createTable(ihtd);
  1356. HTable table = new HTable(conf, userTableName);
  1357. rangePutForIdx2(table);
  1358. rangePutForIdx3(table);
  1359. rangePutForIdx4(table);
  1360. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  1361. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  1362. // SingleColumnValueFilter scvfsub = new SingleColumnValueFilter("cf1".getBytes(),
  1363. // "c1".getBytes(),
  1364. // CompareOp.EQUAL, "5".getBytes());
  1365. SingleColumnValueFilter scvf2sub =
  1366. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  1367. "6".getBytes());
  1368. SingleColumnValueFilter scvf3sub =
  1369. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1370. "2".getBytes());
  1371. // filterList1.addFilter(scvfsub);
  1372. // filterList1.addFilter(scvf2sub);
  1373. // filterList1.addFilter(scvf3sub);
  1374. // SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(),
  1375. // CompareOp.GREATER_OR_EQUAL, "5".getBytes());
  1376. // SingleColumnValueFilter scvf2 = new SingleColumnValueFilter("cf1".getBytes(),
  1377. // "c2".getBytes(),
  1378. // CompareOp.LESS_OR_EQUAL, "5".getBytes());
  1379. SingleColumnValueFilter scvf3 =
  1380. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1381. "5".getBytes());
  1382. // masterFilter.addFilter(scvf);
  1383. // masterFilter.addFilter(scvf2);
  1384. masterFilter.addFilter(scvf2sub);
  1385. masterFilter.addFilter(scvf3sub);
  1386. masterFilter.addFilter(scvf3);
  1387. // masterFilter.addFilter(filterList1);
  1388. Scan scan = new Scan();
  1389. scan.setFilter(masterFilter);
  1390. int i = 0;
  1391. ResultScanner scanner = table.getScanner(scan);
  1392. List<Result> testRes = new ArrayList<Result>();
  1393. Result[] result = scanner.next(1);
  1394. while (result != null && result.length > 0) {
  1395. testRes.add(result[0]);
  1396. i++;
  1397. result = scanner.next(1);
  1398. }
  1399. System.out.println("************* result count......*********** " + testRes.size()
  1400. + " ###### " + testRes);
  1401. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1402. Assert.assertTrue("Seekpoints should get added by index scanner",
  1403. IndexRegionObserver.getSeekpointAdded());
  1404. Assert.assertEquals("It should get two seek points from index scanner.", 6, IndexRegionObserver
  1405. .getMultipleSeekPoints().size());
  1406. Assert.assertTrue("Overall result should have only 6 rows", testRes.size() == 6);
  1407. }
  1408. @Test(timeout = 180000)
  1409. public void testEqualAndRangeCombinationWithMultipleIndices() throws IOException,
  1410. KeeperException, InterruptedException {
  1411. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1412. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1413. Configuration conf = UTIL.getConfiguration();
  1414. String userTableName = "testEqualAndRangeCombinationWithMultipleIndices";
  1415. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1416. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1417. ihtd.addFamily(hcd);
  1418. IndexSpecification indexSpecification =
  1419. createIndexSpecification(hcd, ValueType.String, 10,
  1420. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1421. ihtd.addIndex(indexSpecification);
  1422. indexSpecification =
  1423. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1424. ihtd.addIndex(indexSpecification);
  1425. indexSpecification =
  1426. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1427. ihtd.addIndex(indexSpecification);
  1428. indexSpecification =
  1429. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1430. ihtd.addIndex(indexSpecification);
  1431. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1432. // "c1" }, "idx4");
  1433. // ihtd.addIndex(indexSpecification);
  1434. admin.createTable(ihtd);
  1435. HTable table = new HTable(conf, userTableName);
  1436. rangePutForIdx2(table);
  1437. rangePutForIdx3(table);
  1438. rangePutForIdx4(table);
  1439. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  1440. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  1441. SingleColumnValueFilter scvf =
  1442. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1443. "2".getBytes());
  1444. SingleColumnValueFilter scvf2 =
  1445. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1446. "6".getBytes());
  1447. SingleColumnValueFilter scvf3 =
  1448. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1449. "4".getBytes());
  1450. filterList1.addFilter(scvf);
  1451. filterList1.addFilter(scvf2);
  1452. filterList1.addFilter(scvf3);
  1453. SingleColumnValueFilter scvfsub =
  1454. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  1455. "2".getBytes());
  1456. SingleColumnValueFilter scvf2sub =
  1457. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  1458. "2".getBytes());
  1459. SingleColumnValueFilter scvf3sub =
  1460. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1461. "2".getBytes());
  1462. masterFilter.addFilter(scvfsub);
  1463. masterFilter.addFilter(scvf2sub);
  1464. masterFilter.addFilter(scvf3sub);
  1465. masterFilter.addFilter(filterList1);
  1466. Scan scan = new Scan();
  1467. scan.setFilter(masterFilter);
  1468. int i = 0;
  1469. ResultScanner scanner = table.getScanner(scan);
  1470. List<Result> testRes = new ArrayList<Result>();
  1471. Result[] result = scanner.next(1);
  1472. while (result != null && result.length > 0) {
  1473. testRes.add(result[0]);
  1474. i++;
  1475. result = scanner.next(1);
  1476. }
  1477. System.out.println("************* result count......*********** " + testRes.size()
  1478. + " ###### " + testRes);
  1479. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1480. Assert.assertTrue("Seekpoints should get added by index scanner",
  1481. IndexRegionObserver.getSeekpointAdded());
  1482. Assert.assertEquals("It should get two seek points from index scanner.", 1, IndexRegionObserver
  1483. .getMultipleSeekPoints().size());
  1484. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 1);
  1485. }
  1486. @Test(timeout = 180000)
  1487. public void testEqualAndRangeCombinationWithMultipleIndicesPart2() throws IOException,
  1488. KeeperException, InterruptedException {
  1489. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1490. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1491. Configuration conf = UTIL.getConfiguration();
  1492. String userTableName = "testEqualAndRangeCombinationWithMultipleIndicesPart2";
  1493. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1494. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1495. ihtd.addFamily(hcd);
  1496. IndexSpecification indexSpecification =
  1497. createIndexSpecification(hcd, ValueType.String, 10,
  1498. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1499. ihtd.addIndex(indexSpecification);
  1500. indexSpecification =
  1501. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1502. ihtd.addIndex(indexSpecification);
  1503. indexSpecification =
  1504. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1505. ihtd.addIndex(indexSpecification);
  1506. indexSpecification =
  1507. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1508. ihtd.addIndex(indexSpecification);
  1509. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1510. // "c1" }, "idx4");
  1511. // ihtd.addIndex(indexSpecification);
  1512. admin.createTable(ihtd);
  1513. HTable table = new HTable(conf, userTableName);
  1514. rangePutForIdx2(table);
  1515. rangePutForIdx3(table);
  1516. rangePutForIdx4(table);
  1517. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  1518. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ALL);
  1519. SingleColumnValueFilter scvf =
  1520. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1521. "2".getBytes());
  1522. SingleColumnValueFilter scvf2 =
  1523. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1524. "4".getBytes());
  1525. SingleColumnValueFilter scvf3 =
  1526. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1527. "1".getBytes());
  1528. filterList1.addFilter(scvf);
  1529. filterList1.addFilter(scvf2);
  1530. filterList1.addFilter(scvf3);
  1531. SingleColumnValueFilter scvfsub =
  1532. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  1533. "6".getBytes());
  1534. SingleColumnValueFilter scvf2sub =
  1535. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  1536. "6".getBytes());
  1537. SingleColumnValueFilter scvf3sub =
  1538. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1539. "6".getBytes());
  1540. masterFilter.addFilter(scvfsub);
  1541. masterFilter.addFilter(scvf2sub);
  1542. masterFilter.addFilter(scvf3sub);
  1543. masterFilter.addFilter(filterList1);
  1544. Scan scan = new Scan();
  1545. scan.setFilter(masterFilter);
  1546. int i = 0;
  1547. ResultScanner scanner = table.getScanner(scan);
  1548. List<Result> testRes = new ArrayList<Result>();
  1549. Result[] result = scanner.next(1);
  1550. while (result != null && result.length > 0) {
  1551. testRes.add(result[0]);
  1552. i++;
  1553. result = scanner.next(1);
  1554. }
  1555. System.out.println("************* result count......*********** " + testRes.size()
  1556. + " ###### " + testRes);
  1557. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1558. Assert.assertTrue("Seekpoints should get added by index scanner",
  1559. IndexRegionObserver.getSeekpointAdded());
  1560. Assert.assertEquals("It should get two seek points from index scanner.", 4, IndexRegionObserver
  1561. .getMultipleSeekPoints().size());
  1562. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 4);
  1563. }
  1564. @Test(timeout = 180000)
  1565. public void testOREvaluatorWithMultipleOperatorsInEachLevel() throws IOException,
  1566. KeeperException, InterruptedException {
  1567. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1568. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1569. Configuration conf = UTIL.getConfiguration();
  1570. String userTableName = "testOREvaluatorWithMultipleOperatorsInEachLevel";
  1571. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1572. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1573. ihtd.addFamily(hcd);
  1574. IndexSpecification indexSpecification =
  1575. createIndexSpecification(hcd, ValueType.String, 10,
  1576. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1577. ihtd.addIndex(indexSpecification);
  1578. indexSpecification =
  1579. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1580. ihtd.addIndex(indexSpecification);
  1581. indexSpecification =
  1582. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1583. ihtd.addIndex(indexSpecification);
  1584. indexSpecification =
  1585. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1586. ihtd.addIndex(indexSpecification);
  1587. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1588. // "c1" }, "idx4");
  1589. // ihtd.addIndex(indexSpecification);
  1590. admin.createTable(ihtd);
  1591. HTable table = new HTable(conf, userTableName);
  1592. rangePutForIdx2(table);
  1593. rangePutForIdx3(table);
  1594. rangePutForIdx4(table);
  1595. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  1596. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  1597. SingleColumnValueFilter scvfsub =
  1598. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  1599. "5".getBytes());
  1600. SingleColumnValueFilter scvf2sub =
  1601. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  1602. "5".getBytes());
  1603. SingleColumnValueFilter scvf3sub =
  1604. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1605. "2".getBytes());
  1606. filterList1.addFilter(scvfsub);
  1607. filterList1.addFilter(scvf2sub);
  1608. filterList1.addFilter(scvf3sub);
  1609. SingleColumnValueFilter scvf =
  1610. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1611. "4".getBytes());
  1612. SingleColumnValueFilter scvf2 =
  1613. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  1614. "4".getBytes());
  1615. SingleColumnValueFilter scvf3 =
  1616. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  1617. "4".getBytes());
  1618. masterFilter.addFilter(scvf);
  1619. masterFilter.addFilter(scvf2);
  1620. masterFilter.addFilter(scvf3);
  1621. masterFilter.addFilter(filterList1);
  1622. Scan scan = new Scan();
  1623. scan.setFilter(masterFilter);
  1624. int i = 0;
  1625. ResultScanner scanner = table.getScanner(scan);
  1626. List<Result> testRes = new ArrayList<Result>();
  1627. Result[] result = scanner.next(1);
  1628. while (result != null && result.length > 0) {
  1629. testRes.add(result[0]);
  1630. i++;
  1631. result = scanner.next(1);
  1632. }
  1633. System.out.println("************* result count......*********** " + testRes.size()
  1634. + " ###### " + testRes);
  1635. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1636. Assert.assertTrue("Seekpoints should get added by index scanner",
  1637. IndexRegionObserver.getSeekpointAdded());
  1638. Assert.assertEquals("It should get two seek points from index scanner.", 6, IndexRegionObserver
  1639. .getMultipleSeekPoints().size());
  1640. Assert.assertTrue("Overall result should have only 6 rows", testRes.size() == 6);
  1641. }
  1642. @Test(timeout = 180000)
  1643. public void testIfAllScannersAreRangeInAllLevels() throws IOException, KeeperException,
  1644. InterruptedException {
  1645. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1646. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1647. Configuration conf = UTIL.getConfiguration();
  1648. String userTableName = "testIfAllScannersAreRangeInAllLevels";
  1649. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1650. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1651. ihtd.addFamily(hcd);
  1652. IndexSpecification indexSpecification =
  1653. createIndexSpecification(hcd, ValueType.String, 10,
  1654. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1655. ihtd.addIndex(indexSpecification);
  1656. indexSpecification =
  1657. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1658. ihtd.addIndex(indexSpecification);
  1659. indexSpecification =
  1660. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1661. ihtd.addIndex(indexSpecification);
  1662. indexSpecification =
  1663. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1664. ihtd.addIndex(indexSpecification);
  1665. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1666. // "c1" }, "idx4");
  1667. // ihtd.addIndex(indexSpecification);
  1668. admin.createTable(ihtd);
  1669. HTable table = new HTable(conf, userTableName);
  1670. rangePutForIdx2(table);
  1671. rangePutForIdx3(table);
  1672. rangePutForIdx4(table);
  1673. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  1674. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ALL);
  1675. SingleColumnValueFilter scvf =
  1676. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1677. "2".getBytes());
  1678. SingleColumnValueFilter scvf2 =
  1679. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1680. "4".getBytes());
  1681. SingleColumnValueFilter scvf3 =
  1682. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1683. "1".getBytes());
  1684. filterList1.addFilter(scvf);
  1685. filterList1.addFilter(scvf2);
  1686. filterList1.addFilter(scvf3);
  1687. SingleColumnValueFilter scvfsub =
  1688. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  1689. "2".getBytes());
  1690. SingleColumnValueFilter scvf2sub =
  1691. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1692. "6".getBytes());
  1693. SingleColumnValueFilter scvf3sub =
  1694. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1695. "2".getBytes());
  1696. masterFilter.addFilter(scvfsub);
  1697. masterFilter.addFilter(scvf2sub);
  1698. masterFilter.addFilter(scvf3sub);
  1699. masterFilter.addFilter(filterList1);
  1700. Scan scan = new Scan();
  1701. scan.setFilter(masterFilter);
  1702. int i = 0;
  1703. ResultScanner scanner = table.getScanner(scan);
  1704. List<Result> testRes = new ArrayList<Result>();
  1705. Result[] result = scanner.next(1);
  1706. while (result != null && result.length > 0) {
  1707. testRes.add(result[0]);
  1708. i++;
  1709. result = scanner.next(1);
  1710. }
  1711. System.out.println("************* result count......*********** " + testRes.size()
  1712. + " ###### " + testRes);
  1713. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1714. Assert.assertTrue("Seekpoints should get added by index scanner",
  1715. IndexRegionObserver.getSeekpointAdded());
  1716. Assert.assertEquals("It should get two seek points from index scanner.", 6, IndexRegionObserver
  1717. .getMultipleSeekPoints().size());
  1718. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 6);
  1719. }
  1720. @Test(timeout = 180000)
  1721. public void testANDWithORbranchesWhereEachBranchHavingAtleastOneFilterOtherThanSCVF()
  1722. throws IOException, KeeperException, InterruptedException {
  1723. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1724. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1725. Configuration conf = UTIL.getConfiguration();
  1726. String userTableName =
  1727. "testANDWithORbranchesWhereEachBranchHavingAtleastOneFilterOtherThanSCVF";
  1728. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1729. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1730. ihtd.addFamily(hcd);
  1731. IndexSpecification indexSpecification =
  1732. createIndexSpecification(hcd, ValueType.String, 10,
  1733. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1734. ihtd.addIndex(indexSpecification);
  1735. indexSpecification =
  1736. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1737. ihtd.addIndex(indexSpecification);
  1738. indexSpecification =
  1739. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1740. ihtd.addIndex(indexSpecification);
  1741. indexSpecification =
  1742. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1743. ihtd.addIndex(indexSpecification);
  1744. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1745. // "c1" }, "idx4");
  1746. // ihtd.addIndex(indexSpecification);
  1747. admin.createTable(ihtd);
  1748. HTable table = new HTable(conf, userTableName);
  1749. rangePutForIdx2(table);
  1750. rangePutForIdx3(table);
  1751. rangePutForIdx4(table);
  1752. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  1753. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  1754. SingleColumnValueFilter scvf =
  1755. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1756. "2".getBytes());
  1757. SingleColumnValueFilter scvf2 =
  1758. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  1759. "4".getBytes());
  1760. SingleColumnValueFilter scvf3 =
  1761. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1762. "1".getBytes());
  1763. filterList1.addFilter(scvf);
  1764. filterList1.addFilter(scvf2);
  1765. filterList1.addFilter(scvf3);
  1766. SingleColumnValueFilter scvfsub =
  1767. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  1768. "2".getBytes());
  1769. SingleColumnValueFilter scvf2sub =
  1770. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1771. "6".getBytes());
  1772. SingleColumnValueFilter scvf3sub =
  1773. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1774. "2".getBytes());
  1775. RowFilter rowFilter =
  1776. new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));
  1777. FilterList filterList2 = new FilterList(Operator.MUST_PASS_ONE);
  1778. filterList2.addFilter(scvfsub);
  1779. filterList2.addFilter(scvf2sub);
  1780. filterList2.addFilter(scvf3sub);
  1781. filterList2.addFilter(rowFilter);
  1782. SingleColumnValueFilter scvfsub1 =
  1783. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  1784. "2".getBytes());
  1785. SingleColumnValueFilter scvf2sub2 =
  1786. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1787. "6".getBytes());
  1788. SingleColumnValueFilter scvf3sub3 =
  1789. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1790. "2".getBytes());
  1791. RowFilter rowFilter2 =
  1792. new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));
  1793. FilterList subFilterList = new FilterList(Operator.MUST_PASS_ONE);
  1794. subFilterList.addFilter(scvfsub1);
  1795. subFilterList.addFilter(scvf2sub2);
  1796. subFilterList.addFilter(scvf3sub3);
  1797. subFilterList.addFilter(rowFilter2);
  1798. filterList1.addFilter(subFilterList);
  1799. masterFilter.addFilter(filterList1);
  1800. masterFilter.addFilter(filterList2);
  1801. Scan scan = new Scan();
  1802. scan.setFilter(masterFilter);
  1803. int i = 0;
  1804. ResultScanner scanner = table.getScanner(scan);
  1805. List<Result> testRes = new ArrayList<Result>();
  1806. Result[] result = scanner.next(1);
  1807. while (result != null && result.length > 0) {
  1808. testRes.add(result[0]);
  1809. i++;
  1810. result = scanner.next(1);
  1811. }
  1812. System.out.println("************* result count......*********** " + testRes.size()
  1813. + " ###### " + testRes);
  1814. Assert.assertFalse("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1815. }
  1816. @Test(timeout = 180000)
  1817. public void testORIfEachBranchHavingAtleastOneOtherFilterThanSCVF() throws IOException,
  1818. KeeperException, InterruptedException {
  1819. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1820. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1821. Configuration conf = UTIL.getConfiguration();
  1822. String userTableName = "testORIfEachBranchHavingAtleastOneOtherFilterThanSCVF";
  1823. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1824. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1825. ihtd.addFamily(hcd);
  1826. IndexSpecification indexSpecification =
  1827. createIndexSpecification(hcd, ValueType.String, 10,
  1828. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1829. ihtd.addIndex(indexSpecification);
  1830. indexSpecification =
  1831. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1832. ihtd.addIndex(indexSpecification);
  1833. indexSpecification =
  1834. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1835. ihtd.addIndex(indexSpecification);
  1836. indexSpecification =
  1837. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1838. ihtd.addIndex(indexSpecification);
  1839. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1840. // "c1" }, "idx4");
  1841. // ihtd.addIndex(indexSpecification);
  1842. admin.createTable(ihtd);
  1843. HTable table = new HTable(conf, userTableName);
  1844. rangePutForIdx2(table);
  1845. rangePutForIdx3(table);
  1846. rangePutForIdx4(table);
  1847. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  1848. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  1849. SingleColumnValueFilter scvf =
  1850. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1851. "2".getBytes());
  1852. SingleColumnValueFilter scvf2 =
  1853. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  1854. "4".getBytes());
  1855. SingleColumnValueFilter scvf3 =
  1856. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1857. "1".getBytes());
  1858. filterList1.addFilter(scvf);
  1859. filterList1.addFilter(scvf2);
  1860. filterList1.addFilter(scvf3);
  1861. SingleColumnValueFilter scvfsub =
  1862. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  1863. "2".getBytes());
  1864. SingleColumnValueFilter scvf2sub =
  1865. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1866. "6".getBytes());
  1867. SingleColumnValueFilter scvf3sub =
  1868. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1869. "2".getBytes());
  1870. RowFilter rowFilter =
  1871. new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));
  1872. FilterList filterList2 = new FilterList(Operator.MUST_PASS_ONE);
  1873. filterList2.addFilter(scvfsub);
  1874. filterList2.addFilter(scvf2sub);
  1875. filterList2.addFilter(scvf3sub);
  1876. filterList2.addFilter(rowFilter);
  1877. SingleColumnValueFilter scvfsub1 =
  1878. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  1879. "2".getBytes());
  1880. SingleColumnValueFilter scvf2sub2 =
  1881. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1882. "6".getBytes());
  1883. SingleColumnValueFilter scvf3sub3 =
  1884. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1885. "2".getBytes());
  1886. RowFilter rowFilter2 =
  1887. new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));
  1888. FilterList subFilterList = new FilterList(Operator.MUST_PASS_ONE);
  1889. subFilterList.addFilter(scvfsub1);
  1890. subFilterList.addFilter(scvf2sub2);
  1891. subFilterList.addFilter(scvf3sub3);
  1892. subFilterList.addFilter(rowFilter2);
  1893. filterList1.addFilter(subFilterList);
  1894. masterFilter.addFilter(filterList1);
  1895. masterFilter.addFilter(filterList2);
  1896. Scan scan = new Scan();
  1897. scan.setFilter(masterFilter);
  1898. int i = 0;
  1899. ResultScanner scanner = table.getScanner(scan);
  1900. List<Result> testRes = new ArrayList<Result>();
  1901. Result[] result = scanner.next(1);
  1902. while (result != null && result.length > 0) {
  1903. testRes.add(result[0]);
  1904. i++;
  1905. result = scanner.next(1);
  1906. }
  1907. System.out.println("************* result count......*********** " + testRes.size()
  1908. + " ###### " + testRes);
  1909. Assert.assertFalse("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1910. }
  1911. @Test(timeout = 180000)
  1912. public void testORBranchesInWhichOneBranchHavingOtherFiltersThanSCVF() throws IOException,
  1913. KeeperException, InterruptedException {
  1914. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1915. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1916. Configuration conf = UTIL.getConfiguration();
  1917. String userTableName = "testORBranchesInWhichOneBranchHavingOtherFiltersThanSCVF";
  1918. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1919. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1920. ihtd.addFamily(hcd);
  1921. IndexSpecification indexSpecification =
  1922. createIndexSpecification(hcd, ValueType.String, 10,
  1923. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  1924. ihtd.addIndex(indexSpecification);
  1925. indexSpecification =
  1926. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  1927. ihtd.addIndex(indexSpecification);
  1928. indexSpecification =
  1929. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  1930. ihtd.addIndex(indexSpecification);
  1931. indexSpecification =
  1932. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  1933. ihtd.addIndex(indexSpecification);
  1934. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  1935. // "c1" }, "idx4");
  1936. // ihtd.addIndex(indexSpecification);
  1937. admin.createTable(ihtd);
  1938. HTable table = new HTable(conf, userTableName);
  1939. rangePutForIdx2(table);
  1940. rangePutForIdx3(table);
  1941. rangePutForIdx4(table);
  1942. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  1943. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  1944. SingleColumnValueFilter scvf =
  1945. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1946. "2".getBytes());
  1947. SingleColumnValueFilter scvf2 =
  1948. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  1949. "4".getBytes());
  1950. SingleColumnValueFilter scvf3 =
  1951. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  1952. "1".getBytes());
  1953. filterList1.addFilter(scvf);
  1954. filterList1.addFilter(scvf2);
  1955. filterList1.addFilter(scvf3);
  1956. SingleColumnValueFilter scvfsub =
  1957. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  1958. "2".getBytes());
  1959. SingleColumnValueFilter scvf2sub =
  1960. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  1961. "6".getBytes());
  1962. SingleColumnValueFilter scvf3sub =
  1963. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  1964. "2".getBytes());
  1965. RowFilter rowFilter =
  1966. new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));
  1967. FilterList filterList2 = new FilterList(Operator.MUST_PASS_ONE);
  1968. filterList2.addFilter(scvfsub);
  1969. filterList2.addFilter(scvf2sub);
  1970. filterList2.addFilter(scvf3sub);
  1971. filterList2.addFilter(rowFilter);
  1972. masterFilter.addFilter(filterList1);
  1973. masterFilter.addFilter(filterList2);
  1974. Scan scan = new Scan();
  1975. scan.setFilter(masterFilter);
  1976. int i = 0;
  1977. ResultScanner scanner = table.getScanner(scan);
  1978. List<Result> testRes = new ArrayList<Result>();
  1979. Result[] result = scanner.next(1);
  1980. while (result != null && result.length > 0) {
  1981. testRes.add(result[0]);
  1982. i++;
  1983. result = scanner.next(1);
  1984. }
  1985. System.out.println("************* result count......*********** " + testRes.size()
  1986. + " ###### " + testRes);
  1987. Assert.assertFalse("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  1988. }
  1989. @Test(timeout = 180000)
  1990. public void testANDhavingORbranchWithOtherFilterThanSCVF() throws IOException, KeeperException,
  1991. InterruptedException {
  1992. HBaseAdmin admin = UTIL.getHBaseAdmin();
  1993. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  1994. Configuration conf = UTIL.getConfiguration();
  1995. String userTableName = "testANDhavingORbranchWithOtherFilterThanSCVF";
  1996. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  1997. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  1998. ihtd.addFamily(hcd);
  1999. IndexSpecification indexSpecification =
  2000. createIndexSpecification(hcd, ValueType.String, 10,
  2001. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  2002. ihtd.addIndex(indexSpecification);
  2003. indexSpecification =
  2004. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  2005. ihtd.addIndex(indexSpecification);
  2006. indexSpecification =
  2007. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  2008. ihtd.addIndex(indexSpecification);
  2009. indexSpecification =
  2010. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  2011. ihtd.addIndex(indexSpecification);
  2012. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  2013. // "c1" }, "idx4");
  2014. // ihtd.addIndex(indexSpecification);
  2015. admin.createTable(ihtd);
  2016. HTable table = new HTable(conf, userTableName);
  2017. rangePutForIdx2(table);
  2018. rangePutForIdx3(table);
  2019. rangePutForIdx4(table);
  2020. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2021. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  2022. SingleColumnValueFilter scvf =
  2023. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  2024. "2".getBytes());
  2025. SingleColumnValueFilter scvf2 =
  2026. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  2027. "4".getBytes());
  2028. SingleColumnValueFilter scvf3 =
  2029. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  2030. "1".getBytes());
  2031. filterList1.addFilter(scvf);
  2032. filterList1.addFilter(scvf2);
  2033. filterList1.addFilter(scvf3);
  2034. SingleColumnValueFilter scvfsub =
  2035. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  2036. "2".getBytes());
  2037. SingleColumnValueFilter scvf2sub =
  2038. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  2039. "6".getBytes());
  2040. SingleColumnValueFilter scvf3sub =
  2041. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  2042. "2".getBytes());
  2043. RowFilter rowFilter =
  2044. new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row1")));
  2045. FilterList filterList2 = new FilterList(Operator.MUST_PASS_ONE);
  2046. filterList2.addFilter(scvfsub);
  2047. filterList2.addFilter(scvf2sub);
  2048. filterList2.addFilter(scvf3sub);
  2049. filterList2.addFilter(rowFilter);
  2050. masterFilter.addFilter(filterList1);
  2051. masterFilter.addFilter(filterList2);
  2052. Scan scan = new Scan();
  2053. scan.setFilter(masterFilter);
  2054. int i = 0;
  2055. ResultScanner scanner = table.getScanner(scan);
  2056. List<Result> testRes = new ArrayList<Result>();
  2057. Result[] result = scanner.next(1);
  2058. while (result != null && result.length > 0) {
  2059. testRes.add(result[0]);
  2060. i++;
  2061. result = scanner.next(1);
  2062. }
  2063. System.out.println("************* result count......*********** " + testRes.size()
  2064. + " ###### " + testRes);
  2065. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  2066. Assert.assertTrue("Seekpoints should get added by index scanner",
  2067. IndexRegionObserver.getSeekpointAdded());
  2068. Assert.assertEquals("It should get two seek points from index scanner.", 6, IndexRegionObserver
  2069. .getMultipleSeekPoints().size());
  2070. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 6);
  2071. }
  2072. @Test(timeout = 180000)
  2073. public void testIfAllScannersAreRangeInAllLevelsPart2() throws IOException, KeeperException,
  2074. InterruptedException {
  2075. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2076. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2077. Configuration conf = UTIL.getConfiguration();
  2078. String userTableName = "testIfAllScannersAreRangeInAllLevelsPart2";
  2079. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2080. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2081. ihtd.addFamily(hcd);
  2082. IndexSpecification indexSpecification =
  2083. createIndexSpecification(hcd, ValueType.String, 10,
  2084. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  2085. ihtd.addIndex(indexSpecification);
  2086. indexSpecification =
  2087. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  2088. ihtd.addIndex(indexSpecification);
  2089. indexSpecification =
  2090. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  2091. ihtd.addIndex(indexSpecification);
  2092. indexSpecification =
  2093. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  2094. ihtd.addIndex(indexSpecification);
  2095. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  2096. // "c1" }, "idx4");
  2097. // ihtd.addIndex(indexSpecification);
  2098. admin.createTable(ihtd);
  2099. HTable table = new HTable(conf, userTableName);
  2100. rangePutForIdx2(table);
  2101. rangePutForIdx3(table);
  2102. rangePutForIdx4(table);
  2103. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2104. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  2105. SingleColumnValueFilter scvf =
  2106. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  2107. "2".getBytes());
  2108. SingleColumnValueFilter scvf2 =
  2109. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  2110. "4".getBytes());
  2111. SingleColumnValueFilter scvf3 =
  2112. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  2113. "2".getBytes());
  2114. filterList1.addFilter(scvf);
  2115. filterList1.addFilter(scvf2);
  2116. filterList1.addFilter(scvf3);
  2117. SingleColumnValueFilter scvfsub =
  2118. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  2119. "1".getBytes());
  2120. SingleColumnValueFilter scvf2sub =
  2121. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  2122. "6".getBytes());
  2123. SingleColumnValueFilter scvf3sub =
  2124. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  2125. "4".getBytes());
  2126. masterFilter.addFilter(scvfsub);
  2127. masterFilter.addFilter(scvf2sub);
  2128. masterFilter.addFilter(scvf3sub);
  2129. masterFilter.addFilter(filterList1);
  2130. Scan scan = new Scan();
  2131. scan.setFilter(masterFilter);
  2132. int i = 0;
  2133. ResultScanner scanner = table.getScanner(scan);
  2134. List<Result> testRes = new ArrayList<Result>();
  2135. Result[] result = scanner.next(1);
  2136. while (result != null && result.length > 0) {
  2137. testRes.add(result[0]);
  2138. i++;
  2139. result = scanner.next(1);
  2140. }
  2141. System.out.println("************* result count......*********** " + testRes.size()
  2142. + " ###### " + testRes);
  2143. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  2144. Assert.assertTrue("Seekpoints should get added by index scanner",
  2145. IndexRegionObserver.getSeekpointAdded());
  2146. Assert.assertEquals("It should get two seek points from index scanner.", 1, IndexRegionObserver
  2147. .getMultipleSeekPoints().size());
  2148. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 1);
  2149. }
  2150. @Test(timeout = 180000)
  2151. public void testIfAllScannersAreRangeInAllLevelsPart3() throws IOException, KeeperException,
  2152. InterruptedException {
  2153. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2154. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2155. Configuration conf = UTIL.getConfiguration();
  2156. String userTableName = "testIfAllScannersAreRangeInAllLevelsPart3";
  2157. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2158. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2159. ihtd.addFamily(hcd);
  2160. IndexSpecification indexSpecification =
  2161. createIndexSpecification(hcd, ValueType.String, 10,
  2162. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  2163. ihtd.addIndex(indexSpecification);
  2164. indexSpecification =
  2165. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  2166. ihtd.addIndex(indexSpecification);
  2167. indexSpecification =
  2168. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  2169. ihtd.addIndex(indexSpecification);
  2170. indexSpecification =
  2171. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  2172. ihtd.addIndex(indexSpecification);
  2173. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  2174. // "c1" }, "idx4");
  2175. // ihtd.addIndex(indexSpecification);
  2176. admin.createTable(ihtd);
  2177. HTable table = new HTable(conf, userTableName);
  2178. rangePutForIdx2(table);
  2179. rangePutForIdx3(table);
  2180. rangePutForIdx4(table);
  2181. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2182. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ALL);
  2183. SingleColumnValueFilter scvf =
  2184. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  2185. "2".getBytes());
  2186. SingleColumnValueFilter scvf2 =
  2187. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  2188. "4".getBytes());
  2189. SingleColumnValueFilter scvf3 =
  2190. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  2191. "2".getBytes());
  2192. filterList1.addFilter(scvf);
  2193. filterList1.addFilter(scvf2);
  2194. filterList1.addFilter(scvf3);
  2195. SingleColumnValueFilter scvfsub =
  2196. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  2197. "1".getBytes());
  2198. SingleColumnValueFilter scvf2sub =
  2199. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.LESS_OR_EQUAL,
  2200. "6".getBytes());
  2201. SingleColumnValueFilter scvf3sub =
  2202. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  2203. "2".getBytes());
  2204. masterFilter.addFilter(scvfsub);
  2205. masterFilter.addFilter(scvf2sub);
  2206. masterFilter.addFilter(scvf3sub);
  2207. masterFilter.addFilter(filterList1);
  2208. Scan scan = new Scan();
  2209. scan.setFilter(masterFilter);
  2210. int i = 0;
  2211. ResultScanner scanner = table.getScanner(scan);
  2212. List<Result> testRes = new ArrayList<Result>();
  2213. Result[] result = scanner.next(1);
  2214. while (result != null && result.length > 0) {
  2215. testRes.add(result[0]);
  2216. i++;
  2217. result = scanner.next(1);
  2218. }
  2219. System.out.println("************* result count......*********** " + testRes.size()
  2220. + " ###### " + testRes);
  2221. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  2222. Assert.assertTrue("Seekpoints should get added by index scanner",
  2223. IndexRegionObserver.getSeekpointAdded());
  2224. Assert.assertEquals("It should get two seek points from index scanner.", 1, IndexRegionObserver
  2225. .getMultipleSeekPoints().size());
  2226. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 1);
  2227. }
  2228. @Test(timeout = 180000)
  2229. public void testOREvaluationFromMultipleLevels() throws IOException, KeeperException,
  2230. InterruptedException {
  2231. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2232. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2233. Configuration conf = UTIL.getConfiguration();
  2234. String userTableName = "testOREvaluationFromMultipleLevels";
  2235. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2236. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2237. ihtd.addFamily(hcd);
  2238. IndexSpecification indexSpecification =
  2239. createIndexSpecification(hcd, ValueType.String, 10,
  2240. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  2241. ihtd.addIndex(indexSpecification);
  2242. indexSpecification =
  2243. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  2244. ihtd.addIndex(indexSpecification);
  2245. indexSpecification =
  2246. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  2247. ihtd.addIndex(indexSpecification);
  2248. indexSpecification =
  2249. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  2250. ihtd.addIndex(indexSpecification);
  2251. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  2252. // "c1" }, "idx4");
  2253. // ihtd.addIndex(indexSpecification);
  2254. admin.createTable(ihtd);
  2255. HTable table = new HTable(conf, userTableName);
  2256. rangePutForIdx2(table);
  2257. rangePutForIdx3(table);
  2258. rangePutForIdx4(table);
  2259. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  2260. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  2261. SingleColumnValueFilter scvf =
  2262. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  2263. "1".getBytes());
  2264. SingleColumnValueFilter scvf2 =
  2265. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.GREATER_OR_EQUAL,
  2266. "5".getBytes());
  2267. SingleColumnValueFilter scvf3 =
  2268. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  2269. "4".getBytes());
  2270. filterList1.addFilter(scvf);
  2271. filterList1.addFilter(scvf2);
  2272. filterList1.addFilter(scvf3);
  2273. SingleColumnValueFilter scvfsub =
  2274. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  2275. "2".getBytes());
  2276. SingleColumnValueFilter scvf2sub =
  2277. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.GREATER_OR_EQUAL,
  2278. "6".getBytes());
  2279. SingleColumnValueFilter scvf3sub =
  2280. new SingleColumnValueFilter("cf1".getBytes(), "c3".getBytes(), CompareOp.EQUAL,
  2281. "3".getBytes());
  2282. masterFilter.addFilter(scvfsub);
  2283. masterFilter.addFilter(scvf2sub);
  2284. masterFilter.addFilter(scvf3sub);
  2285. masterFilter.addFilter(filterList1);
  2286. Scan scan = new Scan();
  2287. scan.setFilter(masterFilter);
  2288. int i = 0;
  2289. ResultScanner scanner = table.getScanner(scan);
  2290. List<Result> testRes = new ArrayList<Result>();
  2291. Result[] result = scanner.next(1);
  2292. while (result != null && result.length > 0) {
  2293. testRes.add(result[0]);
  2294. i++;
  2295. result = scanner.next(1);
  2296. }
  2297. System.out.println("************* result count......*********** " + testRes.size()
  2298. + " ###### " + testRes);
  2299. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  2300. Assert.assertTrue("Seekpoints should get added by index scanner",
  2301. IndexRegionObserver.getSeekpointAdded());
  2302. Assert.assertEquals("It should get two seek points from index scanner.", 6, IndexRegionObserver
  2303. .getMultipleSeekPoints().size());
  2304. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 6);
  2305. }
  2306. private void rangePutForIdx2(HTable table) throws IOException {
  2307. Put p = new Put(Bytes.toBytes("row0"));
  2308. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("1"));
  2309. table.put(p);
  2310. p = new Put(Bytes.toBytes("row9"));
  2311. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("2"));
  2312. table.put(p);
  2313. p = new Put(Bytes.toBytes("row1"));
  2314. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("3"));
  2315. table.put(p);
  2316. p = new Put(Bytes.toBytes("row3"));
  2317. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("4"));
  2318. table.put(p);
  2319. p = new Put(Bytes.toBytes("row7"));
  2320. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("5"));
  2321. table.put(p);
  2322. p = new Put(Bytes.toBytes("row15"));
  2323. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("6"));
  2324. table.put(p);
  2325. }
  2326. private void rangePutForIdx3(HTable table) throws IOException {
  2327. Put p = new Put(Bytes.toBytes("row0"));
  2328. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("1"));
  2329. table.put(p);
  2330. p = new Put(Bytes.toBytes("row9"));
  2331. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("2"));
  2332. table.put(p);
  2333. p = new Put(Bytes.toBytes("row1"));
  2334. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("3"));
  2335. table.put(p);
  2336. p = new Put(Bytes.toBytes("row3"));
  2337. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("4"));
  2338. table.put(p);
  2339. p = new Put(Bytes.toBytes("row7"));
  2340. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("5"));
  2341. table.put(p);
  2342. p = new Put(Bytes.toBytes("row15"));
  2343. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("6"));
  2344. table.put(p);
  2345. }
  2346. private void rangePutForIdx4(HTable table) throws IOException {
  2347. Put p = new Put(Bytes.toBytes("row0"));
  2348. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), Bytes.toBytes("1"));
  2349. table.put(p);
  2350. p = new Put(Bytes.toBytes("row9"));
  2351. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), Bytes.toBytes("2"));
  2352. table.put(p);
  2353. p = new Put(Bytes.toBytes("row1"));
  2354. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), Bytes.toBytes("3"));
  2355. table.put(p);
  2356. p = new Put(Bytes.toBytes("row3"));
  2357. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), Bytes.toBytes("4"));
  2358. table.put(p);
  2359. p = new Put(Bytes.toBytes("row7"));
  2360. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), Bytes.toBytes("5"));
  2361. table.put(p);
  2362. p = new Put(Bytes.toBytes("row15"));
  2363. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c3"), Bytes.toBytes("6"));
  2364. table.put(p);
  2365. }
  2366. @Test(timeout = 180000)
  2367. public void testCombinationOfLESSorGREATERwithEQUAL() throws Exception {
  2368. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2369. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2370. Configuration conf = UTIL.getConfiguration();
  2371. String userTableName = "testCombinationOfLESSorGREATERwithEQUAL";
  2372. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2373. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2374. ihtd.addFamily(hcd);
  2375. IndexSpecification indexSpecification =
  2376. createIndexSpecification(hcd, ValueType.String, 10,
  2377. new String[] { "c3", "c4", "c5", "c6" }, "idx1");
  2378. ihtd.addIndex(indexSpecification);
  2379. indexSpecification =
  2380. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  2381. ihtd.addIndex(indexSpecification);
  2382. indexSpecification =
  2383. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  2384. ihtd.addIndex(indexSpecification);
  2385. indexSpecification =
  2386. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c3" }, "idx4");
  2387. ihtd.addIndex(indexSpecification);
  2388. // indexSpecification = createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2",
  2389. // "c1" }, "idx4");
  2390. // ihtd.addIndex(indexSpecification);
  2391. admin.createTable(ihtd);
  2392. HTable table = new HTable(conf, userTableName);
  2393. rangePutForIdx2(table);
  2394. rangePutForIdx3(table);
  2395. rangePutForIdx4(table);
  2396. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  2397. FilterList filterList1 = new FilterList(Operator.MUST_PASS_ONE);
  2398. SingleColumnValueFilter scvf =
  2399. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS,
  2400. "2".getBytes());
  2401. SingleColumnValueFilter scvf3 =
  2402. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2403. "2".getBytes());
  2404. SingleColumnValueFilter scvf2 =
  2405. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  2406. "5".getBytes());
  2407. SingleColumnValueFilter scvf4 =
  2408. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2409. "5".getBytes());
  2410. filterList1.addFilter(scvf);
  2411. filterList1.addFilter(scvf2);
  2412. filterList1.addFilter(scvf3);
  2413. filterList1.addFilter(scvf4);
  2414. masterFilter.addFilter(filterList1);
  2415. Scan scan = new Scan();
  2416. scan.setFilter(masterFilter);
  2417. int i = 0;
  2418. ResultScanner scanner = table.getScanner(scan);
  2419. List<Result> testRes = new ArrayList<Result>();
  2420. Result[] result = scanner.next(1);
  2421. while (result != null && result.length > 0) {
  2422. testRes.add(result[0]);
  2423. i++;
  2424. result = scanner.next(1);
  2425. }
  2426. System.out.println("************* result count......*********** " + testRes.size()
  2427. + " ###### " + testRes);
  2428. Assert.assertTrue("Index flow should get used.", IndexRegionObserver.getIndexedFlowUsed());
  2429. Assert.assertTrue("Seekpoints should get added by index scanner",
  2430. IndexRegionObserver.getSeekpointAdded());
  2431. Assert.assertEquals("It should get two seek points from index scanner.", 4, IndexRegionObserver
  2432. .getMultipleSeekPoints().size());
  2433. Assert.assertTrue("Overall result should have only 2 rows", testRes.size() == 4);
  2434. }
  2435. @Test(timeout = 180000)
  2436. public void testIndexScanWithCaching() throws Exception {
  2437. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2438. ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2439. Configuration conf = UTIL.getConfiguration();
  2440. String userTableName = "testIndexScanWithCaching";
  2441. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2442. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2443. ihtd.addFamily(hcd);
  2444. IndexSpecification indexSpecification =
  2445. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx2");
  2446. ihtd.addIndex(indexSpecification);
  2447. indexSpecification =
  2448. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c2" }, "idx3");
  2449. ihtd.addIndex(indexSpecification);
  2450. SingleColumnValueFilter filter =
  2451. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2452. "apple".getBytes());
  2453. SingleColumnValueFilter filter1 =
  2454. new SingleColumnValueFilter("cf1".getBytes(), "c2".getBytes(), CompareOp.EQUAL,
  2455. "bat".getBytes());
  2456. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ONE);
  2457. masterFilter.addFilter(filter1);
  2458. masterFilter.addFilter(filter);
  2459. admin.createTable(ihtd);
  2460. ZKAssign.blockUntilNoRIT(zkw);
  2461. HTable table = new HTable(conf, "testIndexScanWithCaching");
  2462. putforIDX2(Bytes.toBytes("row1"), table);
  2463. putforIDX3(Bytes.toBytes("row1"), table);
  2464. putforIDX2(Bytes.toBytes("row2"), table);
  2465. putforIDX2(Bytes.toBytes("row3"), table);
  2466. putforIDX3(Bytes.toBytes("row4"), table);
  2467. putforIDX3(Bytes.toBytes("row5"), table);
  2468. putforIDX2(Bytes.toBytes("row5"), table);
  2469. putforIDX2(Bytes.toBytes("row6"), table);
  2470. putforIDX3(Bytes.toBytes("row7"), table);
  2471. Scan scan = new Scan();
  2472. scan.setFilter(masterFilter);
  2473. scan.setCaching(10);
  2474. int i = 0;
  2475. ResultScanner scanner = table.getScanner(scan);
  2476. int nextCalled = 0;
  2477. List<Result> testRes = new ArrayList<Result>();
  2478. Result[] result = scanner.next(10);
  2479. nextCalled++;
  2480. while (result != null && result.length > 1) {
  2481. for (int j = 0; j < result.length; j++) {
  2482. testRes.add(result[j]);
  2483. }
  2484. i++;
  2485. result = scanner.next(10);
  2486. nextCalled++;
  2487. }
  2488. Assert.assertTrue("Index flow should be used.", IndexRegionObserver.getIndexedFlowUsed());
  2489. Assert.assertTrue("Index should fetch some seek points.",
  2490. IndexRegionObserver.getSeekpointAdded());
  2491. Assert.assertEquals("Index should fetch 7 seek points", 7, IndexRegionObserver
  2492. .getMultipleSeekPoints().size());
  2493. Assert.assertEquals("Final result should have 7 rows.", 7, testRes.size());
  2494. Assert.assertEquals("All rows should be fetched in single next call.", 2, nextCalled);
  2495. }
  2496. @Test(timeout = 180000)
  2497. public void testShouldRetrieveNegtiveIntValueWithEqualCondition() throws Exception {
  2498. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2499. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2500. Configuration conf = UTIL.getConfiguration();
  2501. String userTableName = "testOtherDataTypes";
  2502. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2503. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2504. ihtd.addFamily(hcd);
  2505. IndexSpecification indexSpecification =
  2506. createIndexSpecification(hcd, ValueType.Int, 10, new String[] { "c1" }, "idx1");
  2507. ihtd.addIndex(indexSpecification);
  2508. admin.createTable(ihtd);
  2509. HTable table = new HTable(conf, userTableName);
  2510. rangePutForIdx2WithInteger(table);
  2511. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2512. SingleColumnValueFilter scvf =
  2513. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2514. Bytes.toBytes(-4));
  2515. masterFilter.addFilter(scvf);
  2516. Scan scan = new Scan();
  2517. scan.setFilter(masterFilter);
  2518. ResultScanner scanner = table.getScanner(scan);
  2519. List<Result> testRes = new ArrayList<Result>();
  2520. Result[] result = scanner.next(1);
  2521. while (result != null && result.length > 0) {
  2522. testRes.add(result[0]);
  2523. result = scanner.next(1);
  2524. }
  2525. assertTrue(testRes.size() == 1);
  2526. assertTrue(testRes.toString().contains("row3"));
  2527. }
  2528. @Test(timeout = 180000)
  2529. public void testShouldRetrieveNegativeIntValueWithLessCondition() throws Exception {
  2530. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2531. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2532. Configuration conf = UTIL.getConfiguration();
  2533. String userTableName = "testShouldRetrieveNegativeIntValueWithLessCondition";
  2534. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2535. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2536. ihtd.addFamily(hcd);
  2537. IndexSpecification indexSpecification =
  2538. createIndexSpecification(hcd, ValueType.Int, 10, new String[] { "c1" }, "idx1");
  2539. ihtd.addIndex(indexSpecification);
  2540. admin.createTable(ihtd);
  2541. HTable table = new HTable(conf, userTableName);
  2542. rangePutForIdx2WithInteger(table);
  2543. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2544. SingleColumnValueFilter scvf =
  2545. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS,
  2546. (Bytes.toBytes(-4)));
  2547. masterFilter.addFilter(scvf);
  2548. Scan scan = new Scan();
  2549. scan.setFilter(masterFilter);
  2550. ResultScanner scanner = table.getScanner(scan);
  2551. List<Result> testRes = new ArrayList<Result>();
  2552. Result[] result = scanner.next(1);
  2553. while (result != null && result.length > 0) {
  2554. testRes.add(result[0]);
  2555. result = scanner.next(1);
  2556. }
  2557. assertTrue(testRes.size() == 2);
  2558. }
  2559. @Test(timeout = 180000)
  2560. public void testShouldRetriveNegativeIntValueWithGreaterCondition() throws Exception {
  2561. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2562. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2563. Configuration conf = UTIL.getConfiguration();
  2564. String userTableName = "testShouldRetriveNegativeIntValueWithGreaterCondition";
  2565. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2566. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2567. ihtd.addFamily(hcd);
  2568. IndexSpecification indexSpecification =
  2569. createIndexSpecification(hcd, ValueType.Int, 10, new String[] { "c1" }, "idx1");
  2570. ihtd.addIndex(indexSpecification);
  2571. admin.createTable(ihtd);
  2572. HTable table = new HTable(conf, userTableName);
  2573. rangePutForIdx2WithInteger(table);
  2574. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2575. SingleColumnValueFilter scvf =
  2576. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  2577. new IntComparator(Bytes.toBytes(-6)));
  2578. masterFilter.addFilter(scvf);
  2579. Scan scan = new Scan();
  2580. scan.setFilter(masterFilter);
  2581. ResultScanner scanner = table.getScanner(scan);
  2582. List<Result> testRes = new ArrayList<Result>();
  2583. Result[] result = scanner.next(1);
  2584. while (result != null && result.length > 0) {
  2585. testRes.add(result[0]);
  2586. result = scanner.next(1);
  2587. }
  2588. assertTrue(testRes.size() == 5);
  2589. }
  2590. @Test(timeout = 180000)
  2591. public void testShouldRetrieveNegativeIntValue() throws Exception {
  2592. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2593. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2594. Configuration conf = UTIL.getConfiguration();
  2595. String userTableName = "testShouldRetrieveNegativeIntValue";
  2596. HTableDescriptor ihtd = new HTableDescriptor(userTableName);
  2597. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2598. ihtd.addFamily(hcd);
  2599. admin.createTable(ihtd);
  2600. HTable table = new HTable(conf, userTableName);
  2601. rangePutForIdx2WithInteger(table);
  2602. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2603. SingleColumnValueFilter scvf =
  2604. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  2605. new IntComparator(Bytes.toBytes(-6)));
  2606. masterFilter.addFilter(scvf);
  2607. Scan scan = new Scan();
  2608. scan.setFilter(masterFilter);
  2609. ResultScanner scanner = table.getScanner(scan);
  2610. List<Result> testRes = new ArrayList<Result>();
  2611. Result[] result = scanner.next(1);
  2612. while (result != null && result.length > 0) {
  2613. testRes.add(result[0]);
  2614. result = scanner.next(1);
  2615. }
  2616. assertTrue(testRes.size() == 5);
  2617. }
  2618. @Test(timeout = 180000)
  2619. public void testShouldRetrieveNegativeFloatValueWithGreaterCondition() throws Exception {
  2620. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2621. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2622. Configuration conf = UTIL.getConfiguration();
  2623. String userTableName = "testShouldRetrieveNegativeFloatValueWithGreaterCondition";
  2624. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2625. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2626. ihtd.addFamily(hcd);
  2627. IndexSpecification indexSpecification =
  2628. createIndexSpecification(hcd, ValueType.Float, 10, new String[] { "c1" }, "idx1");
  2629. ihtd.addIndex(indexSpecification);
  2630. admin.createTable(ihtd);
  2631. HTable table = new HTable(conf, userTableName);
  2632. rangePutForIdx2WithFloat(table);
  2633. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2634. SingleColumnValueFilter scvf =
  2635. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER,
  2636. new FloatComparator(Bytes.toBytes(-5f)));
  2637. masterFilter.addFilter(scvf);
  2638. Scan scan = new Scan();
  2639. scan.setFilter(masterFilter);
  2640. ResultScanner scanner = table.getScanner(scan);
  2641. List<Result> testRes = new ArrayList<Result>();
  2642. Result[] result = scanner.next(1);
  2643. while (result != null && result.length > 0) {
  2644. testRes.add(result[0]);
  2645. result = scanner.next(1);
  2646. }
  2647. assertTrue(testRes.size() == 4);
  2648. }
  2649. @Test(timeout = 180000)
  2650. public void testShouldRetrieveNegativeFloatValueWithLessCondition() throws Exception {
  2651. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2652. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2653. Configuration conf = UTIL.getConfiguration();
  2654. String userTableName = "testShouldRetrieveNegativeFloatValueWithLessCondition";
  2655. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2656. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2657. ihtd.addFamily(hcd);
  2658. IndexSpecification indexSpecification =
  2659. createIndexSpecification(hcd, ValueType.Float, 10, new String[] { "c1" }, "idx1");
  2660. ihtd.addIndex(indexSpecification);
  2661. admin.createTable(ihtd);
  2662. HTable table = new HTable(conf, userTableName);
  2663. rangePutForIdx2WithFloat(table);
  2664. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2665. SingleColumnValueFilter scvf =
  2666. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS,
  2667. new FloatComparator(Bytes.toBytes(-5f)));
  2668. masterFilter.addFilter(scvf);
  2669. Scan scan = new Scan();
  2670. scan.setFilter(masterFilter);
  2671. ResultScanner scanner = table.getScanner(scan);
  2672. List<Result> testRes = new ArrayList<Result>();
  2673. Result[] result = scanner.next(1);
  2674. while (result != null && result.length > 0) {
  2675. testRes.add(result[0]);
  2676. result = scanner.next(1);
  2677. }
  2678. assertTrue(testRes.size() == 2);
  2679. }
  2680. @Test(timeout = 180000)
  2681. public void testShouldRetrieveNegativeFloatValueWithEqualsCondition() throws Exception {
  2682. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2683. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2684. Configuration conf = UTIL.getConfiguration();
  2685. String userTableName = "testShouldRetrieveNegativeFloatValueWithEqualsCondition";
  2686. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2687. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2688. ihtd.addFamily(hcd);
  2689. IndexSpecification indexSpecification =
  2690. createIndexSpecification(hcd, ValueType.Float, 10, new String[] { "c1" }, "idx1");
  2691. ihtd.addIndex(indexSpecification);
  2692. admin.createTable(ihtd);
  2693. HTable table = new HTable(conf, userTableName);
  2694. rangePutForIdx2WithFloat(table);
  2695. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2696. SingleColumnValueFilter scvf =
  2697. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2698. new FloatComparator(Bytes.toBytes(-5.3f)));
  2699. masterFilter.addFilter(scvf);
  2700. Scan scan = new Scan();
  2701. scan.setFilter(masterFilter);
  2702. ResultScanner scanner = table.getScanner(scan);
  2703. List<Result> testRes = new ArrayList<Result>();
  2704. Result[] result = scanner.next(1);
  2705. while (result != null && result.length > 0) {
  2706. testRes.add(result[0]);
  2707. result = scanner.next(1);
  2708. }
  2709. assertTrue(testRes.size() == 1);
  2710. }
  2711. @Test(timeout = 180000)
  2712. public void testShouldRetrieveNegativeDoubleValueWithLesserThanEqualsCondition() throws Exception {
  2713. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2714. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2715. Configuration conf = UTIL.getConfiguration();
  2716. String userTableName = "testShouldRetrieveNegativeDoubleValueWithLesserThanEqualsCondition";
  2717. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2718. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2719. ihtd.addFamily(hcd);
  2720. IndexSpecification indexSpecification =
  2721. createIndexSpecification(hcd, ValueType.Double, 10, new String[] { "c1" }, "idx1");
  2722. ihtd.addIndex(indexSpecification);
  2723. admin.createTable(ihtd);
  2724. HTable table = new HTable(conf, userTableName);
  2725. rangePutForIdx2WithDouble(table);
  2726. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2727. SingleColumnValueFilter scvf =
  2728. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL,
  2729. new DoubleComparator(Bytes.toBytes(-5.3d)));
  2730. masterFilter.addFilter(scvf);
  2731. Scan scan = new Scan();
  2732. scan.setFilter(masterFilter);
  2733. ResultScanner scanner = table.getScanner(scan);
  2734. List<Result> testRes = new ArrayList<Result>();
  2735. Result[] result = scanner.next(1);
  2736. while (result != null && result.length > 0) {
  2737. testRes.add(result[0]);
  2738. result = scanner.next(1);
  2739. }
  2740. assertEquals(testRes.size(), 2);
  2741. }
  2742. @Test(timeout = 180000)
  2743. public void testShouldRetrieveNegativeDoubleValueWithGreaterThanEqualsCondition()
  2744. throws Exception {
  2745. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2746. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2747. Configuration conf = UTIL.getConfiguration();
  2748. String userTableName = "testShouldRetrieveNegativeDoubleValueWithGreaterThanEqualsCondition";
  2749. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2750. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2751. ihtd.addFamily(hcd);
  2752. IndexSpecification indexSpecification =
  2753. createIndexSpecification(hcd, ValueType.Double, 10, new String[] { "c1" }, "idx1");
  2754. ihtd.addIndex(indexSpecification);
  2755. admin.createTable(ihtd);
  2756. HTable table = new HTable(conf, userTableName);
  2757. rangePutForIdx2WithDouble(table);
  2758. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2759. SingleColumnValueFilter scvf =
  2760. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER_OR_EQUAL,
  2761. new DoubleComparator(Bytes.toBytes(-5.3d)));
  2762. masterFilter.addFilter(scvf);
  2763. Scan scan = new Scan();
  2764. scan.setFilter(masterFilter);
  2765. ResultScanner scanner = table.getScanner(scan);
  2766. List<Result> testRes = new ArrayList<Result>();
  2767. Result[] result = scanner.next(1);
  2768. while (result != null && result.length > 0) {
  2769. testRes.add(result[0]);
  2770. result = scanner.next(1);
  2771. }
  2772. assertTrue(testRes.size() == 5);
  2773. }
  2774. @Test(timeout = 180000)
  2775. public void testCachingWithValuesDistributedAmongMulitpleRegions() throws Exception {
  2776. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2777. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2778. Configuration conf = UTIL.getConfiguration();
  2779. String userTableName = "testCachingWithValuesDistributedAmongMulitpleRegions";
  2780. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2781. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2782. ihtd.addFamily(hcd);
  2783. IndexSpecification indexSpecification =
  2784. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx1");
  2785. ihtd.addIndex(indexSpecification);
  2786. byte[][] split =
  2787. new byte[][] { Bytes.toBytes("row1"), Bytes.toBytes("row2"), Bytes.toBytes("row3"),
  2788. Bytes.toBytes("row4") };
  2789. admin.createTable(ihtd, split);
  2790. HTable table = new HTable(conf, userTableName);
  2791. insert100Rows(table);
  2792. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2793. SingleColumnValueFilter scvf =
  2794. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2795. Bytes.toBytes("5"));
  2796. masterFilter.addFilter(scvf);
  2797. Scan scan = new Scan();
  2798. scan.setCaching(5);
  2799. scan.setFilter(masterFilter);
  2800. ResultScanner scanner = table.getScanner(scan);
  2801. List<Result> testRes = new ArrayList<Result>();
  2802. Result[] result = scanner.next(1);
  2803. int i = 0;
  2804. while (result != null && result.length > 0) {
  2805. System.out.println(Bytes.toString(result[0].getRow()));
  2806. testRes.add(result[0]);
  2807. result = scanner.next(1);
  2808. i++;
  2809. }
  2810. assertEquals(8, i);
  2811. }
  2812. @Test(timeout = 180000)
  2813. public void testCachingWithValuesWhereSomeRegionsDontHaveAnyData() throws Exception {
  2814. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2815. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2816. Configuration conf = UTIL.getConfiguration();
  2817. String userTableName = "testCachingWithValuesWhereSomeRegionsDontHaveAnyData";
  2818. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2819. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2820. ihtd.addFamily(hcd);
  2821. IndexSpecification indexSpecification =
  2822. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx1");
  2823. ihtd.addIndex(indexSpecification);
  2824. byte[][] split =
  2825. new byte[][] { Bytes.toBytes("row1"), Bytes.toBytes("row3"), Bytes.toBytes("row5"),
  2826. Bytes.toBytes("row7") };
  2827. admin.createTable(ihtd, split);
  2828. HTable table = new HTable(conf, userTableName);
  2829. for (int i = 0; i < 10; i++) {
  2830. if (i > 4 && i < 8) {
  2831. continue;
  2832. }
  2833. Put p = new Put(Bytes.toBytes("row" + i));
  2834. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("5"));
  2835. table.put(p);
  2836. }
  2837. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2838. SingleColumnValueFilter scvf =
  2839. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2840. Bytes.toBytes("5"));
  2841. masterFilter.addFilter(scvf);
  2842. Scan scan = new Scan();
  2843. scan.setCaching(5);
  2844. scan.setFilter(masterFilter);
  2845. ResultScanner scanner = table.getScanner(scan);
  2846. List<Result> testRes = new ArrayList<Result>();
  2847. Result[] result = scanner.next(1);
  2848. int i = 0;
  2849. while (result != null && result.length > 0) {
  2850. System.out.println(Bytes.toString(result[0].getRow()));
  2851. testRes.add(result[0]);
  2852. result = scanner.next(1);
  2853. i++;
  2854. }
  2855. assertEquals(7, i);
  2856. }
  2857. @Test(timeout = 180000)
  2858. public void testCachingWithLessNumberOfRowsThanCaching() throws Exception {
  2859. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2860. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2861. Configuration conf = UTIL.getConfiguration();
  2862. String userTableName = "testCachingWithLessNumberOfRowsThanCaching";
  2863. IndexedHTableDescriptor ihtd = new IndexedHTableDescriptor(userTableName);
  2864. HColumnDescriptor hcd = new HColumnDescriptor("cf1");
  2865. ihtd.addFamily(hcd);
  2866. IndexSpecification indexSpecification =
  2867. createIndexSpecification(hcd, ValueType.String, 10, new String[] { "c1" }, "idx1");
  2868. ihtd.addIndex(indexSpecification);
  2869. byte[][] split =
  2870. new byte[][] { Bytes.toBytes("row1"), Bytes.toBytes("row3"), Bytes.toBytes("row5"),
  2871. Bytes.toBytes("row7") };
  2872. admin.createTable(ihtd, split);
  2873. HTable table = new HTable(conf, userTableName);
  2874. for (int i = 0; i < 10; i++) {
  2875. if (i > 4 && i < 8) {
  2876. continue;
  2877. }
  2878. Put p = new Put(Bytes.toBytes("row" + i));
  2879. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("5"));
  2880. table.put(p);
  2881. }
  2882. FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL);
  2883. SingleColumnValueFilter scvf =
  2884. new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL,
  2885. Bytes.toBytes("5"));
  2886. masterFilter.addFilter(scvf);
  2887. Scan scan = new Scan();
  2888. scan.setCaching(10);
  2889. scan.setFilter(masterFilter);
  2890. ResultScanner scanner = table.getScanner(scan);
  2891. List<Result> testRes = new ArrayList<Result>();
  2892. Result[] result = scanner.next(1);
  2893. int i = 0;
  2894. while (result != null && result.length > 0) {
  2895. System.out.println(Bytes.toString(result[0].getRow()));
  2896. testRes.add(result[0]);
  2897. result = scanner.next(1);
  2898. i++;
  2899. }
  2900. assertEquals(7, i);
  2901. }
  2902. private void insert100Rows(HTable table) throws IOException {
  2903. for (int i = 0; i < 8; i++) {
  2904. Put p = new Put(Bytes.toBytes("row" + i));
  2905. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("5"));
  2906. table.put(p);
  2907. }
  2908. }
  2909. private void rangePutForIdx2WithInteger(HTable table) throws IOException {
  2910. Put p = new Put(Bytes.toBytes("row0"));
  2911. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(1));
  2912. table.put(p);
  2913. p = new Put(Bytes.toBytes("row1"));
  2914. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(2));
  2915. table.put(p);
  2916. p = new Put(Bytes.toBytes("row2"));
  2917. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(3));
  2918. table.put(p);
  2919. p = new Put(Bytes.toBytes("row3"));
  2920. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-4));
  2921. table.put(p);
  2922. p = new Put(Bytes.toBytes("row4"));
  2923. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-5));
  2924. table.put(p);
  2925. p = new Put(Bytes.toBytes("row5"));
  2926. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-6));
  2927. table.put(p);
  2928. }
  2929. private void rangePutForIdx2WithFloat(HTable table) throws IOException {
  2930. Put p = new Put(Bytes.toBytes("row0"));
  2931. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(1.5f));
  2932. table.put(p);
  2933. p = new Put(Bytes.toBytes("row1"));
  2934. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(2.89f));
  2935. table.put(p);
  2936. p = new Put(Bytes.toBytes("row2"));
  2937. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(3.9f));
  2938. table.put(p);
  2939. p = new Put(Bytes.toBytes("row3"));
  2940. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-4.7f));
  2941. table.put(p);
  2942. p = new Put(Bytes.toBytes("row4"));
  2943. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-5.3f));
  2944. table.put(p);
  2945. p = new Put(Bytes.toBytes("row5"));
  2946. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-6.456f));
  2947. table.put(p);
  2948. }
  2949. private void rangePutForIdx2WithDouble(HTable table) throws IOException {
  2950. Put p = new Put(Bytes.toBytes("row0"));
  2951. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(1.5d));
  2952. table.put(p);
  2953. p = new Put(Bytes.toBytes("row1"));
  2954. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(2.89d));
  2955. table.put(p);
  2956. p = new Put(Bytes.toBytes("row2"));
  2957. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(3.9d));
  2958. table.put(p);
  2959. p = new Put(Bytes.toBytes("row3"));
  2960. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-4.7d));
  2961. table.put(p);
  2962. p = new Put(Bytes.toBytes("row4"));
  2963. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-5.3d));
  2964. table.put(p);
  2965. p = new Put(Bytes.toBytes("row5"));
  2966. p.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes(-6.456d));
  2967. table.put(p);
  2968. }
  2969. @Test(timeout = 180000)
  2970. public void testComplexRangeScan() throws Exception {
  2971. HBaseAdmin admin = UTIL.getHBaseAdmin();
  2972. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  2973. Configuration conf = UTIL.getConfiguration();
  2974. String tableName = "testComplexRangeScan";
  2975. IndexSpecification spec1 = new IndexSpecification("idx1");
  2976. IndexSpecification spec2 = new IndexSpecification("idx2");
  2977. IndexSpecification spec3 = new IndexSpecification("idx3");
  2978. IndexedHTableDescriptor htd = new IndexedHTableDescriptor(tableName);
  2979. // HTableDescriptor htd = new HTableDescriptor(tableName);
  2980. HColumnDescriptor hcd = new HColumnDescriptor("cf");
  2981. spec1.addIndexColumn(hcd, "detail", ValueType.String, 10);
  2982. spec2.addIndexColumn(hcd, "info", ValueType.String, 10);
  2983. spec3.addIndexColumn(hcd, "value", ValueType.String, 10);
  2984. htd.addFamily(hcd);
  2985. htd.addIndex(spec1);
  2986. htd.addIndex(spec2);
  2987. htd.addIndex(spec3);
  2988. String[] splitkeys = new String[9];
  2989. for (int i = 100, j = 0; i <= 900; i += 100, j++) {
  2990. splitkeys[j] = new Integer(i).toString();
  2991. }
  2992. admin.createTable(htd, Bytes.toByteArrays(splitkeys));
  2993. String rowname = "row";
  2994. String startrow = "";
  2995. int keys = 0;
  2996. List<Put> put = new ArrayList<Put>();
  2997. for (int i = 1, j = 999; i < 1000; i++, j--) {
  2998. if (i % 100 == 0) {
  2999. startrow = splitkeys[keys++];
  3000. }
  3001. Put p = new Put(Bytes.toBytes(startrow + rowname + i));
  3002. p.add(Bytes.toBytes("cf"), Bytes.toBytes("detail"), Bytes.toBytes(new Integer(i).toString()));
  3003. p.add(Bytes.toBytes("cf"), Bytes.toBytes("info"), Bytes.toBytes(new Integer(j).toString()));
  3004. p.add(Bytes.toBytes("cf"), Bytes.toBytes("value"),
  3005. Bytes.toBytes(new Integer(i % 100).toString()));
  3006. System.out.println(p);
  3007. put.add(p);
  3008. }
  3009. HTable table = new HTable(conf, tableName);
  3010. table.put(put);
  3011. Scan s = new Scan();
  3012. s.setCacheBlocks(true);
  3013. FilterList master = new FilterList(Operator.MUST_PASS_ONE);
  3014. SingleColumnValueFilter filter1 =
  3015. new SingleColumnValueFilter("cf".getBytes(), "detail".getBytes(), CompareOp.LESS_OR_EQUAL,
  3016. "6".getBytes());
  3017. filter1.setFilterIfMissing(true);
  3018. SingleColumnValueFilter filter2 =
  3019. new SingleColumnValueFilter("cf".getBytes(), "info".getBytes(), CompareOp.GREATER_OR_EQUAL,
  3020. "992".getBytes());
  3021. filter2.setFilterIfMissing(true);
  3022. SingleColumnValueFilter filter3 =
  3023. new SingleColumnValueFilter("cf".getBytes(), "value".getBytes(), CompareOp.EQUAL,
  3024. "9".getBytes());
  3025. filter3.setFilterIfMissing(true);
  3026. master.addFilter(filter1);
  3027. master.addFilter(filter2);
  3028. master.addFilter(filter3);
  3029. s.setFilter(master);
  3030. // scanOperation(s, conf, tableName);
  3031. assertEquals("data consistency is missed ", 563, scanOperation(s, conf, tableName));
  3032. System.out.println("Done ************");
  3033. s = new Scan();
  3034. s.setFilter(master);
  3035. s.setCaching(5);
  3036. // scanOperation(s, conf, tableName);
  3037. assertEquals("data consistency is missed ", 563, scanOperation(s, conf, tableName));
  3038. }
  3039. @Test(timeout = 180000)
  3040. public void testComplexRangeScanWithAnd() throws Exception {
  3041. HBaseAdmin admin = UTIL.getHBaseAdmin();
  3042. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  3043. Configuration conf = UTIL.getConfiguration();
  3044. String tableName = "RangeScanMetrix_2_new_id";
  3045. IndexSpecification spec1 = new IndexSpecification("idx1");
  3046. IndexSpecification spec2 = new IndexSpecification("idx2");
  3047. IndexSpecification spec3 = new IndexSpecification("idx3");
  3048. IndexedHTableDescriptor htd = new IndexedHTableDescriptor(tableName);
  3049. // HTableDescriptor htd = new HTableDescriptor(tableName);
  3050. HColumnDescriptor hcd = new HColumnDescriptor("cf");
  3051. spec1.addIndexColumn(hcd, "detail", ValueType.String, 10);
  3052. spec2.addIndexColumn(hcd, "info", ValueType.String, 10);
  3053. spec3.addIndexColumn(hcd, "value", ValueType.String, 10);
  3054. htd.addFamily(hcd);
  3055. htd.addIndex(spec1);
  3056. htd.addIndex(spec2);
  3057. htd.addIndex(spec3);
  3058. String[] splitkeys = new String[9];
  3059. for (int i = 100, j = 0; i <= 900; i += 100, j++) {
  3060. splitkeys[j] = new Integer(i).toString();
  3061. }
  3062. admin.createTable(htd, Bytes.toByteArrays(splitkeys));
  3063. String rowname = "row";
  3064. String startrow = "";
  3065. int keys = 0;
  3066. List<Put> put = new ArrayList<Put>();
  3067. for (int i = 1, j = 999; i < 1000; i++, j--) {
  3068. if (i % 100 == 0) {
  3069. startrow = splitkeys[keys++];
  3070. }
  3071. Put p = new Put(Bytes.toBytes(startrow + rowname + i));
  3072. p.add(Bytes.toBytes("cf"), Bytes.toBytes("detail"), Bytes.toBytes(new Integer(i).toString()));
  3073. p.add(Bytes.toBytes("cf"), Bytes.toBytes("info"), Bytes.toBytes(new Integer(j).toString()));
  3074. p.add(Bytes.toBytes("cf"), Bytes.toBytes("value"),
  3075. Bytes.toBytes(new Integer(i % 100).toString()));
  3076. System.out.println(p);
  3077. put.add(p);
  3078. }
  3079. HTable table = new HTable(conf, tableName);
  3080. table.put(put);
  3081. Scan s = new Scan();
  3082. s.setCacheBlocks(true);
  3083. s.setCaching(1);
  3084. FilterList master = new FilterList(Operator.MUST_PASS_ALL);
  3085. SingleColumnValueFilter filter1 =
  3086. new SingleColumnValueFilter("cf".getBytes(), "detail".getBytes(), CompareOp.LESS_OR_EQUAL,
  3087. "65".getBytes());
  3088. filter1.setFilterIfMissing(true);
  3089. SingleColumnValueFilter filter2 =
  3090. new SingleColumnValueFilter("cf".getBytes(), "info".getBytes(), CompareOp.GREATER,
  3091. "900".getBytes());
  3092. filter2.setFilterIfMissing(true);
  3093. SingleColumnValueFilter filter3 =
  3094. new SingleColumnValueFilter("cf".getBytes(), "value".getBytes(),
  3095. CompareOp.GREATER_OR_EQUAL, "5".getBytes());
  3096. filter3.setFilterIfMissing(true);
  3097. master.addFilter(filter1);
  3098. master.addFilter(filter2);
  3099. master.addFilter(filter3);
  3100. s.setFilter(master);
  3101. // scanOperation(s, conf, tableName);
  3102. assertEquals("data consistency is missed ", 18, scanOperation(s, conf, tableName));
  3103. System.out.println("Done ************");
  3104. s = new Scan();
  3105. s.setFilter(master);
  3106. s.setCaching(5);
  3107. // scanOperation(s, conf, tableName);
  3108. assertEquals("data consistency is missed ", 18, scanOperation(s, conf, tableName));
  3109. }
  3110. @Test(timeout = 180000)
  3111. public void testVerifyTheStopRowIsCorrectInCaseOfGreaterOperatorsInSCVF() throws Exception {
  3112. HBaseAdmin admin = UTIL.getHBaseAdmin();
  3113. HBaseTestingUtility.getZooKeeperWatcher(UTIL);
  3114. Configuration conf = UTIL.getConfiguration();
  3115. String tableName = "testDeleteIncosistent";
  3116. IndexSpecification spec1 = new IndexSpecification("idx1");
  3117. IndexedHTableDescriptor htd = new IndexedHTableDescriptor(tableName);
  3118. // HTableDescriptor htd = new HTableDescriptor(tableName);
  3119. HColumnDescriptor hcd = new HColumnDescriptor("cf");
  3120. spec1.addIndexColumn(hcd, "detail", ValueType.String, 10);
  3121. htd.addFamily(hcd);
  3122. htd.addIndex(spec1);
  3123. admin.createTable(htd);
  3124. HTable table = new HTable(conf, "testDeleteIncosistent");
  3125. HTable table2 = new HTable(conf, "testDeleteIncosistent_idx");
  3126. Put p = new Put(Bytes.toBytes("row5"));
  3127. p.add("cf".getBytes(), "detail".getBytes(), "5".getBytes());
  3128. table2.put(IndexUtils.prepareIndexPut(p, spec1, HConstants.EMPTY_START_ROW));
  3129. p = new Put(Bytes.toBytes("row6"));
  3130. p.add("cf".getBytes(), "detail".getBytes(), "6".getBytes());
  3131. table.put(p);
  3132. Scan s = new Scan();
  3133. SingleColumnValueFilter filter1 =
  3134. new SingleColumnValueFilter("cf".getBytes(), "detail".getBytes(), CompareOp.GREATER,
  3135. "5".getBytes());
  3136. s.setFilter(filter1);
  3137. ResultScanner scanner = table.getScanner(s);
  3138. int i = 0;
  3139. for (Result result : scanner) {
  3140. i++;
  3141. }
  3142. assertEquals(1, i);
  3143. }
  3144. private int scanOperation(Scan s, Configuration conf, String tableName) {
  3145. ResultScanner scanner = null;
  3146. int i = 0;
  3147. try {
  3148. HTable table = new HTable(conf, tableName);
  3149. scanner = table.getScanner(s);
  3150. for (Result result : scanner) {
  3151. System.out.println(Bytes.toString(result.getRow()));
  3152. i++;
  3153. }
  3154. } catch (IOException e) {
  3155. e.printStackTrace();
  3156. } finally {
  3157. if (scanner != null) {
  3158. scanner.close();
  3159. }
  3160. }
  3161. System.out.println("******* Return value " + i);
  3162. return i;
  3163. }
  3164. }