Repository navigation
Expand file tree
/
Copy pathmicro_command.go
More file actions
270 lines (237 loc) · 7.84 KB
/
Copy pathmicro_command.go
File metadata and controls
270 lines (237 loc) · 7.84 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// micro_command.go
//
// I "micro comandi" sono la forma canonica del protocollo: un verbo, un
// bersaglio e dei modificatori key=value. Il resto della superficie — i 51 nomi
// storici — resta valido come *alias* (command_alias.go): un riscrittore di
// argomenti più un formattatore di risposta, registrati all'avvio in una tabella
// sola, come registerDefaultReducers fa con i reducer.
//
// Due vincoli tengono in piedi la compatibilità e vanno rispettati da ogni alias
// nuovo:
//
// - i nomi dei campi di risposta sono un contratto di rete (purged=, matches=,
// degree=, count=, next_cursor=, job=). Un alias deve riprodurre la sua
// risposta storica byte per byte; cambia solo l'interno. È il motivo per cui
// una microResponse resta *strutturata* fino al bordo del dispatcher: il
// formattatore rilegge i campi per nome invece di ri-parsare una riga.
// - un comando, una riga. Vale per i micro comandi come per gli alias; una
// lista viaggia in payload=<base64>.
package main
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"sync"
)
// microField è una coppia della riga di risposta. Con Key vuota rende il solo
// valore, che è la forma già usata dai comandi storici (ERROR,not_found).
type microField struct {
Key string
Value string
}
func mf(key string, value string) microField { return microField{Key: key, Value: value} }
func mfi(key string, value int) microField { return microField{Key: key, Value: strconv.Itoa(value)} }
func mfu(key string, value uint64) microField {
return microField{Key: key, Value: strconv.FormatUint(value, 10)}
}
// microResponse è la risposta di un micro comando prima di diventare testo.
type microResponse struct {
Status string
Fields []microField
}
func microOK(fields ...microField) microResponse {
return microResponse{Status: "SUCCESS", Fields: fields}
}
func microPending(fields ...microField) microResponse {
return microResponse{Status: "PENDING", Fields: fields}
}
// microFail rende ERROR,<reason>. reason è il token nudo, senza prefisso.
func microFail(reason string) microResponse {
return microResponse{Status: "ERROR", Fields: []microField{{Value: reason}}}
}
func microFailf(format string, args ...any) microResponse {
return microFail(fmt.Sprintf(format, args...))
}
// microSilent è la risposta vuota: il chiamante ha solo un error da propagare,
// e il front-end non scrive nulla sul filo (il server risponde con l'errore).
func microSilent() microResponse { return microResponse{} }
func (r microResponse) Render() string {
if r.Status == "" {
return ""
}
var b strings.Builder
b.WriteString(r.Status)
for _, field := range r.Fields {
b.WriteString(",")
if field.Key != "" {
b.WriteString(field.Key)
b.WriteString("=")
}
b.WriteString(field.Value)
}
return b.String()
}
func (r microResponse) Get(key string) string {
for _, field := range r.Fields {
if field.Key == key {
return field.Value
}
}
return ""
}
func (r microResponse) Has(key string) bool {
for _, field := range r.Fields {
if field.Key == key {
return true
}
}
return false
}
func (r microResponse) IsError() bool { return r.Status == "ERROR" }
// microArgs è l'argomento di un micro comando già separato nelle sue tre parti.
// Rest serve ai verbi che incapsulano un'altra riga di comando (JOB submit) e
// non possono quindi limitarsi alle coppie key=value.
type microArgs struct {
Target string
Rest string
Params map[string]string
}
func (a microArgs) get(names ...string) string {
for _, name := range names {
if value, ok := a.Params[name]; ok && value != "" {
return value
}
}
return ""
}
func (a microArgs) has(names ...string) bool {
for _, name := range names {
if _, ok := a.Params[name]; ok {
return true
}
}
return false
}
// flag legge un modificatore booleano tollerando l'assenza: def è il valore
// quando il campo non è stato passato affatto.
func (a microArgs) flag(name string, def bool) bool {
raw, ok := a.Params[name]
if !ok || strings.TrimSpace(raw) == "" {
return def
}
return parseBoolFlag(raw)
}
type microHandler func(db *Database, args microArgs) (microResponse, error)
// microCall è un'invocazione di micro comando costruita da un alias. I valori
// binari ci arrivano già ricodificati in x<hex>, così una chiave con spazi o con
// una "x" iniziale sopravvive al passaggio.
type microCall struct {
Verb string
Target string
Rest string
Params map[string]string
}
type microCommandRegistry struct {
mu sync.RWMutex
handlers map[string]microHandler
}
func newMicroCommandRegistry() *microCommandRegistry {
return µCommandRegistry{handlers: make(map[string]microHandler)}
}
func (r *microCommandRegistry) Register(name string, handler microHandler) {
if r == nil || handler == nil {
return
}
r.mu.Lock()
r.handlers[strings.ToUpper(strings.TrimSpace(name))] = handler
r.mu.Unlock()
}
func (r *microCommandRegistry) Resolve(name string) microHandler {
if r == nil {
return nil
}
r.mu.RLock()
defer r.mu.RUnlock()
return r.handlers[strings.ToUpper(strings.TrimSpace(name))]
}
var (
commandRegistriesOnce sync.Once
microCommands *microCommandRegistry
commandAliases *commandAliasRegistry
jobCommands *jobCommandRegistry
)
// ensureCommandRegistries popola le tre tabelle una volta per processo. Sono
// senza stato — il manager dei job invece è per database — quindi vivono a
// livello di package come la registry dei reducer vive nel database.
func ensureCommandRegistries() {
commandRegistriesOnce.Do(func() {
microCommands = newMicroCommandRegistry()
commandAliases = newCommandAliasRegistry()
jobCommands = newJobCommandRegistry()
registerDefaultMicroCommands(microCommands)
registerDefaultJobCommands(jobCommands)
registerDefaultCommandAliases(commandAliases)
})
}
func resolveMicroCommand(name string) microHandler {
ensureCommandRegistries()
return microCommands.Resolve(name)
}
func resolveCommandAlias(name string) *commandAlias {
ensureCommandRegistries()
return commandAliases.Resolve(name)
}
func resolveJobCommand(name string) *jobCommand {
ensureCommandRegistries()
return jobCommands.Resolve(name)
}
func registerDefaultMicroCommands(registry *microCommandRegistry) {
registry.Register("ALIAS", microAlias)
registry.Register("BATCH", microBatch)
registry.Register("DEL", microDel)
registry.Register("GRAPH_CACHE", microGraphCache)
registry.Register("JOB", microJobCommand)
registry.Register("RECORD", microRecord)
}
// splitMicroArgs separa il bersaglio dai modificatori. Il bersaglio è il primo
// token solo se non contiene "=", così "DEL pairs prefix=ctx:" e
// "JOB status id=reduce_1" si leggono allo stesso modo.
func splitMicroArgs(raw string) microArgs {
trimmed := strings.TrimSpace(raw)
args := microArgs{Params: map[string]string{}}
if trimmed == "" {
return args
}
first, rest, _ := strings.Cut(trimmed, " ")
if strings.Contains(first, "=") {
args.Rest = trimmed
} else {
args.Target = strings.ToLower(first)
args.Rest = strings.TrimSpace(rest)
}
args.Params = parseKeyValueArgs(args.Rest)
return args
}
// microParseBytes decodifica un valore binario del dialetto micro: "x"+hex,
// altrimenti testo. La "x" da sola è la stringa vuota — gli alias ricodificano
// sempre in hex, quindi il round-trip è esatto anche per chiavi con spazi.
func microParseBytes(raw string) ([]byte, error) {
if raw == "x" {
return []byte{}, nil
}
return parseValue(raw)
}
// microEncodeBytes è l'inverso: sempre esadecimale, mai ambiguo.
func microEncodeBytes(value []byte) string {
return "x" + hex.EncodeToString(value)
}
// executeMicroCommand esegue un micro comando e ne rende la riga.
func (db *Database) executeMicroCommand(command string, args string) (string, error) {
handler := resolveMicroCommand(command)
if handler == nil {
return "ERROR,unknown_command", nil
}
res, err := handler(db, splitMicroArgs(args))
return res.Render(), err
}