-
Notifications
You must be signed in to change notification settings - Fork 10
/
dgraph.go
118 lines (101 loc) · 2.6 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
func (c *connector) dgraphDropAll() {
log.Println("Drop all data")
op := api.Operation{DropAll: true}
if err := c.dgraphClient.Alter(*c.context, &op); err != nil {
log.Fatal(err)
}
}
func (c *connector) dgraphDropPrevious() {
log.Println("Drop previous data")
for {
txn := c.dgraphClient.NewTxn()
q := `query query($owner: string, $region: string){
list(func: eq(OwnerId, $owner), first: 250) @filter(eq(Region, $region)) {
uid
}
}`
res, err := txn.QueryWithVars(*c.context, q, map[string]string{"$owner": c.awsAccountID, "$region": c.awsRegion})
if err != nil {
log.Println(err.Error())
}
m := make(map[string]cidrNodes) // Cidr are simplest ressources, we use them for an easy Marshal
json.Unmarshal(res.Json, &m)
if len(m["list"]) != 0 {
n, _ := json.Marshal(m["list"])
mu := &api.Mutation{
// CommitNow: true,
DeleteJson: n,
}
_, err = txn.Mutate(*c.context, mu)
if err != nil {
log.Fatal(err)
}
txn.Commit(*c.context)
} else {
break
}
}
}
func (c *connector) dgraphAddSchema() {
log.Println("Add schema")
op := &api.Operation{Schema: getdgraphSchema()}
err := c.dgraphClient.Alter(*c.context, op)
if err != nil {
log.Fatal(err)
}
}
func dgraphDisplaySchema(dgraph, dtype *string) {
connexion, err := grpc.Dial(*dgraph, grpc.WithInsecure())
if err != nil {
log.Fatal(err.Error())
}
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(connexion))
ctx := context.Background()
resp, err := dgraphClient.NewReadOnlyTxn().Query(ctx, `schema(type: [`+*dtype+`]) {type}`)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp.Json))
}
func (c *connector) dgraphAddNodes(nodes interface{}) {
txn := c.dgraphClient.NewTxn()
defer txn.Discard(*c.context)
n, err := json.Marshal(nodes)
if err != nil {
log.Fatal(err)
}
mu := &api.Mutation{
CommitNow: true,
SetJson: n,
}
_, err = txn.Mutate(*c.context, mu)
if err != nil {
log.Fatal(err)
}
}
func (c *connector) dgraphQuery(nodeType string) []byte {
txn := c.dgraphClient.NewReadOnlyTxn()
defer txn.Discard(*c.context)
q := `query query($type: string){
list(func: type($type)) {
uid
dgraph.type
expand(_all_)
}
}`
res, err := txn.QueryWithVars(*c.context, q, map[string]string{"$type": nodeType})
if err != nil {
log.Println(err.Error())
}
return res.Json
}