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

io.deephaven.engine.table.impl.sources.UnboxedTimeBackedColumnSource 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.chunk.ObjectChunk;
import io.deephaven.chunk.WritableChunk;
import io.deephaven.chunk.WritableLongChunk;
import io.deephaven.chunk.WritableObjectChunk;
import io.deephaven.chunk.attributes.Values;
import io.deephaven.engine.rowset.RowSequence;
import io.deephaven.engine.table.ColumnSource;
import io.deephaven.engine.table.SharedContext;
import io.deephaven.engine.table.impl.AbstractColumnSource;
import io.deephaven.engine.table.impl.MutableColumnSourceGetDefaults;
import org.jetbrains.annotations.NotNull;

/**
 * Reinterpret result {@link ColumnSource} implementations that translates various Time sources to {@code long} values.
 */
public abstract class UnboxedTimeBackedColumnSource extends AbstractColumnSource
        implements MutableColumnSourceGetDefaults.ForLong {
    private final ColumnSource alternateColumnSource;

    private class UnboxingFillContext implements FillContext {
        final FillContext alternateFillContext;
        final WritableObjectChunk innerChunk;

        private UnboxingFillContext(final int chunkCapacity, final SharedContext sharedContext) {
            alternateFillContext = alternateColumnSource.makeFillContext(chunkCapacity, sharedContext);
            innerChunk = WritableObjectChunk.makeWritableChunk(chunkCapacity);
        }

        @Override
        public void close() {
            alternateFillContext.close();
            innerChunk.close();
        }
    }

    public UnboxedTimeBackedColumnSource(ColumnSource alternateColumnSource) {
        super(long.class);
        this.alternateColumnSource = alternateColumnSource;
    }

    protected abstract long toEpochNano(TIME_TYPE val);

    @Override
    public long getLong(long rowKey) {
        return toEpochNano(alternateColumnSource.get(rowKey));
    }

    @Override
    public long getPrevLong(long rowKey) {
        return toEpochNano(alternateColumnSource.getPrev(rowKey));
    }

    @Override
    public boolean isImmutable() {
        return alternateColumnSource.isImmutable();
    }

    @Override
    public  boolean allowsReinterpret(
            @NotNull final Class alternateDataType) {
        return alternateDataType == alternateColumnSource.getType();
    }

    @Override
    public  ColumnSource doReinterpret(
            @NotNull final Class alternateDataType) throws IllegalArgumentException {
        // noinspection unchecked
        return (ColumnSource) alternateColumnSource;
    }

    @Override
    public FillContext makeFillContext(final int chunkCapacity, final SharedContext sharedContext) {
        return new UnboxingFillContext(chunkCapacity, sharedContext);
    }

    @Override
    public void fillChunk(@NotNull FillContext context, @NotNull WritableChunk destination,
            @NotNull RowSequence rowSequence) {
        // noinspection unchecked
        final UnboxingFillContext unboxingFillContext = (UnboxingFillContext) context;
        final WritableObjectChunk innerChunk = unboxingFillContext.innerChunk;
        alternateColumnSource.fillChunk(unboxingFillContext.alternateFillContext, innerChunk, rowSequence);
        convertToLong(destination, innerChunk);
        innerChunk.fillWithNullValue(0, innerChunk.size());
    }

    @Override
    public void fillPrevChunk(@NotNull FillContext context, @NotNull WritableChunk destination,
            @NotNull RowSequence rowSequence) {
        // noinspection unchecked
        final UnboxingFillContext unboxingFillContext = (UnboxingFillContext) context;
        final WritableObjectChunk innerChunk = unboxingFillContext.innerChunk;
        alternateColumnSource.fillPrevChunk(unboxingFillContext.alternateFillContext, innerChunk, rowSequence);
        convertToLong(destination, innerChunk);
        innerChunk.fillWithNullValue(0, innerChunk.size());
    }

    private void convertToLong(@NotNull WritableChunk destination,
            ObjectChunk innerChunk) {
        final WritableLongChunk longDestination = destination.asWritableLongChunk();
        for (int ii = 0; ii < innerChunk.size(); ++ii) {
            longDestination.set(ii, toEpochNano(innerChunk.get(ii)));
        }
        longDestination.setSize(innerChunk.size());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy