Skip to content

Commit ece52a5

Browse files
committed
delete multiple oldest versions per pass
1 parent 4d0c52c commit ece52a5

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/lib/util.sh

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ ytoxt() {
456456
# ytox + trim: if the json or xml is over 50MB, remove oldest versions
457457
local f="$1"
458458
local tmp
459+
local del_n=1
460+
local last_json_size=-1
461+
local last_xml_size=-1
459462

460463
[ -f "$f" ] || return 1
461464

@@ -468,10 +471,31 @@ ytoxt() {
468471

469472
json_size=$(stat -c %s "$f" 2>/dev/null || echo -1)
470473

474+
# Stop if we can't shrink anymore.
475+
if [ "$json_size" -eq "$last_json_size" ] && [ "$last_json_size" -ge 0 ]; then
476+
break
477+
fi
478+
last_json_size="$json_size"
479+
471480
if [ "$json_size" -lt 50000000 ]; then
472481
# Only generate/check XML if JSON is already under limit.
473482
xml_size=$(ytox "$f" 2>/dev/null || echo -1)
474-
[ "$xml_size" -ge 50000000 ] || break
483+
if [ "$xml_size" -lt 50000000 ]; then
484+
break
485+
fi
486+
487+
# If XML is still too large, keep trimming, but avoid redoing work forever.
488+
if [ "$xml_size" -eq "$last_xml_size" ] && [ "$last_xml_size" -ge 0 ]; then
489+
break
490+
fi
491+
last_xml_size="$xml_size"
492+
else
493+
# JSON is still too large: increase trimming aggressiveness.
494+
if [ "$json_size" -ge 50000000 ]; then
495+
if [ "$del_n" -lt 65536 ]; then
496+
del_n=$((del_n * 2))
497+
fi
498+
fi
475499
fi
476500

477501
if jq -e '
@@ -486,15 +510,14 @@ ytoxt() {
486510
if type == "number" then .
487511
elif type == "string" then tonumber? // 0
488512
else 0 end;
489-
def trim_versions:
513+
def trim_versions($n):
490514
if ((.version // []) | type == "array") and ((.version // []) | length > 0) then
491-
((.version
492-
| to_entries
493-
| min_by(.value.id | id_to_num)
494-
| .key) // empty) as $idx
495-
| if ($idx | tostring) == "" then .
496-
else .version |= del(.[ $idx ])
497-
end
515+
(
516+
.version
517+
| sort_by(.id | id_to_num)
518+
| .[$n:]
519+
) as $v
520+
| .version = $v
498521
else
499522
.
500523
end;
@@ -503,15 +526,15 @@ ytoxt() {
503526
| (max_by((.value.version // []) | length) // empty) as $max
504527
| map(
505528
if .key == $max.key and (((.value.version // []) | type == "array") and ((.value.version // []) | length > 0))
506-
then (.value |= trim_versions)
529+
then (.value |= trim_versions($n))
507530
else .
508531
end
509532
)
510533
| map(.value))
511534
else
512-
trim_versions
535+
trim_versions($n)
513536
end
514-
' "$f" >"$tmp"
537+
' --argjson n "$del_n" "$f" >"$tmp"
515538
else
516539
jq -c '
517540
if type == "array" then
@@ -539,6 +562,10 @@ ytoxt() {
539562

540563
# Ensure the XML output corresponds to the final JSON.
541564
ytox "$f" >/dev/null 2>&1
565+
566+
# If either JSON or XML is > 100MB, delete each one that is too large.
567+
[ "$(stat -c %s "$f" 2>/dev/null || echo -1)" -lt 100000000 ] || rm -f "$f"
568+
[ "$(stat -c %s "${f%.*}.xml" 2>/dev/null || echo -1)" -lt 100000000 ] || rm -f "${f%.*}.xml"
542569
}
543570

544571
ytoy() {

0 commit comments

Comments
 (0)