MCPcopy
hub / github.com/dgraph-io/dgraph / updateUIDInMutations

Function updateUIDInMutations

edgraph/server.go:1096–1169  ·  view source on GitHub ↗

updateUIDInMutations does following transformations: - uid(v) -> 0x123 -- If v is defined in query block - uid(v) -> _:uid(v) -- Otherwise

(gmu *dql.Mutation, qc *queryContext)

Source from the content-addressed store, hash-verified

1094// - uid(v) -> 0x123 -- If v is defined in query block
1095// - uid(v) -> _:uid(v) -- Otherwise
1096func updateUIDInMutations(gmu *dql.Mutation, qc *queryContext) error {
1097 // usedMutationVars keeps track of variables that are used in mutations.
1098 getNewVals := func(s string) []string {
1099 if strings.HasPrefix(s, "uid(") {
1100 varName := s[4 : len(s)-1]
1101 if uids, ok := qc.uidRes[varName]; ok && len(uids) != 0 {
1102 return uids
1103 }
1104
1105 return []string{"_:" + s}
1106 }
1107
1108 return []string{s}
1109 }
1110
1111 getNewNQuad := func(nq *api.NQuad, s, o string) *api.NQuad {
1112 // The following copy is fine because we only modify Subject and ObjectId.
1113 // The pointer values are not modified across different copies of NQuad.
1114 n := *nq
1115
1116 n.Subject = s
1117 n.ObjectId = o
1118 return &n
1119 }
1120
1121 // Remove the mutations from gmu.Del when no UID was found.
1122 gmuDel := make([]*api.NQuad, 0, len(gmu.Del))
1123 for _, nq := range gmu.Del {
1124 // if Subject or/and Object are variables, each NQuad can result
1125 // in multiple NQuads if any variable stores more than one UIDs.
1126 newSubs := getNewVals(nq.Subject)
1127 newObs := getNewVals(nq.ObjectId)
1128
1129 for _, s := range newSubs {
1130 for _, o := range newObs {
1131 // Blank node has no meaning in case of deletion.
1132 if strings.HasPrefix(s, "_:uid(") ||
1133 strings.HasPrefix(o, "_:uid(") {
1134 continue
1135 }
1136
1137 gmuDel = append(gmuDel, getNewNQuad(nq, s, o))
1138 qc.nquadsCount++
1139 }
1140 if qc.nquadsCount > x.Config.LimitMutationsNquad {
1141 return errors.Errorf("NQuad count in the request: %d, is more that threshold: %d",
1142 qc.nquadsCount, x.Config.LimitMutationsNquad)
1143 }
1144 }
1145 }
1146
1147 gmu.Del = gmuDel
1148
1149 // Update the values in mutation block from the query block.
1150 gmuSet := make([]*api.NQuad, 0, len(gmu.Set))
1151 for _, nq := range gmu.Set {
1152 newSubs := getNewVals(nq.Subject)
1153 newObs := getNewVals(nq.ObjectId)

Callers 1

updateMutationsFunction · 0.85

Calls 1

ErrorfMethod · 0.45

Tested by

no test coverage detected