Repository navigation
Expand file tree
/
Copy pathns.go
More file actions
122 lines (103 loc) · 3.23 KB
/
Copy pathns.go
File metadata and controls
122 lines (103 loc) · 3.23 KB
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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package dgo
import (
"context"
"github.com/dgraph-io/dgo/v250/protos/api"
)
type txnOptions struct {
readOnly bool
bestEffort bool
respFormat api.Request_RespFormat
}
// TxnOption is a function that modifies the txn options.
type TxnOption func(*txnOptions) error
// WithReadOnly sets the txn to be read-only.
func WithReadOnly() TxnOption {
return func(o *txnOptions) error {
o.readOnly = true
return nil
}
}
// WithBestEffort sets the txn to be best effort.
func WithBestEffort() TxnOption {
return func(o *txnOptions) error {
o.readOnly = true
o.bestEffort = true
return nil
}
}
func buildTxnOptions(opts ...TxnOption) (*txnOptions, error) {
topts := &txnOptions{}
for _, opt := range opts {
if err := opt(topts); err != nil {
return nil, err
}
}
if topts.bestEffort {
topts.readOnly = true
}
return topts, nil
}
// RunDQL runs a DQL query in the given namespace. A DQL query could be a mutation
// or a query or an upsert which is a combination of mutations and queries.
func (d *Dgraph) RunDQL(ctx context.Context, q string, opts ...TxnOption) (
*api.Response, error) {
return d.RunDQLWithVars(ctx, q, nil, opts...)
}
// RunDQLWithVars is like RunDQL with variables.
func (d *Dgraph) RunDQLWithVars(ctx context.Context, q string,
vars map[string]string, opts ...TxnOption) (*api.Response, error) {
topts, err := buildTxnOptions(opts...)
if err != nil {
return nil, err
}
req := &api.RunDQLRequest{DqlQuery: q, Vars: vars,
ReadOnly: topts.readOnly, BestEffort: topts.bestEffort, RespFormat: topts.respFormat}
return doWithRetryLogin(ctx, d, func(dc api.DgraphClient) (*api.Response, error) {
return dc.RunDQL(d.getContext(ctx), req)
})
}
// CreateNamespace creates a new namespace with the given name and password for groot user.
func (d *Dgraph) CreateNamespace(ctx context.Context) (uint64, error) {
req := &api.CreateNamespaceRequest{}
resp, err := doWithRetryLogin(ctx, d, func(dc api.DgraphClient) (*api.CreateNamespaceResponse, error) {
return dc.CreateNamespace(d.getContext(ctx), req)
})
if err != nil {
return 0, err
}
return resp.Namespace, nil
}
// DropNamespace deletes the namespace with the given name.
func (d *Dgraph) DropNamespace(ctx context.Context, nsID uint64) error {
req := &api.DropNamespaceRequest{Namespace: nsID}
_, err := doWithRetryLogin(ctx, d, func(dc api.DgraphClient) (*api.DropNamespaceResponse, error) {
return dc.DropNamespace(d.getContext(ctx), req)
})
return err
}
// ListNamespaces returns a map of namespace names to their details.
func (d *Dgraph) ListNamespaces(ctx context.Context) (map[uint64]*api.Namespace, error) {
resp, err := doWithRetryLogin(ctx, d, func(dc api.DgraphClient) (*api.ListNamespacesResponse, error) {
return dc.ListNamespaces(d.getContext(ctx), &api.ListNamespacesRequest{})
})
if err != nil {
return nil, err
}
return resp.Namespaces, nil
}
func doWithRetryLogin[T any](ctx context.Context, d *Dgraph,
f func(dc api.DgraphClient) (*T, error)) (*T, error) {
dc := d.anyClient()
resp, err := f(dc)
if isJwtExpired(err) {
if err := d.retryLogin(ctx); err != nil {
return nil, err
}
return f(dc)
}
return resp, err
}