use of org.apache.asterix.om.types.AOrderedListType 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