-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_mutation_helpers.go
128 lines (111 loc) · 2.88 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
package modusdb
import (
"context"
"fmt"
"reflect"
"github.com/dgraph-io/dgraph/v24/dql"
"github.com/dgraph-io/dgraph/v24/protos/pb"
"github.com/dgraph-io/dgraph/v24/query"
"github.com/dgraph-io/dgraph/v24/schema"
"github.com/dgraph-io/dgraph/v24/worker"
"github.com/hypermodeinc/modusdb/api/apiutils"
"github.com/hypermodeinc/modusdb/api/structreflect"
)
func processStructValue(ctx context.Context, value any, n *Namespace) (any, error) {
if reflect.TypeOf(value).Kind() == reflect.Struct {
value = reflect.ValueOf(value).Interface()
newGid, err := getUidOrMutate(ctx, n.db, n, value)
if err != nil {
return nil, err
}
return newGid, nil
}
return value, nil
}
func processPointerValue(ctx context.Context, value any, n *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, n)
}
}
return value, nil
}
func getUidOrMutate[T any](ctx context.Context, db *DB, n *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, n, object, gid, &dms, sch)
if err != nil {
return 0, err
}
err = n.alterSchemaWithParsed(ctx, sch)
if err != nil {
return 0, err
}
if gid != 0 || cf != nil {
gid, err = getExistingObject(ctx, n, gid, cf, object)
if err != nil && err != apiutils.ErrNoObjFound {
return 0, err
}
if err == nil {
return gid, nil
}
}
gid, err = db.z.nextUID()
if err != nil {
return 0, err
}
dms = make([]*dql.Mutation, 0)
err = generateSetDqlMutationsAndSchema(ctx, n, object, gid, &dms, sch)
if err != nil {
return 0, err
}
err = applyDqlMutations(ctx, db, dms)
if err != nil {
return 0, err
}
return gid, nil
}
func applyDqlMutations(ctx context.Context, db *DB, dms []*dql.Mutation) error {
edges, err := query.ToDirectedEdges(dms, nil)
if err != nil {
return err
}
if !db.isOpen {
return ErrClosedDB
}
startTs, err := db.z.nextTs()
if err != nil {
return err
}
commitTs, err := db.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}},
})
}