updateValInNQuads picks the val() from object and replaces it with its value Assumption is that Subject can contain UID, whereas Object can contain Val If val(variable) exists in a query, but the values are not there for the variable, it will ignore the mutation silently.
(nquads []*api.NQuad, qc *queryContext, isSet bool)
| 997 | // If val(variable) exists in a query, but the values are not there for the variable, |
| 998 | // it will ignore the mutation silently. |
| 999 | func updateValInNQuads(nquads []*api.NQuad, qc *queryContext, isSet bool) []*api.NQuad { |
| 1000 | getNewVals := func(s string) (*types.ShardedMap, bool) { |
| 1001 | if strings.HasPrefix(s, "val(") { |
| 1002 | varName := s[4 : len(s)-1] |
| 1003 | if v, ok := qc.valRes[varName]; ok && v != nil { |
| 1004 | return v, true |
| 1005 | } |
| 1006 | return nil, true |
| 1007 | } |
| 1008 | return nil, false |
| 1009 | } |
| 1010 | |
| 1011 | getValue := func(key uint64, uidToVal *types.ShardedMap) (types.Val, bool) { |
| 1012 | val, ok := uidToVal.Get(key) |
| 1013 | if ok { |
| 1014 | return val, true |
| 1015 | } |
| 1016 | |
| 1017 | // Check if the variable is aggregate variable |
| 1018 | // Only 0 key would exist for aggregate variable |
| 1019 | val, ok = uidToVal.Get(0) |
| 1020 | return val, ok |
| 1021 | } |
| 1022 | |
| 1023 | newNQuads := nquads[:0] |
| 1024 | for _, nq := range nquads { |
| 1025 | // Check if the nquad contains a val() in Object or not. |
| 1026 | // If not then, keep the mutation and continue |
| 1027 | uidToVal, found := getNewVals(nq.ObjectId) |
| 1028 | if !found { |
| 1029 | newNQuads = append(newNQuads, nq) |
| 1030 | continue |
| 1031 | } |
| 1032 | |
| 1033 | // uid(u) <amount> val(amt) |
| 1034 | // For each NQuad, we need to convert the val(variable_name) |
| 1035 | // to *api.Value before applying the mutation. For that, first |
| 1036 | // we convert key to uint64 and get the UID to Value map from |
| 1037 | // the result of the query. |
| 1038 | var key uint64 |
| 1039 | var err error |
| 1040 | switch { |
| 1041 | case nq.Subject[0] == '_' && isSet: |
| 1042 | // in case aggregate val(var) is there, that should work with blank node. |
| 1043 | key = 0 |
| 1044 | case nq.Subject[0] == '_' && !isSet: |
| 1045 | // UID is of format "_:uid(u)". Ignore the delete silently |
| 1046 | continue |
| 1047 | default: |
| 1048 | key, err = strconv.ParseUint(nq.Subject, 0, 64) |
| 1049 | if err != nil { |
| 1050 | // Key conversion failed, ignoring the nquad. Ideally, |
| 1051 | // it shouldn't happen as this is the result of a query. |
| 1052 | glog.Errorf("Conversion of subject %s failed. Error: %s", |
| 1053 | nq.Subject, err.Error()) |
| 1054 | continue |
| 1055 | } |
| 1056 | } |
no test coverage detected