/
persister.go
103 lines (83 loc) · 2.5 KB
/
persister.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
package persister
import (
"encoding/json"
"fmt"
"os"
"reflect"
"github.com/influxdata/telegraf"
)
type Persister struct {
Filename string
register map[string]telegraf.StatefulPlugin
}
func (p *Persister) Init() error {
p.register = make(map[string]telegraf.StatefulPlugin)
return nil
}
func (p *Persister) Register(id string, plugin telegraf.StatefulPlugin) error {
if _, found := p.register[id]; found {
return fmt.Errorf("plugin with ID %q already registered", id)
}
p.register[id] = plugin
return nil
}
func (p *Persister) Load() error {
// Read the states from disk
in, err := os.ReadFile(p.Filename)
if err != nil {
return fmt.Errorf("reading states file failed: %w", err)
}
// Unmarshal the id to serialized states map
var states map[string][]byte
if err := json.Unmarshal(in, &states); err != nil {
return fmt.Errorf("unmarshalling states failed: %w", err)
}
// Get the initialized state as blueprint for unmarshalling
for id, serialized := range states {
// Check if we have a plugin with that ID
plugin, found := p.register[id]
if !found {
continue
}
// Create a new empty state of the "state"-type. As we need a pointer
// of the state, we cannot dereference it here due to the unknown
// nature of the state-type.
nstate := reflect.New(reflect.TypeOf(plugin.GetState())).Interface()
if err := json.Unmarshal(serialized, &nstate); err != nil {
return fmt.Errorf("unmarshalling state for %q failed: %w", id, err)
}
state := reflect.ValueOf(nstate).Elem().Interface()
// Set the state in the plugin
if err := plugin.SetState(state); err != nil {
return fmt.Errorf("setting state of %q failed: %w", id, err)
}
}
return nil
}
func (p *Persister) Store() error {
states := make(map[string][]byte)
// Collect the states and serialize the individual data chunks
// to later serialize all items in the id / serialized-states map
for id, plugin := range p.register {
state, err := json.Marshal(plugin.GetState())
if err != nil {
return fmt.Errorf("marshalling state for id %q failed: %w", id, err)
}
states[id] = state
}
// Serialize the states
serialized, err := json.Marshal(states)
if err != nil {
return fmt.Errorf("marshalling states failed: %w", err)
}
// Write the states to disk
f, err := os.Create(p.Filename)
if err != nil {
return fmt.Errorf("creating states file %q failed: %w", p.Filename, err)
}
defer f.Close()
if _, err := f.Write(serialized); err != nil {
return fmt.Errorf("writing states failed: %w", err)
}
return nil
}