Skip to content

BloomFilter Serialization #6068

Open
Open

Description

BloomFilter size keeps increasing when serializing and deserializing with kryo ?

Below is the relevant code

import com.esotericsoftware.kryo.kryo5.Kryo;
import com.esotericsoftware.kryo.kryo5.io.Input;
import com.esotericsoftware.kryo.kryo5.io.Output;
import com.esotericsoftware.kryo.kryo5.objenesis.strategy.StdInstantiatorStrategy;
import com.esotericsoftware.kryo.kryo5.util.DefaultInstantiatorStrategy;
import com.esotericsoftware.kryo.kryo5.util.Pool;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

protected byte[] kryoSerialize(Object obj) {
    Kryo kryo = kryoPool.obtain();
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Output output = new Output(byteArrayOutputStream);
        kryo.writeObject(output, obj);
        output.close();
        return byteArrayOutputStream.toByteArray();
    } catch (Exception e) {
        return null;
    } finally {
        kryoPool.free(kryo);
    }
}

protected <T> T kryoDeserialize(byte[] bytes, Class<T> clazz) {
    Kryo kryo = kryoPool.obtain();
    try {
        kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        Input input = new Input(byteArrayInputStream);
        input.close();
        return (T) kryo.readObject(input, clazz);
    } catch (Exception e) {
        return null;
    } finally {
        kryoPool.free(kryo);
    }
}

Pool<Kryo> kryoPool = new Pool<Kryo>(true, false, 8) {
    @Override
    protected Kryo create() {
        Kryo kryo = new Kryo();
        kryo.setReferences(true);
        kryo.setRegistrationRequired(false);
        return kryo;
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions