-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
205 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/edu/washington/escience/myria/expression/BitsetExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package edu.washington.escience.myria.expression; | ||
|
||
import edu.washington.escience.myria.Type; | ||
import edu.washington.escience.myria.expression.evaluate.ExpressionOperatorParameter; | ||
|
||
/** | ||
* Returns all bits in an input of BLOB_TYPE as a sequence of BOOLEAN_TYPE | ||
* (enumerating the input bytes in little-endian order). | ||
*/ | ||
public class BitsetExpression extends UnaryExpression { | ||
/***/ | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* This is not really unused, it's used automagically by Jackson deserialization. | ||
*/ | ||
@SuppressWarnings("unused") | ||
private BitsetExpression() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Takes an input of BLOB_TYPE. | ||
* | ||
* @param operand the input blob | ||
*/ | ||
public BitsetExpression(final ExpressionOperator operand) { | ||
super(operand); | ||
} | ||
|
||
@Override | ||
public Type getOutputType(final ExpressionOperatorParameter parameters) { | ||
checkOperandType(Type.BLOB_TYPE, parameters); | ||
return Type.BOOLEAN_TYPE; | ||
} | ||
|
||
@Override | ||
public String getJavaString(final ExpressionOperatorParameter parameters) { | ||
return new StringBuilder() | ||
.append("ByteBuffer bb = (") | ||
.append(getOperand().getJavaString(parameters)) | ||
.append(");\n") | ||
.append("java.util.BitSet bs = java.util.BitSet.valueOf(bb);\n") | ||
.append("int bits_len = bb.capacity() * 8;\n") | ||
.append("boolean[] vals = new boolean[bits_len];\n") | ||
.append("for (int i = 0; i < bits_len; ++i) {\n") | ||
.append("vals[i] = bs.get(i);\n") | ||
.append("}\n") | ||
.append("return vals;\n") | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public String getJavaExpressionWithAppend(final ExpressionOperatorParameter parameters) { | ||
return new StringBuilder() | ||
.append("ByteBuffer bb = (") | ||
.append(getOperand().getJavaString(parameters)) | ||
.append(");\n") | ||
.append("java.util.BitSet bs = java.util.BitSet.valueOf(bb);\n") | ||
.append("int bits_len = bb.capacity() * 8;\n") | ||
.append(Expression.COUNT) | ||
.append(".appendInt(bits_len);\n") | ||
.append("for (int i = 0; i < bits_len; ++i) {\n") | ||
.append(Expression.RESULT) | ||
.append(".appendBoolean(bs.get(i));\n") | ||
.append("}\n") | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public boolean hasArrayOutputType() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/edu/washington/escience/myria/operator/apply/ApplyBitsetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package edu.washington.escience.myria.operator.apply; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import org.junit.Test; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import edu.washington.escience.myria.DbException; | ||
import edu.washington.escience.myria.Schema; | ||
import edu.washington.escience.myria.Type; | ||
import edu.washington.escience.myria.expression.Expression; | ||
import edu.washington.escience.myria.expression.ExpressionOperator; | ||
import edu.washington.escience.myria.expression.BitsetExpression; | ||
import edu.washington.escience.myria.expression.VariableExpression; | ||
import edu.washington.escience.myria.operator.Apply; | ||
import edu.washington.escience.myria.operator.BatchTupleSource; | ||
import edu.washington.escience.myria.storage.TupleBatch; | ||
import edu.washington.escience.myria.storage.TupleBatchBuffer; | ||
import edu.washington.escience.myria.util.TestEnvVars; | ||
|
||
public class ApplyBitsetTest { | ||
|
||
@Test | ||
public void testApply() throws DbException { | ||
final Schema schema = Schema.ofFields("bytes", Type.BLOB_TYPE); | ||
final Schema expectedResultSchema = Schema.ofFields("bits", Type.BOOLEAN_TYPE); | ||
final TupleBatchBuffer input = new TupleBatchBuffer(schema); | ||
|
||
byte[] bytes = new byte[8]; | ||
for (int i = 0; i < 8; ++i) { | ||
bytes[i] = (byte) (1 << i); | ||
} | ||
input.putBlob(0, ByteBuffer.wrap(bytes)); | ||
|
||
ImmutableList.Builder<Expression> expressions = ImmutableList.builder(); | ||
ExpressionOperator colIdx = new VariableExpression(0); | ||
ExpressionOperator bits = new BitsetExpression(colIdx); | ||
Expression expr = new Expression("bits", bits); | ||
expressions.add(expr); | ||
|
||
Apply apply = new Apply(new BatchTupleSource(input), expressions.build()); | ||
apply.open(TestEnvVars.get()); | ||
int rowIdx = 0; | ||
while (!apply.eos()) { | ||
TupleBatch result = apply.nextReady(); | ||
if (result != null) { | ||
assertEquals(expectedResultSchema, result.getSchema()); | ||
|
||
for (int batchIdx = 0; batchIdx < result.numTuples(); ++batchIdx, ++rowIdx) { | ||
int srcByteIdx = rowIdx / 8; | ||
int srcBitIdx = rowIdx % 8; | ||
boolean trueExpected = (srcByteIdx == srcBitIdx); | ||
assertEquals(trueExpected, result.getBoolean(0, batchIdx)); | ||
} | ||
} | ||
} | ||
assertEquals(8 * 8, rowIdx); | ||
apply.close(); | ||
} | ||
} |