static ByteBuffer encodeCellName()

in cassandra-four-zero/src/main/java/org/apache/cassandra/spark/reader/ReaderUtils.java [127:197]


    static ByteBuffer encodeCellName(TableMetadata metadata,
                                     ClusteringPrefix clustering,
                                     ByteBuffer columnName,
                                     ByteBuffer collectionElement)
    {
        boolean isStatic = clustering == Clustering.STATIC_CLUSTERING;

        if (!TableMetadata.Flag.isCompound(metadata.flags))
        {
            if (isStatic)
            {
                return columnName;
            }

            assert clustering.size() == 1 : "Expected clustering size to be 1, but was " + clustering.size();
            return clustering.bufferAt(0);
        }

        // We use comparator.size() rather than clustering.size() because of static clusterings
        int clusteringSize = metadata.comparator.size();
        int size = clusteringSize + (TableMetadata.Flag.isDense(metadata.flags) ? 0 : 1)
                                  + (collectionElement == null ? 0 : 1);
        if (TableMetadata.Flag.isSuper(metadata.flags))
        {
            size = clusteringSize + 1;
        }

        ByteBuffer[] values = new ByteBuffer[size];
        for (int index = 0; index < clusteringSize; index++)
        {
            if (isStatic)
            {
                values[index] = ByteBufferUtil.EMPTY_BYTE_BUFFER;
                continue;
            }

            ByteBuffer value = clustering.bufferAt(index);
            // We can have null (only for dense compound tables for backward compatibility reasons),
            // but that means we're done and should stop there as far as building the composite is concerned
            if (value == null)
            {
                return CompositeType.build(ByteBufferAccessor.instance, Arrays.copyOfRange(values, 0, index));
            }

            values[index] = value;
        }

        if (TableMetadata.Flag.isSuper(metadata.flags))
        {
            // We need to set the "column" (in thrift terms) name, i.e. the value corresponding to the subcomparator.
            // What it is depends on whether this is a cell for a declared "static" column
            // or a "dynamic" column part of the super-column internal map.
            assert columnName != null;  // This should never be null for supercolumns, see decodeForSuperColumn() above
            values[clusteringSize] = columnName.equals(SUPER_COLUMN_MAP_COLUMN)
                                     ? collectionElement
                                     : columnName;
        }
        else
        {
            if (!TableMetadata.Flag.isDense(metadata.flags))
            {
                values[clusteringSize] = columnName;
            }
            if (collectionElement != null)
            {
                values[clusteringSize + 1] = collectionElement;
            }
        }

        return CompositeType.build(ByteBufferAccessor.instance, isStatic, values);
    }