-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutate.go
63 lines (49 loc) · 2.2 KB
/
mutate.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
package quirk
import (
"context"
"sync"
"github.com/dgraph-io/dgo/v2"
)
// mutate is used to upsert a single node using Quirk. All single mutate
// functions call this one to do the upsert.
func (c *Client) mutate(ctx context.Context, dg *dgo.Dgraph,
d *DupleNode, uidMap map[string]UID, m sync.Locker) (bool, error) {
res := c.tryUpsert(ctx, dg.NewTxn(), d)
if res.err != nil {
return res.new, res.err
}
m.Lock()
uidMap[res.identifier] = UID{uid: res.uid, isNew: res.new}
m.Unlock()
return res.new, nil
}
// mutateSingleStruct uses reflect to create a DupleNode struct out of the given
// structure. Once the DupleNode is made then it is passed to mutate to be upserted.
func (c *Client) mutateSingleStruct(ctx context.Context, dg *dgo.Dgraph,
d interface{}, uidMap map[string]UID, m *sync.Mutex) (bool, error) {
// Use reflect to package the predicate and values in slices.
predVals := c.reflectMaps(d)
return c.mutate(ctx, dg, predVals, uidMap, m)
}
// mutateSingleDupleNode passed the DupleNode given to mutate to be upserted.
func (c *Client) mutateSingleDupleNode(ctx context.Context, dg *dgo.Dgraph,
node interface{}, uidMap map[string]UID, m *sync.Mutex) (bool, error) {
return c.mutate(ctx, dg, node.(*DupleNode), uidMap, m)
}
// mutateStringMap loops through the given map to create a DupleNode struct.
// Once the DupleNode is made then it is passed to mutate to be upserted.
func (c *Client) mutateStringMap(ctx context.Context, dg *dgo.Dgraph,
d map[string]string, uidMap map[string]UID, m sync.Locker) (bool, error) {
// Convert out map[string]string to usable predicate and value data.
predVals := c.mapToPredValPairs(d)
return c.mutate(ctx, dg, predVals, uidMap, m)
}
// mutateDynamicMap loops through the given map to create a DupleNode struct.
// Once the DupleNode is made then it is passed to mutate to be upserted.
// Note: The looping process supports multiple Dgraph datatypes.
func (c *Client) mutateDynamicMap(ctx context.Context, dg *dgo.Dgraph,
d map[string]interface{}, uidMap map[string]UID, m sync.Locker) (bool, error) {
// Convert out map[string]interface{} to usable predicate and value data.
predVals := c.dynamicMapToPredValPairs(d)
return c.mutate(ctx, dg, predVals, uidMap, m)
}