private int getMaxLength(ValueType type, int maxValueLength) { if ((type == ValueType.Int || type == ValueType.Float) && maxValueLength != 4) { Log.warn("With integer or float datatypes, the maxValueLength has to be 4 bytes"); return 4; } if ((type == ValueType.Double || type == ValueType.Long) && maxValueLength != 8) { Log.warn("With Double and Long datatypes, the maxValueLength has to be 8 bytes"); return 8; } if ((type == ValueType.Short || type == ValueType.Char) && maxValueLength != 2) { Log.warn("With Short and Char datatypes, the maxValueLength has to be 2 bytes"); return 2; } if (type == ValueType.Byte && maxValueLength != 1) { Log.warn("With Byte datatype, the maxValueLength has to be 1 bytes"); return 1; } if (type == ValueType.String && maxValueLength == 0) { Log.warn("With String datatype, the minimun value length is 2"); maxValueLength = 2; } return maxValueLength; }
@Test public void testAddIndexWithDuplicaIndexNames() throws Exception { TableIndices indices = new TableIndices(); IndexSpecification iSpec = null; try { iSpec = new IndexSpecification("index_name"); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); indices.addIndex(iSpec); iSpec = new IndexSpecification("index_name"); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); indices.addIndex(iSpec); fail("Duplicate index names should not present for same table."); } catch (IllegalArgumentException e) { } }
@Test public void testAddIndexWithIndexNameLengthGreaterThanMaxLength() throws Exception { TableIndices indices = new TableIndices(); IndexSpecification iSpec = null; try { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 7; i++) { sb.append("index_name"); } iSpec = new IndexSpecification(new String(sb)); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); indices.addIndex(iSpec); fail("Index name length should not be more than maximum length " + Constants.DEF_MAX_INDEX_NAME_LENGTH + '.'); } catch (IllegalArgumentException e) { } }
@Test(timeout = 180000) public void testPreCreateShouldNotBeSuccessfulIfIndicesAreNotSameAtBothTypeAndLength() throws IOException, KeeperException, InterruptedException { String userTableName = "testNotConsisIndex4"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("col"); IndexSpecification iSpec1 = new IndexSpecification("Index1"); iSpec1.addIndexColumn(hcd, "q1", ValueType.String, 10); iSpec1.addIndexColumn(hcd, "q2", ValueType.String, 10); ihtd.addFamily(hcd); IndexSpecification iSpec2 = new IndexSpecification("Index2"); iSpec2.addIndexColumn(hcd, "q1", ValueType.Int, 10); iSpec2.addIndexColumn(hcd, "q2", ValueType.String, 7); TableIndices indices = new TableIndices(); indices.addIndex(iSpec1); indices.addIndex(iSpec2); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); boolean returnVal = false; try { admin.createTable(ihtd); fail("IOException should be thrown"); } catch (IOException e) { returnVal = true; } Assert.assertTrue(returnVal); }
private void createTableIfMissing() throws IOException { try { HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(TABLE_NAME)); for (byte[] family : FAMILIES) { ihtd.addFamily(new HColumnDescriptor(family)); } IndexSpecification iSpec = new IndexSpecification("ScanIndex"); iSpec.addIndexColumn(new HColumnDescriptor(Bytes.toString(FAMILY_A)), Bytes.toString(QUALIFIER_NAME) + "1", ValueType.String, 10); TableIndices indices = new TableIndices(); indices.addIndex(iSpec); HBaseAdmin admin = new IndexAdmin(util.getConfiguration()); admin.createTable(ihtd); admin.close(); } catch (TableExistsException tee) { } }
@Test(timeout = 180000) public void testPutWithOneUnitLengthSeparatorWithoutValue() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("testPutWithOneUnitLengthSeparatorWithoutValue")); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", new SeparatorPartition("_", 4), ValueType.String, 10); byte[] value1 = "2ndFloor_solitaire_huawei__karnataka".getBytes(); Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut = IndexUtils.prepareIndexPut(p, spec, region); byte[] indexRowKey = indexPut.getRow(); byte[] actualResult = new byte[10]; System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); byte[] expectedResult = new byte[10]; Assert.assertTrue(Bytes.equals(actualResult, expectedResult)); }
@Test(timeout = 180000) public void testIndexPutWithOffsetAndLength() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("testIndexPutWithOffsetAndLength")); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", new SpatialPartition(20, 2), ValueType.String, 18); byte[] value1 = "AB---CD---EF---GH---IJ---KL---MN---OP---".getBytes(); Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut = IndexUtils.prepareIndexPut(p, spec, region); byte[] indexRowKey = indexPut.getRow(); byte[] actualResult = new byte[2]; System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); byte[] expectedResult = new byte[2]; System.arraycopy("IJ".getBytes(), 0, expectedResult, 0, "IJ".getBytes().length); Assert.assertTrue(Bytes.equals(actualResult, expectedResult)); }
@Test(timeout = 180000) public void testIndexPutwithPositiveIntDataTypes() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("testIndexPutwithPositiveIntDataTypes")); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); // HLog hlog = UTIL.getMiniHBaseCluster().getRegionServer(0).getWAL(); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", ValueType.Int, 4); spec.addIndexColumn(new HColumnDescriptor("col"), "ql2", ValueType.Float, 4); byte[] value1 = Bytes.toBytes(1000); Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut1 = IndexUtils.prepareIndexPut(p, spec, region); int a = 1000; byte[] expectedResult = Bytes.toBytes(a ^ (1 << 31)); byte[] actualResult = new byte[4]; byte[] indexRowKey = indexPut1.getRow(); System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); Assert.assertTrue(Bytes.equals(expectedResult, actualResult)); }
@Test(timeout = 180000) public void testIndexPutWithNegativeIntDataTypes() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("testIndexPutWithNegativeIntDataTypes")); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); // HLog hlog = UTIL.getMiniHBaseCluster().getRegionServer(0).getWAL(); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", ValueType.Int, 4); spec.addIndexColumn(new HColumnDescriptor("col"), "ql2", ValueType.Float, 4); byte[] value1 = Bytes.toBytes(-2562351); Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut = IndexUtils.prepareIndexPut(p, spec, region); int a = -2562351; byte[] expectedResult = Bytes.toBytes(a ^ (1 << 31)); byte[] actualResult = new byte[4]; byte[] indexRowKey = indexPut.getRow(); System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); Assert.assertTrue(Bytes.equals(expectedResult, actualResult)); }
@Test(timeout = 180000) public void testIndexPutWithLongDataTypes() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor("testIndexPutWithNegativeIntDataTypes"); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); // HLog hlog = UTIL.getMiniHBaseCluster().getRegionServer(0).getWAL(); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", ValueType.Long, 4); byte[] value1 = Bytes.toBytes(-2562351L); Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut = IndexUtils.prepareIndexPut(p, spec, region); long a = -2562351L; byte[] expectedResult = Bytes.toBytes(a ^ (1L << 63)); byte[] actualResult = new byte[8]; byte[] indexRowKey = indexPut.getRow(); System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); Assert.assertTrue(Bytes.equals(expectedResult, actualResult)); }
@Test(timeout = 180000) public void testIndexPutWithShortDataTypes() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor("testIndexPutWithNegativeIntDataTypes"); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); // HLog hlog = UTIL.getMiniHBaseCluster().getRegionServer(0).getWAL(); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", ValueType.Short, 4); short s = 1000; byte[] value1 = Bytes.toBytes(s); Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut = IndexUtils.prepareIndexPut(p, spec, region); byte[] expectedResult = Bytes.toBytes(s); expectedResult[0] ^= 1 << 7; byte[] actualResult = new byte[2]; byte[] indexRowKey = indexPut.getRow(); System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); Assert.assertTrue(Bytes.equals(expectedResult, actualResult)); }
@Test(timeout = 180000) public void testIndexPutWithByteDataTypes() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor("testIndexPutWithNegativeIntDataTypes"); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); // HLog hlog = UTIL.getMiniHBaseCluster().getRegionServer(0).getWAL(); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", ValueType.Short, 4); byte b = 100; byte[] value1 = Bytes.toBytes(b); Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut = IndexUtils.prepareIndexPut(p, spec, region); byte[] expectedResult = Bytes.toBytes(b); expectedResult[0] ^= 1 << 7; byte[] actualResult = new byte[2]; byte[] indexRowKey = indexPut.getRow(); System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); Assert.assertTrue(Bytes.equals(expectedResult, actualResult)); }
@Test(timeout = 180000) public void testIndexPutWithCharDataTypes() throws IOException { Path basedir = new Path(DIR + "TestIndexPut"); Configuration conf = TEST_UTIL.getConfiguration(); HTableDescriptor htd = new HTableDescriptor("testIndexPutWithNegativeIntDataTypes"); HRegionInfo info = new HRegionInfo(htd.getTableName(), "ABC".getBytes(), "BBB".getBytes(), false); // HLog hlog = UTIL.getMiniHBaseCluster().getRegionServer(0).getWAL(); HRegion region = HRegion.createHRegion(info, basedir, conf, htd); IndexSpecification spec = new IndexSpecification("index"); spec.addIndexColumn(new HColumnDescriptor("col"), "ql1", ValueType.Char, 4); char c = 'A'; byte[] value1 = new byte[2]; value1[1] = (byte) c; c >>= 8; value1[0] = (byte) c; Put p = new Put("row".getBytes()); p.add("col".getBytes(), "ql1".getBytes(), value1); Put indexPut = IndexUtils.prepareIndexPut(p, spec, region); byte[] actualResult = new byte[2]; byte[] indexRowKey = indexPut.getRow(); System.arraycopy(indexRowKey, 22, actualResult, 0, actualResult.length); Assert.assertTrue(Bytes.equals(value1, actualResult)); }
@Test public void testSingleIndexExpressionWithOneEqualsExpression() throws Exception { String indexName = "idx1"; SingleIndexExpression singleIndexExpression = new SingleIndexExpression(indexName); byte[] value = "1".getBytes(); Column column = new Column(FAMILY1, QUALIFIER1); EqualsExpression equalsExpression = new EqualsExpression(column, value); singleIndexExpression.addEqualsExpression(equalsExpression); Scan scan = new Scan(); scan.setAttribute(Constants.INDEX_EXPRESSION, IndexUtils.toBytes(singleIndexExpression)); Filter filter = new SingleColumnValueFilter(FAMILY1, QUALIFIER1, CompareOp.EQUAL, value); scan.setFilter(filter); ScanFilterEvaluator evaluator = new ScanFilterEvaluator(); List<IndexSpecification> indices = new ArrayList<IndexSpecification>(); IndexSpecification index = new IndexSpecification(indexName); HColumnDescriptor colDesc = new HColumnDescriptor(FAMILY1); index.addIndexColumn(colDesc, COL1, ValueType.String, 10); indices.add(index); HRegion region = initHRegion(tableName.getBytes(), null, null, "testSingleIndexExpressionWithOneEqualsExpression", TEST_UTIL.getConfiguration(), FAMILY1); IndexRegionScanner scanner = evaluator.evaluate(scan, indices, new byte[0], region, tableName); // TODO add assertions }
@Test public void testNoIndexExpression() throws Exception { IndexExpression exp = new NoIndexExpression(); Scan scan = new Scan(); scan.setAttribute(Constants.INDEX_EXPRESSION, IndexUtils.toBytes(exp)); byte[] value1 = Bytes.toBytes("asdf"); scan.setFilter(new SingleColumnValueFilter(FAMILY1, QUALIFIER1, CompareOp.EQUAL, value1)); List<IndexSpecification> indices = new ArrayList<IndexSpecification>(); IndexSpecification is1 = new IndexSpecification("idx1"); HColumnDescriptor colDesc = new HColumnDescriptor(FAMILY1); is1.addIndexColumn(colDesc, COL1, ValueType.String, 15); indices.add(is1); ScanFilterEvaluator evaluator = new ScanFilterEvaluator(); HRegion region = initHRegion(tableName.getBytes(), null, null, "testNoIndexExpression", TEST_UTIL.getConfiguration(), FAMILY1); IndexRegionScanner scanner = evaluator.evaluate(scan, indices, new byte[0], region, tableName); assertNull(scanner); }
/** * @param cf column family * @param qualifier * @param type - If type is specified as null then by default ValueType will be taken as String. * @param maxValueLength * @throws IllegalArgumentException If column family name and/or qualifier is null or blanks.<br/> * If column family name starts with '.',contains control characters or colons. * @see ValueType */ public void addIndexColumn(HColumnDescriptor cf, String qualifier, ValueType type, int maxValueLength) throws IllegalArgumentException { type = checkForType(type); isValidFamilyAndQualifier(cf, qualifier); maxValueLength = getMaxLength(type, maxValueLength); ColumnQualifier cq = new ColumnQualifier(cf.getNameAsString(), qualifier, type, maxValueLength); isNotDuplicateEntry(cq); formMinTTL(cf); formMaxVersions(cf); internalAdd(cq); }
/** * @param cf Column Family * @param qualifier Column Qualifier * @param vp Value Partition * @param type Data Type * @param maxValueLength * @throws IllegalArgumentException */ public void addIndexColumn(HColumnDescriptor cf, String qualifier, ValuePartition vp, ValueType type, int maxValueLength) throws IllegalArgumentException { checkForType(type); isValidFamilyAndQualifier(cf, qualifier); maxValueLength = getMaxLength(type, maxValueLength); ColumnQualifier cq = new ColumnQualifier(cf.getNameAsString(), qualifier, type, maxValueLength, vp); isNotDuplicateEntry(cq); formMinTTL(cf); formMaxVersions(cf); internalAdd(cq); }
private void copyColumnValueToKey(ByteArrayBuilder builder, byte[] colValue, int maxValueLength, ValueType valueType) { colValue = IndexUtils.changeValueAccToDataType(colValue, valueType); builder.put(colValue); int paddingLength = maxValueLength - colValue.length; builder.position(builder.position() + paddingLength); }
public static byte[] changeValueAccToDataType(byte[] value, ValueType valueType) { byte[] valueArr = new byte[value.length]; System.arraycopy(value, 0, valueArr, 0, value.length); if (valueArr.length == 0) return valueArr; switch (valueType) { case String: case Char: break; case Float: float f = Bytes.toFloat(valueArr); if (f > 0) { valueArr[0] ^= (1 << 7); } else { valueArr[0] ^= 0xff; valueArr[1] ^= 0xff; valueArr[2] ^= 0xff; valueArr[3] ^= 0xff; } break; case Double: double d = Bytes.toDouble(valueArr); if (d > 0) { valueArr[0] ^= (1 << 7); } else { for (int i = 0; i < 8; i++) { valueArr[i] ^= 0xff; } } break; case Int: case Long: case Short: case Byte: valueArr[0] ^= (1 << 7); break; } return valueArr; }
@Test(timeout = 180000) public void testShouldRetrieveNegativeFloatValueWithLessCondition() throws Exception { Configuration conf = UTIL.getConfiguration(); String userTableName = "testShouldRetrieveNegativeFloatValueWithLessCondition"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("cf1"); ihtd.addFamily(hcd); TableIndices indices = new TableIndices(); IndexSpecification indexSpecification = createIndexSpecification(hcd, ValueType.Float, 10, new String[] { "c1" }, "idx1"); indices.addIndex(indexSpecification); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); admin.createTable(ihtd); HTable table = new HTable(conf, userTableName); rangePutForIdx2WithFloat(table); FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL); SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS, new FloatComparator(Bytes.toBytes(-5f))); masterFilter.addFilter(scvf); Scan scan = new Scan(); scan.setFilter(masterFilter); ResultScanner scanner = table.getScanner(scan); List<Result> testRes = new ArrayList<Result>(); Result[] result = scanner.next(1); while (result != null && result.length > 0) { testRes.add(result[0]); result = scanner.next(1); } assertTrue(testRes.size() == 2); }
public static HTableDescriptor createIndexedHTableDescriptor(String tableName, String columnFamily, String indexName, String indexColumnFamily, String indexColumnQualifier) throws IOException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); IndexSpecification iSpec = new IndexSpecification(indexName); HColumnDescriptor hcd = new HColumnDescriptor(columnFamily); TableIndices tableIndices = new TableIndices(); tableIndices.addIndex(iSpec); iSpec.addIndexColumn(hcd, indexColumnQualifier, ValueType.String, 10); htd.addFamily(hcd); htd.setValue(Constants.INDEX_SPEC_KEY, tableIndices.toByteArray()); return htd; }
@Test public void testAddIndexColumnWithNotNull() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); Set<ColumnQualifier> indexColumns = iSpec.getIndexColumns(); assertEquals("Size of column qualifiers list should be zero.", 0, indexColumns.size()); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); indexColumns = iSpec.getIndexColumns(); assertTrue("ColumnQualifier state mismatch.", indexColumns.contains(new ColumnQualifier("cf", "cq", ValueType.String, 10))); }
@Test public void testAddIndexColumnWithNullCF() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); try { iSpec.addIndexColumn(null, "cq", ValueType.String, 10); fail("Column family name should not be null in index specification."); } catch (IllegalArgumentException e) { } }
@Test public void testAddIndexColumnWithNullQualifier() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); try { iSpec.addIndexColumn(new HColumnDescriptor("cf"), null, ValueType.String, 10); fail("Column qualifier name should not be null in index specification."); } catch (IllegalArgumentException e) { } }
@Test public void testAddIndexColumnWithBlankCForQualifier() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); try { iSpec.addIndexColumn(new HColumnDescriptor(" "), " ", ValueType.String, 10); fail("Column family name and qualifier should not be blank."); } catch (IllegalArgumentException e) { } }
@Test public void testAddIndexColumnWithBlankCF() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); try { iSpec.addIndexColumn(new HColumnDescriptor(" "), "cq", ValueType.String, 10); fail("Column family name should not be blank."); } catch (IllegalArgumentException e) { } }
@Test public void testAddIndexColumnWithBlankQualifier() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); try { iSpec.addIndexColumn(new HColumnDescriptor("cf"), " ", ValueType.String, 10); fail("Column qualifier name should not be blank."); } catch (IllegalArgumentException e) { } }
@Test public void testAddIndexColumnWithControlCharactersOrColonsInCF() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); try { iSpec.addIndexColumn(new HColumnDescriptor("cf:"), "cq", ValueType.String, 10); fail("Family names should not contain control characters or colons."); } catch (IllegalArgumentException e) { } }
@Test public void testAddIndexColumnWithDuplicaCFandQualifier() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); try { iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); fail("Column familily and qualifier combination should not be" + " repeated in Index Specification."); } catch (IllegalArgumentException e) { } }
@Test(timeout = 180000) public void testShouldRetrieveNegativeDoubleValueWithLesserThanEqualsCondition() throws Exception { Configuration conf = UTIL.getConfiguration(); String userTableName = "testShouldRetrieveNegativeDoubleValueWithLesserThanEqualsCondition"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("cf1"); ihtd.addFamily(hcd); TableIndices indices = new TableIndices(); IndexSpecification indexSpecification = createIndexSpecification(hcd, ValueType.Double, 10, new String[] { "c1" }, "idx1"); indices.addIndex(indexSpecification); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); admin.createTable(ihtd); HTable table = new HTable(conf, userTableName); rangePutForIdx2WithDouble(table); FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL); SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.LESS_OR_EQUAL, new DoubleComparator(Bytes.toBytes(-5.3d))); masterFilter.addFilter(scvf); Scan scan = new Scan(); scan.setFilter(masterFilter); ResultScanner scanner = table.getScanner(scan); List<Result> testRes = new ArrayList<Result>(); Result[] result = scanner.next(1); while (result != null && result.length > 0) { testRes.add(result[0]); result = scanner.next(1); } assertTrue(testRes.size() == 2); }
@Test(timeout = 180000) public void testShouldRetrieveNegativeFloatValueWithEqualsCondition() throws Exception { Configuration conf = UTIL.getConfiguration(); String userTableName = "testShouldRetrieveNegativeFloatValueWithEqualsCondition"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("cf1"); ihtd.addFamily(hcd); TableIndices indices = new TableIndices(); IndexSpecification indexSpecification = createIndexSpecification(hcd, ValueType.Float, 10, new String[] { "c1" }, "idx1"); indices.addIndex(indexSpecification); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); admin.createTable(ihtd); HTable table = new HTable(conf, userTableName); rangePutForIdx2WithFloat(table); FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL); SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL, new FloatComparator(Bytes.toBytes(-5.3f))); masterFilter.addFilter(scvf); Scan scan = new Scan(); scan.setFilter(masterFilter); ResultScanner scanner = table.getScanner(scan); List<Result> testRes = new ArrayList<Result>(); Result[] result = scanner.next(1); while (result != null && result.length > 0) { testRes.add(result[0]); result = scanner.next(1); } assertTrue(testRes.size() == 1); }
@Test public void testMinTTLWhenDefault() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); iSpec.addIndexColumn(new HColumnDescriptor("cf1"), "cq", ValueType.String, 10); assertEquals("The ttl should be " + Integer.MAX_VALUE, HConstants.FOREVER, iSpec.getTTL()); }
@Test(timeout = 180000) public void testShouldRetriveNegativeIntValueWithGreaterCondition() throws Exception { Configuration conf = UTIL.getConfiguration(); String userTableName = "testShouldRetriveNegativeIntValueWithGreaterCondition"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("cf1"); ihtd.addFamily(hcd); TableIndices indices = new TableIndices(); IndexSpecification indexSpecification = createIndexSpecification(hcd, ValueType.Int, 10, new String[] { "c1" }, "idx1"); indices.addIndex(indexSpecification); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); admin.createTable(ihtd); HTable table = new HTable(conf, userTableName); rangePutForIdx2WithInteger(table); FilterList masterFilter = new FilterList(Operator.MUST_PASS_ALL); SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.GREATER, new IntComparator(Bytes.toBytes(-6))); masterFilter.addFilter(scvf); Scan scan = new Scan(); scan.setFilter(masterFilter); ResultScanner scanner = table.getScanner(scan); List<Result> testRes = new ArrayList<Result>(); Result[] result = scanner.next(1); while (result != null && result.length > 0) { testRes.add(result[0]); result = scanner.next(1); } assertTrue(testRes.size() == 5); }
@Test public void testMaxVersionsWhenDefault() throws Exception { IndexSpecification iSpec = new IndexSpecification("index_name"); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); iSpec.addIndexColumn(new HColumnDescriptor("cf1"), "cq", ValueType.String, 10); assertEquals("default max versions should be 1", 1, iSpec.getMaxVersions()); }
@Test public void testIndexSpecificationSerialization() throws Exception { ByteArrayOutputStream bos = null; DataOutputStream dos = null; ByteArrayInputStream bis = null; DataInputStream dis = null; try { bos = new ByteArrayOutputStream(); dos = new DataOutputStream(bos); IndexSpecification iSpec = new IndexSpecification("index_name"); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); iSpec.write(dos); dos.flush(); byte[] byteArray = bos.toByteArray(); bis = new ByteArrayInputStream(byteArray); dis = new DataInputStream(bis); IndexSpecification indexSpecification = new IndexSpecification(); indexSpecification.readFields(dis); assertEquals("Index name mismatch.", indexSpecification.getName(), iSpec.getName()); assertTrue("ColumnQualifier state mismatch.", iSpec.getIndexColumns().contains(new ColumnQualifier("cf", "cq", ValueType.String, 10))); } finally { if (null != bos) { bos.close(); } if (null != dos) { dos.close(); } if (null != bis) { bis.close(); } if (null != dis) { dis.close(); } } }
@Test public void testAddIndex() throws Exception { TableIndices indices = new TableIndices(); IndexSpecification iSpec = new IndexSpecification("index_name"); iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", ValueType.String, 10); indices.addIndex(iSpec); assertEquals("Index name should be equal with actual value.", "index_name", indices.getIndices().get(0) .getName()); assertTrue( "Column qualifier state mismatch.", indices.getIndices().get(0).getIndexColumns() .contains(new ColumnQualifier("cf", "cq", ValueType.String, 10))); }
@Test(timeout = 180000) public void testColocationWhenNormalTableModifiedToIndexedTable() throws Exception { MiniHBaseCluster cluster = UTIL.getHBaseCluster(); String tableName = "testColocationWhenNormalTableModifiedToIndexedTable"; HMaster master = cluster.getMaster(); byte[][] split = new byte[20][]; char c = 'A'; for (int i = 0; i < 20; i++) { byte[] b = { (byte) c }; split[i] = b; c++; } HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); HColumnDescriptor hcd = new HColumnDescriptor("cf"); htd.addFamily(hcd); admin.createTable(htd, split); IndexSpecification iSpec = new IndexSpecification("index_name"); TableIndices tableIndices = new TableIndices(); tableIndices.addIndex(iSpec); iSpec.addIndexColumn(hcd, "cq", ValueType.String, 10); htd.setValue(Constants.INDEX_SPEC_KEY, tableIndices.toByteArray()); String indexTableName = IndexUtils.getIndexTableName(tableName); admin.disableTable(tableName); admin.modifyTable(tableName, htd); admin.enableTable(tableName); boolean isRegionColocated = TestUtils.checkForColocation(master, tableName, indexTableName); assertTrue("User regions and index regions should colocate.", isRegionColocated); }
@Test(timeout = 180000) public void testPreCreateShouldNotBeSuccessfulIfIndicesAreNotSame() throws IOException, KeeperException, InterruptedException { ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL); String userTableName = "testNotConsisIndex1"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("col"); IndexSpecification iSpec1 = new IndexSpecification("Index1"); iSpec1.addIndexColumn(hcd, "q1", ValueType.String, 10); ihtd.addFamily(hcd); TableIndices indices = new TableIndices(); indices.addIndex(iSpec1); IndexSpecification iSpec2 = new IndexSpecification("Index2"); iSpec2.addIndexColumn(hcd, "q1", ValueType.Int, 10); indices.addIndex(iSpec2); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); boolean returnVal = false; try { admin.createTable(ihtd); fail("Exception should be thrown"); } catch (IOException e) { returnVal = true; } Assert.assertTrue(returnVal); ZKAssign.blockUntilNoRIT(zkw); }
@Test(timeout = 180000) public void testPreCreateShouldNotBeSuccessfulIfIndicesAreNotSameAtLength() throws IOException, KeeperException, InterruptedException { ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL); String userTableName = "testNotConsisIndex2"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("col"); IndexSpecification iSpec1 = new IndexSpecification("Index1"); iSpec1.addIndexColumn(hcd, "q1", ValueType.String, 10); iSpec1.addIndexColumn(hcd, "q2", ValueType.String, 4); ihtd.addFamily(hcd); TableIndices indices = new TableIndices(); indices.addIndex(iSpec1); IndexSpecification iSpec2 = new IndexSpecification("Index2"); iSpec2.addIndexColumn(hcd, "q3", ValueType.String, 10); iSpec2.addIndexColumn(hcd, "q2", ValueType.String, 10); indices.addIndex(iSpec2); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); boolean returnVal = false; try { admin.createTable(ihtd); fail("Exception should be thrown"); } catch (IOException e) { returnVal = true; } Assert.assertTrue(returnVal); ZKAssign.blockUntilNoRIT(zkw); }
@Test(timeout = 180000) public void testPreCreateShouldNotBeSuccessfulIfIndicesAreNotSameAtType() throws IOException, KeeperException, InterruptedException { ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL); String userTableName = "testNotConsisIndex3"; HTableDescriptor ihtd = new HTableDescriptor(TableName.valueOf(userTableName)); HColumnDescriptor hcd = new HColumnDescriptor("col"); IndexSpecification iSpec1 = new IndexSpecification("Index1"); iSpec1.addIndexColumn(hcd, "q1", ValueType.String, 10); iSpec1.addIndexColumn(hcd, "q2", ValueType.String, 10); ihtd.addFamily(hcd); IndexSpecification iSpec2 = new IndexSpecification("Index2"); iSpec2.addIndexColumn(hcd, "q1", ValueType.Int, 10); iSpec2.addIndexColumn(hcd, "q3", ValueType.String, 10); TableIndices indices = new TableIndices(); indices.addIndex(iSpec1); indices.addIndex(iSpec2); ihtd.setValue(Constants.INDEX_SPEC_KEY, indices.toByteArray()); boolean returnVal = false; try { admin.createTable(ihtd); fail("Exception should be thrown"); } catch (IOException e) { returnVal = true; } Assert.assertTrue(returnVal); ZKAssign.blockUntilNoRIT(zkw); }