Skip to content

Instantly share code, notes, and snippets.

@nemosupremo
Created September 2, 2013 06:00
Show Gist options
  • Save nemosupremo/6409623 to your computer and use it in GitHub Desktop.
Save nemosupremo/6409623 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/rand"
hyperloglog "github.com/channelmeter/hyperloglog-redigo"
hyperloglogEncoder "github.com/channelmeter/hyperloglog-redigo/encoder"
"github.com/garyburd/redigo/redis"
"io"
"log"
"time"
)
func randB() []byte {
b := make([]byte, 512)
n, err := io.ReadFull(rand.Reader, b)
if n != len(b) || err != nil {
panic(err)
return nil
}
return b
}
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
panic(err)
}
ctr := hyperloglog.CreateCounter(11)
tctr := hyperloglog.CreateTimeSeriesCounter(11)
ctr.SetEncoder(hyperloglogEncoder.SnappyEncoder())
tctr.SetEncoder(hyperloglogEncoder.SnappyEncoder2x())
c.Send("DEL", "test:gohll")
for i := 0; i < 500; i++ {
ctr.Add(c, "test:gohll", randB())
}
log.Printf("Values Added : %d", ctr.Count(c, "test:gohll"))
c.Send("DEL", "test:gohll")
lm := time.Unix(time.Now().Unix()-86400*30, 0)
lw := time.Unix(time.Now().Unix()-86400*7, 0)
mid := time.Unix(time.Now().Unix()-86400*14, 0)
tctr.Add(c, "test:gohll", []byte("Hello World!"), mid)
tctr.Add(c, "test:gohll", []byte("Hello World!"), mid)
tctr.Add(c, "test:gohll", randB(), mid)
tctr.Add(c, "test:gohll", randB(), mid)
tctr.Add(c, "test:gohll", randB(), mid)
tctr.Add(c, "test:gohll", randB(), mid)
tctr.Add(c, "test:gohll", randB(), mid)
tctr.Add(c, "test:gohll", randB(), mid)
tctr.Add(c, "test:gohll", randB(), mid)
tctr.Add(c, "test:gohll", randB(), mid)
log.Printf("Values Added Within This Week : %d", tctr.Count(c, "test:gohll", lw))
log.Printf("Values Added Within This Month: %d", tctr.Count(c, "test:gohll", lm))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment