You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2023/12/21 23:16:42 UTC

(datasketches-go) 18/34: Add reset and test for empty sketch

This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datasketches-go.git

commit 4a86514b9d0240928e5af1e2e3e386b7330b15dd
Author: Pierre Lacave <pi...@datadoghq.com>
AuthorDate: Wed Dec 20 21:38:11 2023 +0100

    Add reset and test for empty sketch
---
 frequencies/long_sketch.go      | 21 +++++++++++++++++++--
 frequencies/long_sketch_test.go | 31 +++++++++++++++++++++++++++----
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/frequencies/long_sketch.go b/frequencies/long_sketch.go
index 71618b4..3d10120 100644
--- a/frequencies/long_sketch.go
+++ b/frequencies/long_sketch.go
@@ -86,13 +86,13 @@ func NewLongSketch(lgMaxMapSize int, lgCurMapSize int) (*LongSketch, error) {
 	}, nil
 }
 
-// NewLongSketchForMaxMapSize constructs a new LongSketch with the given maxMapSize and the
+// NewLongSketchWithMaxMapSize constructs a new LongSketch with the given maxMapSize and the
 // default initialMapSize (8).
 // 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.
-func NewLongSketchForMaxMapSize(maxMapSize int) (*LongSketch, error) {
+func NewLongSketchWithMaxMapSize(maxMapSize int) (*LongSketch, error) {
 	log2OfInt, err := common.ExactLog2(maxMapSize)
 	if err != nil {
 		return nil, fmt.Errorf("maxMapSize, %e", err)
@@ -379,3 +379,20 @@ func (s *LongSketch) toSlice() ([]byte, error) {
 	}
 	return outArr, nil
 }
+
+func (s *LongSketch) Reset() {
+	hasMap, _ := NewReversePurgeLongHashMap(1 << _LG_MIN_MAP_SIZE)
+	s.curMapCap = hasMap.getCapacity()
+	s.offset = 0
+	s.streamWeight = 0
+	s.hashMap = hasMap
+}
+
+/*
+  public void reset() {
+    hashMap = new ReversePurgeLongHashMap(1 << LG_MIN_MAP_SIZE);
+    curMapCap = hashMap.getCapacity();
+    offset = 0;
+    streamWeight = 0;
+  }
+*/
diff --git a/frequencies/long_sketch_test.go b/frequencies/long_sketch_test.go
index feee404..eddfc10 100644
--- a/frequencies/long_sketch_test.go
+++ b/frequencies/long_sketch_test.go
@@ -23,9 +23,9 @@ import (
 )
 
 func TestFrequentItemsStringSerial(t *testing.T) {
-	sketch, err := NewLongSketchForMaxMapSize(8)
+	sketch, err := NewLongSketchWithMaxMapSize(8)
 	assert.NoError(t, err)
-	sketch2, err := NewLongSketchForMaxMapSize(128)
+	sketch2, err := NewLongSketchWithMaxMapSize(128)
 	assert.NoError(t, err)
 	sketch.Update(10, 100)
 	sketch.Update(10, 100)
@@ -89,7 +89,7 @@ func TestFrequentItemsStringSerial(t *testing.T) {
 }
 
 func TestFrequentItemsByteSerial(t *testing.T) {
-	sketch, err := NewLongSketchForMaxMapSize(16)
+	sketch, err := NewLongSketchWithMaxMapSize(16)
 	assert.NoError(t, err)
 	byteArray0, err := sketch.toSlice()
 	newSk0, err := NewLongSketchFromSlice(byteArray0)
@@ -100,7 +100,7 @@ func TestFrequentItemsByteSerial(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, str0, newStr0)
 
-	sketch2, err := NewLongSketchForMaxMapSize(128)
+	sketch2, err := NewLongSketchWithMaxMapSize(128)
 	assert.NoError(t, err)
 	sketch.Update(10, 100)
 	sketch.Update(10, 100)
@@ -167,3 +167,26 @@ func TestFrequentItemsByteSerial(t *testing.T) {
 	assert.Equal(t, mergedSketch.getCurrentMapCapacity(), newSk3.getCurrentMapCapacity())
 	assert.Equal(t, mergedSketch.getStreamLength(), newSk3.getStreamLength())
 }
+
+func TestFrequentItemsByteResetAndEmptySerial(t *testing.T) {
+	sketch, err := NewLongSketchWithMaxMapSize(16)
+	assert.NoError(t, err)
+	sketch.Update(10, 100)
+	sketch.Update(10, 100)
+	sketch.Update(15, 3443)
+	sketch.Update(1000001, 1010230)
+	sketch.Update(1000002, 1010230)
+	sketch.Reset()
+
+	byteArray0, err := sketch.toSlice()
+	assert.NoError(t, err)
+	newSk0, err := NewLongSketchFromSlice(byteArray0)
+	assert.NoError(t, err)
+	str0, err := sketch.serializeToString()
+	assert.NoError(t, err)
+	newStr0, err := newSk0.serializeToString()
+	assert.NoError(t, err)
+	assert.Equal(t, str0, newStr0)
+	assert.Equal(t, sketch.getMaximumMapCapacity(), newSk0.getMaximumMapCapacity())
+	assert.Equal(t, sketch.getCurrentMapCapacity(), newSk0.getCurrentMapCapacity())
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org