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());
}
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);
}
}
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);
}
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);
}
}
}
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));
}
Aggregations