MCPcopy Index your code
hub / github.com/riverqueue/river / buildUniqueKeyString

Function buildUniqueKeyString

internal/dbunique/db_unique.go:58–126  ·  view source on GitHub ↗

Builds a unique key made up of the unique options in place. The key is hashed to become a value for `unique_key`.

(timeGen rivertype.TimeGenerator, uniqueOpts *UniqueOpts, params *rivertype.JobInsertParams)

Source from the content-addressed store, hash-verified

56// Builds a unique key made up of the unique options in place. The key is hashed
57// to become a value for `unique_key`.
58func buildUniqueKeyString(timeGen rivertype.TimeGenerator, uniqueOpts *UniqueOpts, params *rivertype.JobInsertParams) (string, error) {
59 var sb strings.Builder
60
61 if !uniqueOpts.ExcludeKind {
62 sb.WriteString("&kind=" + params.Kind)
63 }
64
65 if uniqueOpts.ByArgs {
66 var encodedArgsForUnique []byte
67 // Get unique JSON keys from the JobArgs struct:
68 uniqueFields, err := structtag.SortedFieldsWithTag(params.Args, "unique")
69 if err != nil {
70 return "", err
71 }
72
73 if len(uniqueFields) > 0 {
74 // Extract unique values from the EncodedArgs JSON
75 uniqueValues := structtag.ExtractValues(params.EncodedArgs, uniqueFields)
76
77 // Assemble the JSON object using bytes.Buffer
78 // Better to overallocate a bit than to allocate multiple times, so just
79 // assume we'll cap out at the length of the full encoded args.
80 sortedJSONWithOnlyUniqueValues := make([]byte, 0, len(params.EncodedArgs))
81
82 sjsonOpts := &sjson.Options{ReplaceInPlace: true}
83 for i, key := range uniqueFields {
84 if uniqueValues[i] == "undefined" {
85 continue
86 }
87 sortedJSONWithOnlyUniqueValues, err = sjson.SetRawBytesOptions(sortedJSONWithOnlyUniqueValues, key, []byte(uniqueValues[i]), sjsonOpts)
88 if err != nil {
89 // Should not happen unless key was invalid
90 return "", err
91 }
92 }
93 encodedArgsForUnique = sortedJSONWithOnlyUniqueValues
94 } else {
95 // Use all keys from EncodedArgs sorted alphabetically
96 keys := sliceutil.Map(gjson.GetBytes(params.EncodedArgs, "@keys").Array(), func(v gjson.Result) string { return v.String() })
97 slices.Sort(keys)
98
99 sortedJSON := make([]byte, 0, len(params.EncodedArgs))
100 sortedJSON = append(sortedJSON, "{}"...)
101 sjsonOpts := &sjson.Options{ReplaceInPlace: true}
102 for _, key := range keys {
103 sortedJSON, err = sjson.SetRawBytesOptions(sortedJSON, key, []byte(gjson.GetBytes(params.EncodedArgs, key).Raw), sjsonOpts)
104 if err != nil {
105 // Should not happen unless key was invalid
106 return "", err
107 }
108 }
109 encodedArgsForUnique = sortedJSON
110 }
111
112 sb.WriteString("&args=")
113 sb.Write(encodedArgsForUnique)
114 }
115

Callers 2

UniqueKeyFunction · 0.85
TestUniqueKeyFunction · 0.85

Calls 5

SortedFieldsWithTagFunction · 0.92
ExtractValuesFunction · 0.92
MapFunction · 0.92
ValOrDefaultFuncFunction · 0.92
WriteMethod · 0.80

Tested by 1

TestUniqueKeyFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…