forked from Tochemey/ego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.go
210 lines (176 loc) · 5.87 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* MIT License
*
* Copyright (c) 2022-2024 Tochemey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package postgres
import (
"context"
"errors"
"fmt"
sq "github.com/Masterminds/squirrel"
"github.com/golang/protobuf/proto"
"github.com/jackc/pgx/v5"
"go.uber.org/atomic"
"github.com/tochemey/ego/v3/egopb"
"github.com/tochemey/ego/v3/internal/postgres"
"github.com/tochemey/ego/v3/persistence"
)
var (
columns = []string{
"persistence_id",
"version_number",
"state_payload",
"state_manifest",
"timestamp",
"shard_number",
}
tableName = "states_store"
)
// DurableStore implements the DurableStore interface
// and helps persist events in a Postgres database
type DurableStore struct {
db postgres.Postgres
sb sq.StatementBuilderType
// hold the connection state to avoid multiple connection of the same instance
connected *atomic.Bool
}
// enforce interface implementation
var _ persistence.StateStore = (*DurableStore)(nil)
// NewStateStore creates a new instance of StateStore
func NewStateStore(config *Config) *DurableStore {
// create the underlying db connection
db := postgres.New(postgres.NewConfig(config.DBHost, config.DBPort, config.DBUser, config.DBPassword, config.DBName))
return &DurableStore{
db: db,
sb: sq.StatementBuilder.PlaceholderFormat(sq.Dollar),
connected: atomic.NewBool(false),
}
}
// Connect connects to the underlying postgres database
func (s *DurableStore) Connect(ctx context.Context) error {
// check whether this instance of the journal is connected or not
if s.connected.Load() {
return nil
}
// connect to the underlying db
if err := s.db.Connect(ctx); err != nil {
return err
}
// set the connection status
s.connected.Store(true)
return nil
}
// Disconnect disconnects from the underlying postgres database
func (s *DurableStore) Disconnect(ctx context.Context) error {
// check whether this instance of the journal is connected or not
if !s.connected.Load() {
return nil
}
// disconnect the underlying database
if err := s.db.Disconnect(ctx); err != nil {
return err
}
// set the connection status
s.connected.Store(false)
return nil
}
// Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (s *DurableStore) Ping(ctx context.Context) error {
// check whether we are connected or not
if !s.connected.Load() {
return s.Connect(ctx)
}
return nil
}
// WriteState writes a durable state into the underlying postgres database
func (s *DurableStore) WriteState(ctx context.Context, state *egopb.DurableState) error {
if !s.connected.Load() {
return errors.New("durable store is not connected")
}
if state == nil || proto.Equal(state, &egopb.DurableState{}) {
return nil
}
tx, err := s.db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted})
if err != nil {
return fmt.Errorf("failed to obtain a database transaction: %w", err)
}
query, args, err := s.sb.Delete(tableName).Where(sq.Eq{"persistence_id": state.GetPersistenceId()}).ToSql()
if err != nil {
return fmt.Errorf("failed to build the delete sql statement: %w", err)
}
if _, err := s.db.Exec(ctx, query, args...); err != nil {
return fmt.Errorf("failed to delete durable state from the database: %w", err)
}
bytea, _ := proto.Marshal(state.GetResultingState())
manifest := string(state.GetResultingState().ProtoReflect().Descriptor().FullName())
statement := s.sb.
Insert(tableName).
Columns(columns...).
Values(
state.GetPersistenceId(),
state.GetVersionNumber(),
bytea,
manifest,
state.GetTimestamp(),
state.GetShard(),
)
query, args, err = statement.ToSql()
if err != nil {
return fmt.Errorf("unable to build sql insert statement: %w", err)
}
_, execErr := tx.Exec(ctx, query, args...)
if execErr != nil {
if err = tx.Rollback(ctx); err != nil {
return fmt.Errorf("unable to rollback db transaction: %w", err)
}
return fmt.Errorf("failed to record durable state: %w", execErr)
}
// commit the transaction
if commitErr := tx.Commit(ctx); commitErr != nil {
return fmt.Errorf("failed to record durable state: %w", commitErr)
}
return nil
}
// GetLatestState fetches the latest durable state of a persistenceID
func (s *DurableStore) GetLatestState(ctx context.Context, persistenceID string) (*egopb.DurableState, error) {
if !s.connected.Load() {
return nil, errors.New("durable store is not connected")
}
statement := s.sb.
Select(columns...).
From(tableName).
Where(sq.Eq{"persistence_id": persistenceID}).
Limit(1)
query, args, err := statement.ToSql()
if err != nil {
return nil, fmt.Errorf("failed to build the select sql statement: %w", err)
}
row := new(row)
err = s.db.Select(ctx, row, query, args...)
if err != nil {
return nil, fmt.Errorf("failed to fetch the latest event from the database: %w", err)
}
if row.PersistenceID == "" {
return nil, nil
}
return row.ToDurableState()
}