Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions hyperloglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,14 @@ func (h *HyperLogLog) GobDecode(b []byte) error {
}
return nil
}

func Load(precision uint8, data []byte) (*HyperLogLog, error) {
h, err := New(precision)

if err != nil {
return nil, err
}

h.reg = data
return h, nil
}
15 changes: 15 additions & 0 deletions hyperloglog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,18 @@ func TestHLLGob(t *testing.T) {
t.Error("unmarshaled structure differs")
}
}

func TestHLLLoad(t *testing.T) {
precision := uint8(8)
data := []byte{3, 0, 2, 2, 5, 4, 2, 0, 1, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 3, 5, 0, 2, 0, 2, 0, 2, 4, 2, 0, 2, 5, 2, 3, 2, 2, 0, 0, 1, 1, 0, 1, 4, 2, 0, 0, 0, 0, 0, 3, 2, 0, 1, 3, 1, 0, 1, 0, 1, 0, 0, 0, 0, 4, 4, 1, 2, 4, 1, 4, 0, 0, 4, 0, 0, 3, 0, 0, 2, 0, 1, 1, 2, 1, 0, 2, 1, 0, 0, 2, 0, 0, 1, 2, 2, 1, 0, 4, 3, 1, 0, 2, 4, 4, 1, 8, 2, 0, 0, 1, 2, 1, 4, 1, 0, 2, 2, 2, 1, 0, 0, 0, 0, 0, 2, 0, 1, 1, 0, 0, 3, 1, 0, 2, 5, 0, 1, 0, 0, 1, 2, 0, 5, 2, 0, 2, 2, 4, 0, 0, 0, 0, 6, 1, 3, 3, 0, 2, 1, 0, 0, 5, 0, 1, 0, 1, 0, 0, 0, 1, 2, 0, 2, 2, 0, 2, 0, 0, 0, 1, 0, 3, 1, 0, 0, 3, 1, 1, 0, 0, 5, 2, 2, 0, 0, 3, 1, 1, 0, 0, 0, 2, 2, 0, 1, 0, 3, 0, 0, 2, 0, 0, 0, 3, 2, 0, 2, 0, 5, 0, 5, 2, 0, 1, 4, 0, 0, 0, 3, 0, 0, 0, 1, 3, 3, 1, 1, 2, 0, 0, 0, 3, 2, 5, 0, 1, 1, 1, 1, 0, 3, 1, 0}

hll, err := Load(precision, data)

if err != nil {
t.Error(err)
}

if hll.Count() != 211 {
t.Error("Loaded count should be 211")
}
}