-
Notifications
You must be signed in to change notification settings - Fork 12
/
common_read_repository.go
136 lines (119 loc) · 4.96 KB
/
common_read_repository.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
package repository
import (
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"golang.org/x/net/context"
)
type CommonReadRepository interface {
ExistsById(ctx context.Context, tenant, id, label string) (bool, error)
ExistsByIdLinkedTo(ctx context.Context, tenant, id, label, linkedToId, linkedToLabel, linkRelationship string) (bool, error)
ExistsByIdLinkedFrom(ctx context.Context, tenant, id, label, linkedFromId, linkedFromLabel, linkRelationship string) (bool, error)
}
type commonReadRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewCommonReadRepository(driver *neo4j.DriverWithContext, database string) CommonReadRepository {
return &commonReadRepository{
driver: driver,
database: database,
}
}
func (r *commonReadRepository) prepareReadSession(ctx context.Context) neo4j.SessionWithContext {
return utils.NewNeo4jReadSession(ctx, *r.driver, utils.WithDatabaseName(r.database))
}
func (r *commonReadRepository) ExistsById(ctx context.Context, tenant, id, label string) (bool, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "CommonReadRepository.ExistsById")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("id", id), log.String("label", label))
cypher := fmt.Sprintf(`MATCH (n:%s {id:$id}) WHERE n:%s_%s RETURN n.id LIMIT 1`, label, label, tenant)
params := map[string]any{
"id": id,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
if queryResult, err := tx.Run(ctx, cypher, params); err != nil {
return false, err
} else {
return queryResult.Next(ctx), nil
}
})
if err != nil {
tracing.TraceErr(span, err)
return false, err
}
span.LogFields(log.Bool("result.exists", result.(bool)))
return result.(bool), err
}
func (r *commonReadRepository) ExistsByIdLinkedTo(ctx context.Context, tenant, id, label, linkedToId, linkedToLabel, linkRelationship string) (bool, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "CommonReadRepository.ExistsById")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("id", id), log.String("label", label), log.String("linkedToId", linkedToId), log.String("linkedToLabel", linkedToLabel), log.String("linkRelationship", linkRelationship))
cypher := fmt.Sprintf(`MATCH (n:%s {id:$id})-`, label)
if linkRelationship != "" {
cypher += fmt.Sprintf(`[:%s]`, linkRelationship)
}
cypher += fmt.Sprintf(`->(m:%s {id:$linkedToId}) WHERE n:%s_%s RETURN n.id LIMIT 1`, linkedToLabel, label, tenant)
params := map[string]any{
"id": id,
"linkedToId": linkedToId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
if queryResult, err := tx.Run(ctx, cypher, params); err != nil {
return false, err
} else {
return queryResult.Next(ctx), nil
}
})
if err != nil {
tracing.TraceErr(span, err)
return false, err
}
span.LogFields(log.Bool("result.exists", result.(bool)))
return result.(bool), err
}
func (r *commonReadRepository) ExistsByIdLinkedFrom(ctx context.Context, tenant, id, label, linkedFromId, linkedFromLabel, linkRelationship string) (bool, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "CommonReadRepository.ExistsById")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("id", id), log.String("label", label), log.String("linkedFromId", linkedFromId), log.String("linkedFromLabel", linkedFromLabel), log.String("linkRelationship", linkRelationship))
cypher := fmt.Sprintf(`MATCH (n:%s {id:$id})<-`, label)
if linkRelationship != "" {
cypher += fmt.Sprintf(`[:%s]`, linkRelationship)
}
cypher += fmt.Sprintf(`-(m:%s {id:$linkedFromId}) WHERE n:%s_%s RETURN n.id LIMIT 1`, linkedFromLabel, label, tenant)
params := map[string]any{
"id": id,
"linkedFromId": linkedFromId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
if queryResult, err := tx.Run(ctx, cypher, params); err != nil {
return false, err
} else {
return queryResult.Next(ctx), nil
}
})
if err != nil {
tracing.TraceErr(span, err)
return false, err
}
span.LogFields(log.Bool("result.exists", result.(bool)))
return result.(bool), err
}