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

io.deephaven.util.codec.StringKeyedMapCodec Maven / Gradle / Ivy

There is a newer version: 0.36.1
Show newest version
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.util.codec;

import java.nio.ByteBuffer;
import java.util.Map;

abstract class StringKeyedMapCodec extends MapCodec {
    StringKeyedMapCodec(String arguments) {
        super(arguments);
    }

    @Override
    int estimateSize(Map input) {
        int estimate = 0;
        for (final String key : input.keySet()) {
            estimate += key.length() + Integer.BYTES;
        }
        estimate *= 1.1;
        estimate += Integer.BYTES + (getValueSize() * input.size());
        return estimate;
    }

    /**
     * Return the size of the values (presuming they are fixed size).
     *
     * If your values are not fixed size, then you must override the {@link #estimateSize(Map)} method and should throw
     * an UnsupportedOperationException.
     *
     * @return the size of each encoded value
     */
    abstract int getValueSize();

    @Override
    String decodeKey(ByteBuffer byteBuffer) {
        return CodecUtil.getUtf8String(byteBuffer);
    }

    @Override
    void encodeKey(ByteBuffer scratch, String key) {
        CodecUtil.putUtf8String(scratch, key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy