use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.
the class RecordFieldsUtil method processListValue.
public void processListValue(IValueReference listArg, IAType fieldType, DataOutput out, int level) throws AsterixException, IOException {
ArrayBackedValueStorage itemValue = getTempBuffer();
IARecordBuilder listRecordBuilder = getRecordBuilder();
AListPointable list = getListPointable();
list.set(listArg);
OrderedListBuilder innerListBuilder = getOrderedListBuilder();
innerListBuilder.reset(listType);
listRecordBuilder.reset(null);
AbstractCollectionType act = (AbstractCollectionType) fieldType;
int itemCount = list.getItemCount();
for (int l = 0; l < itemCount; l++) {
itemValue.reset();
listRecordBuilder.init();
byte tagId = list.getItemTag(act, l);
addFieldType(tagId, listRecordBuilder);
if (tagId == ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
ArrayBackedValueStorage tmpAbvs = getTempBuffer();
list.getItemValue(act, l, tmpAbvs.getDataOutput());
addNestedField(tmpAbvs, act.getItemType(), listRecordBuilder, level + 1);
}
listRecordBuilder.write(itemValue.getDataOutput(), true);
innerListBuilder.addItem(itemValue);
}
innerListBuilder.write(out, true);
}
use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.
the class RecordPairsDescriptor 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 {
// For writing each individual output record.
final ArrayBackedValueStorage itemStorage = new ArrayBackedValueStorage();
final DataOutput itemOutput = itemStorage.getDataOutput();
final RecordBuilder recBuilder = new RecordBuilder();
recBuilder.reset(RecordUtil.FULLY_OPEN_RECORD_TYPE);
// For writing the resulting list of records.
final OrderedListBuilder listBuilder = new OrderedListBuilder();
final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
final DataOutput resultOut = resultStorage.getDataOutput();
// Sets up the constant field names, "name" for the key field, "value" for the value field.
final ArrayBackedValueStorage nameStorage = new ArrayBackedValueStorage();
final ArrayBackedValueStorage valueStorage = new ArrayBackedValueStorage();
AObjectSerializerDeserializer serde = AObjectSerializerDeserializer.INSTANCE;
try {
serde.serialize(new AString("name"), nameStorage.getDataOutput());
serde.serialize(new AString("value"), valueStorage.getDataOutput());
} catch (IOException e) {
throw new HyracksDataException(e);
}
return new IScalarEvaluator() {
private final IScalarEvaluator argEvaluator = args[0].createScalarEvaluator(ctx);
private final IPointable argPtr = new VoidPointable();
private final ARecordVisitablePointable recordVisitablePointable = new ARecordVisitablePointable(recType);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
// Resets the result storage.
resultStorage.reset();
// Gets the input record.
argEvaluator.evaluate(tuple, argPtr);
byte inputTypeTag = argPtr.getByteArray()[argPtr.getStartOffset()];
if (inputTypeTag != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, inputTypeTag, ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
}
recordVisitablePointable.set(argPtr);
listBuilder.reset(AOrderedListType.FULL_OPEN_ORDEREDLIST_TYPE);
List<IVisitablePointable> fieldNames = recordVisitablePointable.getFieldNames();
List<IVisitablePointable> fieldValues = recordVisitablePointable.getFieldValues();
// Adds each field of the input record as a key-value pair into the result.
int numFields = recordVisitablePointable.getFieldNames().size();
for (int fieldIndex = 0; fieldIndex < numFields; ++fieldIndex) {
itemStorage.reset();
recBuilder.init();
recBuilder.addField(nameStorage, fieldNames.get(fieldIndex));
recBuilder.addField(valueStorage, fieldValues.get(fieldIndex));
recBuilder.write(itemOutput, true);
listBuilder.addItem(itemStorage);
}
// Writes the result and sets the result pointable.
listBuilder.write(resultOut, true);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.
the class RecordFieldsUtil method processRecord.
public void processRecord(ARecordPointable recordAccessor, ARecordType recType, DataOutput out, int level) throws IOException, AsterixException {
if (level == 0) {
// Resets pools for recycling objects before processing a top-level record.
resetPools();
}
ArrayBackedValueStorage itemValue = getTempBuffer();
ArrayBackedValueStorage fieldName = getTempBuffer();
OrderedListBuilder orderedListBuilder = getOrderedListBuilder();
orderedListBuilder.reset(listType);
IARecordBuilder fieldRecordBuilder = getRecordBuilder();
fieldRecordBuilder.reset(null);
int schemeFieldCount = recordAccessor.getSchemeFieldCount(recType);
for (int i = 0; i < schemeFieldCount; ++i) {
itemValue.reset();
fieldRecordBuilder.init();
// write name
fieldName.reset();
recordAccessor.getClosedFieldName(recType, i, fieldName.getDataOutput());
addNameField(fieldName, fieldRecordBuilder);
// write type
byte tag = recordAccessor.getClosedFieldTag(recType, i);
addFieldType(tag, fieldRecordBuilder);
// write open
addIsOpenField(false, fieldRecordBuilder);
// write nested or list types
if (tag == ATypeTag.SERIALIZED_RECORD_TYPE_TAG || tag == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG || tag == ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
if (!recordAccessor.isClosedFieldNull(recType, i)) {
IAType fieldType = recordAccessor.getClosedFieldType(recType, i);
ArrayBackedValueStorage tmpValue = getTempBuffer();
tmpValue.reset();
recordAccessor.getClosedFieldValue(recType, i, tmpValue.getDataOutput());
if (tag == ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
addNestedField(tmpValue, fieldType, fieldRecordBuilder, level + 1);
} else if (tag == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG || tag == ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
addListField(tmpValue, fieldType, fieldRecordBuilder, level + 1);
}
}
}
// write record
fieldRecordBuilder.write(itemValue.getDataOutput(), true);
// add item to the list of fields
orderedListBuilder.addItem(itemValue);
}
for (int i = recordAccessor.getOpenFieldCount(recType) - 1; i >= 0; --i) {
itemValue.reset();
fieldRecordBuilder.init();
// write name
fieldName.reset();
recordAccessor.getOpenFieldName(recType, i, fieldName.getDataOutput());
addNameField(fieldName, fieldRecordBuilder);
// write type
byte tag = recordAccessor.getOpenFieldTag(recType, i);
addFieldType(tag, fieldRecordBuilder);
// write open
addIsOpenField(true, fieldRecordBuilder);
// write nested or list types
if (tag == ATypeTag.SERIALIZED_RECORD_TYPE_TAG || tag == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG || tag == ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
IAType fieldType = null;
ArrayBackedValueStorage tmpValue = getTempBuffer();
tmpValue.reset();
recordAccessor.getOpenFieldValue(recType, i, tmpValue.getDataOutput());
if (tag == ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
addNestedField(tmpValue, fieldType, fieldRecordBuilder, level + 1);
} else if (tag == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG || tag == ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
addListField(tmpValue, fieldType, fieldRecordBuilder, level + 1);
}
}
// write record
fieldRecordBuilder.write(itemValue.getDataOutput(), true);
// add item to the list of fields
orderedListBuilder.addItem(itemValue);
}
orderedListBuilder.write(out, true);
}
use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.
the class TweetParser method parseArrayList.
private void parseArrayList(JsonNode jArray, DataOutput output) throws IOException {
ArrayBackedValueStorage itemBuffer = getTempBuffer();
OrderedListBuilder arrayBuilder = (OrderedListBuilder) getArrayBuilder();
arrayBuilder.reset(null);
for (int iter1 = 0; iter1 < jArray.size(); iter1++) {
itemBuffer.reset();
if (writeField(jArray.get(iter1), null, itemBuffer.getDataOutput())) {
arrayBuilder.addItem(itemBuffer);
}
}
arrayBuilder.write(output, true);
}
use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.
the class IndexTupleTranslator method getTupleFromMetadataEntity.
@Override
public ITupleReference getTupleFromMetadataEntity(Index instance) throws HyracksDataException {
// write the key in the first 3 fields of the tuple
tupleBuilder.reset();
aString.setValue(instance.getDataverseName());
stringSerde.serialize(aString, tupleBuilder.getDataOutput());
tupleBuilder.addFieldEndOffset();
aString.setValue(instance.getDatasetName());
stringSerde.serialize(aString, tupleBuilder.getDataOutput());
tupleBuilder.addFieldEndOffset();
aString.setValue(instance.getIndexName());
stringSerde.serialize(aString, tupleBuilder.getDataOutput());
tupleBuilder.addFieldEndOffset();
// write the payload in the fourth field of the tuple
recordBuilder.reset(MetadataRecordTypes.INDEX_RECORDTYPE);
// write field 0
fieldValue.reset();
aString.setValue(instance.getDataverseName());
stringSerde.serialize(aString, fieldValue.getDataOutput());
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_DATAVERSENAME_FIELD_INDEX, fieldValue);
// write field 1
fieldValue.reset();
aString.setValue(instance.getDatasetName());
stringSerde.serialize(aString, fieldValue.getDataOutput());
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_DATASETNAME_FIELD_INDEX, fieldValue);
// write field 2
fieldValue.reset();
aString.setValue(instance.getIndexName());
stringSerde.serialize(aString, fieldValue.getDataOutput());
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_INDEXNAME_FIELD_INDEX, fieldValue);
// write field 3
fieldValue.reset();
aString.setValue(instance.getIndexType().toString());
stringSerde.serialize(aString, fieldValue.getDataOutput());
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_INDEXSTRUCTURE_FIELD_INDEX, fieldValue);
// write field 4
primaryKeyListBuilder.reset((AOrderedListType) MetadataRecordTypes.INDEX_RECORDTYPE.getFieldTypes()[MetadataRecordTypes.INDEX_ARECORD_SEARCHKEY_FIELD_INDEX]);
List<List<String>> searchKey = instance.getKeyFieldNames();
for (List<String> field : searchKey) {
listBuilder.reset(stringList);
for (String subField : field) {
itemValue.reset();
aString.setValue(subField);
stringSerde.serialize(aString, itemValue.getDataOutput());
listBuilder.addItem(itemValue);
}
itemValue.reset();
listBuilder.write(itemValue.getDataOutput(), true);
primaryKeyListBuilder.addItem(itemValue);
}
fieldValue.reset();
primaryKeyListBuilder.write(fieldValue.getDataOutput(), true);
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_SEARCHKEY_FIELD_INDEX, fieldValue);
// write field 5
fieldValue.reset();
if (instance.isPrimaryIndex()) {
booleanSerde.serialize(ABoolean.TRUE, fieldValue.getDataOutput());
} else {
booleanSerde.serialize(ABoolean.FALSE, fieldValue.getDataOutput());
}
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_ISPRIMARY_FIELD_INDEX, fieldValue);
// write field 6
fieldValue.reset();
aString.setValue(Calendar.getInstance().getTime().toString());
stringSerde.serialize(aString, fieldValue.getDataOutput());
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_TIMESTAMP_FIELD_INDEX, fieldValue);
// write field 7
fieldValue.reset();
intSerde.serialize(new AInt32(instance.getPendingOp()), fieldValue.getDataOutput());
recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_PENDINGOP_FIELD_INDEX, fieldValue);
// write optional field 8
if (instance.getGramLength() > 0) {
fieldValue.reset();
nameValue.reset();
aString.setValue(GRAM_LENGTH_FIELD_NAME);
stringSerde.serialize(aString, nameValue.getDataOutput());
intSerde.serialize(new AInt32(instance.getGramLength()), fieldValue.getDataOutput());
recordBuilder.addField(nameValue, fieldValue);
}
if (instance.isEnforcingKeyFileds()) {
// write optional field 9
OrderedListBuilder typeListBuilder = new OrderedListBuilder();
typeListBuilder.reset(new AOrderedListType(BuiltinType.ANY, null));
nameValue.reset();
aString.setValue(INDEX_SEARCHKEY_TYPE_FIELD_NAME);
stringSerde.serialize(aString, nameValue.getDataOutput());
List<IAType> searchKeyType = instance.getKeyFieldTypes();
for (IAType type : searchKeyType) {
itemValue.reset();
aString.setValue(type.getTypeName());
stringSerde.serialize(aString, itemValue.getDataOutput());
typeListBuilder.addItem(itemValue);
}
fieldValue.reset();
typeListBuilder.write(fieldValue.getDataOutput(), true);
recordBuilder.addField(nameValue, fieldValue);
// write optional field 10
fieldValue.reset();
nameValue.reset();
aString.setValue(INDEX_ISENFORCED_FIELD_NAME);
stringSerde.serialize(aString, nameValue.getDataOutput());
booleanSerde.serialize(ABoolean.TRUE, fieldValue.getDataOutput());
recordBuilder.addField(nameValue, fieldValue);
}
List<Integer> keySourceIndicator = instance.getKeyFieldSourceIndicators();
boolean needSerialization = false;
if (keySourceIndicator != null) {
for (int source : keySourceIndicator) {
if (source != 0) {
needSerialization = true;
break;
}
}
}
if (needSerialization) {
listBuilder.reset(int8List);
nameValue.reset();
aString.setValue(INDEX_SEARCHKEY_SOURCE_INDICATOR_FIELD_NAME);
stringSerde.serialize(aString, nameValue.getDataOutput());
for (int source : keySourceIndicator) {
itemValue.reset();
aInt8.setValue((byte) source);
int8Serde.serialize(aInt8, itemValue.getDataOutput());
listBuilder.addItem(itemValue);
}
fieldValue.reset();
listBuilder.write(fieldValue.getDataOutput(), true);
recordBuilder.addField(nameValue, fieldValue);
}
// write record
recordBuilder.write(tupleBuilder.getDataOutput(), true);
tupleBuilder.addFieldEndOffset();
tuple.reset(tupleBuilder.getFieldEndOffsets(), tupleBuilder.getByteArray());
return tuple;
}
Aggregations