Search in sources :

Example 91 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class ImmutableStorageSchemeTest method testWithExpressionsThatEvaluatetoFalse.

@Test
public void testWithExpressionsThatEvaluatetoFalse() throws Exception {
    List<Expression> children = Lists.newArrayListWithExpectedSize(4);
    children.add(CONSTANT_EXPRESSION);
    children.add(FALSE_EVAL_EXPRESSION);
    children.add(LiteralExpression.newConstant(BYTE_ARRAY1, PVarbinary.INSTANCE));
    children.add(FALSE_EVAL_EXPRESSION);
    children.add(LiteralExpression.newConstant(BYTE_ARRAY2, PVarbinary.INSTANCE));
    SingleCellConstructorExpression singleCellConstructorExpression = new SingleCellConstructorExpression(immutableStorageScheme, children);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    singleCellConstructorExpression.evaluate(null, ptr);
    ImmutableBytesPtr ptrCopy = new ImmutableBytesPtr(ptr);
    ColumnValueDecoder decoder = immutableStorageScheme.getDecoder();
    assertTrue(decoder.decode(ptrCopy, 0));
    assertArrayEquals(QueryConstants.EMPTY_COLUMN_VALUE_BYTES, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertFalse(decoder.decode(ptrCopy, 1));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 2));
    assertArrayEquals(BYTE_ARRAY1, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertFalse(decoder.decode(ptrCopy, 3));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 4));
    assertArrayEquals(BYTE_ARRAY2, ptrCopy.copyBytesIfNecessary());
}
Also used : SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) Expression(org.apache.phoenix.expression.Expression) DelegateExpression(org.apache.phoenix.expression.DelegateExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Test(org.junit.Test)

Example 92 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class PCharPadTest method testRelativeByteArrayOrder.

@Test
public void testRelativeByteArrayOrder() throws SQLException {
    String[] inputs = { "foo", "foo!", "fooA", "foo~" };
    PDataType dataType = PChar.INSTANCE;
    Arrays.sort(inputs);
    List<byte[]> ascOrderedInputs = new ArrayList<>(inputs.length);
    SortOrder sortOrder = SortOrder.ASC;
    for (String input : inputs) {
        LiteralExpression expr = LiteralExpression.newConstant(input, dataType, sortOrder);
        ImmutableBytesPtr ptr = new ImmutableBytesPtr(expr.getBytes());
        dataType.pad(ptr, 8, sortOrder);
        ascOrderedInputs.add(ptr.copyBytes());
    }
    Collections.sort(ascOrderedInputs, Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < inputs.length; i++) {
        byte[] bytes = ascOrderedInputs.get(i);
        String resultValue = (String) dataType.toObject(bytes, 0, bytes.length, dataType, sortOrder);
        assertEquals(inputs[i], resultValue);
    }
    List<byte[]> descOrderedInputs = new ArrayList<>(inputs.length);
    sortOrder = SortOrder.DESC;
    for (String input : inputs) {
        LiteralExpression expr = LiteralExpression.newConstant(input, dataType, sortOrder);
        ImmutableBytesPtr ptr = new ImmutableBytesPtr(expr.getBytes());
        dataType.pad(ptr, 8, sortOrder);
        descOrderedInputs.add(ptr.copyBytes());
    }
    Collections.sort(descOrderedInputs, Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < inputs.length; i++) {
        byte[] bytes = descOrderedInputs.get(i);
        String resultValue = (String) dataType.toObject(bytes, 0, bytes.length, dataType, sortOrder);
        assertEquals(inputs[inputs.length - 1 - i], resultValue);
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 93 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class PCharPadTest method test.

public void test(String value, PDataType dataType, int length, SortOrder sortOrder, byte[] result) throws SQLException {
    LiteralExpression expr = LiteralExpression.newConstant(value, dataType, sortOrder);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr(expr.getBytes());
    dataType.pad(ptr, length, sortOrder);
    String resultValue = (String) dataType.toObject(ptr, dataType, sortOrder);
    assertTrue(Arrays.equals(result, ptr.get()));
    assertEquals(value, resultValue);
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)

Example 94 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class HashJoinInfo method deserializeHashJoinFromScan.

@SuppressWarnings("unchecked")
public static HashJoinInfo deserializeHashJoinFromScan(Scan scan) {
    byte[] join = scan.getAttribute(HASH_JOIN);
    if (join == null) {
        return null;
    }
    ByteArrayInputStream stream = new ByteArrayInputStream(join);
    try {
        DataInputStream input = new DataInputStream(stream);
        KeyValueSchema joinedSchema = new KeyValueSchema();
        joinedSchema.readFields(input);
        int count = WritableUtils.readVInt(input);
        ImmutableBytesPtr[] joinIds = new ImmutableBytesPtr[count];
        List<Expression>[] joinExpressions = new List[count];
        JoinType[] joinTypes = new JoinType[count];
        boolean[] earlyEvaluation = new boolean[count];
        KeyValueSchema[] schemas = new KeyValueSchema[count];
        int[] fieldPositions = new int[count];
        for (int i = 0; i < count; i++) {
            joinIds[i] = new ImmutableBytesPtr();
            joinIds[i].readFields(input);
            int nExprs = WritableUtils.readVInt(input);
            joinExpressions[i] = new ArrayList<Expression>(nExprs);
            for (int j = 0; j < nExprs; j++) {
                int expressionOrdinal = WritableUtils.readVInt(input);
                Expression expression = ExpressionType.values()[expressionOrdinal].newInstance();
                expression.readFields(input);
                joinExpressions[i].add(expression);
            }
            int type = WritableUtils.readVInt(input);
            joinTypes[i] = JoinType.values()[type];
            earlyEvaluation[i] = input.readBoolean();
            schemas[i] = new KeyValueSchema();
            schemas[i].readFields(input);
            fieldPositions[i] = WritableUtils.readVInt(input);
        }
        Expression postJoinFilterExpression = null;
        int expressionOrdinal = WritableUtils.readVInt(input);
        if (expressionOrdinal != -1) {
            postJoinFilterExpression = ExpressionType.values()[expressionOrdinal].newInstance();
            postJoinFilterExpression.readFields(input);
        }
        int limit = -1;
        boolean forceProjection = false;
        // both to be upgraded in lock step.
        try {
            limit = WritableUtils.readVInt(input);
            forceProjection = input.readBoolean();
        } catch (EOFException ignore) {
        }
        return new HashJoinInfo(joinedSchema, joinIds, joinExpressions, joinTypes, earlyEvaluation, schemas, fieldPositions, postJoinFilterExpression, limit >= 0 ? limit : null, forceProjection);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) JoinType(org.apache.phoenix.parse.JoinTableNode.JoinType) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Expression(org.apache.phoenix.expression.Expression) EOFException(java.io.EOFException) ArrayList(java.util.ArrayList) List(java.util.List) KeyValueSchema(org.apache.phoenix.schema.KeyValueSchema)

Example 95 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class TestPerRegionIndexWriteCache method testMultipleRegions.

@Test
public void testMultipleRegions() {
    PerRegionIndexWriteCache cache = new PerRegionIndexWriteCache();
    HTableInterfaceReference t1 = new HTableInterfaceReference(new ImmutableBytesPtr(Bytes.toBytes("t1")));
    List<Mutation> mutations = Lists.<Mutation>newArrayList(p);
    List<Mutation> m2 = Lists.<Mutation>newArrayList(p2);
    // add each region
    cache.addEdits(r1, t1, mutations);
    cache.addEdits(r2, t1, m2);
    // check region1
    Multimap<HTableInterfaceReference, Mutation> edits = cache.getEdits(r1);
    Set<Entry<HTableInterfaceReference, Collection<Mutation>>> entries = edits.asMap().entrySet();
    assertEquals("Got more than one table in the the edit map!", 1, entries.size());
    for (Entry<HTableInterfaceReference, Collection<Mutation>> entry : entries) {
        // ensure that we are still storing a list here - otherwise it breaks the parallel writer
        // implementation
        final List<Mutation> stored = (List<Mutation>) entry.getValue();
        assertEquals("Got an unexpected amount of mutations in the entry for region1", 1, stored.size());
        assertEquals("Got an unexpected mutation in the entry for region2", p, stored.get(0));
    }
    // check region2
    edits = cache.getEdits(r2);
    entries = edits.asMap().entrySet();
    assertEquals("Got more than one table in the the edit map!", 1, entries.size());
    for (Entry<HTableInterfaceReference, Collection<Mutation>> entry : entries) {
        // ensure that we are still storing a list here - otherwise it breaks the parallel writer
        // implementation
        final List<Mutation> stored = (List<Mutation>) entry.getValue();
        assertEquals("Got an unexpected amount of mutations in the entry for region2", 1, stored.size());
        assertEquals("Got an unexpected mutation in the entry for region2", p2, stored.get(0));
    }
    // ensure that a second get doesn't have any more edits. This ensures that we don't keep
    // references around to these edits and have a memory leak
    assertNull("Got an entry for a region we removed", cache.getEdits(r1));
}
Also used : Entry(java.util.Map.Entry) HTableInterfaceReference(org.apache.phoenix.hbase.index.table.HTableInterfaceReference) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Mutation(org.apache.hadoop.hbase.client.Mutation) Test(org.junit.Test)

Aggregations

ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)120 Mutation (org.apache.hadoop.hbase.client.Mutation)31 PTable (org.apache.phoenix.schema.PTable)28 ArrayList (java.util.ArrayList)27 Region (org.apache.hadoop.hbase.regionserver.Region)22 PMetaDataEntity (org.apache.phoenix.schema.PMetaDataEntity)22 Test (org.junit.Test)21 Cell (org.apache.hadoop.hbase.Cell)20 Put (org.apache.hadoop.hbase.client.Put)18 List (java.util.List)15 Scan (org.apache.hadoop.hbase.client.Scan)15 Pair (org.apache.hadoop.hbase.util.Pair)15 IOException (java.io.IOException)14 Expression (org.apache.phoenix.expression.Expression)14 PColumn (org.apache.phoenix.schema.PColumn)14 RowLock (org.apache.hadoop.hbase.regionserver.Region.RowLock)13 PSmallint (org.apache.phoenix.schema.types.PSmallint)12 HashMap (java.util.HashMap)11 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)11 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)11