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

io.deephaven.engine.table.impl.util.freezeby.FreezeByOperator Maven / Gradle / Ivy

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

import io.deephaven.chunk.attributes.ChunkLengths;
import io.deephaven.chunk.attributes.ChunkPositions;
import io.deephaven.chunk.attributes.Values;
import io.deephaven.engine.table.ColumnSource;
import io.deephaven.engine.table.TableUpdate;
import io.deephaven.engine.table.WritableColumnSource;
import io.deephaven.engine.rowset.chunkattributes.RowKeys;
import io.deephaven.engine.table.impl.by.IterativeChunkedAggregationOperator;
import io.deephaven.engine.table.impl.sources.*;
import io.deephaven.chunk.*;
import io.deephaven.engine.rowset.RowSequence;
import io.deephaven.engine.rowset.RowSet;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;
import java.util.Collections;
import java.util.Map;

public class FreezeByOperator implements IterativeChunkedAggregationOperator {
    private final WritableColumnSource resultSource;
    private final String name;
    private final FreezeByHelper helper;

    public FreezeByOperator(Class type, String resultName, FreezeByCountOperator freezeByCountOperator) {
        resultSource = ArrayBackedColumnSource.getMemoryColumnSource(0, type);
        name = resultName;
        helper = makeHelper(resultSource, freezeByCountOperator);
    }

    @Override
    public void addChunk(BucketedContext context, Chunk values,
            LongChunk inputRowKeys, IntChunk destinations,
            IntChunk startPositions, IntChunk length,
            WritableBooleanChunk stateModified) {
        helper.addChunk(values, startPositions, destinations, length);
    }

    @Override
    public void removeChunk(BucketedContext context, Chunk values,
            LongChunk inputRowKeys, IntChunk destinations,
            IntChunk startPositions, IntChunk length,
            WritableBooleanChunk stateModified) {}

    @Override
    public void modifyChunk(BucketedContext context, Chunk previousValues,
            Chunk newValues, LongChunk postShiftRowKeys,
            IntChunk destinations, IntChunk startPositions,
            IntChunk length, WritableBooleanChunk stateModified) {}

    @Override
    public boolean addChunk(SingletonContext context, int chunkSize, Chunk values,
            LongChunk inputRowKeys, long destination) {
        helper.addChunk(values, destination);
        return false;
    }


    @Override
    public boolean removeChunk(SingletonContext context, int chunkSize, Chunk values,
            LongChunk inputRowKeys, long destination) {
        return false;
    }

    @Override
    public boolean modifyChunk(SingletonContext context, int chunkSize,
            Chunk previousValues, Chunk newValues,
            LongChunk postShiftRowKeys, long destination) {
        return false;
    }

    @Override
    public void ensureCapacity(long tableSize) {
        resultSource.ensureCapacity(tableSize);
    }

    @Override
    public Map> getResultColumns() {
        return Collections.singletonMap(name, (ColumnSource) resultSource);
    }

    @Override
    public void startTrackingPrevValues() {
        resultSource.startTrackingPrevValues();
    }

    @Override
    public void propagateUpdates(@NotNull TableUpdate downstream,
            @NotNull RowSet newDestinations) {
        if (downstream.removed().isNonempty()) {
            helper.clearIndex(downstream.removed());
        }
    }

    private static FreezeByHelper makeHelper(WritableColumnSource source, FreezeByCountOperator rowCount) {
        switch (source.getChunkType()) {
            default:
            case Boolean:
                throw new IllegalStateException();
            case Char:
                return new CharFreezeByHelper(source, rowCount);
            case Byte:
                return new ByteFreezeByHelper(source, rowCount);
            case Short:
                return new ShortFreezeByHelper(source, rowCount);
            case Int:
                return new IntFreezeByHelper(source, rowCount);
            case Long:
                return new LongFreezeByHelper(source, rowCount);
            case Float:
                return new FloatFreezeByHelper(source, rowCount);
            case Double:
                return new DoubleFreezeByHelper(source, rowCount);
            case Object:
                if (source.getType() == Instant.class) {
                    return new LongFreezeByHelper(((NanosBasedTimeArraySource) source).toEpochNano(), rowCount);
                } else if (source.getType() == Boolean.class) {
                    return new BooleanFreezeByHelper(source, rowCount);
                } else {
                    return new ObjectFreezeByHelper(source, rowCount);
                }
        }
    }

    interface FreezeByHelper {
        void addChunk(Chunk values, IntChunk startPositions,
                IntChunk destinations, IntChunk length);

        void addChunk(Chunk values, long destination);

        void clearIndex(RowSequence removed);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy