forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 10
/
listening.go
90 lines (74 loc) · 2.41 KB
/
listening.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
package types
import (
"io"
"github.com/shapeshift/cosmos-sdk/codec"
)
// WriteListener interface for streaming data out from a KVStore
type WriteListener interface {
// if value is nil then it was deleted
// storeKey indicates the source KVStore, to facilitate using the same WriteListener across separate KVStores
// delete bool indicates if it was a delete; true: delete, false: set
OnWrite(storeKey StoreKey, key []byte, value []byte, delete bool) error
}
// StoreKVPairWriteListener is used to configure listening to a KVStore by
// writing out length-prefixed Protobuf encoded StoreKVPairs to an underlying
// io.Writer object.
type StoreKVPairWriteListener struct {
writer io.Writer
marshaller codec.BinaryCodec
}
// NewStoreKVPairWriteListener wraps creates a StoreKVPairWriteListener with a
// provided io.Writer and codec.BinaryCodec.
func NewStoreKVPairWriteListener(w io.Writer, m codec.Codec) *StoreKVPairWriteListener {
return &StoreKVPairWriteListener{
writer: w,
marshaller: m,
}
}
// OnWrite satisfies the WriteListener interface by writing length-prefixed
// Protobuf encoded StoreKVPairs.
func (wl *StoreKVPairWriteListener) OnWrite(storeKey StoreKey, key []byte, value []byte, delete bool) error {
kvPair := &StoreKVPair{
StoreKey: storeKey.Name(),
Key: key,
Value: value,
Delete: delete,
}
by, err := wl.marshaller.MarshalLengthPrefixed(kvPair)
if err != nil {
return err
}
if _, err := wl.writer.Write(by); err != nil {
return err
}
return nil
}
// MemoryListener listens to the state writes and accumulate the records in memory.
type MemoryListener struct {
key StoreKey
stateCache []StoreKVPair
}
// NewMemoryListener creates a listener that accumulate the state writes in memory.
func NewMemoryListener(key StoreKey) *MemoryListener {
return &MemoryListener{key: key}
}
// OnWrite implements WriteListener interface.
func (fl *MemoryListener) OnWrite(storeKey StoreKey, key []byte, value []byte, delete bool) error {
fl.stateCache = append(fl.stateCache, StoreKVPair{
StoreKey: storeKey.Name(),
Delete: delete,
Key: key,
Value: value,
})
return nil
}
// PopStateCache returns the current state caches and set to nil.
func (fl *MemoryListener) PopStateCache() []StoreKVPair {
res := fl.stateCache
fl.stateCache = nil
return res
}
// StoreKey returns the storeKey it listens to.
func (fl *MemoryListener) StoreKey() StoreKey {
return fl.key
}