public class LongsSketch extends Object
This sketch is useful for tracking approximate frequencies of long items with optional associated counts (long item, long count) that are members of a multiset of such items. The true frequency of an item is defined to be the sum of associated counts.
This implementation provides the following capabilities:
Space Usage
The sketch is initialized with a maxMapSize that specifies the maximum physical length of the internal hash map of the form (long item, long count). The maxMapSize must be a power of 2.
The hash map starts at a very small size (8 entries), and grows as needed up to the specified maxMapSize.
At any moment the internal memory space usage of this sketch is 18 * mapSize bytes, plus a small constant number of additional bytes. The maximum internal memory space usage of this sketch will never exceed 18 * maxMapSize bytes, plus a small constant number of additional bytes.
Maximum Capacity of the Sketch
The LOAD_FACTOR for the hash map is internally set at 75%, which means at any time the map capacity of (item, count) pairs is mapCap = 0.75 * mapSize. The maximum capacity of (item, count) pairs of the sketch is maxMapCap = 0.75 * maxMapSize.
Updating the sketch with (item, count) pairs
If the item is found in the hash map, the mapped count field (the "counter") is incremented by the incoming count, otherwise, a new counter "(item, count) pair" is created. If the number of tracked counters reaches the maximum capacity of the hash map the sketch decrements all of the counters (by an approximately computed median), and removes any non-positive counters.
Accuracy
If fewer than 0.75 * maxMapSize different items are inserted into the sketch the estimated frequencies returned by the sketch will be exact.
The logic of the frequent items sketch is such that the stored counts and true counts are never too different. More specifically, for any item, the sketch can return an estimate of the true frequency of item, along with upper and lower bounds on the frequency (that hold deterministically).
For this implementation and for a specific active item, it is guaranteed that the true frequency will be between the Upper Bound (UB) and the Lower Bound (LB) computed for that item. Specifically, (UB- LB) ≤ W * epsilon, where W denotes the sum of all item counts, and epsilon = 3.5/M, where M is the maxMapSize.
This is a worst case guarantee that applies to arbitrary inputs.1 For inputs typically seen in practice (UB-LB) is usually much smaller.
Background
This code implements a variant of what is commonly known as the "Misra-Gries algorithm". Variants of it were discovered and rediscovered and redesigned several times over the years:
Modifier and Type | Class and Description |
---|---|
static class |
LongsSketch.Row
Row class that defines the return values from a getFrequentItems query.
|
Constructor and Description |
---|
LongsSketch(int maxMapSize)
Construct this sketch with the parameter maxMapSize and the default initialMapSize (8).
|
Modifier and Type | Method and Description |
---|---|
static double |
getAprioriError(int maxMapSize,
long estimatedTotalStreamWeight)
Returns the estimated a priori error given the maxMapSize for the sketch and the
estimatedTotalStreamWeight.
|
int |
getCurrentMapCapacity()
Returns the current number of counters the sketch is configured to support.
|
static double |
getEpsilon(int maxMapSize)
Returns epsilon used to compute a priori error.
|
long |
getEstimate(long item)
Gets the estimate of the frequency of the given item.
|
LongsSketch.Row[] |
getFrequentItems(ErrorType errorType)
Returns an array of Rows that include frequent items, estimates, upper and lower bounds
given an ErrorCondition and the default threshold.
|
LongsSketch.Row[] |
getFrequentItems(long threshold,
ErrorType errorType)
Returns an array of Rows that include frequent items, estimates, upper and lower bounds
given a threshold and an ErrorCondition.
|
static LongsSketch |
getInstance(org.apache.datasketches.memory.Memory srcMem)
Returns a sketch instance of this class from the given srcMem,
which must be a Memory representation of this sketch class.
|
static LongsSketch |
getInstance(String string)
Returns a sketch instance of this class from the given String,
which must be a String representation of this sketch class.
|
long |
getLowerBound(long item)
Gets the guaranteed lower bound frequency of the given item, which can never be
negative.
|
long |
getMaximumError() |
int |
getMaximumMapCapacity()
Returns the maximum number of counters the sketch is configured to support.
|
int |
getNumActiveItems() |
int |
getStorageBytes()
Returns the number of bytes required to store this sketch as an array of bytes.
|
long |
getStreamLength()
Returns the sum of the frequencies (weights or counts) in the stream seen so far by the sketch
|
long |
getUpperBound(long item)
Gets the guaranteed upper bound frequency of the given item.
|
boolean |
isEmpty()
Returns true if this sketch is empty
|
LongsSketch |
merge(LongsSketch other)
This function merges the other sketch into this one.
|
void |
reset()
Resets this sketch to a virgin state.
|
String |
serializeToString()
Returns a String representation of this sketch
|
byte[] |
toByteArray()
Returns a byte array representation of this sketch
|
String |
toString()
Returns a human readable summary of this sketch.
|
static String |
toString(byte[] byteArr)
Returns a human readable string of the preamble of a byte array image of a LongsSketch.
|
static String |
toString(org.apache.datasketches.memory.Memory mem)
Returns a human readable string of the preamble of a Memory image of a LongsSketch.
|
void |
update(long item)
Update this sketch with an item and a frequency count of one.
|
void |
update(long item,
long count)
Update this sketch with a item and a positive frequency count (or weight).
|
public LongsSketch(int maxMapSize)
maxMapSize
- Determines the physical size of the internal hash map managed by this
sketch and must be a power of 2. The maximum capacity of this internal hash map is
0.75 times * maxMapSize. Both the ultimate accuracy and size of this sketch are a
function of maxMapSize.public static LongsSketch getInstance(org.apache.datasketches.memory.Memory srcMem)
srcMem
- a Memory representation of a sketch of this class.
See Memorypublic static LongsSketch getInstance(String string)
string
- a String representation of a sketch of this class.public static double getAprioriError(int maxMapSize, long estimatedTotalStreamWeight)
maxMapSize
- the planned map size to be used when constructing this sketch.estimatedTotalStreamWeight
- the estimated total stream weight.public int getCurrentMapCapacity()
public static double getEpsilon(int maxMapSize)
maxMapSize
- the planned map size to be used when constructing this sketch.public long getEstimate(long item)
item
- the given itempublic long getLowerBound(long item)
item
- the given item.public LongsSketch.Row[] getFrequentItems(long threshold, ErrorType errorType)
The method first examines all active items in the sketch (items that have a counter).
If ErrorType = NO_FALSE_NEGATIVES, this will include an item in the result list if getUpperBound(item) > threshold. There will be no false negatives, i.e., no Type II error. There may be items in the set with true frequencies less than the threshold (false positives).
If ErrorType = NO_FALSE_POSITIVES, this will include an item in the result list if getLowerBound(item) > threshold. There will be no false positives, i.e., no Type I error. There may be items omitted from the set with true frequencies greater than the threshold (false negatives). This is a subset of the NO_FALSE_NEGATIVES case.
threshold
- to include items in the result listerrorType
- determines whether no false positives or no false negatives are
desired.public LongsSketch.Row[] getFrequentItems(ErrorType errorType)
errorType
- determines whether no false positives or no false negatives are
desired.public long getMaximumError()
public int getMaximumMapCapacity()
public int getNumActiveItems()
public int getStorageBytes()
public long getStreamLength()
public long getUpperBound(long item)
item
- the given itempublic boolean isEmpty()
public LongsSketch merge(LongsSketch other)
other
- sketch of this classpublic void reset()
public String serializeToString()
public byte[] toByteArray()
public String toString()
public static String toString(byte[] byteArr)
byteArr
- the given byte arraypublic static String toString(org.apache.datasketches.memory.Memory mem)
mem
- the given Memory objectpublic void update(long item)
item
- for which the frequency should be increased.public void update(long item, long count)
item
- for which the frequency should be increased. The item can be any long value
and is only used by the sketch to determine uniqueness.count
- the amount by which the frequency of the item should be increased.
An count of zero is a no-op, and a negative count will throw an exception.Copyright © 2015–2024 The Apache Software Foundation. All rights reserved.