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

Function appendLogDataWithCap

riverlog/river_log.go:240–267  ·  view source on GitHub ↗
(metadataBytes, newLogEntryBytes []byte, maxTotalBytes int)

Source from the content-addressed store, hash-verified

238}
239
240func appendLogDataWithCap(metadataBytes, newLogEntryBytes []byte, maxTotalBytes int) ([]byte, int, error) {
241 existingLogData := gjson.GetBytes(metadataBytes, metadataKey)
242 var existingLogArrayBytes []byte
243 switch {
244 case !existingLogData.Exists():
245 existingLogArrayBytes = []byte("[]")
246 case existingLogData.IsArray():
247 // Slice raw JSON straight from metadata bytes to avoid an extra copy.
248 existingLogArrayBytes = metadataBytes[existingLogData.Index : existingLogData.Index+len(existingLogData.Raw)]
249 default:
250 return nil, 0, fmt.Errorf("%q value is not an array", metadataKey)
251 }
252
253 existingElementBounds, err := getArrayElementBounds(existingLogArrayBytes)
254 if err != nil {
255 return nil, 0, err
256 }
257
258 // Determine the smallest suffix to keep that still fits with the new entry.
259 // This keeps pruning oldest-first while avoiding repeated full rewrites.
260 keepStart := getKeepStart(existingElementBounds, len(newLogEntryBytes), maxTotalBytes)
261
262 // Build the final array once from the kept suffix plus the new entry.
263 appendedLogDataBytes := buildAppendedArray(existingLogArrayBytes, existingElementBounds, keepStart, newLogEntryBytes)
264 numDroppedEntries := min(keepStart, len(existingElementBounds))
265
266 return appendedLogDataBytes, numDroppedEntries, nil
267}
268
269type arrayElementBounds struct {
270 Start int

Callers 2

TestAppendLogDataWithCapFunction · 0.85
WorkMethod · 0.85

Calls 4

getArrayElementBoundsFunction · 0.85
getKeepStartFunction · 0.85
buildAppendedArrayFunction · 0.85
ErrorfMethod · 0.65

Tested by 1

TestAppendLogDataWithCapFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…