-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.go
More file actions
79 lines (68 loc) · 2.35 KB
/
decode.go
File metadata and controls
79 lines (68 loc) · 2.35 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
package codec
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
raptorq "github.com/LumeraProtocol/rq-go"
"github.com/LumeraProtocol/supernode/pkg/logtrace"
)
type DecodeRequest struct {
ActionID string
Layout Layout
Symbols map[string][]byte
}
type DecodeResponse struct {
Path string
DecodeTmpDir string
}
func (rq *raptorQ) Decode(ctx context.Context, req DecodeRequest) (DecodeResponse, error) {
fields := logtrace.Fields{
logtrace.FieldMethod: "Decode",
logtrace.FieldModule: "rq",
logtrace.FieldActionID: req.ActionID,
}
logtrace.Info(ctx, "RaptorQ decode request received", fields)
processor, err := raptorq.NewDefaultRaptorQProcessor()
if err != nil {
fields[logtrace.FieldError] = err.Error()
return DecodeResponse{}, fmt.Errorf("create RaptorQ processor: %w", err)
}
defer processor.Free()
symbolsDir := filepath.Join(rq.symbolsBaseDir, req.ActionID)
if err := os.MkdirAll(symbolsDir, 0o755); err != nil {
fields[logtrace.FieldError] = err.Error()
return DecodeResponse{}, fmt.Errorf("mkdir %s: %w", symbolsDir, err)
}
// Write symbols to disk
for id, data := range req.Symbols {
symbolPath := filepath.Join(symbolsDir, id)
if err := os.WriteFile(symbolPath, data, 0o644); err != nil {
fields[logtrace.FieldError] = err.Error()
return DecodeResponse{}, fmt.Errorf("write symbol %s: %w", id, err)
}
}
logtrace.Info(ctx, "symbols written to disk", fields)
// ---------- write layout.json ---------- ←★
layoutPath := filepath.Join(symbolsDir, "layout.json")
layoutBytes, err := json.Marshal(req.Layout)
if err != nil {
fields[logtrace.FieldError] = err.Error()
return DecodeResponse{}, fmt.Errorf("marshal layout: %w", err)
}
if err := os.WriteFile(layoutPath, layoutBytes, 0o644); err != nil {
fields[logtrace.FieldError] = err.Error()
return DecodeResponse{}, fmt.Errorf("write layout file: %w", err)
}
logtrace.Info(ctx, "layout.json written", fields)
// Decode
outputPath := filepath.Join(symbolsDir, "output")
if err := processor.DecodeSymbols(symbolsDir, outputPath, layoutPath); err != nil {
fields[logtrace.FieldError] = err.Error()
_ = os.Remove(outputPath)
return DecodeResponse{}, fmt.Errorf("raptorq decode: %w", err)
}
logtrace.Info(ctx, "RaptorQ decoding completed successfully", fields)
return DecodeResponse{Path: outputPath, DecodeTmpDir: symbolsDir}, nil
}