IndexTupleTranslator extends AbstractTupleTranslator<Index> { @Override public ITupleReference getTupleFromMetadataEntity(Index index) throws HyracksDataException { String dataverseCanonicalName = index.getDataverseName().getCanonicalForm(); tupleBuilder.reset(); aString.setValue(dataverseCanonicalName); stringSerde.serialize(aString, tupleBuilder.getDataOutput()); tupleBuilder.addFieldEndOffset(); aString.setValue(index.getDatasetName()); stringSerde.serialize(aString, tupleBuilder.getDataOutput()); tupleBuilder.addFieldEndOffset(); aString.setValue(index.getIndexName()); stringSerde.serialize(aString, tupleBuilder.getDataOutput()); tupleBuilder.addFieldEndOffset(); recordBuilder.reset(MetadataRecordTypes.INDEX_RECORDTYPE); fieldValue.reset(); aString.setValue(dataverseCanonicalName); stringSerde.serialize(aString, fieldValue.getDataOutput()); recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_DATAVERSENAME_FIELD_INDEX, fieldValue); fieldValue.reset(); aString.setValue(index.getDatasetName()); stringSerde.serialize(aString, fieldValue.getDataOutput()); recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_DATASETNAME_FIELD_INDEX, fieldValue); fieldValue.reset(); aString.setValue(index.getIndexName()); stringSerde.serialize(aString, fieldValue.getDataOutput()); recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_INDEXNAME_FIELD_INDEX, fieldValue); fieldValue.reset(); aString.setValue(index.getIndexType().toString()); stringSerde.serialize(aString, fieldValue.getDataOutput()); recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_INDEXSTRUCTURE_FIELD_INDEX, fieldValue); primaryKeyListBuilder.reset((AOrderedListType) MetadataRecordTypes.INDEX_RECORDTYPE .getFieldTypes()[MetadataRecordTypes.INDEX_ARECORD_SEARCHKEY_FIELD_INDEX]); List<List<String>> searchKey = index.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); fieldValue.reset(); if (index.isPrimaryIndex()) { booleanSerde.serialize(ABoolean.TRUE, fieldValue.getDataOutput()); } else { booleanSerde.serialize(ABoolean.FALSE, fieldValue.getDataOutput()); } recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_ISPRIMARY_FIELD_INDEX, fieldValue); fieldValue.reset(); aString.setValue(Calendar.getInstance().getTime().toString()); stringSerde.serialize(aString, fieldValue.getDataOutput()); recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_TIMESTAMP_FIELD_INDEX, fieldValue); fieldValue.reset(); int32Serde.serialize(new AInt32(index.getPendingOp()), fieldValue.getDataOutput()); recordBuilder.addField(MetadataRecordTypes.INDEX_ARECORD_PENDINGOP_FIELD_INDEX, fieldValue); writeOpenFields(index); recordBuilder.write(tupleBuilder.getDataOutput(), true); tupleBuilder.addFieldEndOffset(); tuple.reset(tupleBuilder.getFieldEndOffsets(), tupleBuilder.getByteArray()); return tuple; } @SuppressWarnings("unchecked") protected  IndexTupleTranslator(TxnId txnId, MetadataNode metadataNode, boolean getTuple); @Override ITupleReference getTupleFromMetadataEntity(Index index); static final String GRAM_LENGTH_FIELD_NAME; static final String INDEX_SEARCHKEY_TYPE_FIELD_NAME; static final String INDEX_ISENFORCED_FIELD_NAME; static final String INDEX_SEARCHKEY_SOURCE_INDICATOR_FIELD_NAME; }
@Test public void test() throws AlgebricksException, IOException { Integer[] indicators = { 0, 1, null }; for (Integer indicator : indicators) { Map<String, String> compactionPolicyProperties = new HashMap<>(); compactionPolicyProperties.put("max-mergable-component-size", "1073741824"); compactionPolicyProperties.put("max-tolerance-component-count", "3"); InternalDatasetDetails details = new InternalDatasetDetails(FileStructure.BTREE, PartitioningStrategy.HASH, Collections.singletonList(Collections.singletonList("row_id")), Collections.singletonList(Collections.singletonList("row_id")), indicator == null ? null : Collections.singletonList(indicator), Collections.singletonList(BuiltinType.AINT64), false, null, null); DataverseName dvTest = DataverseName.createSinglePartName("test"); DataverseName dvFoo = DataverseName.createSinglePartName("foo"); DataverseName dvCB = DataverseName.createSinglePartName("CB"); Dataset dataset = new Dataset(dvTest, "d1", dvFoo, "LogType", dvCB, "MetaType", "DEFAULT_NG_ALL_NODES", "prefix", compactionPolicyProperties, details, Collections.emptyMap(), DatasetType.INTERNAL, 115, 0, CompressionManager.NONE); Index index = new Index(dvTest, "d1", "i1", IndexType.BTREE, Collections.singletonList(Collections.singletonList("row_id")), indicator == null ? null : Collections.singletonList(indicator), Collections.singletonList(BuiltinType.AINT64), -1, false, false, false, 0); MetadataNode mockMetadataNode = mock(MetadataNode.class); when(mockMetadataNode.getDatatype(any(), any(DataverseName.class), anyString())).thenReturn(new Datatype( dvTest, "d1", new ARecordType("", new String[] { "row_id" }, new IAType[] { BuiltinType.AINT64 }, true), true)); when(mockMetadataNode.getDataset(any(), any(DataverseName.class), anyString())).thenReturn(dataset); IndexTupleTranslator idxTranslator = new IndexTupleTranslator(null, mockMetadataNode, true); ITupleReference tuple = idxTranslator.getTupleFromMetadataEntity(index); Index deserializedIndex = idxTranslator.getMetadataEntityFromTuple(tuple); if (indicator == null) { Assert.assertEquals(Collections.singletonList(new Integer(0)), deserializedIndex.getKeyFieldSourceIndicators()); } else { Assert.assertEquals(index.getKeyFieldSourceIndicators(), deserializedIndex.getKeyFieldSourceIndicators()); } } }