public class DDSketch extends java.lang.Object implements QuantileSketch<DDSketch>
QuantileSketch with relative-error guarantees. This sketch computes quantile values
with an approximation error that is relative to the actual quantile value. It works with both
positive and negative input values.
For instance, using DDSketch with a relative accuracy guarantee set to 1%, if the
expected quantile value is 100, the computed quantile value is guaranteed to be between 99 and
101. If the expected quantile value is 1000, the computed quantile value is guaranteed to be
between 990 and 1010.
DDSketch works by mapping floating-point input values to bins and counting the number
of values for each bin. The mapping to bins is handled by IndexMapping, while the
underlying structure that keeps track of bin counts is Store. The IndexMapping
and the Store supplier are provided when constructing the sketch and can be adjusted
based on use-case requirements. See DDSketches for preset sketches and some explanation
about the involved tradeoffs.
Note that negative values are inverted before being mapped to the store. That means that if you use a store that collapses the lowest (resp., the highest) indexes, it will affect the input values that are the closest to (resp., the farthest away from) zero.
Note that this implementation is not thread-safe.
| Constructor and Description |
|---|
DDSketch(double relativeAccuracy)
Constructs a simple instance of
DDSketch with the provided relative accuracy guarantee. |
DDSketch(IndexMapping indexMapping,
java.util.function.Supplier<Store> storeSupplier)
Constructs an initially empty quantile sketch using the specified
IndexMapping and
Store supplier. |
DDSketch(IndexMapping indexMapping,
java.util.function.Supplier<Store> negativeValueStoreSupplier,
java.util.function.Supplier<Store> positiveValueStoreSupplier)
Constructs an initially empty quantile sketch using the specified
IndexMapping and
Store suppliers. |
DDSketch(IndexMapping indexMapping,
java.util.function.Supplier<Store> negativeValueStoreSupplier,
java.util.function.Supplier<Store> positiveValueStoreSupplier,
double minIndexedValue)
Constructs an initially empty quantile sketch using the specified
IndexMapping and
Store supplier. |
| Modifier and Type | Method and Description |
|---|---|
void |
accept(double value)
Adds a value to the sketch.
|
void |
accept(double value,
double count)
Adds a value to the sketch with a floating-point
count. |
static DDSketch |
balanced(double relativeAccuracy)
Deprecated.
|
static DDSketch |
balancedCollapsingHighest(double relativeAccuracy,
int maxNumBins)
Deprecated.
|
static DDSketch |
balancedCollapsingLowest(double relativeAccuracy,
int maxNumBins)
Deprecated.
|
void |
clear()
Sets all counts in the sketch to zero.
|
DDSketch |
convert(IndexMapping newIndexMapping,
java.util.function.Supplier<Store> storeSupplier)
Builds a new
DDSketch that encodes the content of this sketch with the specified index
mapping and the specified stores. |
DDSketch |
copy() |
static DDSketch |
decode(Input input,
java.util.function.Supplier<Store> storeSupplier) |
static DDSketch |
decode(Input input,
java.util.function.Supplier<Store> storeSupplier,
IndexMapping indexMapping) |
static DDSketch |
decode(Input input,
java.util.function.Supplier<Store> storeSupplier,
IndexMapping indexMapping,
com.datadoghq.sketch.ddsketch.DDSketch.Decoder fallback) |
void |
decodeAndMergeWith(Input input) |
void |
encode(Output output,
boolean omitIndexMapping) |
static DDSketch |
fast(double relativeAccuracy)
Deprecated.
Use
new DDSketch(new BitwiseLinearlyInterpolatedMapping(relativeAccuracy),
UnboundedSizeDenseStore::new). |
static DDSketch |
fastCollapsingHighest(double relativeAccuracy,
int maxNumBins)
Deprecated.
Use
new DDSketch(new BitwiseLinearlyInterpolatedMapping(relativeAccuracy),
() -> new CollapsingHighestDenseStore(maxNumBins)). |
static DDSketch |
fastCollapsingLowest(double relativeAccuracy,
int maxNumBins)
Deprecated.
Use
new DDSketch(new BitwiseLinearlyInterpolatedMapping(relativeAccuracy),
() -> new CollapsingLowestDenseStore(maxNumBins). |
double |
getCount() |
IndexMapping |
getIndexMapping() |
double |
getMaxValue() |
double |
getMinValue() |
Store |
getNegativeValueStore() |
Store |
getPositiveValueStore() |
double |
getSum()
Returns an approximation of the sum of the values that have been added to the sketch.
|
double |
getValueAtQuantile(double quantile) |
double[] |
getValuesAtQuantiles(double[] quantiles) |
boolean |
isEmpty() |
static DDSketch |
memoryOptimal(double relativeAccuracy)
Deprecated.
|
static DDSketch |
memoryOptimalCollapsingHighest(double relativeAccuracy,
int maxNumBins)
Deprecated.
|
static DDSketch |
memoryOptimalCollapsingLowest(double relativeAccuracy,
int maxNumBins)
Deprecated.
|
void |
mergeWith(DDSketch other)
Merges the other sketch into this one.
|
static DDSketch |
of(IndexMapping indexMapping,
Store negativeValueStore,
Store positiveValueStore,
double zeroCount)
Constructs an instance of
DDSketch from a mapping, possibly non-empty stores and a
non-negative count for the zero bucket. |
java.nio.ByteBuffer |
serialize()
Produces protobuf encoded bytes which are equivalent to using the official protobuf bindings,
without requiring a runtime dependency on protobuf-java.
|
int |
serializedSize() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetAveragepublic DDSketch(IndexMapping indexMapping, java.util.function.Supplier<Store> storeSupplier)
IndexMapping and
Store supplier.indexMapping - the mapping between floating-point values and integer indices to be used by
the sketchstoreSupplier - the store constructor for keeping track of added valuesDDSketchespublic DDSketch(IndexMapping indexMapping, java.util.function.Supplier<Store> negativeValueStoreSupplier, java.util.function.Supplier<Store> positiveValueStoreSupplier)
IndexMapping and
Store suppliers.indexMapping - the mapping between floating-point values and integer indices to be used by
the sketchnegativeValueStoreSupplier - the store constructor for keeping track of added negative
valuespositiveValueStoreSupplier - the store constructor for keeping track of added positive
valuesDDSketchespublic DDSketch(IndexMapping indexMapping, java.util.function.Supplier<Store> negativeValueStoreSupplier, java.util.function.Supplier<Store> positiveValueStoreSupplier, double minIndexedValue)
IndexMapping and
Store supplier.indexMapping - the mapping between floating-point values and integer indices to be used by
the sketchnegativeValueStoreSupplier - the store constructor for keeping track of added negative
valuespositiveValueStoreSupplier - the store constructor for keeping track of added positive
valuesminIndexedValue - the least value that should be distinguished from zeroDDSketchespublic DDSketch(double relativeAccuracy)
DDSketch with the provided relative accuracy guarantee.
This is a convenience constructor, mostly for testing purposes. For preset sketches that are
well-suited to a production setting and to situations where performance and memory usage
constraints are involved, see DDSketches.
relativeAccuracy - the relative accuracy guarantee of the constructed sketchDDSketchespublic static DDSketch of(IndexMapping indexMapping, Store negativeValueStore, Store positiveValueStore, double zeroCount)
DDSketch from a mapping, possibly non-empty stores and a
non-negative count for the zero bucket.indexMapping - the index mappingnegativeValueStore - the store for negative values, possibly non-emptypositiveValueStore - the store for positive values, possibly non-emptyzeroCount - the count for the zero bucket, which should be non-negative (and non-NaN)DDSketchjava.lang.IllegalArgumentException - if the zeroCount is negative or NaNjava.lang.NullPointerException - if one of the arguments is nullpublic IndexMapping getIndexMapping()
public Store getNegativeValueStore()
public Store getPositiveValueStore()
public void accept(double value)
accept in interface QuantileSketch<DDSketch>accept in interface java.util.function.DoubleConsumervalue - the value to be addedjava.lang.IllegalArgumentException - if the value is outside the range that is tracked by the
sketchpublic void accept(double value,
double count)
count.
If count is an integer, calling accept(value, count) is equivalent to
calling accept(value) count times.
accept in interface QuantileSketch<DDSketch>value - the value to be addedcount - the weight associated with the value to be addedjava.lang.IllegalArgumentException - if the value is outside the range that is tracked by the
sketchpublic void mergeWith(DDSketch other)
mergeWith in interface QuantileSketch<DDSketch>other - the sketch to be merged into this onejava.lang.IllegalArgumentException - if the other sketch does not use the same index mappingpublic DDSketch copy()
copy in interface QuantileSketch<DDSketch>public boolean isEmpty()
isEmpty in interface QuantileSketch<DDSketch>public void clear()
QuantileSketchclear in interface QuantileSketch<DDSketch>public double getCount()
getCount in interface QuantileSketch<DDSketch>public double getSum()
IndexMapping used for this sketch.getSum in interface QuantileSketch<DDSketch>public double getMinValue()
getMinValue in interface QuantileSketch<DDSketch>public double getMaxValue()
getMaxValue in interface QuantileSketch<DDSketch>public double getValueAtQuantile(double quantile)
getValueAtQuantile in interface QuantileSketch<DDSketch>quantile - a number between 0 and 1 (both included)public double[] getValuesAtQuantiles(double[] quantiles)
getValuesAtQuantiles in interface QuantileSketch<DDSketch>quantiles - number between 0 and 1 (both included)public DDSketch convert(IndexMapping newIndexMapping, java.util.function.Supplier<Store> storeSupplier)
DDSketch that encodes the content of this sketch with the specified index
mapping and the specified stores. This DDSketch is not modified by the operation.
Note that this conversion degrades the accuracy of the new sketch to the extent that it is
not necessarily upper-bounded by the accuracy of the provided index mapping. See IndexMappingConverter.distributingUniformly(IndexMapping, IndexMapping) for details.
newIndexMapping - the index mapping that the new sketch should use to map values to binsstoreSupplier - a constructor of an initially empty store to be used to encode binsDDSketchpublic void encode(Output output, boolean omitIndexMapping) throws java.io.IOException
java.io.IOExceptionpublic void decodeAndMergeWith(Input input) throws java.io.IOException
java.io.IOExceptionpublic static DDSketch decode(Input input, java.util.function.Supplier<Store> storeSupplier) throws java.io.IOException
java.io.IOExceptionpublic static DDSketch decode(Input input, java.util.function.Supplier<Store> storeSupplier, IndexMapping indexMapping) throws java.io.IOException
java.io.IOExceptionpublic static DDSketch decode(Input input, java.util.function.Supplier<Store> storeSupplier, IndexMapping indexMapping, com.datadoghq.sketch.ddsketch.DDSketch.Decoder fallback) throws java.io.IOException
java.io.IOExceptionpublic int serializedSize()
public java.nio.ByteBuffer serialize()
Currently this API is asymmetric in that there is not an equivalent method to deserialize a
sketch from a protobuf message. DDSketchProtoBinding can be used for this purpose, but
requires a runtime dependency on protobuf-java. This API may be made symmetric in the future.
ByteBuffer.@Deprecated public static DDSketch balanced(double relativeAccuracy)
DDSketches.unboundedDense(double).@Deprecated public static DDSketch balancedCollapsingLowest(double relativeAccuracy, int maxNumBins)
DDSketches.collapsingLowestDense(double, int).@Deprecated public static DDSketch balancedCollapsingHighest(double relativeAccuracy, int maxNumBins)
DDSketches.collapsingHighestDense(double, int).@Deprecated public static DDSketch fast(double relativeAccuracy)
new DDSketch(new BitwiseLinearlyInterpolatedMapping(relativeAccuracy),
UnboundedSizeDenseStore::new).@Deprecated public static DDSketch fastCollapsingLowest(double relativeAccuracy, int maxNumBins)
new DDSketch(new BitwiseLinearlyInterpolatedMapping(relativeAccuracy),
() -> new CollapsingLowestDenseStore(maxNumBins).@Deprecated public static DDSketch fastCollapsingHighest(double relativeAccuracy, int maxNumBins)
new DDSketch(new BitwiseLinearlyInterpolatedMapping(relativeAccuracy),
() -> new CollapsingHighestDenseStore(maxNumBins)).@Deprecated public static DDSketch memoryOptimal(double relativeAccuracy)
DDSketches.logarithmicCollapsingLowestDense(double, int).@Deprecated public static DDSketch memoryOptimalCollapsingLowest(double relativeAccuracy, int maxNumBins)
DDSketches.logarithmicCollapsingLowestDense(double, int).@Deprecated public static DDSketch memoryOptimalCollapsingHighest(double relativeAccuracy, int maxNumBins)
DDSketches.logarithmicCollapsingHighestDense(double, int).