diff --git a/hyperloglog.go b/hyperloglog.go index 1c7e120..bbb85ea 100644 --- a/hyperloglog.go +++ b/hyperloglog.go @@ -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 +} diff --git a/hyperloglog_test.go b/hyperloglog_test.go index 4936e17..55b8151 100644 --- a/hyperloglog_test.go +++ b/hyperloglog_test.go @@ -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") + } +}