forked from nytlabs/st-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_keyvalue.go
184 lines (168 loc) · 3.36 KB
/
source_keyvalue.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package core
import (
"errors"
"sync"
)
func KeyValueStore() SourceSpec {
return SourceSpec{
Name: "key_value",
Type: KEY_VALUE,
New: NewKeyValue,
}
}
func NewKeyValue() Source {
return &KeyValue{
kv: make(map[string]interface{}),
quit: make(chan bool),
}
}
func (k KeyValue) GetType() SourceType {
return KEY_VALUE
}
type KeyValue struct {
kv map[string]interface{}
quit chan bool
sync.Mutex
}
func (k *KeyValue) Get() interface{} {
return k.kv
}
func (k *KeyValue) Set(v interface{}) error {
kv, ok := v.(map[string]interface{})
if !ok {
return errors.New("not a map")
}
k.kv = kv
return nil
}
// retrieves a value from the key value store
func kvGet() Spec {
return Spec{
Name: "kvGet",
Inputs: []Pin{
Pin{"key", STRING},
},
Outputs: []Pin{
Pin{"value", ANY},
},
Source: KEY_VALUE,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
kv := s.(*KeyValue)
key, ok := in[0].(string)
if !ok {
out[0] = NewError("Key is not type string")
return nil
}
if value, ok := kv.kv[key]; !ok {
out[0] = NewError("Key not found")
} else {
out[0] = value
}
return nil
},
}
}
// sets an entry in a key value store
// if the entry is new, emits true
func kvSet() Spec {
return Spec{
Name: "kvSet",
Inputs: []Pin{
Pin{"key", STRING},
Pin{"value", ANY},
},
Outputs: []Pin{
Pin{"new", BOOLEAN},
},
Source: KEY_VALUE,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
kv := s.(*KeyValue)
key, ok := in[0].(string)
if !ok {
out[0] = NewError("Key is not type string")
return nil
}
if _, ok := kv.kv[key]; !ok {
out[0] = true
} else {
out[0] = false
}
kv.kv[in[0].(string)] = in[1]
return nil
},
}
}
// clears the entire map
// TODO: prefer "empty"
// change interface{} to message
func kvClear() Spec {
return Spec{
Name: "kvClear",
Inputs: []Pin{
Pin{"clear", ANY},
},
Outputs: []Pin{
Pin{"cleared", BOOLEAN},
},
Source: KEY_VALUE,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
kv := s.(*KeyValue)
kv.kv = make(map[string]interface{})
out[0] = true
return nil
},
}
}
// dumps the entire map into a message
// should output be named "object" ?
// TODO: convert interface{} to message
// !! should probably double check this to ensure that we don't need a deep copy
func kvDump() Spec {
return Spec{
Name: "kvDump",
Inputs: []Pin{
Pin{"dump", ANY},
},
Outputs: []Pin{
Pin{"object", OBJECT},
},
Source: KEY_VALUE,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
kv := s.(*KeyValue)
outMap := make(map[string]interface{})
for k, v := range kv.kv {
outMap[k] = v
}
out[0] = outMap
return nil
},
}
}
// deletes an entry in a key value store
func kvDelete() Spec {
return Spec{
Name: "kvDelete",
Inputs: []Pin{
Pin{"key", STRING},
},
Outputs: []Pin{
Pin{"deleted", BOOLEAN},
},
Source: KEY_VALUE,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
kv := s.(*KeyValue)
key, ok := in[0].(string)
if !ok {
out[0] = NewError("Key is not type string")
return nil
}
if _, ok := kv.kv[key]; !ok {
out[0] = false
} else {
delete(kv.kv, key)
out[0] = true
}
return nil
},
}
}