@SuppressWarnings("unchecked") public static void writeObject(IAObject obj, DataOutput dataOutput) throws HyracksDataException { switch (obj.getType().getTypeTag()) { case RECORD: { IARecordBuilder recordBuilder = new RecordBuilder(); recordBuilder.reset((ARecordType) obj.getType()); recordBuilder.init(); writeRecord((AMutableRecord) obj, dataOutput, recordBuilder); break; } case ORDEREDLIST: { OrderedListBuilder listBuilder = new OrderedListBuilder(); listBuilder.reset((AOrderedListType) ((AMutableOrderedList) obj).getType()); IACursor cursor = ((AMutableOrderedList) obj).getCursor(); ArrayBackedValueStorage listItemValue = new ArrayBackedValueStorage(); while (cursor.next()) { listItemValue.reset(); IAObject item = cursor.get(); writeObject(item, listItemValue.getDataOutput()); listBuilder.addItem(listItemValue); } listBuilder.write(dataOutput, true); break; } case UNORDEREDLIST: { UnorderedListBuilder listBuilder = new UnorderedListBuilder(); listBuilder.reset((AUnorderedListType) ((AMutableUnorderedList) obj).getType()); IACursor cursor = ((AMutableUnorderedList) obj).getCursor(); ArrayBackedValueStorage listItemValue = new ArrayBackedValueStorage(); while (cursor.next()) { listItemValue.reset(); IAObject item = cursor.get(); writeObject(item, listItemValue.getDataOutput()); listBuilder.addItem(listItemValue); } listBuilder.write(dataOutput, true); break; } default: AqlSerializerDeserializerProvider.INSTANCE .getSerializerDeserializer(obj.getType()) .serialize(obj, dataOutput); break; } }
private void parseRecord(ARecordType recType, DataOutput out, Boolean datasetRec) throws IOException, AsterixException, AdmLexerException { ArrayBackedValueStorage fieldValueBuffer = getTempBuffer(); ArrayBackedValueStorage fieldNameBuffer = getTempBuffer(); IARecordBuilder recBuilder = getRecordBuilder(); BitSet nulls = null; if (datasetRec) { if (recType != null) { nulls = new BitSet(recType.getFieldNames().length); recBuilder.reset(recType); } else { recBuilder.reset(null); } } else if (recType != null) { nulls = new BitSet(recType.getFieldNames().length); recBuilder.reset(recType); } else { recBuilder.reset(null); } recBuilder.init(); int token; boolean inRecord = true; boolean expectingRecordField = false; boolean first = true; Boolean openRecordField = false; int fieldId = 0; IAType fieldType = null; do { token = admLexer.next(); switch (token) { case AdmLexer.TOKEN_END_RECORD: { if (expectingRecordField) { throw new ParseException("Found END_RECORD while expecting a record field."); } inRecord = false; break; } case AdmLexer.TOKEN_STRING_LITERAL: { // we've read the name of the field // now read the content fieldNameBuffer.reset(); fieldValueBuffer.reset(); expectingRecordField = false; if (recType != null) { String fldName = admLexer .getLastTokenImage() .substring(1, admLexer.getLastTokenImage().length() - 1); fieldId = recBuilder.getFieldId(fldName); if (fieldId < 0 && !recType.isOpen()) { throw new ParseException("This record is closed, you can not add extra fields !!"); } else if (fieldId < 0 && recType.isOpen()) { aStringFieldName.setValue( admLexer .getLastTokenImage() .substring(1, admLexer.getLastTokenImage().length() - 1)); stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput()); openRecordField = true; fieldType = null; } else { // a closed field nulls.set(fieldId); fieldType = recType.getFieldTypes()[fieldId]; openRecordField = false; } } else { aStringFieldName.setValue( admLexer .getLastTokenImage() .substring(1, admLexer.getLastTokenImage().length() - 1)); stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput()); openRecordField = true; fieldType = null; } token = admLexer.next(); if (token != AdmLexer.TOKEN_COLON) { throw new ParseException( "Unexpected ADM token kind: " + AdmLexer.tokenKindToString(token) + " while expecting \":\"."); } token = admLexer.next(); this.admFromLexerStream(token, fieldType, fieldValueBuffer.getDataOutput(), false); if (openRecordField) { if (fieldValueBuffer.getByteArray()[0] != ATypeTag.NULL.serialize()) { recBuilder.addField(fieldNameBuffer, fieldValueBuffer); } } else if (NonTaggedFormatUtil.isOptional(recType)) { if (fieldValueBuffer.getByteArray()[0] != ATypeTag.NULL.serialize()) { recBuilder.addField(fieldId, fieldValueBuffer); } } else { recBuilder.addField(fieldId, fieldValueBuffer); } break; } case AdmLexer.TOKEN_COMMA: { if (first) { throw new ParseException("Found COMMA before any record field."); } if (expectingRecordField) { throw new ParseException("Found COMMA while expecting a record field."); } expectingRecordField = true; break; } default: { throw new ParseException( "Unexpected ADM token kind: " + AdmLexer.tokenKindToString(token) + " while parsing record fields."); } } first = false; } while (inRecord); if (recType != null) { nullableFieldId = checkNullConstraints(recType, nulls); if (nullableFieldId != -1) { throw new ParseException( "Field: " + recType.getFieldNames()[nullableFieldId] + " can not be null"); } } recBuilder.write(out, true); }