forked from matthewmcneely/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedded_client.go
More file actions
343 lines (304 loc) · 9.39 KB
/
embedded_client.go
File metadata and controls
343 lines (304 loc) · 9.39 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/dgraph-io/dgo/v250/protos/api"
"github.com/dgraph-io/dgraph/v25/x"
"google.golang.org/grpc"
)
// embeddedDgraphClient implements api.DgraphClient by routing calls to the embedded Engine.
// This allows dgman and dgo to work seamlessly with the embedded Dgraph.
type embeddedDgraphClient struct {
engine *Engine
ns *Namespace
}
// newEmbeddedDgraphClient creates a new embedded client for the given namespace.
func newEmbeddedDgraphClient(engine *Engine, ns *Namespace) *embeddedDgraphClient {
return &embeddedDgraphClient{
engine: engine,
ns: ns,
}
}
func (c *embeddedDgraphClient) Login(
ctx context.Context,
in *api.LoginRequest,
opts ...grpc.CallOption,
) (*api.Response, error) {
// For embedded mode, login is a no-op (no auth required)
return &api.Response{}, nil
}
func (c *embeddedDgraphClient) Query(
ctx context.Context,
in *api.Request,
opts ...grpc.CallOption,
) (*api.Response, error) {
// Attach namespace context
ctx = x.AttachNamespace(ctx, c.ns.ID())
// For requests with both query and mutations (upsert case)
if len(in.Mutations) > 0 && in.Query != "" {
return c.handleUpsert(ctx, in)
}
// Simple mutation (no query)
if len(in.Mutations) > 0 {
uids, err := c.ns.Mutate(ctx, in.Mutations)
if err != nil {
return nil, err
}
// Convert uids map to response format
// dgman expects keys without the "_:" prefix (it strips prefix when looking up)
uidStrings := make(map[string]string)
for k, v := range uids {
key := k
if strings.HasPrefix(k, "_:") {
key = k[2:] // strip "_:" prefix
}
uidStrings[key] = fmt.Sprintf("0x%x", v)
}
return &api.Response{
Uids: uidStrings,
Txn: &api.TxnContext{StartTs: in.StartTs},
}, nil
}
// Query only
return c.engine.query(ctx, c.ns, in.Query, in.Vars)
}
// handleUpsert handles upsert requests (query + mutations) for embedded mode.
// It executes the query first to resolve variable UIDs, then substitutes
// uid(var) references in mutations before applying them.
func (c *embeddedDgraphClient) handleUpsert(ctx context.Context, in *api.Request) (*api.Response, error) {
// Step 1: Transform the upsert query to remove variable definitions
// dgman sends queries like: q_1_0(...) { u_1_0 as uid }
// We need to convert to: q_1_0(...) { uid } and map results back
transformedQuery, varMappings := transformUpsertQuery(in.Query)
// Step 2: Execute the transformed query
queryResp, err := c.engine.query(ctx, c.ns, transformedQuery, in.Vars)
if err != nil {
return nil, fmt.Errorf("upsert query failed: %w", err)
}
// Step 3: Parse query results and map to variable names
varUIDs, err := extractVarUIDsWithMapping(queryResp.Json, varMappings)
if err != nil {
return nil, fmt.Errorf("failed to extract var UIDs: %w", err)
}
// Step 4: Substitute uid(var) references in mutations
for _, mu := range in.Mutations {
substituteUIDVars(mu, varUIDs)
}
// Step 5: Apply mutations using embedded path
uids, err := c.ns.Mutate(ctx, in.Mutations)
if err != nil {
return nil, err
}
// Convert uids map to response format
uidStrings := make(map[string]string)
for k, v := range uids {
key := k
if strings.HasPrefix(k, "_:") {
key = k[2:]
}
uidStrings[key] = fmt.Sprintf("0x%x", v)
}
return &api.Response{
Json: queryResp.Json,
Uids: uidStrings,
Txn: &api.TxnContext{StartTs: in.StartTs},
}, nil
}
func (c *embeddedDgraphClient) Alter(
ctx context.Context,
in *api.Operation,
opts ...grpc.CallOption,
) (*api.Payload, error) {
if in.DropAll {
if err := c.engine.DropAll(ctx); err != nil {
return nil, err
}
return &api.Payload{}, nil
}
if in.DropOp == api.Operation_DATA {
if err := c.engine.dropData(ctx, c.ns); err != nil {
return nil, err
}
return &api.Payload{}, nil
}
if in.Schema != "" {
if err := c.engine.alterSchema(ctx, c.ns, in.Schema); err != nil {
return nil, err
}
}
return &api.Payload{}, nil
}
func (c *embeddedDgraphClient) CommitOrAbort(
ctx context.Context,
in *api.TxnContext,
opts ...grpc.CallOption,
) (*api.TxnContext, error) {
return c.engine.commitOrAbort(ctx, c.ns, in)
}
func (c *embeddedDgraphClient) CheckVersion(
ctx context.Context,
in *api.Check,
opts ...grpc.CallOption,
) (*api.Version, error) {
return &api.Version{Tag: "embedded"}, nil
}
func (c *embeddedDgraphClient) RunDQL(
ctx context.Context,
in *api.RunDQLRequest,
opts ...grpc.CallOption,
) (*api.Response, error) {
return c.engine.query(ctx, c.ns, in.DqlQuery, in.Vars)
}
func (c *embeddedDgraphClient) AllocateIDs(
ctx context.Context,
in *api.AllocateIDsRequest,
opts ...grpc.CallOption,
) (*api.AllocateIDsResponse, error) {
// Not used in embedded mode - UIDs are allocated during mutation
return &api.AllocateIDsResponse{}, nil
}
func (c *embeddedDgraphClient) UpdateExtSnapshotStreamingState(
ctx context.Context,
in *api.UpdateExtSnapshotStreamingStateRequest,
opts ...grpc.CallOption,
) (*api.UpdateExtSnapshotStreamingStateResponse, error) {
// Not supported in embedded mode
return &api.UpdateExtSnapshotStreamingStateResponse{}, nil
}
func (c *embeddedDgraphClient) StreamExtSnapshot(
ctx context.Context,
opts ...grpc.CallOption,
) (api.Dgraph_StreamExtSnapshotClient, error) {
// Not supported in embedded mode
return nil, nil
}
func (c *embeddedDgraphClient) CreateNamespace(
ctx context.Context,
in *api.CreateNamespaceRequest,
opts ...grpc.CallOption,
) (*api.CreateNamespaceResponse, error) {
ns, err := c.engine.CreateNamespace()
if err != nil {
return nil, err
}
return &api.CreateNamespaceResponse{Namespace: ns.ID()}, nil
}
func (c *embeddedDgraphClient) DropNamespace(
ctx context.Context,
in *api.DropNamespaceRequest,
opts ...grpc.CallOption,
) (*api.DropNamespaceResponse, error) {
// Not implemented yet
return &api.DropNamespaceResponse{}, nil
}
func (c *embeddedDgraphClient) ListNamespaces(
ctx context.Context,
in *api.ListNamespacesRequest,
opts ...grpc.CallOption,
) (*api.ListNamespacesResponse, error) {
// Not implemented yet
return &api.ListNamespacesResponse{}, nil
}
// varAsRegex matches "varname as uid" patterns in query blocks
var varAsRegex = regexp.MustCompile(`(\w+)\s+as\s+uid`)
// transformUpsertQuery transforms a dgman upsert query to remove variable definitions.
// Input: { q_1_0(...) { u_1_0 as uid } }
// Output: { q_1_0(...) { uid } } and mapping {"q_1_0": "u_1_0"}
func transformUpsertQuery(query string) (string, map[string]string) {
varMappings := make(map[string]string)
// Find all "varname as uid" patterns and extract the variable names
// dgman uses pattern: q_N_M for query blocks, u_N_M for uid variables
matches := varAsRegex.FindAllStringSubmatch(query, -1)
for _, match := range matches {
if len(match) >= 2 {
varName := match[1] // e.g., "u_1_0"
// Convert u_N_M to q_N_M for block name mapping
if strings.HasPrefix(varName, "u_") {
blockName := "q_" + varName[2:]
varMappings[blockName] = varName
}
}
}
// Replace "varname as uid" with just "uid"
transformedQuery := varAsRegex.ReplaceAllString(query, "uid")
return transformedQuery, varMappings
}
// extractVarUIDsWithMapping parses query results and maps block names to variable names
func extractVarUIDsWithMapping(jsonData []byte, varMappings map[string]string) (map[string]string, error) {
if len(jsonData) == 0 {
return make(map[string]string), nil
}
var result map[string][]map[string]interface{}
if err := json.Unmarshal(jsonData, &result); err != nil {
return nil, err
}
varUIDs := make(map[string]string)
for blockName, nodes := range result {
if len(nodes) > 0 {
if uid, ok := nodes[0]["uid"].(string); ok {
// Map block name to variable name
if varName, ok := varMappings[blockName]; ok {
varUIDs[varName] = uid
} else {
varUIDs[blockName] = uid
}
}
}
}
return varUIDs, nil
}
// uidVarRegex matches uid(varname) patterns in mutation data
var uidVarRegex = regexp.MustCompile(`uid\(([^)]+)\)`)
// substituteUIDVars replaces uid(var) references in mutations with actual UIDs
func substituteUIDVars(mu *api.Mutation, varUIDs map[string]string) {
// Handle SetJson
if len(mu.SetJson) > 0 {
mu.SetJson = []byte(substituteInString(string(mu.SetJson), varUIDs))
}
// Handle DeleteJson
if len(mu.DeleteJson) > 0 {
mu.DeleteJson = []byte(substituteInString(string(mu.DeleteJson), varUIDs))
}
// Handle Set NQuads
for _, nq := range mu.Set {
substituteInNQuad(nq, varUIDs)
}
// Handle Del NQuads
for _, nq := range mu.Del {
substituteInNQuad(nq, varUIDs)
}
}
func substituteInString(s string, varUIDs map[string]string) string {
return uidVarRegex.ReplaceAllStringFunc(s, func(match string) string {
// Extract variable name from uid(varname)
varName := match[4 : len(match)-1] // strip "uid(" and ")"
if uid, ok := varUIDs[varName]; ok {
return uid
}
// If no UID found, convert to blank node for new entity
return "_:uid(" + varName + ")"
})
}
func substituteInNQuad(nq *api.NQuad, varUIDs map[string]string) {
// Substitute in Subject
if strings.HasPrefix(nq.Subject, "uid(") {
varName := nq.Subject[4 : len(nq.Subject)-1]
if uid, ok := varUIDs[varName]; ok {
nq.Subject = uid
}
}
// Substitute in ObjectId
if strings.HasPrefix(nq.ObjectId, "uid(") {
varName := nq.ObjectId[4 : len(nq.ObjectId)-1]
if uid, ok := varUIDs[varName]; ok {
nq.ObjectId = uid
}
}
}