/
store_min.go
110 lines (99 loc) · 2.48 KB
/
store_min.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
package store
import (
"fmt"
"math/big"
"strconv"
"github.com/shopspring/decimal"
pbssinternal "github.com/streamingfast/substreams/pb/sf/substreams/intern/v2"
)
func (b *baseStore) SetMinBigInt(ord uint64, key string, value *big.Int) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET_MIN_BIG_INT,
Ord: ord,
Key: key,
Value: bigIntToBytes(value),
})
}
func (b *baseStore) setMinBigInt(ord uint64, key string, value *big.Int) {
min := new(big.Int)
val, found := b.GetAt(ord, key)
if !found {
min = value
} else {
prev, _ := new(big.Int).SetString(string(val), 10)
if prev != nil && value.Cmp(prev) <= 0 {
min = value
} else {
min = prev
}
}
b.set(ord, key, []byte(min.String()))
}
func (b *baseStore) SetMinInt64(ord uint64, key string, value int64) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET_MIN_INT64,
Ord: ord,
Key: key,
Value: int64ToBytes(value),
})
}
func (b *baseStore) setMinInt64(ord uint64, key string, value int64) {
var min int64
val, found := b.GetAt(ord, key)
if !found {
min = value
} else {
prev, err := strconv.ParseInt(string(val), 10, 64)
if err != nil || value < prev {
min = value
} else {
min = prev
}
}
b.set(ord, key, []byte(fmt.Sprintf("%d", min)))
}
func (b *baseStore) SetMinFloat64(ord uint64, key string, value float64) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET_MIN_FLOAT64,
Ord: ord,
Key: key,
Value: float64ToBytes(value),
})
}
func (b *baseStore) setMinFloat64(ord uint64, key string, value float64) {
var min float64
val, found := b.GetAt(ord, key)
if !found {
min = value
} else {
prev, err := strconv.ParseFloat(string(val), 64)
if err != nil || value <= prev {
min = value
} else {
min = prev
}
}
b.set(ord, key, []byte(strconv.FormatFloat(min, 'g', 100, 64)))
}
func (b *baseStore) SetMinBigDecimal(ord uint64, key string, value decimal.Decimal) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET_MIN_BIG_DECIMAL,
Ord: ord,
Key: key,
Value: bigDecimalToBytes(value),
})
}
func (b *baseStore) setMinBigDecimal(ord uint64, key string, value decimal.Decimal) {
val, found := b.GetAt(ord, key)
if !found {
b.set(ord, key, []byte(value.String()))
return
}
prev, err := decimal.NewFromString(string(val))
prev.Truncate(34)
if err != nil || value.Cmp(prev) <= 0 {
b.set(ord, key, []byte(value.String()))
return
}
b.set(ord, key, []byte(prev.String()))
}