@ref0/git 0.14.0 → 0.20.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 (351) hide show
  1. package/dist/auth/access-jwt.mjs +2 -2
  2. package/dist/auth/authorize.mjs +3 -3
  3. package/dist/auth/fences.d.mts +1 -2
  4. package/dist/auth/fences.mjs +3 -3
  5. package/dist/auth/grants.mjs +4 -4
  6. package/dist/auth/host-function.mjs +18 -1
  7. package/dist/auth/identify.d.mts +1 -1
  8. package/dist/auth/identify.mjs +3 -3
  9. package/dist/auth/token-cleanup.mjs +67 -0
  10. package/dist/auth/token-rows.mjs +1 -1
  11. package/dist/auth/token-text.mjs +9 -1
  12. package/dist/auth/tokens-base.d.mts +35 -0
  13. package/dist/auth/tokens-base.mjs +13 -0
  14. package/dist/auth/tokens-layer.d.mts +2 -27
  15. package/dist/auth/tokens-layer.mjs +6 -11
  16. package/dist/auth/tokens-public.d.mts +3 -3
  17. package/dist/auth/tokens.d.mts +7 -10
  18. package/dist/auth/tokens.mjs +9 -80
  19. package/dist/client/answer.mjs +21 -42
  20. package/dist/client/client.d.mts +2 -4
  21. package/dist/client/git-http.mjs +17 -7
  22. package/dist/client/local.mjs +14 -13
  23. package/dist/client/push-upload.mjs +9 -8
  24. package/dist/client/remote.mjs +8 -10
  25. package/dist/client/run-remote-helper.d.mts +1 -0
  26. package/dist/client/run-remote-helper.mjs +58 -24
  27. package/dist/client/slice-push.d.mts +1 -1
  28. package/dist/client/transport.mjs +105 -46
  29. package/dist/contracts/blobs.d.mts +55 -8
  30. package/dist/contracts/blobs.mjs +16 -2
  31. package/dist/contracts/call-allowance.d.mts +13 -0
  32. package/dist/contracts/call-allowance.mjs +44 -0
  33. package/dist/contracts/calls-spent.mjs +8 -3
  34. package/dist/contracts/client.d.mts +15 -16
  35. package/dist/contracts/commits.d.mts +24 -24
  36. package/dist/contracts/engine.d.mts +6 -1
  37. package/dist/contracts/engine.mjs +10 -1
  38. package/dist/contracts/files.d.mts +10 -10
  39. package/dist/contracts/forks.d.mts +17 -17
  40. package/dist/contracts/git-events.d.mts +29 -1
  41. package/dist/contracts/git-hooks.d.mts +1 -1
  42. package/dist/contracts/git.d.mts +21 -0
  43. package/dist/contracts/grants.d.mts +24 -4
  44. package/dist/contracts/handlers.d.mts +1 -1
  45. package/dist/contracts/lfs.d.mts +28 -2
  46. package/dist/contracts/merges.d.mts +3 -2
  47. package/dist/contracts/objects.d.mts +23 -25
  48. package/dist/contracts/queue.d.mts +40 -12
  49. package/dist/contracts/queue.mjs +3 -4
  50. package/dist/contracts/refs.d.mts +26 -23
  51. package/dist/contracts/remote.d.mts +6 -7
  52. package/dist/contracts/remote.mjs +4 -1
  53. package/dist/contracts/repos.d.mts +29 -138
  54. package/dist/contracts/repos.mjs +5 -3
  55. package/dist/contracts/result.d.mts +35 -16
  56. package/dist/contracts/result.mjs +35 -7
  57. package/dist/contracts/server.d.mts +25 -50
  58. package/dist/contracts/services.d.mts +1 -1
  59. package/dist/contracts/store.d.mts +6 -5
  60. package/dist/contracts/sync.d.mts +11 -7
  61. package/dist/contracts/sync.mjs +12 -2
  62. package/dist/contracts/tags.d.mts +10 -10
  63. package/dist/contracts/time.d.mts +3 -0
  64. package/dist/contracts/{promises.mjs → time.mjs} +1 -1
  65. package/dist/contracts/tokens.d.mts +15 -17
  66. package/dist/contracts/tokens.mjs +3 -2
  67. package/dist/contracts/topics.d.mts +1 -1
  68. package/dist/contracts/trees.d.mts +12 -12
  69. package/dist/contracts/work.d.mts +24 -13
  70. package/dist/core/access.d.mts +1 -2
  71. package/dist/core/access.mjs +20 -9
  72. package/dist/core/ancestry.d.mts +1 -0
  73. package/dist/core/asked.mjs +1 -1
  74. package/dist/core/call-names.mjs +6 -1
  75. package/dist/core/call-services.mjs +11 -3
  76. package/dist/core/events/deliver.d.mts +1 -2
  77. package/dist/core/events/deliver.mjs +43 -36
  78. package/dist/core/events/retired.mjs +119 -0
  79. package/dist/core/forks.mjs +928 -432
  80. package/dist/core/hooks/groups.mjs +1 -1
  81. package/dist/core/imports.mjs +29 -23
  82. package/dist/core/index-job.mjs +122 -34
  83. package/dist/core/index-lost.d.mts +1 -2
  84. package/dist/core/index-lost.mjs +2 -2
  85. package/dist/core/loose-tally.mjs +1 -1
  86. package/dist/core/objects/blobs.mjs +129 -3
  87. package/dist/core/objects/lines.mjs +2 -2
  88. package/dist/core/objects/merges.mjs +4 -1
  89. package/dist/core/objects/objects-layer.d.mts +1 -1
  90. package/dist/core/objects/objects-layer.mjs +3 -3
  91. package/dist/core/objects/reading.mjs +24 -13
  92. package/dist/core/objects/tags.mjs +4 -1
  93. package/dist/core/objects/trees.mjs +2 -2
  94. package/dist/core/objects/writing.mjs +2 -2
  95. package/dist/core/own-writes.mjs +1 -1
  96. package/dist/core/parts.d.mts +4 -5
  97. package/dist/core/parts.mjs +3 -8
  98. package/dist/core/promise.mjs +9 -12
  99. package/dist/core/purge.mjs +287 -83
  100. package/dist/core/push-decide.mjs +2 -0
  101. package/dist/core/push-files.d.mts +1 -1
  102. package/dist/core/push.mjs +58 -47
  103. package/dist/core/receipts.mjs +8 -3
  104. package/dist/core/ref-index-job.mjs +4 -4
  105. package/dist/core/ref-view.mjs +2 -2
  106. package/dist/core/refs.mjs +5 -5
  107. package/dist/core/registered-rows.mjs +2 -2
  108. package/dist/core/repack.mjs +36 -21
  109. package/dist/core/repo-info.mjs +3 -3
  110. package/dist/core/repos.mjs +21 -9
  111. package/dist/core/rules/commit-changes.d.mts +0 -1
  112. package/dist/core/saved-caller-views.d.mts +7 -2
  113. package/dist/core/saved-caller-views.mjs +2 -2
  114. package/dist/core/saved-callers.d.mts +19 -2
  115. package/dist/core/seam.d.mts +16 -26
  116. package/dist/core/seam.mjs +7 -28
  117. package/dist/core/tokens.mjs +1 -1
  118. package/dist/core/view.mjs +7 -15
  119. package/dist/core/work.mjs +59 -14
  120. package/dist/effect.d.mts +3 -12
  121. package/dist/effect.mjs +1 -10
  122. package/dist/engine/failures.mjs +2 -1
  123. package/dist/engine/reads.mjs +4 -4
  124. package/dist/event-streams/stream.d.mts +2 -2
  125. package/dist/event-streams/stream.mjs +37 -14
  126. package/dist/extend.d.mts +2 -3
  127. package/dist/extend.mjs +2 -1
  128. package/dist/http/answers.mjs +8 -5
  129. package/dist/http/core-parts.mjs +95 -79
  130. package/dist/http/handler.mjs +34 -7
  131. package/dist/http/node.d.mts +14 -1
  132. package/dist/http/node.mjs +56 -27
  133. package/dist/http/parts.d.mts +1 -0
  134. package/dist/http/push-upload-job.mjs +30 -26
  135. package/dist/http/push-uploads.mjs +36 -21
  136. package/dist/http/upload-pack.mjs +3 -3
  137. package/dist/index.d.mts +14 -14
  138. package/dist/index.mjs +2 -2
  139. package/dist/lfs/answer.mjs +37 -0
  140. package/dist/lfs/base.d.mts +2 -0
  141. package/dist/lfs/base.mjs +25 -1
  142. package/dist/lfs/copy.d.mts +21 -7
  143. package/dist/lfs/copy.mjs +89 -6
  144. package/dist/lfs/layer.d.mts +15 -1
  145. package/dist/lfs/layer.mjs +24 -11
  146. package/dist/lfs/locks.mjs +2 -2
  147. package/dist/lfs/objects.d.mts +1 -0
  148. package/dist/lfs/objects.mjs +87 -17
  149. package/dist/lfs/pointers.mjs +2 -2
  150. package/dist/lfs/rows.mjs +42 -8
  151. package/dist/lfs/upkeep.mjs +48 -14
  152. package/dist/lfs/wire.mjs +6 -31
  153. package/dist/merges/merges.mjs +2 -2
  154. package/dist/node.d.mts +2 -2
  155. package/dist/protocol/bytes/buffers.mjs +13 -5
  156. package/dist/protocol/bytes/reader.mjs +6 -1
  157. package/dist/protocol/bytes/streams.mjs +27 -1
  158. package/dist/protocol/diff/read.d.mts +4 -3
  159. package/dist/protocol/diff/read.mjs +8 -3
  160. package/dist/protocol/objects/lfs-pointer.d.mts +14 -2
  161. package/dist/protocol/objects/lfs-pointer.mjs +1 -3
  162. package/dist/protocol/objects/links-raw.mjs +24 -10
  163. package/dist/protocol/objects/tag.d.mts +1 -1
  164. package/dist/protocol/objects/tree.mjs +23 -6
  165. package/dist/protocol/packs/delta.d.mts +6 -3
  166. package/dist/protocol/packs/delta.mjs +96 -32
  167. package/dist/protocol/packs/idx.d.mts +2 -1
  168. package/dist/protocol/packs/idx.mjs +22 -3
  169. package/dist/protocol/packs/parts.d.mts +4 -1
  170. package/dist/protocol/packs/parts.mjs +10 -7
  171. package/dist/protocol/packs/reader.d.mts +5 -0
  172. package/dist/protocol/packs/reader.mjs +3 -2
  173. package/dist/protocol/packs/writer.mjs +26 -8
  174. package/dist/queue/advance.mjs +21 -24
  175. package/dist/queue/base.d.mts +55 -0
  176. package/dist/queue/base.mjs +44 -0
  177. package/dist/queue/calls.mjs +51 -54
  178. package/dist/queue/commit.mjs +1 -1
  179. package/dist/queue/host.d.mts +1 -15
  180. package/dist/queue/host.mjs +2 -7
  181. package/dist/queue/layer.d.mts +2 -1
  182. package/dist/queue/layer.mjs +10 -3
  183. package/dist/queue/limits.mjs +2 -0
  184. package/dist/queue/push-option.mjs +9 -24
  185. package/dist/queue/schedule.mjs +1 -1
  186. package/dist/queue/state.mjs +2 -10
  187. package/dist/queue/trim.mjs +5 -3
  188. package/dist/rest/blobs.mjs +29 -5
  189. package/dist/rest/caller.d.mts +4 -4271
  190. package/dist/rest/caller.mjs +4 -0
  191. package/dist/rest/client.d.mts +8 -5
  192. package/dist/rest/client.mjs +19 -2
  193. package/dist/rest/handlers.mjs +50 -5
  194. package/dist/rest/layer.d.mts +2 -2
  195. package/dist/rest/layer.mjs +5 -12
  196. package/dist/rest/me.d.mts +3 -432
  197. package/dist/server/check-fetch.mjs +3 -3
  198. package/dist/server/edge-node.d.mts +11 -8
  199. package/dist/server/edge-node.mjs +9 -6
  200. package/dist/server/edge.d.mts +21 -7
  201. package/dist/server/edge.mjs +32 -11
  202. package/dist/server/extension.mjs +1 -1
  203. package/dist/server/git-layer.d.mts +5 -5
  204. package/dist/server/git-layer.mjs +28 -22
  205. package/dist/server/git-namespace.d.mts +4 -3
  206. package/dist/server/git-namespace.mjs +3 -2
  207. package/dist/server/joined.d.mts +6 -18
  208. package/dist/server/joined.mjs +9 -36
  209. package/dist/server/lifecycle.mjs +18 -106
  210. package/dist/server/registry.mjs +24 -7
  211. package/dist/server/server.mjs +205 -346
  212. package/dist/signin/context.d.mts +1 -2
  213. package/dist/signin/layer.d.mts +1 -1
  214. package/dist/signin/layer.mjs +1 -1
  215. package/dist/storage/allotted.mjs +18 -3
  216. package/dist/storage/batch-upkeep.mjs +274 -0
  217. package/dist/storage/batches.d.mts +8 -0
  218. package/dist/storage/batches.mjs +344 -0
  219. package/dist/storage/calls.d.mts +1 -2
  220. package/dist/storage/calls.mjs +144 -158
  221. package/dist/storage/chunks.d.mts +9 -0
  222. package/dist/storage/chunks.mjs +297 -0
  223. package/dist/storage/clean-up.mjs +72 -9
  224. package/dist/storage/counted-bucket.mjs +4 -4
  225. package/dist/storage/fetch.d.mts +1 -0
  226. package/dist/storage/fetch.mjs +287 -210
  227. package/dist/storage/host-bucket.mjs +1 -2
  228. package/dist/storage/id-mix.mjs +19 -5
  229. package/dist/storage/index-bitmaps.mjs +9 -2
  230. package/dist/storage/index-build.d.mts +0 -1
  231. package/dist/storage/index-build.mjs +80 -23
  232. package/dist/storage/index-fetch.mjs +113 -72
  233. package/dist/storage/index-files.d.mts +3 -0
  234. package/dist/storage/index-files.mjs +10 -5
  235. package/dist/storage/index-graph.mjs +72 -22
  236. package/dist/storage/index-remap.mjs +200 -64
  237. package/dist/storage/index-whole.mjs +127 -0
  238. package/dist/storage/largest-files.mjs +1 -1
  239. package/dist/storage/layout.mjs +23 -0
  240. package/dist/storage/loose.d.mts +7 -4
  241. package/dist/storage/loose.mjs +212 -67
  242. package/dist/storage/memory-budget.mjs +16 -4
  243. package/dist/storage/merge-plan.mjs +3 -2
  244. package/dist/storage/merge-row.mjs +7 -1
  245. package/dist/storage/merge-stream.mjs +164 -25
  246. package/dist/storage/merge-walk.mjs +8 -4
  247. package/dist/storage/merge-write.mjs +1 -1
  248. package/dist/storage/merge.d.mts +1 -1
  249. package/dist/storage/merge.mjs +1 -1
  250. package/dist/storage/object-rows.d.mts +1 -0
  251. package/dist/storage/object-rows.mjs +385 -65
  252. package/dist/storage/oid-table.mjs +171 -0
  253. package/dist/storage/pack-order.mjs +8 -4
  254. package/dist/storage/pack-ranges.d.mts +1 -2
  255. package/dist/storage/pack-ranges.mjs +4 -5
  256. package/dist/storage/pack-reads.d.mts +2 -1
  257. package/dist/storage/pack-reads.mjs +158 -42
  258. package/dist/storage/pack-receive.d.mts +1 -1
  259. package/dist/storage/pack-receive.mjs +2 -2
  260. package/dist/storage/packs.d.mts +1 -1
  261. package/dist/storage/packs.mjs +136 -66
  262. package/dist/storage/placed-reads.mjs +1 -1
  263. package/dist/storage/plan-columns.d.mts +4 -0
  264. package/dist/storage/plan-columns.mjs +238 -0
  265. package/dist/storage/prune-keep.mjs +134 -41
  266. package/dist/storage/prune-walk.mjs +202 -62
  267. package/dist/storage/prune.mjs +194 -91
  268. package/dist/storage/push-allotment.mjs +91 -12
  269. package/dist/storage/push-uploads.mjs +4 -4
  270. package/dist/storage/pushed-commits.mjs +4 -2
  271. package/dist/storage/quarantine.d.mts +1 -1
  272. package/dist/storage/range-cache.mjs +125 -8
  273. package/dist/storage/ready.d.mts +1 -1
  274. package/dist/storage/ready.mjs +8 -6
  275. package/dist/storage/receive-bounds.mjs +23 -8
  276. package/dist/storage/repack-backlog.mjs +6 -3
  277. package/dist/storage/repack.d.mts +1 -1
  278. package/dist/storage/rows.d.mts +1 -1
  279. package/dist/storage/rows.mjs +1 -1
  280. package/dist/storage/runs.mjs +195 -43
  281. package/dist/storage/send-list.d.mts +1 -0
  282. package/dist/storage/send-list.mjs +54 -10
  283. package/dist/storage/single-flight.mjs +13 -7
  284. package/dist/storage/stream-whole.mjs +127 -26
  285. package/dist/storage/unpack.mjs +371 -79
  286. package/dist/storage/visible.mjs +2 -2
  287. package/dist/storage/walk.mjs +192 -38
  288. package/dist/storage/wants.mjs +5 -5
  289. package/dist/store/bodies.mjs +38 -0
  290. package/dist/store/call-count.d.mts +8 -12
  291. package/dist/store/call-count.mjs +34 -46
  292. package/dist/store/catalogue.d.mts +2 -2
  293. package/dist/store/catalogue.mjs +145 -35
  294. package/dist/store/crossing.mjs +1 -3
  295. package/dist/store/cursor-seal.d.mts +1 -2
  296. package/dist/store/cursor-seal.mjs +16 -6
  297. package/dist/store/door.mjs +183 -0
  298. package/dist/store/errors.d.mts +3 -4
  299. package/dist/store/errors.mjs +15 -33
  300. package/dist/store/git-store.mjs +25 -7
  301. package/dist/store/job-try.mjs +9 -32
  302. package/dist/store/list-job.mjs +44 -8
  303. package/dist/store/records.d.mts +3 -0
  304. package/dist/store/records.mjs +3 -0
  305. package/dist/store/repo-record.d.mts +1 -2
  306. package/dist/store/repo-record.mjs +32 -20
  307. package/dist/store/reservations.mjs +1 -1
  308. package/dist/store/scopes.mjs +11 -0
  309. package/dist/store/settle.mjs +4 -4
  310. package/dist/sync/github.mjs +3 -3
  311. package/dist/sync/import.mjs +31 -18
  312. package/dist/sync/lfs-batch.mjs +4 -5
  313. package/dist/sync/lfs-copy.mjs +17 -6
  314. package/dist/sync/mirror.mjs +34 -31
  315. package/dist/sync/said.mjs +16 -37
  316. package/dist/sync/step.mjs +33 -25
  317. package/dist/sync/tries.mjs +9 -9
  318. package/dist/topics/advance.mjs +10 -11
  319. package/dist/topics/base.d.mts +15 -0
  320. package/dist/topics/base.mjs +8 -0
  321. package/dist/topics/host.mjs +1 -1
  322. package/dist/topics/layer.d.mts +4 -9
  323. package/dist/topics/layer.mjs +2 -3
  324. package/dist/topics/members.mjs +4 -4
  325. package/dist/topics/public.d.mts +2 -1
  326. package/dist/topics/topic.mjs +2 -2
  327. package/package.json +5 -4
  328. package/dist/contracts/promises.d.mts +0 -39
  329. package/dist/core/call-names.d.mts +0 -3
  330. package/dist/core/clock.mjs +0 -18
  331. package/dist/lfs/calls.mjs +0 -26
  332. package/dist/rest/api.d.mts +0 -23
  333. package/dist/rest/blobs.d.mts +0 -833
  334. package/dist/rest/commits.d.mts +0 -3543
  335. package/dist/rest/errors.d.mts +0 -2124
  336. package/dist/rest/events.d.mts +0 -426
  337. package/dist/rest/files.d.mts +0 -1342
  338. package/dist/rest/forks.d.mts +0 -2160
  339. package/dist/rest/handlers.d.mts +0 -8
  340. package/dist/rest/merges.d.mts +0 -477
  341. package/dist/rest/queue.d.mts +0 -5917
  342. package/dist/rest/refs.d.mts +0 -2988
  343. package/dist/rest/repos.d.mts +0 -2567
  344. package/dist/rest/tags.d.mts +0 -1316
  345. package/dist/rest/tokens.d.mts +0 -2614
  346. package/dist/rest/topics.d.mts +0 -2224
  347. package/dist/rest/trees.d.mts +0 -1709
  348. package/dist/rest/work.d.mts +0 -1695
  349. package/dist/server/lifecycle.d.mts +0 -2
  350. package/dist/store/promises.d.mts +0 -11
  351. package/dist/store/promises.mjs +0 -327
@@ -1,10 +1,9 @@
1
1
  import "../contracts/git.mjs";
2
- import "../contracts/promises.mjs";
3
2
  import "../contracts/result.mjs";
4
3
  import "../contracts/signin.mjs";
5
4
  import "../auth/tokens.mjs";
6
5
  import "./grants.mjs";
7
6
  import "effect/Effect";
8
- import { Moment } from "@ref0/infra";
7
+ import { Moment, Records } from "@ref0/infra";
9
8
  /** `gh`'s OAuth client ID, which the device flow takes beside the registered clients. */
10
9
  export declare const ghClientId = "178c6fc778ccc68e1d6a";
@@ -1,4 +1,4 @@
1
- import { TokensBase } from "../auth/tokens-layer.mjs";
1
+ import { TokensBase } from "../auth/tokens-base.mjs";
2
2
  import { Git } from "../contracts/git.mjs";
3
3
  import { GitError } from "../contracts/result.mjs";
4
4
  import { SignInLayerOptions } from "../contracts/signin.mjs";
@@ -1,4 +1,4 @@
1
- import { TokensBase } from "../auth/tokens-layer.mjs";
1
+ import { TokensBase } from "../auth/tokens-base.mjs";
2
2
  import { register } from "../contracts/git.mjs";
3
3
  import { Tokens } from "../contracts/tokens.mjs";
4
4
  import { paths } from "./route.mjs";
@@ -89,14 +89,29 @@ const keptOrLeft = (answer, object) => {
89
89
  /**
90
90
  * `ObjectReader.objects` over objects read whole by `each`, as rows are: `oids[0]` read and
91
91
  * charged first, then the rest, each charged as `each` hands it on, before the next is read, and
92
- * none kept past the allotment's room. Repeated IDs are read once.
92
+ * none kept past the allotment's room. Repeated IDs are read once. An object `each` keeps as
93
+ * chunks is charged by its size before its first chunk is read, through the `BeforeChunked` it's
94
+ * given: kept once granted, or left unread.
93
95
  */
94
96
  const allottedRows = (each, oids, allotment) => allotted(allotment, (answer) => Effect.gen(function* () {
95
97
  const asked = [...new Set(oids)];
96
98
  const [first, ...rest] = asked;
97
99
  if (first === void 0) return answer.read(asked);
98
- yield* each([first], (object) => keptOrLeft(answer, object));
99
- if (rest.length > 0) yield* each(rest, (object) => keptOrLeft(answer, object));
100
+ const charged = /* @__PURE__ */ new Set();
101
+ const before = (oid, size) => {
102
+ const settle = (given) => {
103
+ if (given === "kept") charged.add(oid);
104
+ else if (given === "left") answer.leave(oid);
105
+ else return Effect.fail(given.refused);
106
+ return Effect.succeed(given === "kept");
107
+ };
108
+ const given = answer.granted(size);
109
+ return Effect.isEffect(given) ? Effect.flatMap(given, settle) : settle(given);
110
+ };
111
+ const reads = each(before);
112
+ const handed = (object) => charged.has(object.oid) ? Effect.sync(() => answer.keep(object)) : keptOrLeft(answer, object);
113
+ yield* reads([first], handed);
114
+ if (rest.length > 0) yield* reads(rest, handed);
100
115
  return answer.read(asked);
101
116
  }));
102
117
  export { AllottedObjects, allotted, allottedRows, keptOrLeft };
@@ -0,0 +1,274 @@
1
+ import { bestEffort, decodeRow } from "../store/errors.mjs";
2
+ import { rowBytes } from "../store/outbox.mjs";
3
+ import { LooseTally } from "../store/records.mjs";
4
+ import { looseRows } from "../store/repo-record.mjs";
5
+ import { looseKey, newChangeId, repoScope } from "../store/scopes.mjs";
6
+ import { batchKey, batchQuarantineKey, batchesOf, batchesRead, isBatchId, stagedOid, stagedPrefix, stagedSizeKey, storeSkew } from "./batches.mjs";
7
+ import { chunkKeys, chunkedMoved, chunkedOf, chunksPrefix } from "./chunks.mjs";
8
+ import { objectCollections } from "./layout.mjs";
9
+ import { heldIdsAt, largeBatch, noRows, objectKey, tooLargeRead } from "./object-rows.mjs";
10
+ import { QuarantineRow } from "./rows.mjs";
11
+ import * as Effect from "effect/Effect";
12
+ import { limits } from "@ref0/infra";
13
+ // src/storage/batch-upkeep.ts
14
+ /** The batch quarantine rows one upkeep step reads at most: past it, the next step reads the rest. */
15
+ const quarantinesMost = 64;
16
+ /** A step that changes nothing and asks for the next at once: the repo moved under its reads. */
17
+ const readAgain = {
18
+ change: { rows: noRows },
19
+ more: true
20
+ };
21
+ /** One page of a batch's staged rows, `rowReadBatch` or, past 16 MiB, `largeBatch`, with `also`. */
22
+ const stagedPage = (store, repo, batch, also = []) => {
23
+ const read = (limit) => Effect.map(store.read({
24
+ scope: repoScope(repo),
25
+ reads: [{
26
+ collection: objectCollections.stagedObjects,
27
+ prefix: stagedPrefix(batch),
28
+ limit
29
+ }, ...also]
30
+ }), (snapshot) => ({
31
+ snapshot,
32
+ rows: snapshot.results[0] ?? []
33
+ }));
34
+ return read(100).pipe(Effect.catchIf(tooLargeRead, () => read(largeBatch)));
35
+ };
36
+ /** One page of an abandoned batch's rows in `collection`, under `prefix`. */
37
+ const prefixPage = (store, repo, collection, prefix, limit) => Effect.map(store.read({
38
+ scope: repoScope(repo),
39
+ reads: [{
40
+ collection,
41
+ prefix,
42
+ limit
43
+ }]
44
+ }), (snapshot) => ({
45
+ snapshot,
46
+ rows: snapshot.results[0] ?? []
47
+ }));
48
+ /** One page of an abandoned batch's chunks: `largeBatch` of them, each about a row's 1 MiB. */
49
+ const chunksPage = (store, repo, batch) => prefixPage(store, repo, objectCollections.objectChunks, chunksPrefix(batch), largeBatch);
50
+ /** The size markers one sweep step reads at most: ~80 B each, ~150 KiB per page, well under 10k writes. */
51
+ const sweepSizesLimit = 1e3;
52
+ /** One page of an abandoned batch's size markers. */
53
+ const sizesPage = (store, repo, batch) => prefixPage(store, repo, objectCollections.stagedSizes, stagedPrefix(batch), sweepSizesLimit);
54
+ /**
55
+ * The writes a staged row may take of a promotion commit: its put (or inline duplicate), staged
56
+ * row delete, size marker delete, and chunks' deletes when a chunked object is a duplicate.
57
+ */
58
+ const promotedWrites = (row) => 3 + (chunkedOf(row.value)?.chunks ?? 0);
59
+ /**
60
+ * A staged row's share of a promotion commit, as JSON: its value once, put in `objects`, and its
61
+ * keys — the put's, the staged row's delete, its size marker's delete, and each of its chunks'
62
+ * deletes when a chunked object is a duplicate.
63
+ */
64
+ const promotedBytes = (row) => rowBytes(row) + promotedWrites(row) * (row.id.length + 128);
65
+ /** `rows`' first rows whose puts and deletes fit `promotionBytes` and one commit's writes: always the first. */
66
+ const promotable = (rows) => {
67
+ let bytes = 0;
68
+ let writes = 0;
69
+ let taken = 0;
70
+ for (const row of rows) {
71
+ bytes += promotedBytes(row);
72
+ writes += promotedWrites(row);
73
+ if (taken > 0 && (bytes > 6291456 || writes > limits.writes / 2)) break;
74
+ taken += 1;
75
+ }
76
+ return rows.slice(0, taken);
77
+ };
78
+ /**
79
+ * One page of promoting `batch`, read at one version: each staged row put into `objects` and
80
+ * deleted from the batch, or, for an object `objects` holds at that version already, deleted alone,
81
+ * its row and bytes taken off the tally. A page that finds the batch empty deletes its `batches`
82
+ * row. A page holds as many of the rows read as fit `promotionBytes`, the rest left to the next.
83
+ * `undefined` once the repo moved between its two reads, since a change from rows read at another
84
+ * version could take a duplicate off the tally twice.
85
+ */
86
+ const promotionPage = /* @__PURE__ */ Effect.fnUntraced(function* (store, repo, batch, also = []) {
87
+ const { snapshot, rows: read } = yield* stagedPage(store, repo, batch, also);
88
+ const rows = promotable(read);
89
+ const oids = rows.map(({ id }) => stagedOid(id));
90
+ const held = yield* heldIdsAt(store, repo, snapshot.version, oids);
91
+ if (held === void 0) return void 0;
92
+ const put = [];
93
+ const deleted = rows.flatMap(({ collection, id }) => [{
94
+ collection,
95
+ id
96
+ }, stagedSizeKey(batch, stagedOid(id))]);
97
+ const dropped = {
98
+ objects: 0,
99
+ bytes: 0
100
+ };
101
+ for (const row of rows) {
102
+ const oid = stagedOid(row.id);
103
+ const chunked = chunkedOf(row.value);
104
+ if (held.has(oid) && chunked !== void 0) {
105
+ const counted = chunkedMoved(chunked);
106
+ dropped.objects -= counted.objects;
107
+ dropped.bytes -= counted.bytes;
108
+ deleted.push(...chunkKeys(chunked));
109
+ } else if (held.has(oid)) {
110
+ dropped.objects -= 1;
111
+ dropped.bytes -= rowBytes(row);
112
+ } else put.push({
113
+ ...objectKey(oid),
114
+ value: row.value
115
+ });
116
+ }
117
+ if (rows.length === 0) deleted.push(batchKey(batch));
118
+ return {
119
+ change: {
120
+ put,
121
+ delete: deleted,
122
+ rows: dropped
123
+ },
124
+ snapshot,
125
+ done: rows.length === 0
126
+ };
127
+ });
128
+ /**
129
+ * What upkeep does for a repo's staged batches, at `listed`'s version, the version the core commits
130
+ * the step at: a step, or, when no batch needs one, when the next is due. In order:
131
+ * - every open batch past its `removeAt` is committed `abandoned`, its `removeAt` kept, which
132
+ * refuses a late publication, as `finalRead` reads the push's own row;
133
+ * - the first abandoned batch with staged rows has a page of them deleted; one found empty by a read
134
+ * at `removeAt` plus `storeSkew` or later has its quarantine row deleted, since every staging add
135
+ * carries a `by` no later than `removeAt`, which the store checks on its own clock, so none lands
136
+ * after that read. One found empty sooner is due then;
137
+ * - the oldest published batch has a page promoted.
138
+ * With none of these, it answers when the next is due: an open batch's `removeAt`, or an empty
139
+ * abandoned batch's sweep end.
140
+ * While a batch is published it always answers a step, so a step after it, a prune's or a merge's,
141
+ * runs only at a version that lists none: no row a reader may find in a batch leaves `objects`.
142
+ */
143
+ const maintainBatches = /* @__PURE__ */ Effect.fnUntraced(function* (store, listed, at) {
144
+ const { repo, version } = listed;
145
+ const first = yield* store.read({
146
+ scope: repoScope(repo),
147
+ reads: [{
148
+ collection: objectCollections.batchQuarantine,
149
+ limit: quarantinesMost
150
+ }, batchesRead]
151
+ });
152
+ const [quarantineRows = [], batchRows = []] = first.results;
153
+ if (quarantineRows.length === 0 && batchRows.length === 0) return {};
154
+ if (first.version !== version) return { step: readAgain };
155
+ const quarantined = [];
156
+ for (const row of quarantineRows) {
157
+ if (!isBatchId(row.id)) continue;
158
+ const value = yield* decodeRow(QuarantineRow, row, `The batch's quarantine row '${row.id}'`);
159
+ quarantined.push({
160
+ batch: row.id,
161
+ value
162
+ });
163
+ }
164
+ const published = yield* batchesOf(batchRows);
165
+ const expired = quarantined.filter(({ value }) => value.state === "open" && value.removeAt <= at);
166
+ if (expired.length > 0) {
167
+ const put = expired.map(({ batch, value }) => ({
168
+ ...batchQuarantineKey(batch),
169
+ value: {
170
+ removeAt: value.removeAt,
171
+ state: "abandoned"
172
+ }
173
+ }));
174
+ const due = Math.min(...expired.map(({ value }) => value.removeAt + storeSkew));
175
+ return { step: {
176
+ change: {
177
+ put,
178
+ rows: noRows
179
+ },
180
+ more: true,
181
+ due
182
+ } };
183
+ }
184
+ const opens = quarantined.filter(({ value }) => value.state === "open");
185
+ let due = opens.length === 0 ? void 0 : Math.min(...opens.map(({ value }) => value.removeAt));
186
+ for (const { batch, value } of quarantined.filter(({ value }) => value.state === "abandoned")) {
187
+ for (const page of [
188
+ stagedPage,
189
+ chunksPage,
190
+ sizesPage
191
+ ]) {
192
+ const { snapshot, rows } = yield* page(store, repo, batch);
193
+ if (snapshot.version !== version) return { step: readAgain };
194
+ if (rows.length > 0) return { step: {
195
+ change: {
196
+ delete: rows.map(({ collection, id }) => ({
197
+ collection,
198
+ id
199
+ })),
200
+ rows: noRows
201
+ },
202
+ more: true
203
+ } };
204
+ }
205
+ const endsAt = value.removeAt + storeSkew;
206
+ if (at >= endsAt) return { step: {
207
+ change: {
208
+ delete: [batchQuarantineKey(batch)],
209
+ rows: noRows
210
+ },
211
+ more: true
212
+ } };
213
+ due = due === void 0 ? endsAt : Math.min(due, endsAt);
214
+ }
215
+ const oldest = published[0];
216
+ if (oldest === void 0) {
217
+ const later = {};
218
+ if (due !== void 0) later.due = due;
219
+ if (quarantined.length >= quarantinesMost) later.step = readAgain;
220
+ return later;
221
+ }
222
+ const page = yield* promotionPage(store, repo, oldest.batch);
223
+ if (page === void 0 || page.snapshot.version !== version) return { step: readAgain };
224
+ return { step: {
225
+ change: page.change,
226
+ more: true
227
+ } };
228
+ });
229
+ /**
230
+ * `step` with `due` joined to its own, the sooner kept, as a job keeps only its earliest time: a
231
+ * step of its own when there's none.
232
+ */
233
+ const withBatchesDue = (step, due) => {
234
+ if (due === void 0) return step;
235
+ if (step === void 0) return {
236
+ change: { rows: noRows },
237
+ more: false,
238
+ due
239
+ };
240
+ return {
241
+ ...step,
242
+ due: step.due === void 0 ? due : Math.min(step.due, due)
243
+ };
244
+ };
245
+ /** `loose()`'s upkeep: its staged batches', as `maintainBatches` says, and nothing else. */
246
+ const looseUpkeep = (store, listed, at) => Effect.map(maintainBatches(store, listed, at), ({ step, due }) => step ?? withBatchesDue(void 0, due));
247
+ /**
248
+ * An accepted push's own batch promoted, at most `pages` pages, each a commit of its own at the
249
+ * version it read, with the tally moved as upkeep's would: a page another write beat is read again,
250
+ * and counts. The push's commit saved `git.repack`, which promotes the rest; a failure is dropped,
251
+ * since the push has landed.
252
+ */
253
+ const promoteOwn = (store, repo, batch, pages) => bestEffort(Effect.gen(function* () {
254
+ const scope = repoScope(repo);
255
+ for (let page = 0; page < pages; page++) {
256
+ const read = yield* promotionPage(store, repo, batch, [looseKey, batchKey(batch)]);
257
+ if (read === void 0) continue;
258
+ const [, tallyRows = [], batchRows = []] = read.snapshot.results;
259
+ if (batchRows.length === 0) return;
260
+ const tallyRow = tallyRows[0];
261
+ const tally = tallyRow === void 0 ? void 0 : yield* decodeRow(LooseTally, tallyRow, "The repo's object rows' tally");
262
+ const change = {
263
+ id: newChangeId(),
264
+ expect: read.snapshot.version,
265
+ put: [...read.change.put ?? [], ...looseRows(tally, read.change.rows)],
266
+ delete: [...read.change.delete ?? []]
267
+ };
268
+ if ((yield* store.commit({
269
+ scope,
270
+ change
271
+ })).ok && read.done) return;
272
+ }
273
+ }));
274
+ export { looseUpkeep, maintainBatches, promoteOwn, withBatchesDue };
@@ -0,0 +1,8 @@
1
+ import "../contracts/ids.mjs";
2
+ import "../contracts/objects.mjs";
3
+ import "../contracts/result.mjs";
4
+ import "../contracts/services.mjs";
5
+ import "../contracts/store.mjs";
6
+ import "effect/Effect";
7
+ import "effect/Schema";
8
+ import { Key, Moment, Read, Row } from "@ref0/infra";
@@ -0,0 +1,344 @@
1
+ import { fail, gitError } from "../contracts/result.mjs";
2
+ import { bestEffort, decodeRow } from "../store/errors.mjs";
3
+ import { rowBytes } from "../store/outbox.mjs";
4
+ import { randomText } from "../store/random.mjs";
5
+ import { heldAddition } from "../store/repo-record.mjs";
6
+ import { newChangeId, repoScope } from "../store/scopes.mjs";
7
+ import { objectCollections } from "./layout.mjs";
8
+ import { repackJobRow } from "./repack-job.mjs";
9
+ import { QuarantineRow, removeAt } from "./rows.mjs";
10
+ import * as Clock from "effect/Clock";
11
+ import * as Effect from "effect/Effect";
12
+ import * as Schema from "effect/Schema";
13
+ import { jobLeaseMargin, limits } from "@ref0/infra";
14
+ // src/storage/batches.ts
15
+ const newBatchId = () => `b_${randomText(12)}`;
16
+ const isBatchId = (text) => /^b_[A-Za-z0-9_-]{16}$/.test(text);
17
+ /**
18
+ * The rows and bytes of JSON a push's own commit holds as rows: past either, its rows are
19
+ * staged as a batch, so its commit keeps room for its refs, events and jobs.
20
+ */
21
+ const fastPath = {
22
+ rows: 5e3,
23
+ bytes: 6291456
24
+ };
25
+ /**
26
+ * The bytes of JSON one promotion page puts into `objects` and deletes from its batch at most: a
27
+ * batch holds up to a commit's 8 MiB of rows, so a page of all of them, put and deleted, wouldn't
28
+ * fit one. The rest of the commit's 8 MiB is room for the tally and the call's own JSON.
29
+ */
30
+ const promotionBytes = 6291456;
31
+ /**
32
+ * How far the store's clock may run from Git's, each way: the quarter of `jobLeaseMargin` that
33
+ * `deleteCommitWithin`'s TSDoc gives it. A sweep's end waits it past a batch's `removeAt`.
34
+ */
35
+ const storeSkew = jobLeaseMargin / 4;
36
+ /** A published batch: its physical rows, their value bytes, and when its push published it. */
37
+ const BatchRow = /* @__PURE__ */ Schema.Struct({
38
+ objects: Schema.Int,
39
+ bytes: Schema.Int,
40
+ at: Schema.Int
41
+ });
42
+ const batchKey = (batch) => ({
43
+ collection: objectCollections.batches,
44
+ id: batch
45
+ });
46
+ const batchQuarantineKey = (batch) => ({
47
+ collection: objectCollections.batchQuarantine,
48
+ id: batch
49
+ });
50
+ /** A batch's rows, under its own prefix of `stagedObjects`. */
51
+ const stagedPrefix = (batch) => `${batch}/`;
52
+ const stagedKey = (batch, oid) => ({
53
+ collection: objectCollections.stagedObjects,
54
+ id: `${stagedPrefix(batch)}${oid}`
55
+ });
56
+ /** A staged object row's value size as JSON bytes, kept under `stagedSizes/<batch>/<oid>`. */
57
+ const SizeMarker = /* @__PURE__ */ Schema.Struct({ bytes: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)) });
58
+ const stagedSizeKey = (batch, oid) => ({
59
+ collection: objectCollections.stagedSizes,
60
+ id: `${stagedPrefix(batch)}${oid}`
61
+ });
62
+ /** The size marker written beside `staged` in the same staging add. */
63
+ const stagedSizeRow = (batch, oid, staged) => ({
64
+ ...stagedSizeKey(batch, oid),
65
+ value: { bytes: rowBytes(staged) }
66
+ });
67
+ /** The object a staged row's ID names, after its batch's prefix. */
68
+ const stagedOid = (id) => id.slice(id.indexOf("/") + 1);
69
+ /** The read of a repo's published batches: one past `batchesMost`, so a longer list shows. */
70
+ const batchesRead = {
71
+ collection: objectCollections.batches,
72
+ limit: 17
73
+ };
74
+ /** The batches a `batchesRead` answered, oldest published first, as checked IDs. */
75
+ const batchesOf = /* @__PURE__ */ Effect.fnUntraced(function* (rows) {
76
+ const listed = [];
77
+ for (const row of rows) {
78
+ if (!isBatchId(row.id)) return yield* fail("DATA_LOSS", `The batch row '${row.id}' names no batch`);
79
+ listed.push({
80
+ batch: row.id,
81
+ row: yield* decodeRow(BatchRow, row, `The batch row '${row.id}'`)
82
+ });
83
+ }
84
+ return listed.toSorted((a, b) => a.row.at - b.row.at || (a.batch < b.batch ? -1 : 1));
85
+ });
86
+ /** The refusal of a push that would publish a batch past `most`, with `batches` waiting. */
87
+ const awaitingUpkeep = (batches, most) => gitError("UNAVAILABLE", `The repo has ${batches} large pushes waiting to be stored; it holds at most ${most}`, {
88
+ hint: "Push again in a few minutes, once upkeep has stored them",
89
+ context: {
90
+ reason: "batches-awaiting-upkeep",
91
+ batches,
92
+ most
93
+ }
94
+ });
95
+ /**
96
+ * A new batch's quarantine row, added outside the version with `git.repack` due at its
97
+ * `removeAt`, after a read that found fewer than `batchesMost` published: a push past it is refused
98
+ * before it stages a row. The cap holds at the push's commit, which reads them again; this read
99
+ * only spares a push that can't land its staging.
100
+ */
101
+ const openBatch = /* @__PURE__ */ Effect.fnUntraced(function* (input) {
102
+ const { store, repo, at } = input;
103
+ const scope = repoScope(repo);
104
+ const listed = (yield* store.read({
105
+ scope,
106
+ reads: [batchesRead]
107
+ })).results[0] ?? [];
108
+ if (listed.length >= 16) return yield* Effect.fail(awaitingUpkeep(listed.length, 16));
109
+ const batch = newBatchId();
110
+ const due = input.removeAt ?? removeAt(at, "14 days");
111
+ const opening = {
112
+ put: [{
113
+ ...batchQuarantineKey(batch),
114
+ value: {
115
+ removeAt: due,
116
+ state: "open"
117
+ }
118
+ }],
119
+ jobs: [repackJobRow(due)]
120
+ };
121
+ const by = input.removeAt === void 0 ? opening : {
122
+ ...opening,
123
+ by: input.removeAt
124
+ };
125
+ const addition = yield* heldAddition(`The push's batch for repo ${repo}`, by);
126
+ yield* store.add({
127
+ scope,
128
+ addition
129
+ });
130
+ return {
131
+ batch,
132
+ quarantine: batchQuarantineKey(batch),
133
+ removeAt: due
134
+ };
135
+ });
136
+ /** The bytes one staging add carries at most, under a store call's 8 MiB with room for its keys. */
137
+ const stageBytes = fastPath.bytes;
138
+ const batchStager = (store, repo, opened) => {
139
+ const scope = repoScope(repo);
140
+ let put = [];
141
+ let bytes = 0;
142
+ const flush = Effect.fnUntraced(function* () {
143
+ if (put.length === 0) return;
144
+ const addition = yield* heldAddition(`The push's batch for repo ${repo}`, {
145
+ put,
146
+ by: opened.removeAt
147
+ });
148
+ put = [];
149
+ bytes = 0;
150
+ yield* store.add({
151
+ scope,
152
+ addition
153
+ });
154
+ });
155
+ return {
156
+ push: Effect.fnUntraced(function* (row) {
157
+ const staged = {
158
+ ...stagedKey(opened.batch, row.id),
159
+ value: row.value
160
+ };
161
+ const marker = stagedSizeRow(opened.batch, row.id, staged);
162
+ const pairBytes = stagedBytes(staged) + stagedBytes(marker);
163
+ if (put.length > 0 && (put.length + 2 > limits.writes || bytes + pairBytes > stageBytes)) yield* flush();
164
+ put.push(staged, marker);
165
+ bytes += pairBytes;
166
+ }),
167
+ flush
168
+ };
169
+ };
170
+ /**
171
+ * `rows`, each an `objects` row, added under `opened`'s prefix of `stagedObjects` beside its
172
+ * `stagedSizes` marker as an indivisible pair, a store add of at most `limits.writes` rows and
173
+ * `stageBytes` at a time, in order. Each add carries the batch's `removeAt` as its `by`, or the
174
+ * push's own `HeldBy` when that is sooner, so the store refuses one that lands past it, and a
175
+ * sweep that starts later finds every row that will ever land. An add whose outcome is unknown
176
+ * fails the receive, so the batch is never published.
177
+ */
178
+ const stageRows = /* @__PURE__ */ Effect.fnUntraced(function* (store, repo, opened, rows) {
179
+ const stager = batchStager(store, repo, opened);
180
+ for (const row of rows) yield* stager.push(row);
181
+ yield* stager.flush();
182
+ });
183
+ /** A sample batch ID of `newBatchId()`'s exact length, for sizing `stageRows` calls up front. */
184
+ const sampleBatchId = "b_0000000000000000";
185
+ const makeStageRowsCounter = (initialCalls, initialPut, initialBytes) => {
186
+ let calls = initialCalls;
187
+ let put = initialPut;
188
+ let bytes = initialBytes;
189
+ return {
190
+ add: (row) => {
191
+ const staged = {
192
+ ...stagedKey(sampleBatchId, row.id),
193
+ value: row.value
194
+ };
195
+ const pairBytes = stagedBytes(staged) + stagedBytes(stagedSizeRow(sampleBatchId, row.id, staged));
196
+ if (put > 0 && (put + 2 > limits.writes || bytes + pairBytes > stageBytes)) {
197
+ calls += 1;
198
+ put = 0;
199
+ bytes = 0;
200
+ }
201
+ put += 2;
202
+ bytes += pairBytes;
203
+ },
204
+ calls: () => put > 0 ? calls + 1 : calls,
205
+ clone: () => makeStageRowsCounter(calls, put, bytes)
206
+ };
207
+ };
208
+ /**
209
+ * An incremental counter of the exact `store.add` calls `stageRows` makes for rows added so far,
210
+ * matching `batchStager`'s paired `[staged, marker]` `limits.writes` and `stageBytes` cuts.
211
+ */
212
+ const stageRowsCounter = () => makeStageRowsCounter(0, 0, 0);
213
+ /**
214
+ * The exact number of `store.add` calls `stageRows` makes for `rows`: 0 when empty, otherwise
215
+ * matching `batchStager`'s paired `[staged, marker]` `limits.writes` and `stageBytes` cuts.
216
+ */
217
+ const stageRowsCalls = (rows) => {
218
+ const counter = stageRowsCounter();
219
+ for (const row of rows) counter.add(row);
220
+ return counter.calls();
221
+ };
222
+ /** How many times an abandon tries its commit as other commits move the repo. */
223
+ const abandonTries = 3;
224
+ /**
225
+ * A batch whose push ends with no commit that published it, committed `abandoned` at its own read's
226
+ * version while its row is still `open`, keeping its `removeAt`, with `git.repack` due when the
227
+ * sweep may end it: a staging add in flight lands by `removeAt`, so ending it sooner could leave
228
+ * rows behind. A publication that landed, one whose answer was lost too, has taken the row, and
229
+ * one still on its way is refused by the version this moves. Other commits that keep moving the
230
+ * repo, past `abandonTries`, or a failure, leave the row for upkeep to abandon at its `removeAt`;
231
+ * its own failure is dropped.
232
+ */
233
+ const abandonBatch = (store, repo, batch) => bestEffort(Effect.gen(function* () {
234
+ const scope = repoScope(repo);
235
+ const key = batchQuarantineKey(batch);
236
+ for (let tried = 0; tried < abandonTries; tried++) {
237
+ const snapshot = yield* store.read({
238
+ scope,
239
+ reads: [key]
240
+ });
241
+ const row = snapshot.results[0]?.[0];
242
+ if (row === void 0) return;
243
+ const held = yield* decodeRow(QuarantineRow, row, `The batch's quarantine row '${batch}'`);
244
+ if (held.state !== "open") return;
245
+ const change = {
246
+ id: newChangeId(),
247
+ expect: snapshot.version,
248
+ put: [{
249
+ ...key,
250
+ value: {
251
+ removeAt: held.removeAt,
252
+ state: "abandoned"
253
+ }
254
+ }],
255
+ jobs: [repackJobRow(held.removeAt + storeSkew)]
256
+ };
257
+ if ((yield* store.commit({
258
+ scope,
259
+ change
260
+ })).ok) return;
261
+ }
262
+ }));
263
+ /**
264
+ * The rows each staged push's change holds for its own reads before its commit, by object ID, as
265
+ * `objects` rows: a staged push's change puts only its batch's row, its objects staged apart.
266
+ * Keyed by the change, which a wrapper that passes the receive's answer on keeps.
267
+ */
268
+ const ownRows = /* @__PURE__ */ new WeakMap();
269
+ /**
270
+ * The change that publishes `opened`: its `batches` row, and `moved`, its rows as the tally counts
271
+ * them, each physical row once. `rows`, the push's own, are kept for its reads before its commit.
272
+ */
273
+ const publication = (opened, rows, moved, at) => {
274
+ const value = {
275
+ objects: moved.objects,
276
+ bytes: moved.bytes,
277
+ at
278
+ };
279
+ const change = {
280
+ put: [{
281
+ ...batchKey(opened.batch),
282
+ value
283
+ }],
284
+ rows: moved
285
+ };
286
+ ownRows.set(change, new Map(rows.map((row) => [row.id, row])));
287
+ return change;
288
+ };
289
+ /** The `objects` rows a staged push's change keeps for its own reads: none for any other change. */
290
+ const stagedRowsOf = (change) => change === void 0 ? void 0 : ownRows.get(change);
291
+ /** What one row takes of a commit's bytes, as `fastPath.bytes` and a staging add count it. */
292
+ const stagedBytes = (row) => rowBytes(row) + row.id.length + 64;
293
+ /** Whether a receive's rows go to a batch: past one commit's `fastPath`. */
294
+ const isStaged = (rows) => rows.length > fastPath.rows || rows.reduce((sum, row) => sum + stagedBytes(row), 0) > fastPath.bytes;
295
+ /**
296
+ * `opened`'s quarantine row abandoned, keeping its `removeAt`, for a commit that publishes another
297
+ * batch in its place: upkeep sweeps its rows, and ends it only past `removeAt` and the skew.
298
+ */
299
+ const abandonedRow = (opened) => ({
300
+ ...opened.quarantine,
301
+ value: {
302
+ removeAt: opened.removeAt,
303
+ state: "abandoned"
304
+ }
305
+ });
306
+ /**
307
+ * A receive's `objects` rows as its push's change: the rows themselves within `fastPath`, or past
308
+ * it, staged as a new batch the change publishes, under the quarantine it names. A staging that
309
+ * fails or is interrupted abandons its batch before it answers. A receive that staged `chunks`
310
+ * already stages its rows beside them, in that batch, whatever their size; its caller abandons it.
311
+ */
312
+ const rowsChange = /* @__PURE__ */ Effect.fnUntraced(function* (store, repo, rows, moved, chunks) {
313
+ if (chunks !== void 0) {
314
+ yield* stageRows(store, repo, chunks, rows);
315
+ return {
316
+ change: publication(chunks, rows, moved, yield* Clock.currentTimeMillis),
317
+ quarantine: chunks.quarantine
318
+ };
319
+ }
320
+ if (!isStaged(rows)) return { change: {
321
+ put: [...rows],
322
+ rows: moved
323
+ } };
324
+ const at = yield* Clock.currentTimeMillis;
325
+ const opened = yield* openBatch({
326
+ store,
327
+ repo,
328
+ at
329
+ });
330
+ yield* stageRows(store, repo, opened, rows).pipe(Effect.onError(() => abandonBatch(store, repo, opened.batch)));
331
+ return {
332
+ change: publication(opened, rows, moved, at),
333
+ quarantine: opened.quarantine
334
+ };
335
+ });
336
+ /** The batch a receive staged, which its push's commit publishes: none for any other receive. */
337
+ const stagedBatchOf = (received) => {
338
+ const key = received?.quarantine;
339
+ if (key?.collection !== objectCollections.batchQuarantine) return void 0;
340
+ return isBatchId(key.id) ? key.id : void 0;
341
+ };
342
+ /** Whether a receive staged a batch, so its push's commit holds the repo to `batchesMost`. */
343
+ const isStagedReceive = (received) => stagedBatchOf(received) !== void 0;
344
+ export { BatchRow, SizeMarker, abandonBatch, abandonedRow, awaitingUpkeep, batchKey, batchQuarantineKey, batchStager, batchesOf, batchesRead, fastPath, isBatchId, isStaged, isStagedReceive, newBatchId, openBatch, promotionBytes, publication, rowsChange, stageBytes, stageRows, stageRowsCalls, stageRowsCounter, stagedBatchOf, stagedBytes, stagedKey, stagedOid, stagedPrefix, stagedRowsOf, stagedSizeKey, stagedSizeRow, storeSkew };