use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.
the class InternalDatasetDetails method writePropertyTypeRecord.
protected void writePropertyTypeRecord(String name, String value, DataOutput out, ARecordType recordType) throws HyracksDataException {
IARecordBuilder propertyRecordBuilder = new RecordBuilder();
ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
propertyRecordBuilder.reset(recordType);
AMutableString aString = new AMutableString("");
@SuppressWarnings("unchecked") ISerializerDeserializer<AString> stringSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
// write field 0
fieldValue.reset();
aString.setValue(name);
stringSerde.serialize(aString, fieldValue.getDataOutput());
propertyRecordBuilder.addField(0, fieldValue);
// write field 1
fieldValue.reset();
aString.setValue(value);
stringSerde.serialize(aString, fieldValue.getDataOutput());
propertyRecordBuilder.addField(1, fieldValue);
propertyRecordBuilder.write(out, true);
}
use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.
the class TweetParser method writeRecord.
public void writeRecord(JsonNode obj, DataOutput out, ARecordType curRecType) throws IOException {
IAType[] curTypes = null;
String[] curFNames = null;
int fieldN;
int attrIdx;
int expectedFieldsCount = 0;
ArrayBackedValueStorage fieldValueBuffer = getTempBuffer();
ArrayBackedValueStorage fieldNameBuffer = getTempBuffer();
IARecordBuilder recBuilder = getRecordBuilder();
if (curRecType != null) {
curTypes = curRecType.getFieldTypes();
curFNames = curRecType.getFieldNames();
for (IAType curType : curTypes) {
if (!(curType instanceof AUnionType)) {
expectedFieldsCount++;
}
}
}
recBuilder.reset(curRecType);
recBuilder.init();
if (curRecType != null && !curRecType.isOpen()) {
// closed record type
fieldN = curFNames.length;
for (int iter1 = 0; iter1 < fieldN; iter1++) {
fieldValueBuffer.reset();
DataOutput fieldOutput = fieldValueBuffer.getDataOutput();
if (obj.get(curFNames[iter1]).isNull() && !(curTypes[iter1] instanceof AUnionType)) {
if (curRecType.isClosedField(curFNames[iter1])) {
throw new RuntimeDataException(ErrorCode.PARSER_TWEET_PARSER_CLOSED_FIELD_NULL, curFNames[iter1]);
} else {
continue;
}
} else {
if (writeField(obj.get(curFNames[iter1]), curTypes[iter1], fieldOutput)) {
recBuilder.addField(iter1, fieldValueBuffer);
}
}
}
} else {
//open record type
int closedFieldCount = 0;
IAType curFieldType = null;
String attrName;
Iterator<String> iter = obj.fieldNames();
while (iter.hasNext()) {
attrName = iter.next();
if (obj.get(attrName) == null || obj.get(attrName).isNull() || obj.size() == 0) {
continue;
}
attrIdx = checkAttrNameIdx(curFNames, attrName);
if (curRecType != null) {
curFieldType = curRecType.getFieldType(attrName);
}
fieldValueBuffer.reset();
fieldNameBuffer.reset();
DataOutput fieldOutput = fieldValueBuffer.getDataOutput();
if (writeField(obj.get(attrName), curFieldType, fieldOutput)) {
if (attrIdx == -1) {
aString.setValue(attrName);
stringSerde.serialize(aString, fieldNameBuffer.getDataOutput());
recBuilder.addField(fieldNameBuffer, fieldValueBuffer);
} else {
recBuilder.addField(attrIdx, fieldValueBuffer);
closedFieldCount++;
}
}
}
if (curRecType != null && closedFieldCount < expectedFieldsCount) {
throw new HyracksDataException("Non-null field is null");
}
}
recBuilder.write(out, true);
}
use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.
the class ClassAdParser method parseRecord.
private void parseRecord(ARecordType recType, ClassAd pAd, DataOutput out) throws IOException, AsterixException {
ArrayBackedValueStorage fieldValueBuffer = getTempBuffer();
ArrayBackedValueStorage fieldNameBuffer = getTempBuffer();
IARecordBuilder recBuilder = getRecordBuilder();
BitSet nulls = null;
if (recType != null) {
nulls = getBitSet();
recBuilder.reset(recType);
} else {
recBuilder.reset(null);
}
recBuilder.init();
Boolean openRecordField = false;
int fieldId = 0;
IAType fieldType = null;
// new stuff
Map<CaseInsensitiveString, ExprTree> attrs = pAd.getAttrList();
for (Entry<CaseInsensitiveString, ExprTree> entry : attrs.entrySet()) {
// reset buffers
fieldNameBuffer.reset();
fieldValueBuffer.reset();
// take care of field name
String fldName = entry.getKey().get();
if (recType != null) {
fieldId = recBuilder.getFieldId(fldName);
if (fieldId < 0 && !recType.isOpen()) {
throw new HyracksDataException("This record is closed, you can not add extra fields !!");
} else if (fieldId < 0 && recType.isOpen()) {
aStringFieldName.setValue(fldName);
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(fldName);
stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput());
openRecordField = true;
fieldType = null;
}
// add field value to value buffer
writeFieldValueToBuffer(fieldType, fieldValueBuffer.getDataOutput(), fldName, entry.getValue(), pAd);
if (openRecordField) {
if (fieldValueBuffer.getByteArray()[0] != ATypeTag.MISSING.serialize()) {
recBuilder.addField(fieldNameBuffer, fieldValueBuffer);
}
} else if (NonTaggedFormatUtil.isOptional(fieldType)) {
if (fieldValueBuffer.getByteArray()[0] != ATypeTag.MISSING.serialize()) {
recBuilder.addField(fieldId, fieldValueBuffer);
}
} else {
recBuilder.addField(fieldId, fieldValueBuffer);
}
}
if (recType != null) {
int optionalFieldId = checkOptionalConstraints(recType, nulls);
if (optionalFieldId != -1) {
throw new HyracksDataException("Field: " + recType.getFieldNames()[optionalFieldId] + " can not be optional");
}
}
recBuilder.write(out, true);
}
use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.
the class ARecordSerializerDeserializer method serialize.
// This serialize method will NOT work if <code>recordType</code> is not equal to the type of the instance.
@SuppressWarnings("unchecked")
public void serialize(ARecord instance, DataOutput out, boolean writeTypeTag) throws HyracksDataException {
IARecordBuilder recordBuilder = new RecordBuilder();
ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
recordBuilder.reset(recordType);
recordBuilder.init();
if (recordType != null) {
int fieldIndex = 0;
for (; fieldIndex < recordType.getFieldNames().length; ++fieldIndex) {
fieldValue.reset();
serializers[fieldIndex].serialize(instance.getValueByPos(fieldIndex), fieldValue.getDataOutput());
recordBuilder.addField(fieldIndex, fieldValue);
}
recordBuilder.write(out, writeTypeTag);
} else {
serializeSchemalessRecord(instance, out, writeTypeTag);
}
}
use of org.apache.asterix.builders.IARecordBuilder in project asterixdb by apache.
the class ExternalDatasetDetails method writeDatasetDetailsRecordType.
@SuppressWarnings("unchecked")
@Override
public void writeDatasetDetailsRecordType(DataOutput out) throws HyracksDataException {
IARecordBuilder externalRecordBuilder = new RecordBuilder();
OrderedListBuilder listBuilder = new OrderedListBuilder();
ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
ArrayBackedValueStorage itemValue = new ArrayBackedValueStorage();
externalRecordBuilder.reset(MetadataRecordTypes.EXTERNAL_DETAILS_RECORDTYPE);
AMutableString aString = new AMutableString("");
ISerializerDeserializer<AString> stringSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
ISerializerDeserializer<ADateTime> dateTimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
ISerializerDeserializer<AInt32> intSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
// write field 0
fieldValue.reset();
aString.setValue(this.getAdapter());
stringSerde.serialize(aString, fieldValue.getDataOutput());
externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_DATASOURCE_ADAPTER_FIELD_INDEX, fieldValue);
// write field 1
listBuilder.reset((AOrderedListType) MetadataRecordTypes.EXTERNAL_DETAILS_RECORDTYPE.getFieldTypes()[1]);
for (Map.Entry<String, String> property : this.properties.entrySet()) {
String name = property.getKey();
String value = property.getValue();
itemValue.reset();
DatasetUtil.writePropertyTypeRecord(name, value, itemValue.getDataOutput(), MetadataRecordTypes.DATASOURCE_ADAPTER_PROPERTIES_RECORDTYPE);
listBuilder.addItem(itemValue);
}
fieldValue.reset();
listBuilder.write(fieldValue.getDataOutput(), true);
externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_PROPERTIES_FIELD_INDEX, fieldValue);
// write field 2
fieldValue.reset();
dateTimeSerde.serialize(new ADateTime(lastRefreshTime.getTime()), fieldValue.getDataOutput());
externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_LAST_REFRESH_TIME_FIELD_INDEX, fieldValue);
// write field 3
fieldValue.reset();
intSerde.serialize(new AInt32(state.ordinal()), fieldValue.getDataOutput());
externalRecordBuilder.addField(MetadataRecordTypes.EXTERNAL_DETAILS_ARECORD_TRANSACTION_STATE_FIELD_INDEX, fieldValue);
externalRecordBuilder.write(out, true);
}
Aggregations