-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquicksum.go
71 lines (65 loc) · 2.04 KB
/
quicksum.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
package tormenta
import "github.com/dgraph-io/badger"
func quickSum(target interface{}, item *badger.Item) {
// TODO: is there a more efficient way to increment
// the sum target given that we don't know what type it is
switch target.(type) {
// Signed Ints
case *int:
// Reminder - decoding the index values only works for fixed length integers
// So in the case of ints, we set up an int32 target and use
// that to accumulate
acc := *target.(*int)
var int32target int32
extractIndexValue(item.Key(), &int32target)
*target.(*int) = acc + int(int32target)
case *int8:
acc := *target.(*int8)
extractIndexValue(item.Key(), target)
*target.(*int8) = acc + *target.(*int8)
case *int16:
acc := *target.(*int16)
extractIndexValue(item.Key(), target)
*target.(*int16) = acc + *target.(*int16)
case *int32:
acc := *target.(*int32)
extractIndexValue(item.Key(), target)
*target.(*int32) = acc + *target.(*int32)
case *int64:
acc := *target.(*int64)
extractIndexValue(item.Key(), target)
*target.(*int64) = acc + *target.(*int64)
// Unsigned ints
case *uint:
// See above for notes on variable vs fixed length
acc := *target.(*uint)
var uint32target uint32
extractIndexValue(item.Key(), &uint32target)
*target.(*uint) = acc + uint(uint32target)
case *uint8:
acc := *target.(*uint8)
extractIndexValue(item.Key(), target)
*target.(*uint8) = acc + *target.(*uint8)
case *uint16:
acc := *target.(*uint16)
extractIndexValue(item.Key(), target)
*target.(*uint16) = acc + *target.(*uint16)
case *uint32:
acc := *target.(*uint32)
extractIndexValue(item.Key(), target)
*target.(*uint32) = acc + *target.(*uint32)
case *uint64:
acc := *target.(*uint64)
extractIndexValue(item.Key(), target)
*target.(*uint64) = acc + *target.(*uint64)
// Floats
case *float64:
acc := *target.(*float64)
extractIndexValue(item.Key(), target)
*target.(*float64) = acc + *target.(*float64)
case *float32:
acc := *target.(*float32)
extractIndexValue(item.Key(), target)
*target.(*float32) = acc + *target.(*float32)
}
}