All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.spi.SplitWeight Maven / Gradle / Ivy

There is a newer version: 458
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.spi;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.errorprone.annotations.DoNotCall;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.function.Function;

import static io.airlift.slice.SizeOf.instanceSize;
import static java.lang.Math.addExact;
import static java.lang.Math.multiplyExact;

public final class SplitWeight
{
    private static final int INSTANCE_SIZE = instanceSize(SplitWeight.class);

    private static final long UNIT_VALUE = 100;
    private static final int UNIT_SCALE = 2; // Decimal scale such that (10 ^ UNIT_SCALE) == UNIT_VALUE
    private static final SplitWeight STANDARD_WEIGHT = new SplitWeight(UNIT_VALUE);

    private final long value;

    private SplitWeight(long value)
    {
        if (value <= 0) {
            throw new IllegalArgumentException("value must be > 0, found: " + value);
        }
        this.value = value;
    }

    /**
     * @return The internal integer representation for this weight value
     */
    @JsonValue
    public long getRawValue()
    {
        return value;
    }

    @Override
    public boolean equals(Object other)
    {
        if (!(other instanceof SplitWeight)) {
            return false;
        }
        return this.value == ((SplitWeight) other).value;
    }

    @Override
    public int hashCode()
    {
        return Long.hashCode(value);
    }

    @Override
    public String toString()
    {
        if (value == UNIT_VALUE) {
            return "1";
        }
        return BigDecimal.valueOf(value, -UNIT_SCALE).stripTrailingZeros().toPlainString();
    }

    /**
     * Produces a {@link SplitWeight} from the raw internal value representation. This method is intended
     * primarily for JSON deserialization, and connectors should use not call this factory method directly
     * to construct {@link SplitWeight} instances. Instead, connectors should use {@link SplitWeight#fromProportion(double)}
     * to avoid breakages that could arise if {@link SplitWeight#UNIT_VALUE} changes in the future.
     */
    @JsonCreator
    @DoNotCall // For JSON serialization only
    public static SplitWeight fromRawValue(long value)
    {
        return fromRawValueInternal(value);
    }

    /**
     * Produces a {@link SplitWeight} that corresponds to the {@link SplitWeight#standard()} weight
     * proportionally, i.e., a parameter of 1.0 will be equivalent to the standard weight
     * and a value of 0.5 will be 1/2 of the standard split weight. Valid arguments
     * must be greater than zero and finite. Connectors should prefer constructing split weights
     * using this factory method rather than passing a raw integer value in case the integer representation
     * of a standard split needs to change in the future.
     *
     * @param weight the proportional weight relative to a standard split, expressed as a double
     * @return a {@link SplitWeight} with a raw value corresponding to the requested proportion
     */
    public static SplitWeight fromProportion(double weight)
    {
        if (weight <= 0 || !Double.isFinite(weight)) {
            throw new IllegalArgumentException("Invalid weight: " + weight);
        }
        // Must round up to avoid small weights rounding to 0
        return fromRawValueInternal((long) Math.ceil(weight * UNIT_VALUE));
    }

    private static SplitWeight fromRawValueInternal(long value)
    {
        return value == UNIT_VALUE ? STANDARD_WEIGHT : new SplitWeight(value);
    }

    public static SplitWeight standard()
    {
        return STANDARD_WEIGHT;
    }

    public static long rawValueForStandardSplitCount(int splitCount)
    {
        if (splitCount < 0) {
            throw new IllegalArgumentException("splitCount must be >= 0, found: " + splitCount);
        }
        return multiplyExact(splitCount, UNIT_VALUE);
    }

    public static  long rawValueSum(Collection collection, Function getter)
    {
        long sum = 0;
        for (T item : collection) {
            long value = getter.apply(item).getRawValue();
            sum = addExact(sum, value);
        }
        return sum;
    }

    public long getRetainedSizeInBytes()
    {
        return INSTANCE_SIZE;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy