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

io.deephaven.engine.table.impl.sources.NanosBasedTimeSparseArraySource Maven / Gradle / Ivy

There is a newer version: 0.37.1
Show newest version
/**
 * Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending
 */
package io.deephaven.engine.table.impl.sources;

import io.deephaven.base.verify.Require;
import io.deephaven.chunk.Chunk;
import io.deephaven.chunk.LongChunk;
import io.deephaven.chunk.WritableChunk;
import io.deephaven.chunk.attributes.Values;
import io.deephaven.engine.rowset.RowSequence;
import io.deephaven.engine.rowset.RowSet;
import io.deephaven.engine.rowset.chunkattributes.RowKeys;
import io.deephaven.engine.table.ColumnSource;
import io.deephaven.engine.table.SharedContext;
import io.deephaven.engine.table.WritableColumnSource;
import io.deephaven.engine.table.WritableSourceWithPrepareForParallelPopulation;
import io.deephaven.engine.table.impl.util.ShiftData;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * Array-backed ColumnSource for TIME_TYPEs. Allows reinterpret as long.
 */
public abstract class NanosBasedTimeSparseArraySource extends AbstractDeferredGroupingColumnSource
        implements FillUnordered, WritableColumnSource, InMemoryColumnSource,
        PossiblyImmutableColumnSource, WritableSourceWithPrepareForParallelPopulation, ShiftData.RowSetShiftCallback,
        ConvertibleTimeSource {

    protected final LongSparseArraySource nanoSource;

    public NanosBasedTimeSparseArraySource(@NotNull final Class type) {
        this(type, new LongSparseArraySource());
    }

    public NanosBasedTimeSparseArraySource(@NotNull final Class type,
            @NotNull final LongSparseArraySource nanoSource) {
        super(type);
        this.nanoSource = nanoSource;
    }

    @Override
    public void ensureCapacity(final long capacity, final boolean nullFilled) {
        nanoSource.ensureCapacity(capacity, nullFilled);
    }

    @Override
    public void prepareForParallelPopulation(final RowSequence rowSequence) {
        nanoSource.prepareForParallelPopulation(rowSequence);
    }

    // region Getters & Setters
    protected abstract TIME_TYPE makeValue(final long nanos);

    protected abstract long toNanos(final TIME_TYPE value);

    @Override
    public void set(long key, TIME_TYPE value) {
        nanoSource.set(key, toNanos(value));
    }

    @Override
    public void set(long key, long value) {
        nanoSource.set(key, value);
    }

    @Override
    public void setNull(long key) {
        nanoSource.setNull(key);
    }

    @Override
    public TIME_TYPE get(long rowKey) {
        return makeValue(getLong(rowKey));
    }

    @Override
    public TIME_TYPE getPrev(long rowKey) {
        return makeValue(getPrevLong(rowKey));
    }

    @Override
    public long getLong(long rowKey) {
        return nanoSource.getLong(rowKey);
    }

    @Override
    public long getPrevLong(long rowKey) {
        return nanoSource.getPrevLong(rowKey);
    }

    @Override
    public void shift(final RowSet keysToShift, long shiftDelta) {
        nanoSource.shift(keysToShift, shiftDelta);
    }
    // endregion

    // region SparseArraySource impl
    @Override
    public void startTrackingPrevValues() {
        nanoSource.startTrackingPrevValues();
    }

    @Override
    public void setImmutable() {
        nanoSource.setImmutable();
    }

    // endregion

    // region Chunking
    @Override
    public FillContext makeFillContext(final int chunkCapacity, final SharedContext sharedContext) {
        return nanoSource.makeFillContext(chunkCapacity, sharedContext);
    }

    @Override
    public void fillChunk(
            @NotNull FillContext context,
            @NotNull WritableChunk destination,
            @NotNull RowSequence rowSequence) {
        if (rowSequence.getAverageRunLengthEstimate() < USE_RANGES_AVERAGE_RUN_LENGTH) {
            nanoSource.fillByKeys(destination, rowSequence, this::makeValue);
        } else {
            nanoSource.fillByRanges(destination, rowSequence, this::makeValue);
        }
    }

    // TODO (https://github.com/deephaven/deephaven-core/issues/4224): Override fillPrevChunk when suitable

    @Override
    public boolean providesFillUnordered() {
        return true;
    }

    @Override
    public void fillChunkUnordered(
            @NotNull final FillContext context,
            @NotNull final WritableChunk dest,
            @NotNull LongChunk keys) {
        nanoSource.fillByUnRowSequence(dest, keys, this::makeValue);
    }

    @Override
    public void fillPrevChunkUnordered(
            @NotNull final FillContext context,
            @NotNull final WritableChunk dest,
            @NotNull LongChunk keys) {
        nanoSource.fillPrevByUnRowSequence(dest, keys, this::makeValue);
    }

    @Override
    public FillFromContext makeFillFromContext(final int chunkCapacity) {
        return nanoSource.makeFillFromContext(chunkCapacity);
    }

    public void fillFromChunk(
            @NotNull FillFromContext context,
            @NotNull Chunk src,
            @NotNull RowSequence rowSequence) {
        if (rowSequence.getAverageRunLengthEstimate() < USE_RANGES_AVERAGE_RUN_LENGTH) {
            nanoSource.fillFromChunkByKeys(rowSequence, src, this::toNanos);
        } else {
            nanoSource.fillFromChunkByRanges(rowSequence, src, this::toNanos);
        }
    }

    @Override
    public void fillFromChunkUnordered(
            @NotNull FillFromContext context,
            @NotNull Chunk src,
            @NotNull LongChunk keys) {
        nanoSource.fillFromChunkUnordered(context, src, keys, this::toNanos);
    }

    @Override
    public void setNull(RowSequence rowSequence) {
        nanoSource.setNull(rowSequence);
    }
    // endregion

    // region Reinterpretation
    @Override
    public  boolean allowsReinterpret(
            @NotNull final Class alternateDataType) {
        return alternateDataType == long.class || alternateDataType == Instant.class;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected  ColumnSource doReinterpret(
            @NotNull Class alternateDataType) {
        if (alternateDataType == this.getType()) {
            return (ColumnSource) this;
        } else if (alternateDataType == long.class || alternateDataType == Long.class) {
            return (ColumnSource) toEpochNano();
        } else if (alternateDataType == Instant.class) {
            return (ColumnSource) toInstant();
        }

        throw new IllegalArgumentException(
                "Cannot reinterpret `" + getType().getName() + "` to `" + alternateDataType.getName() + "`");
    }

    @Override
    public boolean supportsTimeConversion() {
        return true;
    }

    @Override
    public ColumnSource toZonedDateTime(@NotNull final ZoneId zone) {
        return new ZonedDateTimeSparseArraySource(Require.neqNull(zone, "zone"), nanoSource);
    }

    @Override
    public ColumnSource toLocalDate(@NotNull final ZoneId zone) {
        return new LongAsLocalDateColumnSource(nanoSource, zone);
    }

    @Override
    public ColumnSource toLocalTime(@NotNull final ZoneId zone) {
        return new LongAsLocalTimeColumnSource(nanoSource, zone);
    }

    @Override
    public ColumnSource toInstant() {
        return new InstantSparseArraySource(nanoSource);
    }

    @Override
    public ColumnSource toEpochNano() {
        return nanoSource;
    }
    // endregion
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy