-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdriver.go
More file actions
329 lines (295 loc) · 9.42 KB
/
driver.go
File metadata and controls
329 lines (295 loc) · 9.42 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package neogo
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/auth"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
"golang.org/x/sync/semaphore"
"github.com/rlch/neogo/internal"
"github.com/rlch/neogo/query"
)
// New creates a new neogo [Driver] from a [neo4j.DriverWithContext].
func New(
target string,
auth auth.TokenManager,
configurers ...Configurer,
) (Driver, error) {
cfg := &Config{
Config: *defaultConfig(),
}
for _, c := range configurers {
c(cfg)
}
neo4j, err := neo4j.NewDriverWithContext(
target,
auth,
func(c *config.Config) { *c = cfg.Config },
)
if err != nil {
return nil, fmt.Errorf("failed to create Neo4J driver: %w", err)
}
d := driver{
db: neo4j,
causalConsistencyKey: cfg.CausalConsistencyKey,
sessionSemaphore: semaphore.NewWeighted(int64(cfg.Config.MaxConnectionPoolSize)),
}
// Register types from config
if len(cfg.Types) > 0 {
d.registerTypes(cfg.Types...)
}
return &d, nil
}
type (
// Driver represents a pool of connections to a neo4j server or cluster. It
// provides an entrypoint to a neogo [query.Client], which can be used to build
// cypher queries.
//
// It's safe for concurrent use.
Driver interface {
// DB returns the underlying neo4j driver.
DB() neo4j.DriverWithContext
// ReadSession creates a new read-access session based on the specified session configuration.
ReadSession(ctx context.Context, configurers ...func(*neo4j.SessionConfig)) readSession
// WriteSession creates a new write-access session based on the specified session configuration.
WriteSession(ctx context.Context, configurers ...func(*neo4j.SessionConfig)) writeSession
// Exec creates a new transaction + session and executes the given Cypher
// query.
//
// The access mode is inferred from the clauses used in the query. If using
// Cypher() to inject a write query, one should use [WithSessionConfig] to
// override the access mode.
//
// The session is closed after the query is executed.
Exec(configurers ...func(*execConfig)) Query
}
// Expression is an interface for compiling a Cypher expression outside the context of a query.
Expression = query.Expression
// Query is the interface for constructing a Cypher query.
Query = query.Query
// Work is a function that allows Cypher to be executed within a Transaction.
Work func(start func() Query) error
// Transaction represents an explicit transaction that can be committed or rolled back.
Transaction interface {
// Run executes a statement on this transaction and returns a result
// Contexts terminating too early negatively affect connection pooling and degrade the driver performance.
Run(work Work) error
// Commit commits the transaction
// Contexts terminating too early negatively affect connection pooling and degrade the driver performance.
Commit(ctx context.Context) error
// Rollback rolls back the transaction
// Contexts terminating too early negatively affect connection pooling and degrade the driver performance.
Rollback(ctx context.Context) error
// Close rolls back the actual transaction if it's not already committed/rolled back
// and closes all resources associated with this transaction
// Contexts terminating too early negatively affect connection pooling and degrade the driver performance.
Close(ctx context.Context, joinedErrors ...error) error
}
readSession interface {
// Session returns the underlying Neo4J session.
Session() neo4j.SessionWithContext
// Close closes any open resources and marks this session as unusable.
// Contexts terminating too early negatively affect connection pooling and degrade the driver performance.
Close(ctx context.Context, joinedErrors ...error) error
// ReadTransaction executes the given unit of work in a AccessModeRead transaction with retry logic in place.
// Contexts terminating too early negatively affect connection pooling and degrade the driver performance.
ReadTransaction(ctx context.Context, work Work, configurers ...func(*neo4j.TransactionConfig)) error
BeginTransaction(ctx context.Context, configurers ...func(*neo4j.TransactionConfig)) (Transaction, error)
}
writeSession interface {
readSession
// ExecuteWrite executes the given unit of work in a AccessModeWrite transaction with retry logic in place.
// Contexts terminating too early negatively affect connection pooling and degrade the driver performance.
WriteTransaction(ctx context.Context, work Work, configurers ...func(*neo4j.TransactionConfig)) error
}
)
type (
driver struct {
registry
db neo4j.DriverWithContext
causalConsistencyKey func(ctx context.Context) string
sessionSemaphore *semaphore.Weighted
}
session struct {
*driver
registry
db neo4j.DriverWithContext
execConfig execConfig
session neo4j.SessionWithContext
currentTx neo4j.ManagedTransaction
releaseOnce sync.Once
done chan struct{}
}
transactionImpl struct {
session *session
tx neo4j.ExplicitTransaction
}
)
func (d *driver) DB() neo4j.DriverWithContext { return d.db }
func (d *driver) Exec(configurers ...func(*execConfig)) Query {
sessionConfig := neo4j.SessionConfig{}
txConfig := neo4j.TransactionConfig{}
config := execConfig{
SessionConfig: &sessionConfig,
TransactionConfig: &txConfig,
}
for _, c := range configurers {
c(&config)
}
if reflect.ValueOf(sessionConfig).IsZero() {
config.SessionConfig = nil
}
if reflect.ValueOf(txConfig).IsZero() {
config.TransactionConfig = nil
}
session := &session{
driver: d,
registry: d.registry,
db: d.db,
execConfig: config,
}
return session.newClient(internal.NewCypherClient())
}
func (d *driver) ensureCausalConsistency(ctx context.Context, sc *neo4j.SessionConfig) {
if d == nil || d.causalConsistencyKey == nil {
return
}
var key string
if key = d.causalConsistencyKey(ctx); key == "" {
return
}
bookmarks := causalConsistencyCache[key]
if bookmarks == nil {
return
}
sc.Bookmarks = bookmarks
}
func (d *driver) ReadSession(ctx context.Context, configurers ...func(*neo4j.SessionConfig)) readSession {
config := neo4j.SessionConfig{}
for _, c := range configurers {
c(&config)
}
config.AccessMode = neo4j.AccessModeRead
d.ensureCausalConsistency(ctx, &config)
if err := d.sessionSemaphore.Acquire(ctx, 1); err != nil {
panic(fmt.Errorf("failed to acquire session semaphore: %w", err))
}
sess := d.db.NewSession(ctx, config)
s := &session{
driver: d,
registry: d.registry,
db: d.db,
session: sess,
done: make(chan struct{}),
}
go func() {
select {
case <-ctx.Done():
s.releaseOnce.Do(s.releaseSemaphore)
case <-s.done:
}
}()
return s
}
func (d *driver) WriteSession(ctx context.Context, configurers ...func(*neo4j.SessionConfig)) writeSession {
config := neo4j.SessionConfig{}
for _, c := range configurers {
c(&config)
}
config.AccessMode = neo4j.AccessModeWrite
d.ensureCausalConsistency(ctx, &config)
if err := d.sessionSemaphore.Acquire(ctx, 1); err != nil {
panic(fmt.Errorf("failed to acquire session semaphore: %w", err))
}
sess := d.db.NewSession(ctx, config)
s := &session{
driver: d,
registry: d.registry,
db: d.db,
session: sess,
done: make(chan struct{}),
}
go func() {
select {
case <-ctx.Done():
s.releaseOnce.Do(s.releaseSemaphore)
case <-s.done:
}
}()
return s
}
func (s *session) Session() neo4j.SessionWithContext {
return s.session
}
func (s *session) releaseSemaphore() {
s.driver.sessionSemaphore.Release(1)
}
func (s *session) Close(ctx context.Context, errs ...error) error {
if s.done != nil {
close(s.done)
}
sessErr := s.session.Close(ctx)
s.releaseOnce.Do(s.releaseSemaphore)
if sessErr != nil {
errs = append(errs, sessErr)
return errors.Join(errs...)
}
if len(errs) == 0 {
return nil
}
return errors.Join(errs...)
}
func (s *session) ReadTransaction(ctx context.Context, work Work, configurers ...func(*neo4j.TransactionConfig)) error {
_, err := s.session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
return nil, work(func() Query {
c := s.newClient(internal.NewCypherClient())
c.currentTx = tx
return c
})
}, configurers...)
return err
}
func (s *session) WriteTransaction(ctx context.Context, work Work, configurers ...func(*neo4j.TransactionConfig)) error {
_, err := s.session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
return nil, work(func() Query {
c := s.newClient(internal.NewCypherClient())
c.currentTx = tx
return c
})
}, configurers...)
return err
}
func (s *session) BeginTransaction(ctx context.Context, configurers ...func(*neo4j.TransactionConfig)) (Transaction, error) {
tx, err := s.session.BeginTransaction(ctx, configurers...)
if err != nil {
return nil, err
}
return &transactionImpl{s, tx}, nil
}
func (t *transactionImpl) Run(work Work) error {
return work(func() Query {
c := t.session.newClient(internal.NewCypherClient())
c.currentTx = t.tx
return c
})
}
func (t *transactionImpl) Commit(ctx context.Context) error {
return t.tx.Commit(ctx)
}
func (t *transactionImpl) Rollback(ctx context.Context) error {
return t.tx.Rollback(ctx)
}
func (t *transactionImpl) Close(ctx context.Context, errs ...error) error {
sessErr := t.tx.Close(ctx)
if sessErr != nil {
errs = append(errs, sessErr)
return errors.Join(errs...)
}
if len(errs) == 0 {
return nil
}
return errors.Join(errs...)
}