use of org.apache.asterix.om.base.APoint in project asterixdb by apache.
the class JObjectUtil method getJType.
public static IJObject getJType(ATypeTag typeTag, IAType type, ByteArrayAccessibleDataInputStream dis, IObjectPool<IJObject, IAType> objectPool) throws IOException {
IJObject jObject;
switch(typeTag) {
case INTEGER:
{
int v = dis.readInt();
jObject = objectPool.allocate(BuiltinType.AINT32);
((JInt) jObject).setValue(v);
break;
}
case FLOAT:
{
float v = dis.readFloat();
jObject = objectPool.allocate(BuiltinType.AFLOAT);
((JFloat) jObject).setValue(v);
break;
}
case DOUBLE:
{
double value = dis.readDouble();
jObject = objectPool.allocate(BuiltinType.ADOUBLE);
((JDouble) jObject).setValue(value);
break;
}
case STRING:
{
String v = dis.readUTF();
jObject = objectPool.allocate(BuiltinType.ASTRING);
((JString) jObject).setValue(v);
break;
}
case BOOLEAN:
jObject = objectPool.allocate(BuiltinType.ABOOLEAN);
((JBoolean) jObject).setValue(dis.readBoolean());
break;
case DATE:
{
int d = dis.readInt();
jObject = objectPool.allocate(BuiltinType.ADATE);
((JDate) jObject).setValue(d);
break;
}
case DATETIME:
{
jObject = objectPool.allocate(BuiltinType.ADATETIME);
long value = dis.readLong();
((JDateTime) jObject).setValue(value);
break;
}
case DURATION:
{
jObject = objectPool.allocate(BuiltinType.ADURATION);
int months = dis.readInt();
long msecs = dis.readLong();
((JDuration) jObject).setValue(months, msecs);
break;
}
case TIME:
{
jObject = objectPool.allocate(BuiltinType.ATIME);
int time = dis.readInt();
((JTime) jObject).setValue(time);
break;
}
case INTERVAL:
{
jObject = objectPool.allocate(BuiltinType.AINTERVAL);
long start = dis.readLong();
long end = dis.readLong();
byte intervalType = dis.readByte();
((JInterval) jObject).setValue(start, end, intervalType);
break;
}
case CIRCLE:
{
jObject = objectPool.allocate(BuiltinType.ACIRCLE);
double x = dis.readDouble();
double y = dis.readDouble();
double radius = dis.readDouble();
JPoint jpoint = (JPoint) objectPool.allocate(BuiltinType.APOINT);
jpoint.setValue(x, y);
((JCircle) jObject).setValue(jpoint, radius);
break;
}
case POINT:
{
jObject = objectPool.allocate(BuiltinType.APOINT);
double x = dis.readDouble();
double y = dis.readDouble();
((JPoint) jObject).setValue(x, y);
break;
}
case POINT3D:
{
jObject = objectPool.allocate(BuiltinType.APOINT3D);
double x = dis.readDouble();
double y = dis.readDouble();
double z = dis.readDouble();
((JPoint3D) jObject).setValue(x, y, z);
break;
}
case LINE:
{
jObject = objectPool.allocate(BuiltinType.ALINE);
double x1 = dis.readDouble();
double y1 = dis.readDouble();
double x2 = dis.readDouble();
double y2 = dis.readDouble();
JPoint jpoint1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
jpoint1.setValue(x1, y1);
JPoint jpoint2 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
jpoint2.setValue(x2, y2);
((JLine) jObject).setValue(jpoint1, jpoint2);
break;
}
case POLYGON:
{
jObject = objectPool.allocate(BuiltinType.APOLYGON);
short numberOfPoints = dis.readShort();
List<JPoint> points = new ArrayList<JPoint>();
for (int i = 0; i < numberOfPoints; i++) {
JPoint p1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
p1.setValue(dis.readDouble(), dis.readDouble());
points.add(p1);
}
((JPolygon) jObject).setValue(points.toArray(new APoint[] {}));
break;
}
case RECTANGLE:
{
jObject = objectPool.allocate(BuiltinType.ARECTANGLE);
double x1 = dis.readDouble();
double y1 = dis.readDouble();
double x2 = dis.readDouble();
double y2 = dis.readDouble();
JPoint jpoint1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
jpoint1.setValue(x1, y1);
JPoint jpoint2 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
jpoint2.setValue(x2, y2);
((JRectangle) jObject).setValue(jpoint1, jpoint2);
break;
}
case MULTISET:
{
AUnorderedListType listType = (AUnorderedListType) type;
IAType elementType = listType.getItemType();
jObject = objectPool.allocate(listType);
boolean fixedSize = false;
ATypeTag tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(dis.readByte());
switch(tag) {
case STRING:
case OBJECT:
case ARRAY:
case MULTISET:
case ANY:
fixedSize = false;
break;
default:
fixedSize = true;
break;
}
// list size
dis.readInt();
int numberOfitems;
numberOfitems = dis.readInt();
if (numberOfitems <= 0) {
break;
}
if (!fixedSize) {
for (int i = 0; i < numberOfitems; i++) {
dis.readInt();
}
}
for (int i = 0; i < numberOfitems; i++) {
IJObject v = getJType(elementType.getTypeTag(), elementType, dis, objectPool);
((JUnorderedList) jObject).add(v);
}
break;
}
case ARRAY:
{
AOrderedListType listType = (AOrderedListType) type;
IAType elementType = listType.getItemType();
jObject = objectPool.allocate(listType);
boolean fixedSize = false;
ATypeTag tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(dis.readByte());
switch(tag) {
case STRING:
case OBJECT:
case ARRAY:
case MULTISET:
case ANY:
fixedSize = false;
break;
default:
fixedSize = true;
break;
}
// list size
dis.readInt();
int numberOfitems;
numberOfitems = dis.readInt();
if (numberOfitems <= 0) {
break;
}
if (!fixedSize) {
for (int i = 0; i < numberOfitems; i++) {
dis.readInt();
}
}
for (int i = 0; i < numberOfitems; i++) {
IJObject v = getJType(elementType.getTypeTag(), elementType, dis, objectPool);
((JOrderedList) jObject).add(v);
}
break;
}
case OBJECT:
ARecordType recordType = (ARecordType) type;
int numberOfSchemaFields = recordType.getFieldTypes().length;
byte[] recordBits = dis.getInputStream().getArray();
boolean isExpanded = false;
dis.getInputStream();
int[] fieldOffsets = new int[numberOfSchemaFields];
IJObject[] closedFields = new IJObject[numberOfSchemaFields];
// reading length is not required.
dis.skip(4);
if (recordType.isOpen()) {
isExpanded = dis.readBoolean();
if (isExpanded) {
dis.readInt();
} else {
}
} else {
}
if (numberOfSchemaFields > 0) {
dis.readInt();
int nullBitMapOffset = 0;
boolean hasOptionalFields = NonTaggedFormatUtil.hasOptionalField(recordType);
if (hasOptionalFields) {
nullBitMapOffset = dis.getInputStream().getPosition();
dis.getInputStream();
} else {
dis.getInputStream();
}
for (int i = 0; i < numberOfSchemaFields; i++) {
fieldOffsets[i] = dis.readInt();
}
for (int fieldNumber = 0; fieldNumber < numberOfSchemaFields; fieldNumber++) {
if (hasOptionalFields) {
byte b1 = recordBits[nullBitMapOffset + fieldNumber / 8];
int p = 1 << (7 - (fieldNumber % 8));
if ((b1 & p) == 0) {
continue;
}
}
IAType[] fieldTypes = recordType.getFieldTypes();
ATypeTag fieldValueTypeTag = null;
IAType fieldType = fieldTypes[fieldNumber];
if (fieldTypes[fieldNumber].getTypeTag() == ATypeTag.UNION) {
if (((AUnionType) fieldTypes[fieldNumber]).isUnknownableType()) {
fieldType = ((AUnionType) fieldTypes[fieldNumber]).getActualType();
fieldValueTypeTag = fieldType.getTypeTag();
}
} else {
fieldValueTypeTag = fieldTypes[fieldNumber].getTypeTag();
}
closedFields[fieldNumber] = getJType(fieldValueTypeTag, fieldType, dis, objectPool);
}
}
if (isExpanded) {
int numberOfOpenFields = dis.readInt();
String[] fieldNames = new String[numberOfOpenFields];
IAType[] fieldTypes = new IAType[numberOfOpenFields];
IJObject[] openFields = new IJObject[numberOfOpenFields];
for (int i = 0; i < numberOfOpenFields; i++) {
dis.readInt();
dis.readInt();
}
for (int i = 0; i < numberOfOpenFields; i++) {
fieldNames[i] = AStringSerializerDeserializer.INSTANCE.deserialize(dis).getStringValue();
ATypeTag openFieldTypeTag = SerializerDeserializerUtil.deserializeTag(dis);
openFields[i] = getJType(openFieldTypeTag, null, dis, objectPool);
fieldTypes[i] = openFields[i].getIAObject().getType();
}
ARecordType openPartRecType = new ARecordType(null, fieldNames, fieldTypes, true);
if (numberOfSchemaFields > 0) {
ARecordType mergedRecordType = mergeRecordTypes(recordType, openPartRecType);
IJObject[] mergedFields = mergeFields(closedFields, openFields);
jObject = objectPool.allocate(recordType);
return new JRecord(mergedRecordType, mergedFields);
} else {
return new JRecord(recordType, openFields);
}
} else {
return new JRecord(recordType, closedFields);
}
default:
throw new RuntimeDataException(ErrorCode.LIBRARY_JOBJECT_UTIL_ILLEGAL_ARGU_TYPE, typeTag);
}
return jObject;
}
use of org.apache.asterix.om.base.APoint in project asterixdb by apache.
the class ARectangleSerializerDeserializer method deserialize.
@Override
public ARectangle deserialize(DataInput in) throws HyracksDataException {
try {
APoint p1 = APointSerializerDeserializer.INSTANCE.deserialize(in);
APoint p2 = APointSerializerDeserializer.INSTANCE.deserialize(in);
return new ARectangle(p1, p2);
} catch (IOException e) {
throw new HyracksDataException(e);
}
}
use of org.apache.asterix.om.base.APoint in project asterixdb by apache.
the class ACircleSerializerDeserializer method deserialize.
@Override
public ACircle deserialize(DataInput in) throws HyracksDataException {
try {
APoint center = APointSerializerDeserializer.INSTANCE.deserialize(in);
double radius = ADoubleSerializerDeserializer.INSTANCE.deserialize(in).getDoubleValue();
return new ACircle(center, radius);
} catch (IOException e) {
throw new HyracksDataException(e);
}
}
use of org.apache.asterix.om.base.APoint in project asterixdb by apache.
the class LineRectanglePolygonAccessor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final DataOutput out = resultStorage.getDataOutput();
private final IPointable argPtr = new VoidPointable();
private final IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
private final OrderedListBuilder listBuilder = new OrderedListBuilder();
private final ArrayBackedValueStorage inputVal = new ArrayBackedValueStorage();
private final AOrderedListType pointListType = new AOrderedListType(BuiltinType.APOINT, null);
private final AMutablePoint aPoint = new AMutablePoint(0, 0);
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<APoint> pointSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.APOINT);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
eval.evaluate(tuple, argPtr);
byte[] bytes = argPtr.getByteArray();
int startOffset = argPtr.getStartOffset();
resultStorage.reset();
try {
if (bytes[startOffset] == ATypeTag.SERIALIZED_LINE_TYPE_TAG) {
listBuilder.reset(pointListType);
inputVal.reset();
double startX = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getStartPointCoordinateOffset(Coordinate.X));
double startY = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getStartPointCoordinateOffset(Coordinate.Y));
aPoint.setValue(startX, startY);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
inputVal.reset();
double endX = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getEndPointCoordinateOffset(Coordinate.X));
double endY = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ALineSerializerDeserializer.getEndPointCoordinateOffset(Coordinate.Y));
aPoint.setValue(endX, endY);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
listBuilder.write(out, true);
} else if (bytes[startOffset] == ATypeTag.SERIALIZED_RECTANGLE_TYPE_TAG) {
listBuilder.reset(pointListType);
inputVal.reset();
double x1 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getBottomLeftCoordinateOffset(Coordinate.X));
double y1 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getBottomLeftCoordinateOffset(Coordinate.Y));
aPoint.setValue(x1, y1);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
inputVal.reset();
double x2 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getUpperRightCoordinateOffset(Coordinate.X));
double y2 = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + ARectangleSerializerDeserializer.getUpperRightCoordinateOffset(Coordinate.Y));
aPoint.setValue(x2, y2);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
listBuilder.write(out, true);
} else if (bytes[startOffset] == ATypeTag.SERIALIZED_POLYGON_TYPE_TAG) {
int numOfPoints = AInt16SerializerDeserializer.getShort(bytes, startOffset + APolygonSerializerDeserializer.getNumberOfPointsOffset());
if (numOfPoints < 3) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_POLYGON_TYPE_TAG);
}
listBuilder.reset(pointListType);
for (int i = 0; i < numOfPoints; ++i) {
inputVal.reset();
double x = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + APolygonSerializerDeserializer.getCoordinateOffset(i, Coordinate.X));
double y = ADoubleSerializerDeserializer.getDouble(bytes, startOffset + APolygonSerializerDeserializer.getCoordinateOffset(i, Coordinate.Y));
aPoint.setValue(x, y);
pointSerde.serialize(aPoint, inputVal.getDataOutput());
listBuilder.addItem(inputVal);
}
listBuilder.write(out, true);
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_LINE_TYPE_TAG, ATypeTag.SERIALIZED_RECTANGLE_TYPE_TAG, ATypeTag.SERIALIZED_POLYGON_TYPE_TAG);
}
} catch (IOException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.APoint in project asterixdb by apache.
the class CreatePointDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable inputArg0 = new VoidPointable();
private IPointable inputArg1 = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
private AMutablePoint aPoint = new AMutablePoint(0, 0);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<APoint> pointSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.APOINT);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
eval0.evaluate(tuple, inputArg0);
eval1.evaluate(tuple, inputArg1);
byte[] bytes0 = inputArg0.getByteArray();
int offset0 = inputArg0.getStartOffset();
byte[] bytes1 = inputArg1.getByteArray();
int offset1 = inputArg1.getStartOffset();
// type-check: (double, double)
if (bytes0[offset0] != ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
}
if (bytes1[offset1] != ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
}
resultStorage.reset();
try {
aPoint.setValue(ADoubleSerializerDeserializer.getDouble(bytes0, offset0 + 1), ADoubleSerializerDeserializer.getDouble(bytes1, offset1 + 1));
pointSerde.serialize(aPoint, out);
} catch (IOException e1) {
throw new HyracksDataException(e1);
}
result.set(resultStorage);
}
};
}
};
}
Aggregations