This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
591 lines (475 loc) · 14.6 KB
/
session.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Copyright (c) 2022 MindStand Technologies, Inc
//
// 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 gogm
import (
"errors"
"fmt"
"reflect"
dsl "github.com/mindstand/go-cypherdsl"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
const defaultDepth = 1
const AccessModeRead = neo4j.AccessModeRead
const AccessModeWrite = neo4j.AccessModeWrite
type SessionConfig neo4j.SessionConfig
// Deprecated: Session will be removed in a later release in favor of SessionV2
type Session struct {
gogm *Gogm
neoSess neo4j.Session
tx neo4j.Transaction
DefaultDepth int
mode neo4j.AccessMode
}
// uses global gogm
// Deprecated: Gogm.NewSession instead
func NewSession(readonly bool) (*Session, error) {
return newSession(globalGogm, readonly)
}
func newSession(gogm *Gogm, readonly bool) (*Session, error) {
if gogm == nil {
return nil, errors.New("gogm instance cannot be nil")
}
if gogm.isNoOp {
return nil, errors.New("please set global gogm instance with SetGlobalGogm()")
}
if gogm.driver == nil {
return nil, errors.New("gogm driver not initialized")
}
session := &Session{
gogm: gogm,
}
var mode neo4j.AccessMode
if readonly {
mode = AccessModeRead
} else {
mode = AccessModeWrite
}
neoSess := gogm.driver.NewSession(neo4j.SessionConfig{AccessMode: mode, FetchSize: neo4j.FetchDefault})
session.neoSess = neoSess
session.mode = mode
session.DefaultDepth = defaultDepth
return session, nil
}
// Deprecated: Gogm.NewSessionWithConfig instead
func NewSessionWithConfig(conf SessionConfig) (*Session, error) {
return newSessionWithConfig(globalGogm, conf)
}
func newSessionWithConfig(gogm *Gogm, conf SessionConfig) (*Session, error) {
if gogm == nil {
return nil, errors.New("gogm instance is nil")
}
if gogm.isNoOp {
return nil, errors.New("please set global gogm instance with SetGlobalGogm()")
}
if gogm.driver == nil {
return nil, errors.New("gogm driver not initialized")
}
neoSess := gogm.driver.NewSession(neo4j.SessionConfig{
AccessMode: conf.AccessMode,
Bookmarks: conf.Bookmarks,
DatabaseName: conf.DatabaseName,
FetchSize: neo4j.FetchDefault,
})
return &Session{
neoSess: neoSess,
DefaultDepth: defaultDepth,
mode: conf.AccessMode,
gogm: gogm,
}, nil
}
func (s *Session) Begin() error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
if s.tx != nil {
return fmt.Errorf("transaction already started: %w", ErrTransaction)
}
var err error
s.tx, err = s.neoSess.BeginTransaction()
if err != nil {
return err
}
return nil
}
func (s *Session) Rollback() error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
if s.tx == nil {
return fmt.Errorf("cannot rollback nil transaction: %w", ErrTransaction)
}
err := s.tx.Rollback()
if err != nil {
return err
}
s.tx = nil
return nil
}
func (s *Session) RollbackWithError(originalError error) error {
err := s.Rollback()
if err != nil {
return fmt.Errorf("original error: `%s`, rollback error: `%s`", originalError.Error(), err.Error())
}
return originalError
}
func (s *Session) Commit() error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
if s.tx == nil {
return fmt.Errorf("cannot commit nil transaction: %w", ErrTransaction)
}
err := s.tx.Commit()
if err != nil {
return err
}
s.tx = nil
return nil
}
func (s *Session) Load(respObj interface{}, id string) error {
return s.LoadDepthFilterPagination(respObj, id, s.DefaultDepth, nil, nil, nil)
}
func (s *Session) LoadDepth(respObj interface{}, id string, depth int) error {
return s.LoadDepthFilterPagination(respObj, id, depth, nil, nil, nil)
}
func (s *Session) LoadDepthFilter(respObj interface{}, id string, depth int, filter dsl.ConditionOperator, params map[string]interface{}) error {
return s.LoadDepthFilterPagination(respObj, id, depth, filter, params, nil)
}
func (s *Session) LoadDepthFilterPagination(respObj interface{}, id string, depth int, filter dsl.ConditionOperator, params map[string]interface{}, pagination *Pagination) error {
respType := reflect.TypeOf(respObj)
//validate type is ptr
if respType.Kind() != reflect.Ptr {
return errors.New("respObj must be type ptr")
}
//"deref" reflect interface type
respType = respType.Elem()
//get the type name -- this maps directly to the label
respObjName := respType.Name()
//will need to keep track of these variables
varName := "n"
var query dsl.Cypher
var err error
//make the query based off of the load strategy
switch s.gogm.config.LoadStrategy {
case PATH_LOAD_STRATEGY:
query, err = PathLoadStrategyOne(varName, respObjName, "uuid", "uuid", false, depth, filter)
if err != nil {
return err
}
case SCHEMA_LOAD_STRATEGY:
query, err = SchemaLoadStrategyOne(s.gogm, varName, respObjName, "uuid", "uuid", false, depth, filter)
if err != nil {
return err
}
default:
return errors.New("unknown load strategy")
}
//if the query requires pagination, set that up
if pagination != nil {
if err = pagination.Paginate(query); err != nil {
return err
}
}
if params == nil {
params = map[string]interface{}{
"uuid": id,
}
} else {
params["uuid"] = id
}
// handle if in transaction
cyp, err := query.ToCypher()
if err != nil {
return err
}
return s.runReadOnly(cyp, params, respObj)
}
func (s *Session) LoadAll(respObj interface{}) error {
return s.LoadAllDepthFilterPagination(respObj, s.DefaultDepth, nil, nil, nil)
}
func (s *Session) LoadAllDepth(respObj interface{}, depth int) error {
return s.LoadAllDepthFilterPagination(respObj, depth, nil, nil, nil)
}
func (s *Session) LoadAllDepthFilter(respObj interface{}, depth int, filter dsl.ConditionOperator, params map[string]interface{}) error {
return s.LoadAllDepthFilterPagination(respObj, depth, filter, params, nil)
}
func (s *Session) LoadAllDepthFilterPagination(respObj interface{}, depth int, filter dsl.ConditionOperator, params map[string]interface{}, pagination *Pagination) error {
rawRespType := reflect.TypeOf(respObj)
if rawRespType.Kind() != reflect.Ptr {
return fmt.Errorf("respObj must be a pointer to a slice, instead it is %T", respObj)
}
//deref to a slice
respType := rawRespType.Elem()
//validate type is ptr
if respType.Kind() != reflect.Slice {
return fmt.Errorf("respObj must be type slice, instead it is %T", respObj)
}
//"deref" reflect interface type
respType = respType.Elem()
if respType.Kind() == reflect.Ptr {
//slice of pointers
respType = respType.Elem()
}
//get the type name -- this maps directly to the label
respObjName := respType.Name()
//will need to keep track of these variables
varName := "n"
var query dsl.Cypher
var err error
//make the query based off of the load strategy
switch s.gogm.config.LoadStrategy {
case PATH_LOAD_STRATEGY:
query, err = PathLoadStrategyMany(varName, respObjName, depth, filter)
if err != nil {
return err
}
case SCHEMA_LOAD_STRATEGY:
query, err = SchemaLoadStrategyMany(s.gogm, varName, respObjName, depth, filter)
if err != nil {
return err
}
default:
return errors.New("unknown load strategy")
}
//if the query requires pagination, set that up
if pagination != nil {
if err = pagination.Paginate(query); err != nil {
return err
}
}
// handle if in transaction
cyp, err := query.ToCypher()
if err != nil {
return err
}
return s.runReadOnly(cyp, params, respObj)
}
func (s *Session) LoadAllEdgeConstraint(respObj interface{}, endNodeType, endNodeField string, edgeConstraint interface{}, minJumps, maxJumps, depth int, filter dsl.ConditionOperator) error {
rawRespType := reflect.TypeOf(respObj)
if rawRespType.Kind() != reflect.Ptr {
return fmt.Errorf("respObj must be a pointer to a slice, instead it is %T", respObj)
}
//deref to a slice
respType := rawRespType.Elem()
//validate type is ptr
if respType.Kind() != reflect.Slice {
return fmt.Errorf("respObj must be type slice, instead it is %T", respObj)
}
//"deref" reflect interface type
respType = respType.Elem()
if respType.Kind() == reflect.Ptr {
//slice of pointers
respType = respType.Elem()
}
//get the type name -- this maps directly to the label
respObjName := respType.Name()
//will need to keep track of these variables
varName := "n"
var query dsl.Cypher
var err error
// there is no Schema Load Strategy implementation of EdgeConstraint as it would involve pathfinding within the schema (which would be expensive)
query, err = PathLoadStrategyEdgeConstraint(varName, respObjName, endNodeType, endNodeField, minJumps, maxJumps, depth, filter)
if err != nil {
return err
}
// handle if in transaction
cyp, err := query.ToCypher()
if err != nil {
return err
}
return s.runReadOnly(cyp, map[string]interface{}{
endNodeField: edgeConstraint,
}, respObj)
}
func (s *Session) runReadOnly(cyp string, params map[string]interface{}, respObj interface{}) error {
// if in tx, run normally else run in managed tx
if s.tx != nil {
result, err := s.tx.Run(cyp, params)
if err != nil {
return err
}
return decode(s.gogm, result, respObj)
}
// run inside managed transaction if not already in a transaction
_, err := s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
res, err := tx.Run(cyp, params)
if err != nil {
return nil, err
}
return nil, decode(s.gogm, res, respObj)
})
if err != nil {
return fmt.Errorf("failed auto read tx, %w", err)
}
return nil
}
func (s *Session) Save(saveObj interface{}) error {
return s.SaveDepth(saveObj, s.DefaultDepth)
}
func (s *Session) SaveDepth(saveObj interface{}, depth int) error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
// handle if in transaction
return s.runWrite(saveDepth(s.gogm, saveObj, depth))
}
func (s *Session) Delete(deleteObj interface{}) error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
if deleteObj == nil {
return errors.New("deleteObj can not be nil")
}
// handle if in transaction
workFunc, err := deleteNode(deleteObj)
if err != nil {
return fmt.Errorf("failed to generate work func for delete, %w", err)
}
return s.runWrite(workFunc)
}
func (s *Session) DeleteUUID(uuid string) error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
// handle if in transaction
return s.runWrite(deleteByUuids(uuid))
}
func (s *Session) runWrite(work neo4j.TransactionWork) error {
// if already in a transaction
if s.tx != nil {
_, err := work(s.tx)
if err != nil {
return fmt.Errorf("failed to save in manual tx, %w", err)
}
return nil
}
_, err := s.neoSess.WriteTransaction(work)
if err != nil {
return fmt.Errorf("failed to save in auto transaction, %w", err)
}
return nil
}
func (s *Session) Query(query string, properties map[string]interface{}, respObj interface{}) error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
if s.mode == neo4j.AccessModeRead {
return s.runReadOnly(query, properties, respObj)
}
return s.runWrite(func(tx neo4j.Transaction) (interface{}, error) {
res, err := tx.Run(query, properties)
if err != nil {
return nil, err
}
return nil, decode(s.gogm, res, respObj)
})
}
func (s *Session) QueryRaw(query string, properties map[string]interface{}) ([][]interface{}, error) {
if s.neoSess == nil {
return nil, errors.New("neo4j connection not initialized")
}
var err error
if s.tx != nil {
res, err := s.tx.Run(query, properties)
if err != nil {
return nil, fmt.Errorf("failed to execute query, %w", err)
}
return s.parseResult(res), nil
} else {
var ires interface{}
if s.mode == AccessModeRead {
ires, err = s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
res, err := tx.Run(query, properties)
if err != nil {
return nil, err
}
return s.parseResult(res), nil
})
} else {
ires, err = s.neoSess.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
res, err := tx.Run(query, properties)
if err != nil {
return nil, err
}
return s.parseResult(res), nil
})
}
if err != nil {
return nil, fmt.Errorf("failed to run auto transaction, %w", err)
}
result, ok := ires.([][]interface{})
if !ok {
return nil, fmt.Errorf("failed to cast %T to [][]interface{}", ires)
}
return result, nil
}
}
func (s *Session) parseResult(res neo4j.Result) [][]interface{} {
var result [][]interface{}
// we have to wrap everything because the driver only exposes interfaces which are not serializable
for res.Next() {
valLen := len(res.Record().Values)
valCap := cap(res.Record().Values)
if valLen != 0 {
vals := make([]interface{}, valLen, valCap)
for i, val := range res.Record().Values {
switch v := val.(type) {
case neo4j.Path:
vals[i] = v
case neo4j.Relationship:
vals[i] = v
case neo4j.Node:
vals[i] = v
default:
vals[i] = v
}
}
result = append(result, vals)
}
}
return result
}
func (s *Session) PurgeDatabase() error {
if s.neoSess == nil {
return errors.New("neo4j connection not initialized")
}
// handle if in transaction
cyp, err := dsl.QB().Match(dsl.Path().V(dsl.V{Name: "n"}).Build()).Delete(true, "n").ToCypher()
if err != nil {
return err
}
return s.runWrite(func(tx neo4j.Transaction) (interface{}, error) {
return tx.Run(cyp, nil)
})
}
func (s *Session) Close() error {
if s.neoSess == nil {
return fmt.Errorf("cannot close nil connection: %w", ErrInternal)
}
// handle tx
if s.tx != nil {
s.gogm.logger.Warn("attempting to close a session with a pending transaction. Tx is being rolled back")
err := s.tx.Rollback()
if err != nil {
return err
}
s.tx = nil
}
return s.neoSess.Close()
}