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. |
void |
accept(double value,
long count)
Adds a value to the sketch as many times as specified by
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 |
copy() |
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). |
static DDSketch |
fromProto(java.util.function.Supplier<? extends Store> storeSupplier,
com.datadoghq.sketch.ddsketch.proto.DDSketch proto)
Builds a new instance of
DDSketch based on the provided protobuf representation. |
double |
getCount() |
IndexMapping |
getIndexMapping() |
double |
getMaxValue() |
double |
getMinValue() |
Store |
getNegativeValueStore() |
Store |
getPositiveValueStore() |
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.
|
com.datadoghq.sketch.ddsketch.proto.DDSketch |
toProto()
Generates a protobuf representation of this
DDSketch. |
public 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 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,
long count)
count.accept in interface QuantileSketch<DDSketch>value - the value to be addedcount - the number of times the value is 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.value - the value to be addedcount - the weight associated with the value to be addedjava.lang.IllegalArgumentException - if count is negativepublic 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 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 com.datadoghq.sketch.ddsketch.proto.DDSketch toProto()
DDSketch.DDSketchpublic static DDSketch fromProto(java.util.function.Supplier<? extends Store> storeSupplier, com.datadoghq.sketch.ddsketch.proto.DDSketch proto)
DDSketch based on the provided protobuf representation.storeSupplier - the constructor of the Store implementation to be used for encoding bin countersproto - the protobuf representation of a sketchDDSketch that matches the protobuf representation@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).