-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_mutation_helpers.go
133 lines (115 loc) · 3.07 KB
/
api_mutation_helpers.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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package modusdb
import (
"context"
"fmt"
"reflect"
"github.com/hypermodeinc/dgraph/v24/dql"
"github.com/hypermodeinc/dgraph/v24/protos/pb"
"github.com/hypermodeinc/dgraph/v24/query"
"github.com/hypermodeinc/dgraph/v24/schema"
"github.com/hypermodeinc/dgraph/v24/worker"
"github.com/hypermodeinc/modusdb/api/apiutils"
"github.com/hypermodeinc/modusdb/api/structreflect"
)
func processStructValue(ctx context.Context, value any, ns *Namespace) (any, error) {
if reflect.TypeOf(value).Kind() == reflect.Struct {
value = reflect.ValueOf(value).Interface()
newGid, err := getUidOrMutate(ctx, ns.engine, ns, value)
if err != nil {
return nil, err
}
return newGid, nil
}
return value, nil
}
func processPointerValue(ctx context.Context, value any, ns *Namespace) (any, error) {
reflectValueType := reflect.TypeOf(value)
if reflectValueType.Kind() == reflect.Pointer {
reflectValueType = reflectValueType.Elem()
if reflectValueType.Kind() == reflect.Struct {
value = reflect.ValueOf(value).Elem().Interface()
return processStructValue(ctx, value, ns)
}
}
return value, nil
}
func getUidOrMutate[T any](ctx context.Context, engine *Engine, ns *Namespace, object T) (uint64, error) {
gid, cfKeyValue, err := structreflect.GetUniqueConstraint[T](object)
if err != nil {
return 0, err
}
var cf *ConstrainedField
if cfKeyValue != nil {
cf = &ConstrainedField{Key: cfKeyValue.Key(), Value: cfKeyValue.Value()}
}
dms := make([]*dql.Mutation, 0)
sch := &schema.ParsedSchema{}
err = generateSetDqlMutationsAndSchema(ctx, ns, object, gid, &dms, sch)
if err != nil {
return 0, err
}
err = engine.alterSchemaWithParsed(ctx, sch)
if err != nil {
return 0, err
}
if gid != 0 || cf != nil {
gid, err = getExistingObject(ctx, ns, gid, cf, object)
if err != nil && err != apiutils.ErrNoObjFound {
return 0, err
}
if err == nil {
return gid, nil
}
}
gid, err = engine.z.nextUID()
if err != nil {
return 0, err
}
dms = make([]*dql.Mutation, 0)
err = generateSetDqlMutationsAndSchema(ctx, ns, object, gid, &dms, sch)
if err != nil {
return 0, err
}
err = applyDqlMutations(ctx, engine, dms)
if err != nil {
return 0, err
}
return gid, nil
}
func applyDqlMutations(ctx context.Context, engine *Engine, dms []*dql.Mutation) error {
edges, err := query.ToDirectedEdges(dms, nil)
if err != nil {
return err
}
if !engine.isOpen.Load() {
return ErrClosedEngine
}
startTs, err := engine.z.nextTs()
if err != nil {
return err
}
commitTs, err := engine.z.nextTs()
if err != nil {
return err
}
m := &pb.Mutations{
GroupId: 1,
StartTs: startTs,
Edges: edges,
}
m.Edges, err = query.ExpandEdges(ctx, m)
if err != nil {
return fmt.Errorf("error expanding edges: %w", err)
}
p := &pb.Proposal{Mutations: m, StartTs: startTs}
if err := worker.ApplyMutations(ctx, p); err != nil {
return err
}
return worker.ApplyCommited(ctx, &pb.OracleDelta{
Txns: []*pb.TxnStatus{{StartTs: startTs, CommitTs: commitTs}},
})
}