|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.compositeindex.datacube.startree.meta; |
| 10 | + |
| 11 | +import org.apache.lucene.codecs.lucene99.Lucene99Codec; |
| 12 | +import org.apache.lucene.index.DocValuesType; |
| 13 | +import org.apache.lucene.index.FieldInfo; |
| 14 | +import org.apache.lucene.index.FieldInfos; |
| 15 | +import org.apache.lucene.index.IndexOptions; |
| 16 | +import org.apache.lucene.index.SegmentInfo; |
| 17 | +import org.apache.lucene.index.SegmentWriteState; |
| 18 | +import org.apache.lucene.index.VectorEncoding; |
| 19 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 20 | +import org.apache.lucene.store.Directory; |
| 21 | +import org.apache.lucene.store.IOContext; |
| 22 | +import org.apache.lucene.store.IndexInput; |
| 23 | +import org.apache.lucene.store.IndexOutput; |
| 24 | +import org.apache.lucene.util.InfoStream; |
| 25 | +import org.apache.lucene.util.Version; |
| 26 | +import org.opensearch.index.compositeindex.CompositeIndexMetadata; |
| 27 | +import org.opensearch.index.compositeindex.datacube.Dimension; |
| 28 | +import org.opensearch.index.compositeindex.datacube.Metric; |
| 29 | +import org.opensearch.index.compositeindex.datacube.MetricStat; |
| 30 | +import org.opensearch.index.compositeindex.datacube.NumericDimension; |
| 31 | +import org.opensearch.index.compositeindex.datacube.startree.StarTreeField; |
| 32 | +import org.opensearch.index.compositeindex.datacube.startree.StarTreeFieldConfiguration; |
| 33 | +import org.opensearch.index.compositeindex.datacube.startree.aggregators.MetricAggregatorInfo; |
| 34 | +import org.opensearch.index.compositeindex.datacube.startree.aggregators.MetricEntry; |
| 35 | +import org.opensearch.index.compositeindex.datacube.startree.utils.StarTreeBuilderUtils; |
| 36 | +import org.opensearch.index.fielddata.IndexNumericFieldData; |
| 37 | +import org.opensearch.test.OpenSearchTestCase; |
| 38 | +import org.junit.Before; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | +import java.nio.charset.StandardCharsets; |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.HashMap; |
| 45 | +import java.util.HashSet; |
| 46 | +import java.util.List; |
| 47 | +import java.util.UUID; |
| 48 | + |
| 49 | +import static org.opensearch.index.compositeindex.CompositeIndexConstants.MAGIC_MARKER; |
| 50 | +import static org.opensearch.index.mapper.CompositeMappedFieldType.CompositeFieldType.STAR_TREE; |
| 51 | + |
| 52 | +public class StarTreeMetaTests extends OpenSearchTestCase { |
| 53 | + |
| 54 | + private IndexOutput metaOut; |
| 55 | + private IndexInput metaIn; |
| 56 | + private StarTreeField starTreeField; |
| 57 | + private SegmentWriteState writeState; |
| 58 | + private Directory directory; |
| 59 | + private FieldInfo[] fieldsInfo; |
| 60 | + private List<Dimension> dimensionsOrder; |
| 61 | + private List<String> fields = List.of(); |
| 62 | + private List<Metric> metrics; |
| 63 | + private List<MetricAggregatorInfo> metricAggregatorInfos = new ArrayList<>(); |
| 64 | + private int segmentDocumentCount; |
| 65 | + private long dataFilePointer; |
| 66 | + private long dataFileLength; |
| 67 | + |
| 68 | + @Before |
| 69 | + public void setup() throws IOException { |
| 70 | + fields = List.of("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10"); |
| 71 | + directory = newFSDirectory(createTempDir()); |
| 72 | + SegmentInfo segmentInfo = new SegmentInfo( |
| 73 | + directory, |
| 74 | + Version.LATEST, |
| 75 | + Version.LUCENE_9_11_0, |
| 76 | + "test_segment", |
| 77 | + 6, |
| 78 | + false, |
| 79 | + false, |
| 80 | + new Lucene99Codec(), |
| 81 | + new HashMap<>(), |
| 82 | + UUID.randomUUID().toString().substring(0, 16).getBytes(StandardCharsets.UTF_8), |
| 83 | + new HashMap<>(), |
| 84 | + null |
| 85 | + ); |
| 86 | + |
| 87 | + fieldsInfo = new FieldInfo[fields.size()]; |
| 88 | + for (int i = 0; i < fieldsInfo.length; i++) { |
| 89 | + fieldsInfo[i] = new FieldInfo( |
| 90 | + fields.get(i), |
| 91 | + i, |
| 92 | + false, |
| 93 | + false, |
| 94 | + true, |
| 95 | + IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, |
| 96 | + DocValuesType.SORTED_NUMERIC, |
| 97 | + -1, |
| 98 | + Collections.emptyMap(), |
| 99 | + 0, |
| 100 | + 0, |
| 101 | + 0, |
| 102 | + 0, |
| 103 | + VectorEncoding.FLOAT32, |
| 104 | + VectorSimilarityFunction.EUCLIDEAN, |
| 105 | + false, |
| 106 | + false |
| 107 | + ); |
| 108 | + } |
| 109 | + FieldInfos fieldInfos = new FieldInfos(fieldsInfo); |
| 110 | + writeState = new SegmentWriteState(InfoStream.getDefault(), segmentInfo.dir, segmentInfo, fieldInfos, null, newIOContext(random())); |
| 111 | + } |
| 112 | + |
| 113 | + public void test_starTreeMetadata() throws IOException { |
| 114 | + dimensionsOrder = List.of( |
| 115 | + new NumericDimension("field1"), |
| 116 | + new NumericDimension("field3"), |
| 117 | + new NumericDimension("field5"), |
| 118 | + new NumericDimension("field8") |
| 119 | + ); |
| 120 | + metrics = List.of( |
| 121 | + new Metric("field2", List.of(MetricStat.SUM)), |
| 122 | + new Metric("field4", List.of(MetricStat.SUM)), |
| 123 | + new Metric("field6", List.of(MetricStat.COUNT)), |
| 124 | + new Metric("field9", List.of(MetricStat.MIN)), |
| 125 | + new Metric("field10", List.of(MetricStat.MAX)) |
| 126 | + ); |
| 127 | + int maxLeafDocs = randomInt(); |
| 128 | + StarTreeFieldConfiguration starTreeFieldConfiguration = new StarTreeFieldConfiguration( |
| 129 | + maxLeafDocs, |
| 130 | + new HashSet<>(), |
| 131 | + StarTreeFieldConfiguration.StarTreeBuildMode.ON_HEAP |
| 132 | + ); |
| 133 | + starTreeField = new StarTreeField("star_tree", dimensionsOrder, metrics, starTreeFieldConfiguration); |
| 134 | + |
| 135 | + for (Metric metric : metrics) { |
| 136 | + for (MetricStat metricType : metric.getMetrics()) { |
| 137 | + MetricAggregatorInfo metricAggregatorInfo = new MetricAggregatorInfo( |
| 138 | + metricType, |
| 139 | + metric.getField(), |
| 140 | + starTreeField.getName(), |
| 141 | + IndexNumericFieldData.NumericType.DOUBLE |
| 142 | + ); |
| 143 | + metricAggregatorInfos.add(metricAggregatorInfo); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + dataFileLength = randomLong(); |
| 148 | + dataFilePointer = randomLong(); |
| 149 | + segmentDocumentCount = randomInt(); |
| 150 | + metaOut = directory.createOutput("star-tree-metadata", IOContext.DEFAULT); |
| 151 | + StarTreeBuilderUtils.serializeStarTreeMetadata( |
| 152 | + metaOut, |
| 153 | + starTreeField, |
| 154 | + writeState, |
| 155 | + metricAggregatorInfos, |
| 156 | + segmentDocumentCount, |
| 157 | + dataFilePointer, |
| 158 | + dataFileLength |
| 159 | + ); |
| 160 | + metaOut.close(); |
| 161 | + metaIn = directory.openInput("star-tree-metadata", IOContext.READONCE); |
| 162 | + assertEquals(MAGIC_MARKER, metaIn.readLong()); |
| 163 | + |
| 164 | + CompositeIndexMetadata compositeIndexMetadata = new CompositeIndexMetadata(metaIn); |
| 165 | + assertEquals(starTreeField.getName(), compositeIndexMetadata.getCompositeFieldName()); |
| 166 | + assertEquals(STAR_TREE, compositeIndexMetadata.getCompositeFieldType()); |
| 167 | + |
| 168 | + StarTreeMetadata starTreeMetadata = compositeIndexMetadata.getStarTreeMetadata(); |
| 169 | + assertNotNull(starTreeMetadata); |
| 170 | + |
| 171 | + for (int i = 0; i < dimensionsOrder.size(); i++) { |
| 172 | + assertEquals( |
| 173 | + writeState.fieldInfos.fieldInfo(dimensionsOrder.get(i).getField()).getFieldNumber(), |
| 174 | + starTreeMetadata.getDimensionFieldNumbers().get(i), |
| 175 | + 0 |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + for (int i = 0; i < metricAggregatorInfos.size(); i++) { |
| 180 | + MetricEntry metricEntry = starTreeMetadata.getMetricEntries().get(i); |
| 181 | + assertEquals(metricAggregatorInfos.get(i).getField(), metricEntry.getMetricName()); |
| 182 | + assertEquals(metricAggregatorInfos.get(i).getMetricStat(), metricEntry.getMetricStat()); |
| 183 | + } |
| 184 | + assertEquals(segmentDocumentCount, starTreeMetadata.getSegmentAggregatedDocCount(), 0); |
| 185 | + assertEquals(maxLeafDocs, starTreeMetadata.getMaxLeafDocs(), 0); |
| 186 | + assertEquals( |
| 187 | + starTreeFieldConfiguration.getSkipStarNodeCreationInDims().size(), |
| 188 | + starTreeMetadata.getSkipStarNodeCreationInDims().size() |
| 189 | + ); |
| 190 | + for (String skipStarNodeCreationInDims : starTreeField.getStarTreeConfig().getSkipStarNodeCreationInDims()) { |
| 191 | + Integer skipStarNodeCreationInDimsFieldNumber = writeState.fieldInfos.fieldInfo(skipStarNodeCreationInDims).getFieldNumber(); |
| 192 | + assertTrue(starTreeMetadata.getSkipStarNodeCreationInDims().contains(skipStarNodeCreationInDimsFieldNumber)); |
| 193 | + } |
| 194 | + assertEquals(starTreeFieldConfiguration.getBuildMode(), starTreeMetadata.getStarTreeBuildMode()); |
| 195 | + assertEquals(dataFileLength, starTreeMetadata.getDataLength()); |
| 196 | + assertEquals(dataFilePointer, starTreeMetadata.getDataStartFilePointer()); |
| 197 | + |
| 198 | + metaIn.close(); |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public void tearDown() throws Exception { |
| 204 | + super.tearDown(); |
| 205 | + metaOut.close(); |
| 206 | + metaIn.close(); |
| 207 | + directory.close(); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments