-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpostgres.go
188 lines (162 loc) · 3.83 KB
/
postgres.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
185
186
187
188
package goharvest
import (
"database/sql"
"fmt"
"sort"
"github.com/google/uuid"
// init postgres driver
"github.com/lib/pq"
)
type database struct {
db *sql.DB
markStmt *sql.Stmt
purgeStmt *sql.Stmt
resetStmt *sql.Stmt
}
const markQueryTemplate = `
-- mark query
UPDATE %s
SET leader_id = $1
WHERE id IN (
SELECT id FROM %s
WHERE leader_id IS NULL OR leader_id != $1
ORDER BY id
LIMIT $2
)
RETURNING id, create_time, kafka_topic, kafka_key, kafka_value, kafka_header_keys, kafka_header_values, leader_id
`
const purgeQueryTemplate = `
-- purge query
DELETE FROM %s
WHERE id = $1
`
const resetQueryTemplate = `
-- reset query
UPDATE %s
SET leader_id = NULL
WHERE id = $1
`
func closeResource(stmt *sql.Stmt) {
if stmt != nil {
stmt.Close()
}
}
func closeResources(stmts ...*sql.Stmt) {
for _, resource := range stmts {
closeResource(resource)
}
}
type databaseProvider func() (*sql.DB, error)
// StandardPostgresBindingProvider returns a DatabaseBindingProvider that connects to a real Postgres database.
func StandardPostgresBindingProvider() DatabaseBindingProvider {
return NewPostgresBinding
}
// NewPostgresBinding creates a Postgres binding for the given dataSource and outboxTable args.
func NewPostgresBinding(dataSource string, outboxTable string) (DatabaseBinding, error) {
return newPostgresBinding(func() (*sql.DB, error) {
return sql.Open("postgres", dataSource)
}, outboxTable)
}
func newPostgresBinding(dbProvider databaseProvider, outboxTable string) (DatabaseBinding, error) {
success := false
var db *sql.DB
var markStmt, purgeStmt, resetStmt *sql.Stmt
defer func() {
if !success {
if db != nil {
db.Close()
}
closeResources(markStmt, purgeStmt, resetStmt)
}
}()
db, err := dbProvider()
if err != nil {
return nil, err
}
db.SetMaxOpenConns(2)
db.SetMaxIdleConns(2)
markStmt, err = db.Prepare(fmt.Sprintf(markQueryTemplate, outboxTable, outboxTable))
if err != nil {
return nil, err
}
purgeStmt, err = db.Prepare(fmt.Sprintf(purgeQueryTemplate, outboxTable))
if err != nil {
return nil, err
}
resetStmt, err = db.Prepare(fmt.Sprintf(resetQueryTemplate, outboxTable))
if err != nil {
return nil, err
}
success = true
return &database{
db: db,
markStmt: markStmt,
purgeStmt: purgeStmt,
resetStmt: resetStmt,
}, nil
}
func (db *database) Mark(leaderID uuid.UUID, limit int) ([]OutboxRecord, error) {
rows, err := db.markStmt.Query(leaderID, limit)
if err != nil {
return nil, err
}
defer rows.Close()
records := make([]OutboxRecord, 0, limit)
for rows.Next() {
record := OutboxRecord{}
var keys []string
var values []string
err := rows.Scan(
&record.ID,
&record.CreateTime,
&record.KafkaTopic,
&record.KafkaKey,
&record.KafkaValue,
pq.Array(&keys),
pq.Array(&values),
&record.LeaderID,
)
if err != nil {
return nil, err
}
numKeys := len(keys)
if len(keys) != len(values) {
return nil, fmt.Errorf("unequal number of header keys (%d) and values (%d)", numKeys, len(values))
}
record.KafkaHeaders = make(KafkaHeaders, numKeys)
for i := 0; i < numKeys; i++ {
record.KafkaHeaders[i] = KafkaHeader{keys[i], values[i]}
}
records = append(records, record)
}
sort.Slice(records, func(i, j int) bool {
return records[i].ID < records[j].ID
})
return records, nil
}
func (db *database) Purge(id int64) (bool, error) {
res, err := db.purgeStmt.Exec(id)
if err != nil {
return false, err
}
affected, _ := res.RowsAffected()
if affected != 1 {
return false, nil
}
return true, err
}
func (db *database) Reset(id int64) (bool, error) {
res, err := db.resetStmt.Exec(id)
if err != nil {
return false, err
}
affected, _ := res.RowsAffected()
if affected != 1 {
return false, nil
}
return true, err
}
func (db *database) Dispose() {
db.db.Close()
closeResources(db.markStmt, db.purgeStmt, db.resetStmt)
}