public SortedValueDictionary getSortedCollector()

in processing/src/main/java/org/apache/druid/segment/nested/ValueDictionary.java [152:350]


  public SortedValueDictionary getSortedCollector()
  {
    final ComparatorSortedDimensionDictionary<String> sortedStringDimensionDictionary =
        stringDictionary.sort();

    Indexed<String> strings = new Indexed<>()
    {
      @Override
      public int size()
      {
        return stringDictionary.size();
      }

      @Override
      public String get(int index)
      {
        return sortedStringDimensionDictionary.getValueFromSortedId(index);
      }

      @Override
      public int indexOf(String value)
      {
        int id = stringDictionary.getId(value);
        return id < 0
               ? DimensionDictionary.ABSENT_VALUE_ID
               : sortedStringDimensionDictionary.getSortedIdFromUnsortedId(id);
      }

      @Override
      public Iterator<String> iterator()
      {
        return IndexedIterable.create(this).iterator();
      }

      @Override
      public void inspectRuntimeShape(RuntimeShapeInspector inspector)
      {
        // nothing to inspect
      }
    };

    final ComparatorSortedDimensionDictionary<Long> sortedLongDimensionDictionary =
        longDictionary.sort();

    Indexed<Long> longs = new Indexed<>()
    {
      @Override
      public int size()
      {
        return longDictionary.size();
      }

      @Override
      public Long get(int index)
      {
        return sortedLongDimensionDictionary.getValueFromSortedId(index);
      }

      @Override
      public int indexOf(Long value)
      {
        int id = longDictionary.getId(value);
        return id < 0
               ? DimensionDictionary.ABSENT_VALUE_ID
               : sortedLongDimensionDictionary.getSortedIdFromUnsortedId(id);
      }

      @Override
      public Iterator<Long> iterator()
      {
        return IndexedIterable.create(this).iterator();
      }

      @Override
      public void inspectRuntimeShape(RuntimeShapeInspector inspector)
      {
        // nothing to inspect
      }
    };

    final ComparatorSortedDimensionDictionary<Double> sortedDoubleDimensionDictionary =
        doubleDictionary.sort();

    Indexed<Double> doubles = new Indexed<>()
    {
      @Override
      public int size()
      {
        return doubleDictionary.size();
      }

      @Override
      public Double get(int index)
      {
        return sortedDoubleDimensionDictionary.getValueFromSortedId(index);
      }

      @Override
      public int indexOf(Double value)
      {
        int id = doubleDictionary.getId(value);
        return id < 0
               ? DimensionDictionary.ABSENT_VALUE_ID
               : sortedDoubleDimensionDictionary.getSortedIdFromUnsortedId(id);
      }

      @Override
      public Iterator<Double> iterator()
      {
        return IndexedIterable.create(this).iterator();
      }

      @Override
      public void inspectRuntimeShape(RuntimeShapeInspector inspector)
      {
        // nothing to inspect
      }
    };

    // offset by 1 because nulls are ignored by the indexer, but always global id 0
    final int adjustLongs = 1 + strings.size();
    final int adjustDoubles = adjustLongs + longs.size();
    TreeSet<Object[]> sortedArrays = new TreeSet<>(new Comparator<>()
    {
      @Override
      public int compare(Object[] o1, Object[] o2)
      {
        return FrontCodedIntArrayIndexedWriter.ARRAY_COMPARATOR.compare(convertArray(o1), convertArray(o2));
      }

      @Nullable
      private int[] convertArray(Object[] array)
      {
        if (array == null) {
          return null;
        }
        final int[] globalIds = new int[array.length];
        for (int i = 0; i < array.length; i++) {
          if (array[i] == null) {
            globalIds[i] = 0;
          } else if (array[i] instanceof String) {
            // offset by 1 because nulls are ignored by the indexer, but always global id 0
            globalIds[i] = 1 + strings.indexOf((String) array[i]);
          } else if (array[i] instanceof Long) {
            globalIds[i] = longs.indexOf((Long) array[i]) + adjustLongs;
          } else if (array[i] instanceof Double) {
            globalIds[i] = doubles.indexOf((Double) array[i]) + adjustDoubles;
          } else {
            globalIds[i] = -1;
          }
          Preconditions.checkArgument(
              globalIds[i] >= 0,
              "unknown global id [%s] for value [%s]",
              globalIds[i],
              array[i]
          );
        }
        return globalIds;
      }
    });
    sortedArrays.addAll(stringArrays);
    sortedArrays.addAll(longArrays);
    sortedArrays.addAll(doubleArrays);
    Indexed<Object[]> sortedArraysIndexed = new Indexed<>()
    {
      @Override
      public Iterator<Object[]> iterator()
      {
        return sortedArrays.iterator();
      }

      @Override
      public int size()
      {
        return sortedArrays.size();
      }

      @Nullable
      @Override
      public Object[] get(int index)
      {
        return new Object[0];
      }

      @Override
      public int indexOf(@Nullable Object[] value)
      {
        throw new UnsupportedOperationException("indexOf not supported");
      }

      @Override
      public void inspectRuntimeShape(RuntimeShapeInspector inspector)
      {
        // nothing to inspect
      }
    };

    return new SortedValueDictionary(strings, longs, doubles, sortedArraysIndexed, null);
  }