/** Initialize dictionaries for dimension values */ private void initializeAndBuildDictionaries( Schema schema, Map<String, ColumnIndexCreationInfo> columnInfo, File file) throws Exception { for (final FieldSpec spec : schema.getAllFieldSpecs()) { final ColumnIndexCreationInfo info = columnInfo.get(spec.getName()); if (info.isCreateDictionary()) { dictionaryCreatorMap.put( spec.getName(), new SegmentDictionaryCreator( info.hasNulls(), info.getSortedUniqueElementsArray(), spec, file)); } else { throw new RuntimeException("Creation of indices without dictionaries is not implemented!"); } dictionaryCreatorMap.get(spec.getName()).build(); } // Add __ALL__ to dimension dictionaries for (DimensionFieldSpec spec : schema.getDimensionFieldSpecs()) { Object allValue = StarTreeIndexNode.getAllValue(spec); if (schema.getFieldSpecFor(spec.getName()).isSingleValueField()) { Object allIndex = dictionaryCreatorMap.get(spec.getName()).indexOfSV(allValue); } else { Object allIndex = dictionaryCreatorMap.get(spec.getName()).indexOfMV(allValue); } } }
@Override public void seal() throws ConfigurationException, IOException { // Write all the aggregate rows to the aggregate segment LOG.info("Writing aggregate segment..."); long startMillis = System.currentTimeMillis(); int currentAggregateDocumentId = 0; Iterator<StarTreeTableRow> itr = starTreeBuilder.getTable().getAllCombinations(); while (itr.hasNext()) { StarTreeTableRow next = itr.next(); if (next.getDimensions().contains(StarTreeIndexNode.all())) { // Write using that document ID to all columns for (final String column : dictionaryCreatorMap.keySet()) { Object dictionaryIndex = null; // TODO: Is this okay? if (starTreeDimensionDictionary.containsKey(column)) { // Index the dimension value Integer dimensionId = starTreeDimensionDictionary.get(column); Integer dimensionValue = next.getDimensions().get(dimensionId); if (dimensionValue == StarTreeIndexNode.all()) { // Use all value Object allValue = StarTreeIndexNode.getAllValue(schema.getFieldSpecFor(column)); if (schema.getFieldSpecFor(column).isSingleValueField()) { dictionaryIndex = dictionaryCreatorMap.get(column).indexOfSV(allValue); } else { dictionaryIndex = dictionaryCreatorMap.get(column).indexOfMV(allValue); } } else { dictionaryIndex = dimensionValue; } } else if (starTreeMetricDictionary.containsKey(column)) { // Index the aggregate metric Integer metricId = starTreeMetricDictionary.get(column); Object columnValueToIndex = next.getMetrics().get(metricId); if (schema.getFieldSpecFor(column).isSingleValueField()) { dictionaryIndex = dictionaryCreatorMap.get(column).indexOfSV(columnValueToIndex); } else { dictionaryIndex = dictionaryCreatorMap.get(column).indexOfMV(columnValueToIndex); } } else { // Just index the raw value Object columnValueToIndex = StarTreeIndexNode.getAllValue(schema.getFieldSpecFor(column)); if (schema.getFieldSpecFor(column).isSingleValueField()) { dictionaryIndex = dictionaryCreatorMap.get(column).indexOfSV(columnValueToIndex); } else { dictionaryIndex = dictionaryCreatorMap.get(column).indexOfMV(columnValueToIndex); } } if (schema.getFieldSpecFor(column).isSingleValueField()) { ((SingleValueForwardIndexCreator) aggregateForwardIndexCreatorMap.get(column)) .index(currentAggregateDocumentId, (Integer) dictionaryIndex); } else { ((MultiValueForwardIndexCreator) aggregateForwardIndexCreatorMap.get(column)) .index(currentAggregateDocumentId, (int[]) dictionaryIndex); } if (config.createInvertedIndexEnabled()) { aggregateInvertedIndexCreatorMap .get(column) .add(currentAggregateDocumentId, dictionaryIndex); } } currentAggregateDocumentId++; } } long endMillis = System.currentTimeMillis(); LOG.info("Done writing aggregate segment (took {} ms)", endMillis - startMillis); for (final String column : forwardIndexCreatorMap.keySet()) { forwardIndexCreatorMap.get(column).close(); if (config.createInvertedIndexEnabled()) { invertedIndexCreatorMap.get(column).seal(); } dictionaryCreatorMap.get(column).close(); } for (final String column : aggregateForwardIndexCreatorMap.keySet()) { aggregateForwardIndexCreatorMap.get(column).close(); if (config.createInvertedIndexEnabled()) { aggregateInvertedIndexCreatorMap.get(column).seal(); } // n.b. The dictionary from raw data is used } writeMetadata(outDir, starTreeBuilder.getTotalRawDocumentCount()); // Write star tree LOG.info("Writing " + V1Constants.STARTREE_FILE); startMillis = System.currentTimeMillis(); File starTreeFile = new File(starTreeDir, V1Constants.STARTREE_FILE); OutputStream starTreeOutputStream = new FileOutputStream(starTreeFile); starTreeBuilder.getTree().writeTree(starTreeOutputStream); starTreeOutputStream.close(); endMillis = System.currentTimeMillis(); LOG.info("Wrote StarTree file (took {} ms)", endMillis - startMillis); // Copy the dictionary files into startree directory // n.b. this is done so the segment is as stand-alone as possible, though could be removed as an // optimization File[] dictionaryFiles = outDir.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(V1Constants.Dict.FILE_EXTENTION); } }); for (File dictionaryFile : dictionaryFiles) { FileUtils.copyFile(dictionaryFile, new File(starTreeDir, dictionaryFile.getName())); } // Write star tree metadata writeMetadata(starTreeDir, starTreeBuilder.getTotalAggregateDocumentCount()); }