Class HLL
- All Implemented Interfaces:
Cloneable
long elements. Useful for computing the approximate
cardinality of a stream of data in very small storage.
A modified version of the 'HyperLogLog' data structure and algorithm is used, which combines both probabilistic and non-probabilistic techniques to improve the accuracy and storage requirements of the original algorithm.
More specifically, initializing and storing a new HLL will allocate a sentinel value
symbolizing the empty set (HLLType.EMPTY). After adding the first few values, a sorted
list of unique integers is stored in a HLLType.EXPLICIT hash set. When configured,
accuracy can be sacrificed for memory footprint: the values in the sorted list are "promoted" to
a "HLLType.SPARSE" map-based HyperLogLog structure. Finally, when enough registers are
set, the map-based HLL will be converted to a bit-packed "HLLType.FULL" HyperLogLog
structure.
This data structure is interoperable with the implementations found at:
- postgresql-hll, and
- js-hll
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRaw(long rawValue) AddsrawValuedirectly to the HLL.longComputes the cardinality of the HLL.voidclear()Clears the HLL.clone()Create a deep copy of this HLL.static HLLfromBytes(byte[] bytes) Deserializes the HLL (intoBytes(ISchemaVersion)format) serialized intobytes.getType()byte[]toBytes()Serializes the HLL to an array of bytes in correspondence with the format of the default schema version,SerializationUtil.DEFAULT_SCHEMA_VERSION.byte[]toBytes(ISchemaVersion schemaVersion) Serializes the HLL to an array of bytes in correspondence with the format of the specified schema version.voidComputes the union of HLLs and stores the result in this instance.
-
Field Details
-
MINIMUM_LOG2M_PARAM
public static final int MINIMUM_LOG2M_PARAM- See Also:
-
MAXIMUM_LOG2M_PARAM
public static final int MAXIMUM_LOG2M_PARAM- See Also:
-
MINIMUM_REGWIDTH_PARAM
public static final int MINIMUM_REGWIDTH_PARAM- See Also:
-
MAXIMUM_REGWIDTH_PARAM
public static final int MAXIMUM_REGWIDTH_PARAM- See Also:
-
MINIMUM_EXPTHRESH_PARAM
public static final int MINIMUM_EXPTHRESH_PARAM- See Also:
-
MAXIMUM_EXPTHRESH_PARAM
public static final int MAXIMUM_EXPTHRESH_PARAM- See Also:
-
MAXIMUM_EXPLICIT_THRESHOLD
public static final int MAXIMUM_EXPLICIT_THRESHOLD- See Also:
-
-
Constructor Details
-
HLL
NOTE: Arguments here are named and structured identically to those in the PostgreSQL implementation, which can be found here.- Parameters:
log2m- log-base-2 of the number of registers used in the HyperLogLog algorithm. Must be at least 4 and at most 30.regwidth- number of bits used per register in the HyperLogLog algorithm. Must be at least 1 and at most 8.expthresh- tunes when theHLLType.EXPLICITtoHLLType.SPARSEpromotion occurs, based on the set's cardinality. Must be at least -1 and at most 18.sparseon- Flag indicating if theHLLType.SPARSErepresentation should be used.type- the type in the promotion hierarchy which this instance should start at. This cannot benull.
-
HLL
public HLL(int log2m, int regwidth) Construct an empty HLL with the givenlog2mandregwidth.This is equivalent to calling
HLL(log2m, regwidth, -1, true, HLLType.EMPTY).- Parameters:
log2m- log-base-2 of the number of registers used in the HyperLogLog algorithm. Must be at least 4 and at most 30.regwidth- number of bits used per register in the HyperLogLog algorithm. Must be at least 1 and at most 8.- See Also:
-
-
Method Details
-
getType
- Returns:
- the type in the promotion hierarchy of this instance. This will never be
null.
-
addRaw
public void addRaw(long rawValue) AddsrawValuedirectly to the HLL.- Parameters:
rawValue- the value to be added. It is very important that this value already be hashed with a strong (but not necessarily cryptographic) hash function. For instance, the Murmur3 implementation in Google's Guava library is an excellent hash function for this purpose and, for seeds greater than zero, matches the output of the hash provided in the PostgreSQL implementation.
-
cardinality
public long cardinality()Computes the cardinality of the HLL.- Returns:
- the cardinality of HLL. This will never be negative.
-
clear
public void clear()Clears the HLL. The HLL will have cardinality zero and will act as if no elements have been added.NOTE: Unlike
addRaw(long),cleardoes NOT handle transitions betweenHLLTypes - a probabilistic type will remain probabilistic after being cleared. -
union
Computes the union of HLLs and stores the result in this instance.- Parameters:
other- the otherHLLinstance to union into this one. This cannot benull.
-
toBytes
public byte[] toBytes()Serializes the HLL to an array of bytes in correspondence with the format of the default schema version,SerializationUtil.DEFAULT_SCHEMA_VERSION.- Returns:
- the array of bytes representing the HLL. This will never be
nullor empty.
-
toBytes
Serializes the HLL to an array of bytes in correspondence with the format of the specified schema version.- Parameters:
schemaVersion- the schema version dictating the serialization format- Returns:
- the array of bytes representing the HLL. This will never be
nullor empty.
-
fromBytes
Deserializes the HLL (intoBytes(ISchemaVersion)format) serialized intobytes.- Parameters:
bytes- the serialized bytes of new HLL- Returns:
- the deserialized HLL. This will never be
null. - See Also:
-
clone
Create a deep copy of this HLL.- Overrides:
clonein classObject- Throws:
CloneNotSupportedException- See Also:
-