-
Notifications
You must be signed in to change notification settings - Fork 2
/
hyperloglog_redis.go
301 lines (281 loc) · 8.7 KB
/
hyperloglog_redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
Implements probabilistic data structure hyperloglog used in estimating unique entries in a
large dataset.
Hyperloglog: A probabilistic data structure used for estimating the cardinality
(number of unique elements) of in a very large dataset.
Refer: https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/40671.pdf
The package implements both in-mem and Redis backed solutions for the data structures. The
in-memory data structures are thread-safe.
*/
package gostatix
import (
"context"
"encoding/json"
"fmt"
"strconv"
"github.com/kwertop/gostatix/internal/util"
"github.com/redis/go-redis/v9"
)
// HyperLogLogRedis is the Redis backed implementation of BaseHyperLogLog
// _key_ holds the Redis key to the list which has the registers
// _metadataKey_ is used to store the additional information about HyperLogLogRedis
// for retrieving the sketch by the Redis key
type HyperLogLogRedis struct {
AbstractHyperLogLog
key string
metadataKey string
}
// NewHyperLogLogRedis creates new HyperLogLogRedis with the specified _numRegisters_
func NewHyperLogLogRedis(numRegisters uint64) (*HyperLogLogRedis, error) {
abstractLog, err := makeAbstractHyperLogLog(numRegisters)
if err != nil {
return nil, err
}
key := util.GenerateRandomString(16)
metadataKey := util.GenerateRandomString(16)
h := &HyperLogLogRedis{*abstractLog, key, metadataKey}
metadata := make(map[string]interface{})
metadata["numRegisters"] = h.numRegisters
metadata["key"] = h.key
err = getRedisClient().HSet(context.Background(), h.metadataKey, metadata).Err()
if err != nil {
return nil, fmt.Errorf("gostatix: error creating count min sketch redis, error: %v", err)
}
err = h.initRegisters()
if err != nil {
return nil, err
}
return h, nil
}
// NewHyperLogLogRedisFromKey is used to create a new Redis backed HyperLogLogRedis from the
// _metadataKey_ (the Redis key used to store the metadata about the hyperloglog) passed.
// For this to work, value should be present in Redis at _key_
func NewHyperLogLogRedisFromKey(metadataKey string) (*HyperLogLogRedis, error) {
values, err := getRedisClient().HGetAll(context.Background(), metadataKey).Result()
if err != nil {
return nil, fmt.Errorf("gostatix: error creating hyeprloglog from redis key, error: %v", err)
}
numRegisters, _ := strconv.Atoi(values["numRegisters"])
abstractLog, err := makeAbstractHyperLogLog(uint64(numRegisters))
if err != nil {
return nil, err
}
h := &HyperLogLogRedis{*abstractLog, values["key"], metadataKey}
return h, nil
}
// MetadataKey returns the metadataKey
func (h *HyperLogLogRedis) MetadataKey() string {
return h.metadataKey
}
// Update sets the count of the passed _data_ (byte slice) to the hashed location
// in the Redis list at _key_
func (h *HyperLogLogRedis) Update(data []byte) error {
registerIndex, count := h.getRegisterIndexAndCount(data)
return h.updateRegisters(uint8(registerIndex), uint8(count))
}
// Count returns the number of distinct elements so far
// _withCorrection_ is used to specify if correction is to be done for large registers
// _withRoundingOff_ is used to specify if rounding off is required for estimation
func (h *HyperLogLogRedis) Count(withCorrection bool, withRoundingOff bool) (uint64, error) {
harmonicMean, err := h.computeHarmonicMean()
if err != nil {
return 0, err
}
return h.getEstimation(harmonicMean, withCorrection, withRoundingOff), nil
}
// Merge merges two HyperLogLogRedis data structures
func (h *HyperLogLogRedis) Merge(g *HyperLogLogRedis) error {
if h.numRegisters != g.numRegisters {
return fmt.Errorf("gostatix: number of registers %d, %d don't match", h.numRegisters, g.numRegisters)
}
return h.mergeRegisters(g.key)
}
// Equals checks if two HyperLogLogRedis data structures are equal
func (h *HyperLogLogRedis) Equals(g *HyperLogLogRedis) (bool, error) {
if h.numRegisters != g.numRegisters {
return false, nil
}
return h.compareRegisters(g.key)
}
// Export JSON marshals the HyperLogLogRedis and returns a byte slice containing the data
func (h *HyperLogLogRedis) Export() ([]byte, error) {
result, err := getRedisClient().LRange(
context.Background(),
h.key,
0,
-1,
).Result()
if err != nil {
return nil, fmt.Errorf("gostatix: error fetching registers from redis, error: %v", err)
}
registers := make([]uint8, h.numRegisters)
for i := range registers {
val, _ := strconv.Atoi(result[i])
registers[i] = uint8(val)
}
return json.Marshal(hyperLogLogJSON{h.numRegisters, h.numBytesPerHash, h.correctionBias, registers, h.key})
}
// Import JSON unmarshals the _data_ into the HyperLogLogRedis
func (h *HyperLogLogRedis) Import(data []byte, withNewKey bool) error {
var g hyperLogLogJSON
err := json.Unmarshal(data, &g)
if err != nil {
return err
}
h.numRegisters = g.NumRegisters
h.numBytesPerHash = g.NumBytesPerHash
h.correctionBias = g.CorrectionBias
if withNewKey {
h.key = util.GenerateRandomString(16)
} else {
h.key = g.Key
}
return h.importRegisters(g.Registers)
}
func (h *HyperLogLogRedis) importRegisters(registers []uint8) error {
args := make([]interface{}, len(registers))
for i := range registers {
args[i] = interface{}(registers[i])
}
importRegistersScript := redis.NewScript(`
local key = KEYS[1]
local size = #ARGV
local registers = {}
for i=1, size do
registers[i] = tonumber(ARGV[i])
end
redis.call('RPUSH', key, unpack(registers))
return true
`)
_, err := importRegistersScript.Run(
context.Background(),
getRedisClient(),
[]string{h.key},
args...,
).Bool()
if err != nil {
return fmt.Errorf("gostatix: error importing registers for key: %s, error: %v", h.key, err)
}
return nil
}
func (h *HyperLogLogRedis) mergeRegisters(key string) error {
mergeRegistersScript := redis.NewScript(`
local key1 = KEYS[1]
local key2 = KEYS[2]
local size = ARGV[1]
local vals1 = redis.pcall('LRANGE', key1, 0, -1)
local vals2 = redis.pcall('LRANGE', key2, 0, -1)
for i=1, tonumber(size) do
if tonumber(vals1[i]) < tonumber(vals2[i]) then
vals1[i] = vals2[i]
end
end
redis.pcall('LPUSH', key1, unpack(vals1))
return true
`)
_, err := mergeRegistersScript.Run(
context.Background(),
getRedisClient(),
[]string{h.key, key},
h.numRegisters,
).Bool()
if err != nil {
return fmt.Errorf("gostatix: error while merging registers %s with %s, error: %v", h.key, key, err)
}
return nil
}
func (h *HyperLogLogRedis) compareRegisters(key string) (bool, error) {
equals := redis.NewScript(`
local key1 = KEYS[1]
local key2 = KEYS[2]
local size = ARGV[1]
local vals1 = redis.pcall('LRANGE', key1, 0, -1)
local vals2 = redis.pcall('LRANGE', key2, 0, -1)
for i=1, tonumber(size) do
if tonumber(vals1[i]) ~= tonumber(vals2[i]) then
return false
end
end
return true
`)
ok, err := equals.Run(
context.Background(),
getRedisClient(),
[]string{h.key, key},
h.numRegisters,
).Bool()
if err != nil {
return false, fmt.Errorf("gostatix: error while comparing registers %s with %s, error: %v", h.key, key, err)
}
return ok, nil
}
func (h *HyperLogLogRedis) computeHarmonicMean() (float64, error) {
harmonicMeanScript := redis.NewScript(`
local key = KEYS[1]
local size = ARGV[1]
local hmean = 0.0
local values = redis.pcall('LRANGE', key, 0, -1)
for i=1, tonumber(size) do
local value = (-1)*tonumber(values[i])
hmean = hmean + 2^(value)
end
return hmean
`)
hmean, err := harmonicMeanScript.Run(
context.Background(),
getRedisClient(),
[]string{h.key},
h.numRegisters,
).Float64()
if err != nil {
return 0, fmt.Errorf("gostatix: error while computing harmonic mean of hyperloglog, error: %v", err)
}
return hmean, nil
}
func (h *HyperLogLogRedis) updateRegisters(index, count uint8) error {
updateList := redis.NewScript(`
local key = KEYS[1]
local index = tonumber(ARGV[1])
local val = tonumber(ARGV[2])
local count = redis.call('LINDEX', key, index)
if val > tonumber(count) then
count = val
end
redis.call('LSET', key, index, count)
return true
`)
_, err := updateList.Run(
context.Background(),
getRedisClient(),
[]string{h.key},
index,
count,
).Bool()
if err != nil {
return fmt.Errorf("gostatix: error while updating hyperloglog registers in redis, error: %v", err)
}
return nil
}
func (h *HyperLogLogRedis) initRegisters() error {
initList := redis.NewScript(`
local key = KEYS[1]
local size = ARGV[1]
local registers = {}
for i=1, tonumber(size)/2 do
registers[i] = 0
end
redis.call('LPUSH', key, unpack(registers))
redis.call('LPUSH', key, unpack(registers))
return true
`)
_, err := initList.Run(
context.Background(),
getRedisClient(),
[]string{h.key},
h.numRegisters,
).Bool()
if err != nil {
return fmt.Errorf("gostatix: error while initializing hyperloglog registers in redis, error: %v", err)
}
return nil
}