This repository was archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdgraph.go
137 lines (120 loc) · 2.62 KB
/
dgraph.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
package main
import (
"context"
"encoding/json"
"math/rand"
"sync/atomic"
"time"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
type dgraph struct {
c *dgo.Dgraph
}
func newDgraph(hosts []string) (*dgraph, error) {
var cli []api.DgraphClient
for _, host := range hosts {
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
return nil, err
}
cli = append(cli, api.NewDgraphClient(conn))
}
return &dgraph{c: dgo.NewDgraphClient(cli...)}, nil
}
func (d *dgraph) setup(ctx context.Context) error {
return d.c.Alter(ctx, &api.Operation{
Schema: `
docKey: string @index(hash) @upsert .
body: string .
type Doc {
docKey: string
body: string
}
`,
})
}
var randSeed = time.Now().UnixNano()
func (d *dgraph) run(ctx context.Context, size int) (*timingSet, error) {
var (
r = rand.New(rand.NewSource(atomic.AddInt64(&randSeed, 1)))
t = &timingSet{}
tx = d.c.NewTxn()
docKey = randomString(r, 8)
docBody = randomString(r, size)
)
defer tx.Discard(ctx)
uid, dur, err := d.lookupDoc(ctx, tx, docKey)
t.Add("lookupDoc", dur)
if err != nil {
return nil, err
}
uid, dur, err = d.updateDoc(ctx, tx, uid, docKey, docBody)
t.Add("updateDoc", dur)
if err != nil {
return nil, err
}
commitDone := t.Start("commit")
err = tx.Commit(ctx)
commitDone()
if err != nil {
return nil, err
}
return t, nil
}
func (d *dgraph) lookupDoc(ctx context.Context, tx *dgo.Txn, key string) (string, time.Duration, error) {
r, err := tx.QueryWithVars(ctx, `
query Doc($key: string) {
q(func: eq(docKey, $key)) {
uid
body
}
}
`, map[string]string{"$key": key})
if err != nil {
return "", 0, err
}
var result struct {
Q []struct {
UID string
}
}
if err = json.Unmarshal(r.GetJson(), &result); err != nil {
return "", 0, err
}
if len(result.Q) > 0 {
return result.Q[0].UID, d.latency(r), nil
}
return "", d.latency(r), nil
}
func (d *dgraph) updateDoc(ctx context.Context, tx *dgo.Txn, uid, key, body string) (string, time.Duration, error) {
data := map[string]interface{}{
"dgraph.type": "Doc",
"uid": uid,
"docKey": key,
"body": body,
}
if uid == "" {
data["uid"] = "_:new"
}
var (
mu = &api.Mutation{}
err error
)
mu.SetJson, err = json.Marshal(data)
if err != nil {
return "", 0, err
}
r, err := tx.Mutate(ctx, mu)
if err != nil {
return "", 0, err
}
if uid == "" {
uid = r.GetUids()["new"]
}
return uid, d.latency(r), err
}
func (d *dgraph) latency(r *api.Response) time.Duration {
return time.Duration(r.GetLatency().GetProcessingNs())
}