(metadataBytes, newLogEntryBytes []byte, maxTotalBytes int)
| 238 | } |
| 239 | |
| 240 | func 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 | |
| 269 | type arrayElementBounds struct { |
| 270 | Start int |
searching dependent graphs…