use of java.io.DataOutput in project geode by apache.
the class DataSerializableJUnitTest method testStatArchiveCompactValueSerialization.
/**
* Test for {@link StatArchiveWriter#writeCompactValue} and
* {@link StatArchiveWriter#readCompactValue}. Also added test for
* ByteBufferInputStream#readUnsigned* methods (bug #41197).
*/
@Test
public void testStatArchiveCompactValueSerialization() throws Exception {
// test all combos of valueToTest and + and -offsets
long[] valuesToTest = new long[] { 0, Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE };
int[] offsets = new int[] { 0, 1, 4, 9, 14, 15, 16, -1, -4, -9, -14, -15, -16 };
// write all combos of longs to the outputstream
HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
DataOutput out = hdos;
for (long valueToTest : valuesToTest) {
for (int offset : offsets) {
long val = valueToTest + offset;
StatArchiveWriter.writeCompactValue(val, out);
}
}
// now read all the combos
byte[] bytes = hdos.toByteArray();
DataInput in = new DataInputStream(new ByteArrayInputStream(bytes));
for (long valueToTest : valuesToTest) {
for (int offset : offsets) {
long expectedVal = valueToTest + offset;
long val = StatArchiveWriter.readCompactValue(in);
assertEquals(expectedVal, val);
}
}
// also test using ByteBufferInputStream (bug #41197)
in = new ByteBufferInputStream(ByteBuffer.wrap(bytes));
for (long valueToTest : valuesToTest) {
for (int offset : offsets) {
long expectedVal = valueToTest + offset;
long val = StatArchiveWriter.readCompactValue(in);
assertEquals(expectedVal, val);
}
}
// now check ByteBufferInputStream#readUnsignedShort explicitly
// readUnsignedByte is already tested in StatArchiveWriter.readCompactValue
// above likely in a more thorough manner than a simple explicit test would
short[] shortValuesToTest = new short[] { 0, Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE };
ByteBufferOutputStream bos = new ByteBufferOutputStream();
out = new DataOutputStream(bos);
for (short valueToTest : shortValuesToTest) {
for (int offset : offsets) {
short val = (short) (valueToTest + offset);
out.writeShort(val);
}
}
// now read all the combos
in = new ByteBufferInputStream(bos.getContentBuffer());
for (short valueToTest : shortValuesToTest) {
for (int offset : offsets) {
int expectedVal = (valueToTest + offset) & 0xffff;
int val = in.readUnsignedShort();
assertEquals(expectedVal, val);
}
}
}
use of java.io.DataOutput in project asterixdb by apache.
the class DatetimeFromUnixTimeInSecsDescriptor method createEvaluatorFactory.
/* (non-Javadoc)
* @see org.apache.asterix.runtime.base.IScalarFunctionDynamicDescriptor#createEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.ICopyEvaluatorFactory[])
*/
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable argPtr = new VoidPointable();
private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
// possible output types
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ADateTime> datetimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
private AMutableDateTime aDatetime = new AMutableDateTime(0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval.evaluate(tuple, argPtr);
byte[] bytes = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
ATypeTag argPtrTypeTag = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]];
switch(argPtrTypeTag) {
case TINYINT:
aDatetime.setValue(AInt8SerializerDeserializer.getByte(bytes, offset + 1) * 1000l);
break;
case SMALLINT:
aDatetime.setValue(AInt16SerializerDeserializer.getShort(bytes, offset + 1) * 1000l);
break;
case INTEGER:
aDatetime.setValue(AInt32SerializerDeserializer.getInt(bytes, offset + 1) * 1000l);
break;
case BIGINT:
aDatetime.setValue(AInt64SerializerDeserializer.getLong(bytes, offset + 1) * 1000l);
break;
default:
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG);
}
datetimeSerde.serialize(aDatetime, out);
result.set(resultStorage);
}
};
}
};
}
use of java.io.DataOutput in project asterixdb by apache.
the class DayOfWeekDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable argPtr = new VoidPointable();
private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
// possible returning types
@SuppressWarnings("unchecked")
private ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
private AMutableInt64 aInt64 = new AMutableInt64(0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval.evaluate(tuple, argPtr);
byte[] bytes = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
int daysSinceAnchor;
int reminder = 0;
if (bytes[offset] == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
daysSinceAnchor = (int) (ADateTimeSerializerDeserializer.getChronon(bytes, offset + 1) / GregorianCalendarSystem.CHRONON_OF_DAY);
reminder = (int) (ADateTimeSerializerDeserializer.getChronon(bytes, offset + 1) % GregorianCalendarSystem.CHRONON_OF_DAY);
} else if (bytes[offset] == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
daysSinceAnchor = ADateSerializerDeserializer.getChronon(bytes, offset + 1);
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG, ATypeTag.SERIALIZED_DATE_TYPE_TAG);
}
// adjust the day before 1970-01-01
if (daysSinceAnchor < 0 && reminder != 0) {
daysSinceAnchor -= 1;
}
// compute the weekday (0-based, and 0 = Sunday). Adjustment is needed as
// the anchor day is Thursday.
int weekday = (daysSinceAnchor + ANCHOR_WEEKDAY) % 7;
// handle the negative weekday
if (weekday < 0) {
weekday += 7;
}
// convert from 0-based to 1-based (so 7 = Sunday)
if (weekday == 0) {
weekday = 7;
}
aInt64.setValue(weekday);
int64Serde.serialize(aInt64, out);
result.set(resultStorage);
}
};
}
};
}
use of java.io.DataOutput in project asterixdb by apache.
the class NonTaggedDataFormat method createMBRFactory.
@SuppressWarnings("unchecked")
@Override
public IScalarEvaluatorFactory[] createMBRFactory(ARecordType recType, List<String> fldName, int recordColumn, int dimension, List<String> filterFieldName, boolean isPointMBR) throws AlgebricksException {
IScalarEvaluatorFactory evalFactory = getFieldAccessEvaluatorFactory(recType, fldName, recordColumn);
int numOfFields = isPointMBR ? dimension : dimension * 2;
IScalarEvaluatorFactory[] evalFactories = new IScalarEvaluatorFactory[numOfFields + (filterFieldName == null ? 0 : 1)];
ArrayBackedValueStorage abvs1 = new ArrayBackedValueStorage();
DataOutput dos1 = abvs1.getDataOutput();
try {
AInt32 ai = new AInt32(dimension);
SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(ai.getType()).serialize(ai, dos1);
} catch (HyracksDataException e) {
throw new AlgebricksException(e);
}
IScalarEvaluatorFactory dimensionEvalFactory = new ConstantEvalFactory(Arrays.copyOf(abvs1.getByteArray(), abvs1.getLength()));
for (int i = 0; i < numOfFields; i++) {
ArrayBackedValueStorage abvs2 = new ArrayBackedValueStorage();
DataOutput dos2 = abvs2.getDataOutput();
try {
AInt32 ai = new AInt32(i);
SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(ai.getType()).serialize(ai, dos2);
} catch (HyracksDataException e) {
throw new AlgebricksException(e);
}
IScalarEvaluatorFactory coordinateEvalFactory = new ConstantEvalFactory(Arrays.copyOf(abvs2.getByteArray(), abvs2.getLength()));
evalFactories[i] = new CreateMBREvalFactory(evalFactory, dimensionEvalFactory, coordinateEvalFactory);
}
if (filterFieldName != null) {
evalFactories[numOfFields] = getFieldAccessEvaluatorFactory(recType, filterFieldName, recordColumn);
}
return evalFactories;
}
use of java.io.DataOutput in project asterixdb by apache.
the class NonTaggedDataFormat method getConstantEvalFactory.
@SuppressWarnings("unchecked")
@Override
public IScalarEvaluatorFactory getConstantEvalFactory(IAlgebricksConstantValue value) throws AlgebricksException {
IAObject obj = null;
if (value.isMissing()) {
obj = AMissing.MISSING;
} else if (value.isTrue()) {
obj = ABoolean.TRUE;
} else if (value.isFalse()) {
obj = ABoolean.FALSE;
} else {
AsterixConstantValue acv = (AsterixConstantValue) value;
obj = acv.getObject();
}
ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
DataOutput dos = abvs.getDataOutput();
try {
SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(obj.getType()).serialize(obj, dos);
} catch (HyracksDataException e) {
throw new AlgebricksException(e);
}
return new ConstantEvalFactory(Arrays.copyOf(abvs.getByteArray(), abvs.getLength()));
}
Aggregations