-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsubscribe.go
86 lines (70 loc) · 1.6 KB
/
subscribe.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
package main
import (
"context"
"fmt"
"github.com/jackc/pgx/v4"
"database/sql"
)
type Sum struct {
sum float64 `json:"sum"`
}
func main() {
ctx := context.Background()
connStr := "postgres://MATERIALIZE_USERNAME:APP_SPECIFIC_PASSWORD@MATERIALIZE_HOST:6875/materialize?sslmode=require"
conn, err := pgx.Connect(ctx, connStr)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Connected to Materialize!")
}
tx, err := conn.Begin(ctx)
if err != nil {
fmt.Println(err)
return
}
defer tx.Rollback(ctx)
_, err = tx.Exec(ctx, "DECLARE c CURSOR FOR SUBSCRIBE (SELECT sum FROM counter_sum) WITH (PROGRESS);")
if err != nil {
fmt.Println(err)
return
}
// Define a struct to hold the data returned from the query
type subscribeResult struct {
MzTimestamp int64
MzProgress bool
MzDiff sql.NullInt64
Sum sql.NullFloat64
}
state := NewState(false)
var buffer []Update
for {
rows, err := tx.Query(ctx, "FETCH ALL c")
if err != nil {
fmt.Println(err)
tx.Rollback(ctx)
return
}
for rows.Next() {
var r subscribeResult
if err := rows.Scan(&r.MzTimestamp, &r.MzProgress, &r.MzDiff, &r.Sum); err != nil {
fmt.Println(err)
tx.Rollback(ctx)
return
}
if r.MzProgress {
state.Update(buffer, r.MzTimestamp)
fmt.Println(state.getState())
// Clean buffer
buffer = []Update{}
} else {
var update = Update{ diff: r.MzDiff.Int64, value: map[string]float64{"sum": r.Sum.Float64}}
buffer = append(buffer, update)
}
}
}
err = tx.Commit(ctx)
if err != nil {
fmt.Println(err)
}
defer conn.Close(context.Background())
}