-
Notifications
You must be signed in to change notification settings - Fork 45
/
value_set.go
106 lines (92 loc) · 2.54 KB
/
value_set.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
package store
import (
"fmt"
"strings"
pbssinternal "github.com/streamingfast/substreams/pb/sf/substreams/intern/v2"
pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1"
)
func (b *baseStore) SetBytesIfNotExists(ord uint64, key string, value []byte) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET_BYTES_IF_NOT_EXISTS,
Ord: ord,
Key: key,
Value: cloneBytes(value),
})
}
func (b *baseStore) SetIfNotExists(ord uint64, key string, value string) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET_IF_NOT_EXISTS,
Ord: ord,
Key: key,
Value: cloneBytes([]byte(value)),
})
}
func (b *baseStore) SetBytes(ord uint64, key string, value []byte) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET_BYTES,
Ord: ord,
Key: key,
Value: cloneBytes(value),
})
}
func (b *baseStore) Set(ord uint64, key string, value string) {
b.kvOps.Add(&pbssinternal.Operation{
Type: pbssinternal.Operation_SET,
Ord: ord,
Key: key,
Value: cloneBytes([]byte(value)),
})
}
func (b *baseStore) set(ord uint64, key string, value []byte) {
// FIXME(abourget): these should return an error up the stack instead, would bubble up
// in the wasm/module.go and fail the query, with proper error propagation.
if strings.HasPrefix(key, "__!__") {
panic("key prefix __!__ is reserved for internal system use.")
}
if uint64(len(value)) > b.itemSizeLimit {
panic(fmt.Sprintf("key %q attempted to write %d bytes (capped at %d)", key, len(value), b.itemSizeLimit))
}
if len(key) == 0 {
panic(fmt.Sprintf("invalid key %q", key))
}
cpValue := make([]byte, len(value))
copy(cpValue, value)
val, found := b.getLast(key)
var delta *pbsubstreams.StoreDelta
if found {
delta = &pbsubstreams.StoreDelta{
Operation: pbsubstreams.StoreDelta_UPDATE,
Ordinal: ord,
Key: key,
OldValue: val,
NewValue: cpValue,
}
} else {
delta = &pbsubstreams.StoreDelta{
Operation: pbsubstreams.StoreDelta_CREATE,
Ordinal: ord,
Key: key,
OldValue: nil,
NewValue: cpValue,
}
}
b.ApplyDelta(delta)
b.deltas = append(b.deltas, delta)
}
func (b *baseStore) setIfNotExists(ord uint64, key string, value []byte) {
_, found := b.getLast(key)
if found {
return
}
cpValue := make([]byte, len(value))
copy(cpValue, value)
delta := &pbsubstreams.StoreDelta{
Operation: pbsubstreams.StoreDelta_CREATE,
Ordinal: ord,
Key: key,
OldValue: nil,
NewValue: cpValue,
}
b.ApplyDelta(delta)
b.deltas = append(b.deltas, delta)
}