| 55 | } |
| 56 | |
| 57 | func newCardinality(precision uint8, staleTime, sketchDuration time.Duration) *Cardinality { |
| 58 | // If parameters are out of bounds, set defaults. |
| 59 | if precision < 4 || precision > 18 { |
| 60 | precision = 14 |
| 61 | } |
| 62 | if staleTime <= 1*time.Minute || sketchDuration < 1*time.Minute || staleTime < sketchDuration { |
| 63 | staleTime = 15 * time.Minute |
| 64 | sketchDuration = 5 * time.Minute |
| 65 | } |
| 66 | |
| 67 | sketchesLength := int((staleTime + sketchDuration) / sketchDuration) // ceil |
| 68 | if sketchesLength < 2 { |
| 69 | sketchesLength = 2 |
| 70 | } |
| 71 | |
| 72 | sketches := make([]*hll.Sketch, sketchesLength) |
| 73 | for i := range sketches { |
| 74 | sketches[i], _ = hll.NewSketch(precision, true) |
| 75 | } |
| 76 | |
| 77 | cachedMerge, _ := hll.NewSketch(precision, true) |
| 78 | return &Cardinality{ |
| 79 | sketchDuration: sketchDuration, |
| 80 | sketchesLength: sketchesLength, |
| 81 | current: 0, |
| 82 | sketches: sketches, |
| 83 | cachedMerge: cachedMerge, |
| 84 | lastAdvance: time.Now(), |
| 85 | precision: precision, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func (c *Cardinality) Insert(hash uint64) { |
| 90 | c.mu.Lock() |