use of java.io.DataOutput in project asterixdb by apache.
the class DatasetUtil method createDummyKeyProviderOp.
/**
* Creates a dummy key provider operator for the primary index scan.
*
* @param spec,
* the job specification.
* @param dataset,
* the dataset to scan.
* @param metadataProvider,
* the metadata provider.
* @return a dummy key provider operator.
* @throws AlgebricksException
*/
public static IOperatorDescriptor createDummyKeyProviderOp(JobSpecification spec, Dataset dataset, MetadataProvider metadataProvider) throws AlgebricksException {
Pair<IFileSplitProvider, AlgebricksPartitionConstraint> primarySplitsAndConstraint = metadataProvider.getSplitProviderAndConstraints(dataset);
AlgebricksPartitionConstraint primaryPartitionConstraint = primarySplitsAndConstraint.second;
// Build dummy tuple containing one field with a dummy value inside.
ArrayTupleBuilder tb = new ArrayTupleBuilder(1);
DataOutput dos = tb.getDataOutput();
tb.reset();
try {
// Serialize dummy value into a field.
IntegerSerializerDeserializer.INSTANCE.serialize(0, dos);
} catch (HyracksDataException e) {
throw new AsterixException(e);
}
// Add dummy field.
tb.addFieldEndOffset();
ISerializerDeserializer[] keyRecDescSers = { IntegerSerializerDeserializer.INSTANCE };
RecordDescriptor keyRecDesc = new RecordDescriptor(keyRecDescSers);
ConstantTupleSourceOperatorDescriptor keyProviderOp = new ConstantTupleSourceOperatorDescriptor(spec, keyRecDesc, tb.getFieldEndOffsets(), tb.getByteArray(), tb.getSize());
AlgebricksPartitionConstraintHelper.setPartitionConstraintInJobSpec(spec, keyProviderOp, primaryPartitionConstraint);
return keyProviderOp;
}
use of java.io.DataOutput in project asterixdb by apache.
the class AbstractIntervalLogicFuncDescriptor 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() {
protected final IntervalLogic il = new IntervalLogic();
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private TaggedValuePointable argPtr0 = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
private TaggedValuePointable argPtr1 = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
private AIntervalPointable interval0 = (AIntervalPointable) AIntervalPointable.FACTORY.createPointable();
private AIntervalPointable interval1 = (AIntervalPointable) AIntervalPointable.FACTORY.createPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
// possible output types
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ABoolean> booleanSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval0.evaluate(tuple, argPtr0);
eval1.evaluate(tuple, argPtr1);
byte typeTag0 = argPtr0.getTag();
if (typeTag0 != ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, typeTag0, ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
byte typeTag1 = argPtr0.getTag();
if (typeTag1 != ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, typeTag1, ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
argPtr0.getValue(interval0);
argPtr1.getValue(interval1);
if (typeTag0 != typeTag1) {
throw new IncompatibleTypeException(getIdentifier(), typeTag0, typeTag1);
}
ABoolean res = compareIntervals(il, interval0, interval1) ? ABoolean.TRUE : ABoolean.FALSE;
booleanSerde.serialize(res, out);
result.set(resultStorage);
}
};
}
};
}
use of java.io.DataOutput in project asterixdb by apache.
the class CalendarDurationFromDateTimeDescriptor 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 argPtr0 = new VoidPointable();
private IPointable argPtr1 = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ADuration> durationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);
private AMutableDuration aDuration = new AMutableDuration(0, 0);
private GregorianCalendarSystem calInstanct = GregorianCalendarSystem.getInstance();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval0.evaluate(tuple, argPtr0);
eval1.evaluate(tuple, argPtr1);
byte[] bytes0 = argPtr0.getByteArray();
int offset0 = argPtr0.getStartOffset();
byte[] bytes1 = argPtr1.getByteArray();
int offset1 = argPtr1.getStartOffset();
if (bytes0[offset0] != ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
}
if (bytes1[offset1] != ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DURATION_TYPE_TAG);
}
int yearMonthDurationInMonths = ADurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1);
long dayTimeDurationInMs = ADurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
long startingTimePoint = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
long endingTimePoint = DurationArithmeticOperations.addDuration(startingTimePoint, yearMonthDurationInMonths, dayTimeDurationInMs, false);
if (startingTimePoint == endingTimePoint) {
aDuration.setValue(0, 0);
} else {
boolean negative = false;
if (endingTimePoint < startingTimePoint) {
negative = true;
// swap the starting and ending time, so that ending time is always larger than the
// starting time.
long tmpTime = endingTimePoint;
endingTimePoint = startingTimePoint;
startingTimePoint = tmpTime;
}
int year0 = calInstanct.getYear(startingTimePoint);
int month0 = calInstanct.getMonthOfYear(startingTimePoint, year0);
int year1 = calInstanct.getYear(endingTimePoint);
int month1 = calInstanct.getMonthOfYear(endingTimePoint, year1);
int year = year1 - year0;
int month = month1 - month0;
int day = calInstanct.getDayOfMonthYear(endingTimePoint, year1, month1) - calInstanct.getDayOfMonthYear(startingTimePoint, year0, month0);
int hour = calInstanct.getHourOfDay(endingTimePoint) - calInstanct.getHourOfDay(startingTimePoint);
int min = calInstanct.getMinOfHour(endingTimePoint) - calInstanct.getMinOfHour(startingTimePoint);
int sec = calInstanct.getSecOfMin(endingTimePoint) - calInstanct.getSecOfMin(startingTimePoint);
int ms = calInstanct.getMillisOfSec(endingTimePoint) - calInstanct.getMillisOfSec(startingTimePoint);
if (ms < 0) {
ms += GregorianCalendarSystem.CHRONON_OF_SECOND;
sec -= 1;
}
if (sec < 0) {
sec += GregorianCalendarSystem.CHRONON_OF_MINUTE / GregorianCalendarSystem.CHRONON_OF_SECOND;
min -= 1;
}
if (min < 0) {
min += GregorianCalendarSystem.CHRONON_OF_HOUR / GregorianCalendarSystem.CHRONON_OF_MINUTE;
hour -= 1;
}
if (hour < 0) {
hour += GregorianCalendarSystem.CHRONON_OF_DAY / GregorianCalendarSystem.CHRONON_OF_HOUR;
day -= 1;
}
if (day < 0) {
boolean isLeapYear = calInstanct.isLeapYear(year1);
// need to "borrow" the days in previous month to make the day positive; when month is
// 1 (Jan), Dec will be borrowed
day += isLeapYear ? (GregorianCalendarSystem.DAYS_OF_MONTH_LEAP[(12 + month1 - 2) % 12]) : (GregorianCalendarSystem.DAYS_OF_MONTH_ORDI[(12 + month1 - 2) % 12]);
month -= 1;
}
if (month < 0) {
month += GregorianCalendarSystem.MONTHS_IN_A_YEAR;
year -= 1;
}
if (negative) {
aDuration.setValue(-1 * (year * GregorianCalendarSystem.MONTHS_IN_A_YEAR + month), -1 * (day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + min * GregorianCalendarSystem.CHRONON_OF_MINUTE + sec * GregorianCalendarSystem.CHRONON_OF_SECOND + ms));
} else {
aDuration.setValue(year * GregorianCalendarSystem.MONTHS_IN_A_YEAR + month, day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + min * GregorianCalendarSystem.CHRONON_OF_MINUTE + sec * GregorianCalendarSystem.CHRONON_OF_SECOND + ms);
}
}
durationSerde.serialize(aDuration, out);
result.set(resultStorage);
}
};
}
};
}
use of java.io.DataOutput in project asterixdb by apache.
the class StringSplitDescriptor 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() {
// Argument evaluators.
private final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);
private final IScalarEvaluator patternEval = args[1].createScalarEvaluator(ctx);
// Argument pointers.
private final IPointable argString = new VoidPointable();
private final IPointable argPattern = new VoidPointable();
private final UTF8StringPointable argStrPtr = new UTF8StringPointable();
private final UTF8StringPointable argPatternPtr = new UTF8StringPointable();
// For an output string item.
private final ArrayBackedValueStorage itemStorge = new ArrayBackedValueStorage();
private final DataOutput itemOut = itemStorge.getDataOutput();
private final byte[] tempLengthArray = new byte[5];
// For the output list of strings.
private final AOrderedListType intListType = new AOrderedListType(BuiltinType.ASTRING, null);
private final OrderedListBuilder listBuilder = new OrderedListBuilder();
private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final DataOutput out = resultStorage.getDataOutput();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
try {
resultStorage.reset();
// Calls argument evaluators.
stringEval.evaluate(tuple, argString);
patternEval.evaluate(tuple, argPattern);
// Gets the bytes of the source string.
byte[] srcString = argString.getByteArray();
int srcOffset = argString.getStartOffset();
int srcLen = argString.getLength();
// Type check for the first argument.
if (srcString[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, srcString[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
// Gets the bytes of the pattern string.
byte[] patternString = argPattern.getByteArray();
int patternOffset = argPattern.getStartOffset();
int patternLen = argPattern.getLength();
// Type check for the second argument.
if (patternString[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, patternString[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
// Sets the UTF8 String pointables.
argStrPtr.set(srcString, srcOffset + 1, srcLen - 1);
argPatternPtr.set(patternString, patternOffset + 1, patternLen - 1);
// Gets the string length of the source string.
int inputStringLen = UTF8StringUtil.getUTFLength(srcString, srcOffset + 1);
int inputStringStart = srcOffset + 1 + UTF8StringUtil.getNumBytesToStoreLength(inputStringLen);
// Gets the string length of the pattern string.
int inputPatternLen = UTF8StringUtil.getUTFLength(patternString, patternOffset + 1);
// Handles the case that the pattern is "".
boolean emptyStringPattern = inputPatternLen == 0;
// Builds a list of strings.
listBuilder.reset(intListType);
int itemStrStart = 0;
int nextMatchStart;
while (itemStrStart < inputStringLen && (nextMatchStart = UTF8StringPointable.find(argStrPtr, argPatternPtr, false, itemStrStart)) >= 0) {
// Adds an item string.
addItemString(srcString, inputStringStart, itemStrStart, emptyStringPattern ? nextMatchStart + 1 : nextMatchStart);
itemStrStart = nextMatchStart + (emptyStringPattern ? 1 : inputPatternLen);
}
if (!emptyStringPattern) {
addItemString(srcString, inputStringStart, itemStrStart, inputStringLen);
}
listBuilder.write(out, true);
result.set(resultStorage);
} catch (IOException e1) {
throw new HyracksDataException(e1);
}
}
private void addItemString(byte[] srcString, int inputStringStart, int itemStartOffset, int nextMatchStart) throws IOException {
int itemLen = nextMatchStart - itemStartOffset;
int cbytes = UTF8StringUtil.encodeUTF8Length(itemLen, tempLengthArray, 0);
itemStorge.reset();
itemOut.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
itemOut.write(tempLengthArray, 0, cbytes);
if (itemLen > 0) {
itemOut.write(srcString, inputStringStart + itemStartOffset, itemLen);
}
listBuilder.addItem(itemStorge);
}
};
}
};
}
use of java.io.DataOutput in project asterixdb by apache.
the class StringToCodePointDescriptor 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() {
protected final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
protected final DataOutput out = resultStorage.getDataOutput();
protected final IPointable argPtr = new VoidPointable();
protected final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);
protected final AOrderedListType intListType = new AOrderedListType(BuiltinType.AINT64, null);
private OrderedListBuilder listBuilder = new OrderedListBuilder();
private ArrayBackedValueStorage inputVal = new ArrayBackedValueStorage();
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
private final AMutableInt64 aInt64 = new AMutableInt64(0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
try {
resultStorage.reset();
stringEval.evaluate(tuple, argPtr);
byte[] serString = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
int len = UTF8StringUtil.getUTFLength(serString, offset + 1);
int start = offset + 1 + UTF8StringUtil.getNumBytesToStoreLength(len);
int pos = 0;
listBuilder.reset(intListType);
while (pos < len) {
int codePoint = UTF8StringUtil.UTF8ToCodePoint(serString, start + pos);
pos += UTF8StringUtil.charSize(serString, start + pos);
inputVal.reset();
aInt64.setValue(codePoint);
int64Serde.serialize(aInt64, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
}
listBuilder.write(out, true);
} else {
throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
result.set(resultStorage);
} catch (IOException e1) {
throw new HyracksDataException(e1);
}
}
};
}
};
}
Aggregations