package org.apache.cassandra.io.sstable.metadata;
import java.io.IOException;
import com.clearspring.analytics.stream.cardinality.HyperLogLogPlus;
import com.clearspring.analytics.stream.cardinality.ICardinality;
import org.apache.cassandra.db.TypeSizes;
import org.apache.cassandra.io.sstable.format.Version;
import org.apache.cassandra.io.util.DataInputPlus;
import org.apache.cassandra.io.util.DataOutputPlus;
import org.apache.cassandra.utils.ByteBufferUtil;
public class CompactionMetadata extends MetadataComponent
{
public static final IMetadataComponentSerializer serializer = new CompactionMetadataSerializer();
public final ICardinality cardinalityEstimator;
public CompactionMetadata(ICardinality cardinalityEstimator)
{
this.cardinalityEstimator = cardinalityEstimator;
}
public MetadataType getType()
{
return MetadataType.COMPACTION;
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
return true;
}
@Override
public int hashCode()
{
return 31;
}
public static class CompactionMetadataSerializer implements IMetadataComponentSerializer<CompactionMetadata>
{
public int serializedSize(Version version, CompactionMetadata component) throws IOException
{
int sz = 0;
if (version.hasCompactionAncestors())
{
sz = 4;
}
byte[] serializedCardinality = component.cardinalityEstimator.getBytes();
return TypeSizes.sizeof(serializedCardinality.length) + serializedCardinality.length + sz;
}
public void serialize(Version version, CompactionMetadata component, DataOutputPlus out) throws IOException
{
if (version.hasCompactionAncestors())
{
out.writeInt(0);
}
ByteBufferUtil.writeWithLength(component.cardinalityEstimator.getBytes(), out);
}
public CompactionMetadata deserialize(Version version, DataInputPlus in) throws IOException
{
if (version.hasCompactionAncestors())
{
int nbAncestors = in.readInt();
in.skipBytes(nbAncestors * TypeSizes.sizeof(nbAncestors));
}
ICardinality cardinality = HyperLogLogPlus.Builder.build(ByteBufferUtil.readBytes(in, in.readInt()));
return new CompactionMetadata(cardinality);
}
}
}