-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgraph_txn.go
176 lines (141 loc) · 3.81 KB
/
dgraph_txn.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
package agora
import (
"encoding/json"
"log"
"time"
"github.com/brianbroderick/logit"
"github.com/dgraph-io/dgo/v210/protos/api"
)
// MutateDgraph is a helper func to run Mutate operations on Dgraph
func MutateDgraph(jsonStr []byte) *api.Response {
c := NewDgraphTxn()
defer c.DiscardTxn()
mu := &api.Mutation{
CommitNow: true,
SetJson: jsonStr,
}
assigned, err := c.Txn.Mutate(c.Ctx, mu)
if err != nil {
logit.Fatal(" %e", err)
}
return assigned
}
// DeleteDgraph is a helper func to run Mutate operations on Dgraph
func DeleteDgraph(jsonStr []byte) *api.Response {
c := NewDgraphTxn()
defer c.DiscardTxn()
mu := &api.Mutation{
CommitNow: true,
DeleteJson: jsonStr,
}
assigned, err := c.Txn.Mutate(c.Ctx, mu)
if err != nil {
logit.Fatal(" %e", err)
}
return assigned
}
// QueryDgraph runs a query operation on DGraph and returns the JSON response
func QueryDgraph(query string) []byte {
c := NewDgraphTxn()
defer c.DiscardTxn()
resp, err := c.Txn.Query(c.Ctx, query)
if err != nil {
return RetryQueryDgraph(query, 1)
}
return resp.Json
}
// RetryQueryDgraph retries the query. This is so dgraph has time to boot up in Docker.
func RetryQueryDgraph(query string, attempt int) []byte {
logit.Warn("Retrying Query")
time.Sleep(500 * time.Millisecond)
c := NewDgraphTxn()
defer c.DiscardTxn()
resp, err := c.Txn.Query(c.Ctx, query)
if err != nil {
if attempt > 20 {
return RetryQueryDgraph(query, attempt+1)
} else {
logit.Warn("Query Error: %e.", err)
logit.Warn("Retried too many times. Not trying again.")
return []byte{}
}
}
return resp.Json
}
// QueryDgraphWithVars runs a query operation on DGraph with variables and returns the JSON response
func QueryDgraphWithVars(query string, variables map[string]string) []byte {
c := NewDgraphTxn()
defer c.DiscardTxn()
resp, err := c.Txn.QueryWithVars(c.Ctx, query, variables)
if err != nil {
return RetryQueryDgraphWithVars(query, variables, 1)
}
return resp.Json
}
// RetryQueryDgraphWithVars retries a query on DGraph with variables and returns the JSON response
func RetryQueryDgraphWithVars(query string, variables map[string]string, attempt int) []byte {
logit.Warn("Retrying Query")
time.Sleep(500 * time.Millisecond)
c := NewDgraphTxn()
defer c.DiscardTxn()
resp, err := c.Txn.QueryWithVars(c.Ctx, query, variables)
if err != nil {
if attempt > 20 {
return RetryQueryDgraphWithVars(query, variables, attempt+1)
} else {
logit.Warn("Query Error: %e.", err)
logit.Warn("Retried too many times. Not trying again.")
return []byte{}
}
}
return resp.Json
}
func ResolveQuery(r interface{}, query string) error {
j := QueryDgraph(query)
err := json.Unmarshal(j, &r)
if err != nil {
return err
}
return nil
}
func ResolveQueryWithVars(r interface{}, query string, variables map[string]string) error {
j := QueryDgraphWithVars(query, variables)
err := json.Unmarshal(j, &r)
if err != nil {
return err
}
return nil
}
// AlterDgraph runs an alter Dgraph operation
func AlterDgraph(op *api.Operation) {
c := NewDgraphTxn()
defer c.DiscardTxn()
err := c.Dg.Alter(c.Ctx, op)
if err != nil {
log.Fatal(err)
}
}
// This isn't working with vars. Need to fix
// UpsertDgraph is a helper func to run Upsert operations on Dgraph
// func UpsertDgraph(query string, variables map[string]string, cond string, jsonStr []byte) []byte {
// c := NewDgraphTxn()
// defer c.DiscardTxn()
// mu := &api.Mutation{
// SetJson: jsonStr,
// }
// // if cond != "" {
// // mu.Cond = cond
// // }
// req := &api.Request{
// Query: query,
// Vars: variables,
// Mutations: []*api.Mutation{mu},
// CommitNow: true,
// }
// // // Update email only if matching uid found.
// resp, err := c.Txn.Do(c.Ctx, req)
// if err != nil {
// logit.Fatal(" Query Error: %e", err)
// }
// return resp.Json
// }