-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathns_v25.go
148 lines (123 loc) · 3.99 KB
/
ns_v25.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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package dgo
import (
"context"
"errors"
"math/rand"
apiv25 "github.com/dgraph-io/dgo/v250/protos/api.v25"
)
const (
RootNamespace = "root"
)
var (
ErrUnsupportedAPI = errors.New("API is not supported by the version of dgraph cluster")
)
type txnOptions struct {
readOnly bool
bestEffort bool
respFormat apiv25.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, nsName string, q string, opts ...TxnOption) (
*apiv25.RunDQLResponse, error) {
return d.RunDQLWithVars(ctx, nsName, q, nil, opts...)
}
// RunDQLWithVars is like RunDQL with variables.
func (d *Dgraph) RunDQLWithVars(ctx context.Context, nsName string, q string,
vars map[string]string, opts ...TxnOption) (*apiv25.RunDQLResponse, error) {
topts, err := buildTxnOptions(opts...)
if err != nil {
return nil, err
}
req := &apiv25.RunDQLRequest{NsName: nsName, DqlQuery: q, Vars: vars,
ReadOnly: topts.readOnly, BestEffort: topts.bestEffort}
return doWithRetryLogin(ctx, d, func(dc apiv25.DgraphClient) (*apiv25.RunDQLResponse, 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, name string) error {
req := &apiv25.CreateNamespaceRequest{NsName: name}
_, err := doWithRetryLogin(ctx, d, func(dc apiv25.DgraphClient) (*apiv25.CreateNamespaceResponse, error) {
return dc.CreateNamespace(d.getContext(ctx), req)
})
return err
}
// DropNamespace deletes the namespace with the given name.
func (d *Dgraph) DropNamespace(ctx context.Context, name string) error {
req := &apiv25.DropNamespaceRequest{NsName: name}
_, err := doWithRetryLogin(ctx, d, func(dc apiv25.DgraphClient) (*apiv25.DropNamespaceResponse, error) {
return dc.DropNamespace(d.getContext(ctx), req)
})
return err
}
// RenameNamespace renames the namespace from the given name to the new name.
func (d *Dgraph) RenameNamespace(ctx context.Context, from string, to string) error {
req := &apiv25.UpdateNamespaceRequest{NsName: from, RenameToNs: to}
_, err := doWithRetryLogin(ctx, d, func(dc apiv25.DgraphClient) (*apiv25.UpdateNamespaceResponse, error) {
return dc.UpdateNamespace(d.getContext(ctx), req)
})
return err
}
// ListNamespaces returns a map of namespace names to their details.
func (d *Dgraph) ListNamespaces(ctx context.Context) (map[string]*apiv25.Namespace, error) {
resp, err := doWithRetryLogin(ctx, d, func(dc apiv25.DgraphClient) (*apiv25.ListNamespacesResponse, error) {
return dc.ListNamespaces(d.getContext(ctx), &apiv25.ListNamespacesRequest{})
})
if err != nil {
return nil, err
}
return resp.NsList, nil
}
func (d *Dgraph) anyClientv25() apiv25.DgraphClient {
//nolint:gosec
return d.dcv25[rand.Intn(len(d.dcv25))]
}
func doWithRetryLogin[T any](ctx context.Context, d *Dgraph,
f func(dc apiv25.DgraphClient) (*T, error)) (*T, error) {
if d.useV1 {
return nil, ErrUnsupportedAPI
}
dc := d.anyClientv25()
resp, err := f(dc)
if isJwtExpired(err) {
if err := d.retryLogin(ctx); err != nil {
return nil, err
}
return f(dc)
}
return resp, err
}