io.deephaven.util.codec.StringKeyedMapCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deephaven-codec-builtin Show documentation
Show all versions of deephaven-codec-builtin Show documentation
Codec Builtin: Deephaven builtin codec implementations
//
// 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);
}
}