@ref0/git 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/access/private-refs.mjs +5 -4
  2. package/dist/contracts/forks.d.mts +3 -2
  3. package/dist/contracts/git.d.mts +23 -5
  4. package/dist/contracts/queue.d.mts +5 -1
  5. package/dist/contracts/queue.mjs +1 -0
  6. package/dist/contracts/result.d.mts +7 -5
  7. package/dist/contracts/result.mjs +7 -5
  8. package/dist/contracts/sync.d.mts +11 -0
  9. package/dist/core/access.d.mts +1 -0
  10. package/dist/core/access.mjs +10 -11
  11. package/dist/core/asked.mjs +30 -0
  12. package/dist/core/events/deliver.d.mts +1 -0
  13. package/dist/core/events/deliver.mjs +5 -1
  14. package/dist/core/forks.mjs +39 -13
  15. package/dist/core/imports.mjs +9 -4
  16. package/dist/core/index-job.mjs +8 -6
  17. package/dist/core/objects/reading.mjs +17 -9
  18. package/dist/core/parts.d.mts +2 -1
  19. package/dist/core/push.mjs +11 -8
  20. package/dist/core/refs.mjs +6 -3
  21. package/dist/core/registered-rows.d.mts +1 -0
  22. package/dist/core/registered-rows.mjs +7 -5
  23. package/dist/core/repack.mjs +5 -3
  24. package/dist/core/saved-caller-views.d.mts +18 -0
  25. package/dist/core/saved-caller-views.mjs +17 -4
  26. package/dist/core/saved-callers-public.d.mts +3 -3
  27. package/dist/core/saved-callers-public.mjs +3 -2
  28. package/dist/core/saved-callers.d.mts +7 -0
  29. package/dist/extend.d.mts +2 -1
  30. package/dist/extend.mjs +2 -1
  31. package/dist/http/push-uploads.mjs +6 -3
  32. package/dist/lfs/locks.mjs +14 -9
  33. package/dist/lfs/objects.mjs +27 -20
  34. package/dist/queue/advance.mjs +8 -4
  35. package/dist/queue/calls.mjs +21 -1
  36. package/dist/rest/blobs.d.mts +15 -15
  37. package/dist/rest/commits.d.mts +80 -80
  38. package/dist/rest/files.d.mts +103 -103
  39. package/dist/rest/forks.d.mts +39 -39
  40. package/dist/rest/layer.mjs +8 -1
  41. package/dist/rest/queue.d.mts +170 -170
  42. package/dist/rest/refs.d.mts +128 -128
  43. package/dist/rest/tokens.d.mts +187 -187
  44. package/dist/rest/trees.d.mts +41 -41
  45. package/dist/server/git-namespace.d.mts +3 -3
  46. package/dist/server/server.mjs +6 -4
  47. package/dist/storage/memory-budget.mjs +128 -6
  48. package/dist/storage/merge-cut.mjs +5 -4
  49. package/dist/storage/pack-reads.mjs +239 -27
  50. package/dist/storage/prune-seen.mjs +11 -6
  51. package/dist/storage/prune-walk.mjs +168 -68
  52. package/dist/storage/quarantine.mjs +2 -0
  53. package/dist/store/repo-record.d.mts +25 -0
  54. package/dist/store/repo-record.mjs +55 -1
  55. package/dist/sync/github.mjs +10 -1
  56. package/dist/sync/import.d.mts +12 -0
  57. package/dist/sync/import.mjs +3 -2
  58. package/dist/sync/mirror.mjs +24 -17
  59. package/dist/sync/said.mjs +204 -0
  60. package/dist/sync/step.mjs +8 -8
  61. package/dist/sync/tries.mjs +34 -18
  62. package/dist/sync.d.mts +2 -1
  63. package/dist/sync.mjs +2 -1
  64. package/dist/topics/advance.mjs +10 -3
  65. package/package.json +3 -3
@@ -310,6 +310,14 @@ var MemoryBudget = class {
310
310
  #indexes = /* @__PURE__ */ new Set();
311
311
  /** The clock of the last operation that asked, so a release's wake reads the same time. */
312
312
  #now = Date.now;
313
+ /**
314
+ * Each admitted push's place in admission order, which upkeep's allotments, counted with pushes,
315
+ * never take: when two pushes' grows wait on each other's room, the newer yields (#2361).
316
+ */
317
+ #order = /* @__PURE__ */ new WeakMap();
318
+ #admissions = 0;
319
+ /** Push allotments whose grow yielded to an older push's, released in that step, by what each held (#2361). */
320
+ #yielders = /* @__PURE__ */ new WeakMap();
313
321
  constructor(total, reserve) {
314
322
  this.total = total;
315
323
  this.reserve = reserve;
@@ -584,6 +592,7 @@ var MemoryBudget = class {
584
592
  } finally {
585
593
  this.#waking = false;
586
594
  }
595
+ this.#yieldFor(void 0, 0);
587
596
  }
588
597
  /**
589
598
  * Waits in `queue` as the waiter `enter` makes, `ms` at most; `None` past it. A wait interrupted,
@@ -610,7 +619,7 @@ var MemoryBudget = class {
610
619
  * by its timeout too, gives back an allotment granted it that it never took.
611
620
  */
612
621
  #wait(queue, push, bytes, ms, busy, own, asker, grower) {
613
- return this.#queued(queue, ms, (resume) => {
622
+ const waited = this.#queued(queue, ms, (resume) => {
614
623
  let granted;
615
624
  return {
616
625
  waiter: {
@@ -624,11 +633,13 @@ var MemoryBudget = class {
624
633
  granted = allotment;
625
634
  own(allotment);
626
635
  resume(allotment);
627
- }
636
+ },
637
+ refuse: () => resume(void 0)
628
638
  },
629
639
  left: () => granted?.release()
630
640
  };
631
641
  });
642
+ return Effect.map(waited, Option.flatMap(Option.fromUndefinedOr));
632
643
  }
633
644
  /**
634
645
  * An allotment released when the scope ends, on every exit. Its finalizer is added first, and
@@ -654,8 +665,12 @@ var MemoryBudget = class {
654
665
  admit(bytes) {
655
666
  return this.#scoped((own) => {
656
667
  if (bytes > this.total - this.reserve) return Effect.fail(pushTooLarge);
657
- if (this.#stuck(this.#now()) === void 0 && this.#admits(bytes)) return Effect.succeed(own(this.#grant(bytes, true)));
658
- return this.#wait(this.#pushes, true, bytes, pushWaitMs, void 0, own).pipe(Effect.flatMap(Option.match({
668
+ const admitted = (allotment) => {
669
+ this.#order.set(allotment, this.#admissions += 1);
670
+ return own(allotment);
671
+ };
672
+ if (this.#stuck(this.#now()) === void 0 && this.#admits(bytes)) return Effect.succeed(admitted(this.#grant(bytes, true)));
673
+ return this.#wait(this.#pushes, true, bytes, pushWaitMs, void 0, admitted).pipe(Effect.flatMap(Option.match({
659
674
  onNone: () => Effect.fail(pushBusy),
660
675
  onSome: Effect.succeed
661
676
  })));
@@ -724,6 +739,9 @@ var MemoryBudget = class {
724
739
  if (allotment.released || bytes <= 0) return Effect.void;
725
740
  if (allotment.bytes + bytes > this.mostOf(allotment.push)) return Effect.fail(tooLarge ?? busy);
726
741
  if (this.tryGrow(allotment, bytes)) return Effect.void;
742
+ const yielded = this.#yieldFor(allotment, bytes);
743
+ if (yielded === "refused") return Effect.andThen(this.#refusedGrow(allotment, bytes), Effect.fail(busy));
744
+ if (yielded === "granted" || yielded === "others" && this.tryGrow(allotment, bytes)) return Effect.void;
727
745
  const adopt = (granted) => {
728
746
  granted.adopted();
729
747
  allotment.added(granted.bytes);
@@ -736,11 +754,104 @@ var MemoryBudget = class {
736
754
  })));
737
755
  });
738
756
  }
757
+ /** Whether `allotment` is an admitted push's, and `other`'s admitted before it. */
758
+ #older(other, allotment) {
759
+ const mine = this.#order.get(allotment);
760
+ const theirs = this.#order.get(other);
761
+ return mine !== void 0 && theirs !== void 0 && theirs < mine;
762
+ }
763
+ /**
764
+ * Whether a push's grow of `bytes` fits once `newer`, pushes' allotments, give back all they
765
+ * hold and every other room shrinks to its floor: `newer` are then what the grow waits on. Never
766
+ * while a push is stuck at the head, which no release passes.
767
+ */
768
+ #fitsWithout(newer, bytes) {
769
+ if (this.#stuck(this.#now()) !== void 0) return false;
770
+ let admitted = 0;
771
+ let held = 0;
772
+ let shrinks = 0;
773
+ for (const each of newer) {
774
+ admitted += each.admitted;
775
+ held += each.held;
776
+ }
777
+ for (const room of this.#rooms) if (room.owner === void 0 || !newer.includes(room.owner)) shrinks += room.extra;
778
+ return this.#admitted - admitted + bytes <= this.total - this.reserve && this.#allotted - held - shrinks + bytes <= this.total;
779
+ }
780
+ /**
781
+ * Whether the pushes' holdings leave no room for a push's grow of `bytes`: it waits on their
782
+ * bytes, which no read's end frees. A grow that waits only on a read's or a fetch's allotment
783
+ * lands once it ends, so it neither yields nor is yielded to.
784
+ */
785
+ #pushesBlock(bytes) {
786
+ return this.#admitted + bytes > this.total - this.reserve;
787
+ }
788
+ /**
789
+ * `asker`'s grow of `bytes`, which doesn't fit, beside every push's grow that waits (#2361), or,
790
+ * at the end of the queue's wake, with no `asker`, the waiting grows alone. When
791
+ * the oldest of them waits on the pushes' holdings, the newest that do too yield, as few as fit
792
+ * the oldest's: each is refused busy, its bytes back in this step, and the oldest's grow is
793
+ * granted in it, before the queue's wake serves anything else. That early grant can start the
794
+ * oldest push's next grow, and its `growWaitMs`, sooner, so a fetch holding room past it can
795
+ * refuse that grow where a later grant wouldn't, as on main a holder past 5 s refuses a grow.
796
+ * What became of `asker`'s grow: `granted`, `refused`, `others` when only others' were, or
797
+ * undefined.
798
+ */
799
+ #yieldFor(asker, bytes) {
800
+ if (asker !== void 0 && !this.#order.has(asker)) return void 0;
801
+ const grows = asker === void 0 ? [] : [{
802
+ grower: asker,
803
+ bytes,
804
+ waiter: void 0
805
+ }];
806
+ for (const waiter of this.#reads) if (!("room" in waiter) && waiter.grower !== void 0 && this.#order.has(waiter.grower)) grows.push({
807
+ grower: waiter.grower,
808
+ bytes: waiter.bytes,
809
+ waiter
810
+ });
811
+ grows.sort((one, other) => this.#older(one.grower, other.grower) ? 1 : -1);
812
+ const oldest = grows.pop();
813
+ if (oldest === void 0 || !this.#pushesBlock(oldest.bytes)) return void 0;
814
+ const yielding = [];
815
+ for (const each of grows) {
816
+ if (!this.#pushesBlock(each.bytes)) continue;
817
+ yielding.push(each);
818
+ const newer = yielding.map(({ grower }) => grower);
819
+ if (this.#fitsWithout(newer, oldest.bytes)) return this.#yielded(asker, oldest, yielding);
820
+ }
821
+ }
822
+ /** `#yieldFor`'s step: `yielding` refused and released, then `oldest`'s grow granted. */
823
+ #yielded(asker, oldest, yielding) {
824
+ const waking = this.#waking;
825
+ this.#waking = true;
826
+ let granted = false;
827
+ try {
828
+ for (const { grower, waiter } of yielding) {
829
+ if (waiter !== void 0) this.#reads.splice(this.#reads.indexOf(waiter), 1);
830
+ this.#yielders.set(grower, grower.bytes);
831
+ grower.release();
832
+ waiter?.refuse();
833
+ }
834
+ const { grower, waiter } = oldest;
835
+ if (waiter === void 0) granted = this.tryGrow(grower, oldest.bytes);
836
+ else if (this.#fits(waiter.bytes, true, waiter.asker)) {
837
+ this.#reads.splice(this.#reads.indexOf(waiter), 1);
838
+ waiter.admitted(this.#grant(waiter.bytes, true, waiter.busy, waiter.asker));
839
+ }
840
+ } finally {
841
+ this.#waking = waking;
842
+ }
843
+ this.#wake();
844
+ if (yielding.some(({ grower }) => grower === asker)) return "refused";
845
+ return granted ? "granted" : "others";
846
+ }
739
847
  /**
740
848
  * A grow refused busy, one line in the log with who asked and what was free as its wait ended,
741
- * so a refusal beside upkeep shows as a pattern (#1300).
849
+ * so a refusal beside upkeep shows as a pattern (#1300), or, for a push's that yielded to an
850
+ * older push's, that it did and what it held.
742
851
  */
743
852
  #refusedGrow(allotment, bytes) {
853
+ const held = this.#yielders.get(allotment);
854
+ if (held !== void 0) return Effect.logWarning(`The push allotment's grow of ${bytes} B, holding ${held} B, yielded to an older push whose grow waits on its room, and was refused busy`);
744
855
  const kind = this.#upkeeps.has(allotment) ? "upkeep" : allotment.push ? "push" : "read";
745
856
  const free = this.total - this.#allotted;
746
857
  const beside = this.total - this.reserve - this.#admitted;
@@ -834,6 +945,17 @@ const upkeepRuns = {
834
945
  ways: 32
835
946
  };
836
947
  /**
948
+ * The room a prune walk's reader holds its packs' `.idx` and `.rev` in, whatever their sum: a
949
+ * fixed size, never sized up to what's spare, which a walk charges once in place of each pack's
950
+ * (#2230). About 1,050,000 objects' indexes at 32 B an object. A merge cuts each pack it writes so
951
+ * its index fits, and a walk refuses a listed pack whose index alone passes it.
952
+ *
953
+ * It fits beside the rest of a walk at its bounds: `pruneWalkHeld()`'s 47,844,271, this
954
+ * 33,554,432 and the read reserve's 4,194,688 come to 85,593,391, under the default memory's
955
+ * 103,284,736 allottable, which leaves 17,691,345 for the seen bits: about 141,500,000 objects.
956
+ */
957
+ const pruneIndexFloor = 33554432;
958
+ /**
837
959
  * What a prune walk's seen set holds (#2019): a bit for each object of each of `packs`, at its rank,
838
960
  * and for each of `outside` objects no listed pack holds, its ID, at `fetchEntryBytes`. A part's
839
961
  * save puts the same bytes, the IDs at 20 each, so its read holds no more.
@@ -977,4 +1099,4 @@ var AllottedPointers = class {
977
1099
  return this.#pointers.finish();
978
1100
  }
979
1101
  };
980
- export { AllottedPointers, MemoryAllotment, MemoryBudget, allottedWithin, fetchBusy, growWaitMs, headStuckMs, joinRoomOf, outsideOn, pruneSeenBytes, pushBusy, pushEntryTooLarge, pushIndexTooLarge, pushMemoryOf, pushTooLarge, pushWaitMs, readBusy, readerMemoryOf, upkeepMemoryOf, upkeepRuns, upkeepTooLarge };
1102
+ export { AllottedPointers, MemoryAllotment, MemoryBudget, allottedWithin, fetchBusy, growWaitMs, headStuckMs, joinRoomOf, outsideOn, pruneIndexFloor, pruneSeenBytes, pushBusy, pushEntryTooLarge, pushIndexTooLarge, pushMemoryOf, pushTooLarge, pushWaitMs, readBusy, readerMemoryOf, upkeepMemoryOf, upkeepRuns, upkeepTooLarge };
@@ -1,7 +1,7 @@
1
1
  import { sizedPutCalls } from "./calls.mjs";
2
2
  import { upkeepRuns } from "./memory-budget.mjs";
3
3
  import { searchReads } from "./pack-ranges.mjs";
4
- import { packIndexerLimits } from "./pack-reads.mjs";
4
+ import { indexBytes, packIndexerLimits } from "./pack-reads.mjs";
5
5
  // src/storage/merge-cut.ts
6
6
  /** A pack before its first entry: its 12-byte header and 20-byte trailer. */
7
7
  const packStart = {
@@ -44,10 +44,11 @@ const passObjectsMost = 15e5;
44
44
  const writeCallsOf = (spend, multipart, bounds = upkeepRuns) => spend.reads + sizedPutCalls(multipart, spend.bytes) + Math.ceil(spend.objects * indexRecordBytes / bounds.runBytes) * sizedPutCalls(multipart, bounds.runBytes) + Math.ceil(spend.pairs * 8 / pairRunBytes) * sizedPutCalls(multipart, pairRunBytes);
45
45
  /**
46
46
  * Whether a pack at `spend` ends before the entry that would take it to `next`: past
47
- * `maxPackSize`, a write try's calls or the index pass's. Never before its first entry, so an
48
- * entry past them alone goes in a pack of its own.
47
+ * `maxPackSize`, an index past `pruneIndexFloor`, so a prune walk holds any pack a merge writes
48
+ * (#2230), a write try's calls or the index pass's. Never before its first entry, so an entry past
49
+ * them alone goes in a pack of its own.
49
50
  */
50
- const cutsBefore = (spend, next, limits, bounds = upkeepRuns) => spend.objects > 0 && (next.bytes > limits.maxPackSize || limits.writeCalls !== void 0 && writeCallsOf(next, limits.multipart, bounds) > limits.writeCalls || limits.indexMost !== void 0 && limits.indexCalls(next.objects, next.bytes) > limits.indexMost);
51
+ const cutsBefore = (spend, next, limits, bounds = upkeepRuns) => spend.objects > 0 && (next.bytes > limits.maxPackSize || indexBytes(next) > 33554432 || limits.writeCalls !== void 0 && writeCallsOf(next, limits.multipart, bounds) > limits.writeCalls || limits.indexMost !== void 0 && limits.indexCalls(next.objects, next.bytes) > limits.indexMost);
51
52
  /**
52
53
  * The bucket calls resolving one entry written whole takes in an input of `objects`, its chain read
53
54
  * in `blocks` block reads: the indexer reads at most `chainReads` entries, and finds each base but
@@ -863,6 +863,22 @@ const readInto = (bucket, key, most, named) => bucketDo(Effect.suspend(() => {
863
863
  }), Effect.map(() => bytes.subarray(0, at)), Effect.catchIf((error) => Predicate.isTagged(error, "NotFound"), () => Effect.succeed(void 0)));
864
864
  }));
865
865
  /**
866
+ * Why a reader under `indexFloor` refused: a lookup needed a pack whose index its room let go of,
867
+ * which a prune walk's try takes as its end, to go on in a try of its own.
868
+ */
869
+ var IndexReloaded = class extends Error {};
870
+ /** The refusal of a load of `pack`'s index, which a reader's room let go of. */
871
+ const indexReloaded = (pack) => {
872
+ const message = `Pack ${pack}'s index was let go of by the reader's room, which loads a pack once`;
873
+ return gitError("RESOURCE_EXHAUSTED", message, { cause: new IndexReloaded(message) });
874
+ };
875
+ /** Whether `error` is a reader's refusal to load again an index its room let go of. */
876
+ const isIndexReloaded = (error) => error.cause instanceof IndexReloaded;
877
+ /** Why a reader under `indexFloor` refused: a load past its caller's calls, as `canLoad` says. */
878
+ var IndexShort = class extends Error {};
879
+ /** Whether `error` is a reader's refusal to load an index past its caller's calls. */
880
+ const isIndexShort = (error) => error.cause instanceof IndexShort;
881
+ /**
866
882
  * One operation's reads of a repo's objects under `packs()`: its rows first, a small push's,
867
883
  * then each pack its list names, by the pack's `.idx`. An object is resolved from the pack's
868
884
  * neighbourhoods the operation keeps, each delta's base from the same pack, and hashed again
@@ -881,18 +897,117 @@ const packReader = (input) => {
881
897
  const charged = /* @__PURE__ */ new Set();
882
898
  const charging = new SingleFlight();
883
899
  const loadings = /* @__PURE__ */ new Set();
900
+ const { indexFloor } = input;
901
+ const roomHeld = /* @__PURE__ */ new Map();
902
+ const letGo = /* @__PURE__ */ new Set();
903
+ let roomBytes = 0;
904
+ const unlisted = /* @__PURE__ */ new Set();
884
905
  /**
885
- * `pack`'s `.idx` and `.rev` charged to the reader's allotment, once a reader: marked only once
886
- * granted, so a read at once on the reader waits on the charge before it loads, and a refused or
887
- * interrupted charge is asked again by the next read, never skipped.
906
+ * `bytes` charged to the reader's allotment once a reader under `id`, a pack's or the room's,
907
+ * those `tells` told: marked only once granted, so a read at once on the reader waits on the
908
+ * charge before it loads, and a refused or interrupted charge is asked again by the next read,
909
+ * never skipped.
888
910
  */
889
- const charge = Effect.fnUntraced(function* (pack) {
890
- if (charged.has(pack.id)) return;
891
- const granted = yield* charging.run(pack.id, Effect.tap(memory.index(indexBytes(pack)), () => Effect.sync(() => {
892
- charged.add(pack.id);
893
- for (const told of loadings) told(pack.id);
911
+ const chargeOnce = Effect.fnUntraced(function* (id, bytes, tells) {
912
+ if (charged.has(id)) return;
913
+ const granted = yield* charging.run(id, Effect.tap(memory.index(bytes), () => Effect.sync(() => {
914
+ charged.add(id);
915
+ if (tells) for (const told of loadings) told(id);
894
916
  })));
895
- if (Option.isNone(granted)) yield* charge(pack);
917
+ if (Option.isNone(granted)) yield* chargeOnce(id, bytes, tells);
918
+ });
919
+ /** `pack`'s `.idx` and `.rev` charged once a reader, each pack's own, as `chargeOnce` says. */
920
+ const charge = (pack) => chargeOnce(pack.id, indexBytes(pack), true);
921
+ /** The room's least recently used let go of until `bytes` more fit in `floor`. */
922
+ const makeRoom = (bytes, floor) => {
923
+ for (const id of kept.keys()) {
924
+ if (roomBytes + bytes <= floor) return;
925
+ kept.delete(id);
926
+ roomBytes -= roomHeld.get(id) ?? 0;
927
+ roomHeld.delete(id);
928
+ letGo.add(id);
929
+ }
930
+ };
931
+ /**
932
+ * Under `indexFloor`, `pack`'s `.idx` and `.rev` from the room, made its most recently used: on
933
+ * its first load, the floor charged once for the reader, then the least recently used let go
934
+ * until it fits, before the load, so the reader never holds past the floor while it reads. One
935
+ * the room let go of fails as `indexReloaded`, and one past `canLoad` as `indexShort`.
936
+ */
937
+ const roomed = Effect.fnUntraced(function* (pack, { bytes: floor, canLoad }) {
938
+ const known = kept.get(pack.id);
939
+ if (known !== void 0) {
940
+ kept.delete(pack.id);
941
+ kept.set(pack.id, known);
942
+ return known;
943
+ }
944
+ if (letGo.has(pack.id)) return yield* Effect.fail(indexReloaded(pack.id));
945
+ if (canLoad?.() === false) {
946
+ const message = `Pack ${pack.id}'s index would load past its reader's calls`;
947
+ return yield* Effect.fail(gitError("RESOURCE_EXHAUSTED", message, { cause: new IndexShort(message) }));
948
+ }
949
+ yield* chargeOnce("", floor, false);
950
+ const bytes = indexBytes(pack);
951
+ makeRoom(bytes, floor);
952
+ for (const told of loadings) told(pack.id);
953
+ const loaded = yield* indexes.load(bucket, pack);
954
+ const raced = kept.get(pack.id);
955
+ if (raced !== void 0) return raced;
956
+ makeRoom(bytes, floor);
957
+ kept.set(pack.id, loaded);
958
+ roomHeld.set(pack.id, bytes);
959
+ roomBytes += bytes;
960
+ return loaded;
961
+ });
962
+ /**
963
+ * The listed packs in the order a lookup under `indexFloor` looks in them: those the room holds,
964
+ * then those it never loaded, then those it let go of, each in their listed order, so a lookup
965
+ * loads a pack the room let go of only when what's left of it may be there.
966
+ */
967
+ const roomOrder = () => {
968
+ const held = [];
969
+ const fresh = [];
970
+ const gone = [];
971
+ for (const pack of listed.packs) (kept.has(pack.id) ? held : letGo.has(pack.id) ? gone : fresh).push(pack);
972
+ return [
973
+ ...held,
974
+ ...fresh,
975
+ ...gone
976
+ ];
977
+ };
978
+ /**
979
+ * Each listed pack that holds some of `oids`, visited with them by offset, in listed order, each
980
+ * pack's index loaded as `indexed` loads it. Under `indexFloor`, in the room's order, each
981
+ * visited while its index is the room's, before the next pack's loads, so a lookup holds one
982
+ * pack's index at a time, and those no listed pack holds are kept in `unlisted`, up to the
983
+ * floor's. Answers those of `oids` no listed pack holds. A pack in `skip` isn't looked in: its
984
+ * caller proved it holds none of `oids`.
985
+ */
986
+ const byPacks = Effect.fnUntraced(function* (oids, visit, skip) {
987
+ const asked = [...new Set(oids)];
988
+ const known = asked.filter((oid) => unlisted.has(oid));
989
+ let left = known.length === 0 ? asked : asked.filter((oid) => !unlisted.has(oid));
990
+ for (const pack of indexFloor === void 0 ? listed.packs : roomOrder()) {
991
+ if (left.length === 0) return known;
992
+ if (skip?.has(pack.id) === true) continue;
993
+ const loaded = indexFloor === void 0 ? yield* indexed(pack) : yield* roomed(pack, indexFloor);
994
+ const entries = [];
995
+ left = left.filter((oid) => {
996
+ const object = loaded.index.find(oid);
997
+ if (object === void 0) return true;
998
+ entries.push({
999
+ oid,
1000
+ offset: object.offset,
1001
+ crc32: object.crc32
1002
+ });
1003
+ return false;
1004
+ });
1005
+ if (entries.length > 0) yield* visit(loaded, entries.sort((a, b) => a.offset - b.offset));
1006
+ }
1007
+ if (indexFloor !== void 0) {
1008
+ for (const oid of left) if (unlisted.size < indexFloor.unlisted) unlisted.add(oid);
1009
+ }
1010
+ return [...known, ...left];
896
1011
  });
897
1012
  const unnamed = (pack) => !named.has(pack.id);
898
1013
  /** The packs the repo lists now that `listed` didn't, such as a merge's that folded rows. */
@@ -949,11 +1064,12 @@ const packReader = (input) => {
949
1064
  const look = Effect.fnUntraced(function* (packs, held, going = false) {
950
1065
  for (const listedPack of packs) {
951
1066
  if (left.length === 0) break;
952
- const known = held.get(listedPack.id);
953
- if (known === void 0) yield* charge(listedPack);
954
- const indexed = known === void 0 ? yield* indexes.load(bucket, listedPack).pipe(Effect.asSome, Effect.catch((error) => going && error.code === "DATA_LOSS" ? Effect.succeedNone : Effect.fail(error))) : Option.some(known);
1067
+ const room = held === kept ? indexFloor : void 0;
1068
+ const known = room === void 0 ? held.get(listedPack.id) : void 0;
1069
+ if (room === void 0 && known === void 0) yield* charge(listedPack);
1070
+ const indexed = room !== void 0 ? Option.some(yield* roomed(listedPack, room)) : known === void 0 ? yield* indexes.load(bucket, listedPack).pipe(Effect.asSome, Effect.catch((error) => going && error.code === "DATA_LOSS" ? Effect.succeedNone : Effect.fail(error))) : Option.some(known);
955
1071
  if (Option.isNone(indexed)) continue;
956
- held.set(listedPack.id, indexed.value);
1072
+ if (room === void 0) held.set(listedPack.id, indexed.value);
957
1073
  left = left.filter((oid) => {
958
1074
  const object = indexed.value.index.find(oid);
959
1075
  if (object === void 0) return true;
@@ -1010,7 +1126,24 @@ const packReader = (input) => {
1010
1126
  const where = yield* located(oids, take, skip);
1011
1127
  for (const [oid, { indexed, offset }] of where) yield* take(yield* wholeAt(oid, indexed, offset));
1012
1128
  });
1013
- const each = eachOf();
1129
+ /**
1130
+ * `each` under `indexFloor`: the listed packs' objects a pack at a time, each read whole while its
1131
+ * pack's index is the room's; then, as `located` orders them, the rows of those no listed pack
1132
+ * holds, then the packs listed since and those leaving.
1133
+ */
1134
+ const eachInRoom = Effect.fnUntraced(function* (oids, take) {
1135
+ const left = yield* byPacks(oids, (indexed, entries) => Effect.forEach(entries, ({ oid, offset }) => Effect.flatMap(wholeAt(oid, indexed, offset), take), { discard: true }));
1136
+ if (left.length === 0) return;
1137
+ const inRows = /* @__PURE__ */ new Set();
1138
+ yield* rows(left, (object) => {
1139
+ inRows.add(object.oid);
1140
+ return take(object);
1141
+ });
1142
+ const missing = left.filter((oid) => !inRows.has(oid));
1143
+ if (missing.length === 0) return;
1144
+ for (const [oid, { indexed, offset }] of yield* found(missing, "beyond")) yield* take(yield* wholeAt(oid, indexed, offset));
1145
+ });
1146
+ const each = indexFloor === void 0 ? eachOf() : eachInRoom;
1014
1147
  /** Which of `oids` the repo holds, read whole, through `take`'s reads. */
1015
1148
  const heldOf = (take) => Effect.fnUntraced(function* (oids) {
1016
1149
  const objects = /* @__PURE__ */ new Map();
@@ -1198,13 +1331,13 @@ const packReader = (input) => {
1198
1331
  */
1199
1332
  const storedOf = Effect.fnUntraced(function* (oids, skip) {
1200
1333
  const entries = /* @__PURE__ */ new Map();
1201
- for (const [oid, { indexed, offset, crc32 }] of byPlace(yield* found(oids, "all", skip))) {
1334
+ const add = Effect.fnUntraced(function* (oid, indexed, at) {
1202
1335
  const { pack } = indexed;
1336
+ const { offset, crc32 } = at;
1203
1337
  const end = entryEnd(indexed, offset);
1204
1338
  if (end === void 0 || end <= offset) return yield* Effect.fail(lost(`Pack ${pack.id}'s reverse index has no entry at ${offset}`));
1205
1339
  const holds = yield* holdsAt(indexed, offset);
1206
- if (holds === void 0) continue;
1207
- entries.set(oid, {
1340
+ if (holds !== void 0) entries.set(oid, {
1208
1341
  oid,
1209
1342
  pack,
1210
1343
  offset,
@@ -1212,7 +1345,13 @@ const packReader = (input) => {
1212
1345
  crc32,
1213
1346
  holds
1214
1347
  });
1215
- }
1348
+ });
1349
+ const left = indexFloor === void 0 || skip !== void 0 ? oids : yield* byPacks(oids, (indexed, at) => Effect.forEach(at, (each) => add(each.oid, indexed, each), { discard: true }));
1350
+ const from = left === oids ? "all" : "beyond";
1351
+ for (const [oid, place] of byPlace(yield* found(left, from, skip))) yield* add(oid, place.indexed, {
1352
+ oid,
1353
+ ...place
1354
+ });
1216
1355
  return entries;
1217
1356
  });
1218
1357
  const read = (entry, at) => {
@@ -1314,19 +1453,27 @@ const packReader = (input) => {
1314
1453
  /** Those of `oids` stored as whole blobs, by their entries' headers, none of them read. */
1315
1454
  const wholeBlobs = Effect.fnUntraced(function* (oids) {
1316
1455
  const blobs = /* @__PURE__ */ new Set();
1317
- for (const [oid, { indexed, offset }] of byPlace(yield* found(oids))) {
1456
+ const add = Effect.fnUntraced(function* (oid, indexed, offset) {
1318
1457
  const holds = yield* holdsAt(indexed, offset);
1319
1458
  if (holds?.kind === "whole" && holds.type === "blob") blobs.add(oid);
1320
- }
1459
+ });
1460
+ const left = yield* byPacks(oids, (indexed, at) => Effect.forEach(at, ({ oid, offset }) => add(oid, indexed, offset), { discard: true }));
1461
+ for (const [oid, { indexed, offset }] of byPlace(yield* found(left, "beyond"))) yield* add(oid, indexed, offset);
1321
1462
  return blobs;
1322
1463
  });
1323
1464
  /**
1324
- * `oids` in the order whose reads take the fewest blocks: the listed packs' objects by pack and
1325
- * offset, from their `.idx` alone with no header read, then the rest as they came (#662).
1465
+ * `oids` in the order whose reads take the fewest blocks, a group a pack: the listed packs'
1466
+ * objects by offset, from their `.idx` alone with no header read, the pack looked in last first,
1467
+ * then the rest as they came, as one group (#662). Under `indexFloor`, the pack looked in last,
1468
+ * and those before it the room still holds, are read without a load, and a batch that takes from
1469
+ * one group needs one pack's index.
1326
1470
  */
1327
- const inPackOrder = Effect.fnUntraced(function* (oids) {
1328
- const where = yield* found(oids, "listed");
1329
- return [...byPlace(where).map(([oid]) => oid), ...oids.filter((oid) => !where.has(oid))];
1471
+ const inPackGroups = Effect.fnUntraced(function* (oids) {
1472
+ const groups = [];
1473
+ const left = yield* byPacks(oids, (_, entries) => Effect.sync(() => void groups.push(entries.map(({ oid }) => oid))));
1474
+ groups.reverse();
1475
+ if (left.length > 0) groups.push(left);
1476
+ return groups;
1330
1477
  });
1331
1478
  /**
1332
1479
  * Lets go of what the reader holds: its read cache and the indexes it kept. Its blocks stay
@@ -1337,6 +1484,10 @@ const packReader = (input) => {
1337
1484
  const release = () => {
1338
1485
  wholeAt.clear();
1339
1486
  kept.clear();
1487
+ roomHeld.clear();
1488
+ letGo.clear();
1489
+ unlisted.clear();
1490
+ roomBytes = 0;
1340
1491
  };
1341
1492
  /**
1342
1493
  * A listed pack's `.idx` and `.rev` as this reader holds them: loaded and charged as its own
@@ -1344,6 +1495,7 @@ const packReader = (input) => {
1344
1495
  * walk read them once between them, and none when the cache holds them.
1345
1496
  */
1346
1497
  const indexed = Effect.fnUntraced(function* (pack) {
1498
+ if (indexFloor !== void 0) return yield* roomed(pack, indexFloor);
1347
1499
  const known = kept.get(pack.id);
1348
1500
  if (known !== void 0) return known;
1349
1501
  yield* charge(pack);
@@ -1352,6 +1504,64 @@ const packReader = (input) => {
1352
1504
  return loaded;
1353
1505
  });
1354
1506
  /**
1507
+ * Where each of `oids` is for a prune walk's seen set, as `byPacks` finds it: its pack's ID and
1508
+ * its rank there, in the order it visits the packs. Under `indexFloor`, by the first in the
1509
+ * room's order, so an object two packs hold may be placed by either. A pack in `skip` isn't
1510
+ * looked in: its caller proved it holds none of `oids`.
1511
+ */
1512
+ const ranks = Effect.fnUntraced(function* (oids, skip) {
1513
+ const at = /* @__PURE__ */ new Map();
1514
+ yield* byPacks(oids, ({ pack, index }, entries) => Effect.sync(() => {
1515
+ for (const { oid } of entries) {
1516
+ const rank = index.rankOf(oid);
1517
+ if (rank !== void 0) at.set(oid, {
1518
+ pack: pack.id,
1519
+ rank
1520
+ });
1521
+ }
1522
+ }), skip);
1523
+ return at;
1524
+ });
1525
+ /**
1526
+ * For a prune walk's history: the first listed pack that holds some of `oids`, of those the room
1527
+ * holds and then those it never loaded, in listed order, with the rank there of each it holds,
1528
+ * so a walk reads what one pack holds before it loads the next (#2230). A pack the room let go
1529
+ * of isn't looked in: what only it holds waits for a reader of its own. With none, `unlisted`
1530
+ * says whether every listed pack was looked in, none let go of, which proves no listed pack holds
1531
+ * them: they're then kept as `unlisted` are, up to the floor's.
1532
+ */
1533
+ const nextPack = Effect.fnUntraced(function* (oids) {
1534
+ const asked = [...new Set(oids)].filter((oid) => !unlisted.has(oid));
1535
+ const placed = /* @__PURE__ */ new Map();
1536
+ const every = letGo.size === 0;
1537
+ if (asked.length === 0) return {
1538
+ placed,
1539
+ unlisted: true
1540
+ };
1541
+ for (const pack of indexFloor === void 0 ? listed.packs : roomOrder()) {
1542
+ if (letGo.has(pack.id)) break;
1543
+ const { index } = yield* indexed(pack);
1544
+ for (const oid of asked) {
1545
+ const rank = index.rankOf(oid);
1546
+ if (rank !== void 0) placed.set(oid, {
1547
+ pack: pack.id,
1548
+ rank
1549
+ });
1550
+ }
1551
+ if (placed.size > 0) return {
1552
+ placed,
1553
+ unlisted: false
1554
+ };
1555
+ }
1556
+ if (every && indexFloor !== void 0) {
1557
+ for (const oid of asked) if (unlisted.size < indexFloor.unlisted) unlisted.add(oid);
1558
+ }
1559
+ return {
1560
+ placed,
1561
+ unlisted: every
1562
+ };
1563
+ });
1564
+ /**
1355
1565
  * `ObjectReader.startingWith`: the IDs that start with `prefix` in the listed packs and the rows;
1356
1566
  * then, unless the rows were read at the listing's own version, in the packs listed since and
1357
1567
  * those leaving, where a merge that folded rows after this read's listing put them. Each pack is
@@ -1424,7 +1634,9 @@ const packReader = (input) => {
1424
1634
  object,
1425
1635
  objects,
1426
1636
  wholeBlobs,
1427
- inPackOrder,
1637
+ inPackGroups,
1638
+ nextPack,
1639
+ ranks,
1428
1640
  release
1429
1641
  };
1430
1642
  return input.visible === void 0 ? reads : narrowedTo(reads, input.visible);
@@ -1476,4 +1688,4 @@ const narrowedTo = (reads, visible) => {
1476
1688
  wholeBlobs: (oids) => Effect.flatMap(only(oids), reads.wholeBlobs)
1477
1689
  };
1478
1690
  };
1479
- export { PackBlocks, PackIndexes, blockBytes, blockHeld, blocksHeld, entryEnd, hashedNowMost, hashedRunMost, headerLimits, indexBytes, indexSeedShare, loadedLookup, packFileBytes, packIndexerLimits, packObjectBytes, packObjects, packReader, rangedLookup, readCacheBytes, seedBytes };
1691
+ export { IndexReloaded, IndexShort, PackBlocks, PackIndexes, blockBytes, blockHeld, blocksHeld, entryEnd, hashedNowMost, hashedRunMost, headerLimits, indexBytes, indexSeedShare, isIndexReloaded, isIndexShort, loadedLookup, packFileBytes, packIndexerLimits, packObjectBytes, packObjects, packReader, rangedLookup, readCacheBytes, seedBytes };
@@ -65,6 +65,12 @@ const seenSet = /* @__PURE__ */ Effect.fnUntraced(function* (packs, outsideMost,
65
65
  count += 1;
66
66
  return true;
67
67
  };
68
+ /** Whether `oid`, at `at` or outside the listed packs, is seen already, as `add` asks, unmarked. */
69
+ const has = (oid, at) => {
70
+ const set = at === void 0 ? void 0 : bits.get(at.pack);
71
+ if (set === void 0 || at === void 0) return outside.has(oid);
72
+ return ((set[at.rank >>> 3] ?? 0) & 1 << (at.rank & 7)) !== 0;
73
+ };
68
74
  if (from !== void 0) {
69
75
  const { saved, bytes, place } = from;
70
76
  const length = seenLength(saved.packs, saved.outside);
@@ -79,12 +85,10 @@ const seenSet = /* @__PURE__ */ Effect.fnUntraced(function* (packs, outsideMost,
79
85
  into.set(held);
80
86
  for (const byte of held) count += popCount(byte);
81
87
  }
82
- for (let first = 0; first < saved.outside; first += 64) {
83
- const batch = [];
84
- for (let id = first; id < Math.min(saved.outside, first + 64); id += 1) batch.push(hexOf(bytes.subarray(at + id * 20, at + id * 20 + 20)));
85
- const placed = yield* place(batch);
86
- for (const oid of batch) if (add(oid, placed.get(oid)) === "full") return yield* Effect.fail(gitError("DATA_LOSS", `A walk's save holds more than ${outsideMost} objects no listed pack holds`));
87
- }
88
+ const ids = [];
89
+ for (let id = 0; id < saved.outside; id += 1) ids.push(hexOf(bytes.subarray(at + id * 20, at + id * 20 + 20)));
90
+ const placed = yield* place(ids);
91
+ for (const oid of ids) if (add(oid, placed.get(oid)) === "full") return yield* Effect.fail(gitError("DATA_LOSS", `A walk's save holds more than ${outsideMost} objects no listed pack holds`));
88
92
  }
89
93
  /** The set's bytes, and the layout a save names them by under `key`. */
90
94
  const saved = (key) => {
@@ -114,6 +118,7 @@ const seenSet = /* @__PURE__ */ Effect.fnUntraced(function* (packs, outsideMost,
114
118
  };
115
119
  return {
116
120
  add,
121
+ has,
117
122
  saved,
118
123
  size: () => count
119
124
  };