diff --git a/modules/mesh-delivery-sdk/parts/receive.go b/modules/mesh-delivery-sdk/parts/receive.go index d2e9d8c48..fa81503d9 100644 --- a/modules/mesh-delivery-sdk/parts/receive.go +++ b/modules/mesh-delivery-sdk/parts/receive.go @@ -128,6 +128,10 @@ type Receiver[M any] struct { Full uint64 // Holds says the holder already holds a part, which then counts as held at the open and is not sent. Optional. Holds func(t Transfer[M], digest string) bool + // HoldsWhole says the holder holds the file itself the transfer brings (its manifest or reference at the name and + // version its shape names), never from its parts alone (design 47 H2 H-A-481 second addendum): a reopen that lacks + // nothing is then dropped without a second store. Optional: nil never drops, and the reopen finishes as usual. + HoldsWhole func(t Transfer[M]) bool // Check is the caller's check of a whole transfer before anything is stored; its error is the refusal's reason. // Optional. Check func(t Transfer[M]) error @@ -195,7 +199,7 @@ func (r *Receiver[M]) fullPct() uint64 { // goes on from there. A transfer is finished or taken over, never left open (design 47 H2 H-A-481 and its addendum): // one opened again with nothing lacked (its finish was cut) is finished, by its opener or by another caller with the // same digest, once each part taken in it is read back and hashes to its digest (a part that does not is lacked again), -// and dropped without a second store when the holder already holds every part; one that lacks a part is another +// and dropped without a second store when the holder already holds the file itself (HoldsWhole); one that lacks a part is another // caller's only once its last part is older than TakeOver, and is refused to it before. A new transfer that would pass the cap is refused; parts the holder holds count as held; when nothing is // lacked the transfer ends at once, its idle bound armed before the finish, so a cut finish never leaves it open. func (r *Receiver[M]) Open(id, opener string, parts []Want, shape M) (Received, error) { @@ -273,17 +277,11 @@ func (r *Receiver[M]) Open(id, opener string, parts []Want, shape M) (Received, return Received{ID: id, Lacks: t.Lacks()}, r.arm(id) } -// heldWhole says the holder already holds every part of t (Holds), so a finish cut after its store stores nothing again. +// heldWhole says the holder already holds the file t brings (HoldsWhole), so a finish cut after its store stores nothing +// again; parts held alone never say it (H-A-481 second addendum), since a publish cut before its store must still be +// said as published or refused. func (r *Receiver[M]) heldWhole(t Transfer[M]) bool { - if r.Holds == nil { - return false - } - for _, p := range t.Parts { - if !r.Holds(t, p.Digest) { - return false - } - } - return true + return r.HoldsWhole != nil && r.HoldsWhole(t) } // verify reads each part taken in t back from the objects and hashes it against its digest, as Take does at a part's diff --git a/modules/mesh-delivery-sdk/parts/receive_test.go b/modules/mesh-delivery-sdk/parts/receive_test.go index a023c63aa..7ef3c9de5 100644 --- a/modules/mesh-delivery-sdk/parts/receive_test.go +++ b/modules/mesh-delivery-sdk/parts/receive_test.go @@ -297,7 +297,7 @@ func aHolderTakesAFileInParts(t *testing.T) { var order []string pkg.Arm = func(id string, _ time.Duration) error { order = append(order, "arm "+id); return nil } store := pkg.Store - defer func() { pkg.Arm, pkg.Holds, pkg.Store = nil, nil, store }() + defer func() { pkg.Arm, pkg.Holds, pkg.HoldsWhole, pkg.Store = nil, nil, nil, store }() // cut leaves c1 as a cut finish does: "whole" all chunks there, "lacked" a part short, "gone" its chunks dropped // before the cut, "wrong" a chunk that does not hash. cut := func(id, how string) { @@ -319,23 +319,27 @@ func aHolderTakesAFileInParts(t *testing.T) { for _, c := range []struct { name, opener, how string after, takeOver time.Duration - storeHolds bool + storeHolds string // "parts": Holds answers every part; "file": HoldsWhole answers the file want string // "stored", "dropped", "lacked again", "lacks" (taken over) or the refusal's words }{ {name: "the opener opens a cut finish again", opener: "builder", how: "whole", want: "stored"}, {name: "another caller opens a cut finish", opener: "other", how: "whole", want: "stored"}, {name: "a cut after the chunks were dropped", opener: "builder", how: "gone", want: "lacked again"}, {name: "a cut finish whose chunk does not hash", opener: "builder", how: "wrong", want: "lacked again"}, - {name: "a cut after the store", opener: "builder", how: "whole", storeHolds: true, want: "dropped"}, + {name: "a cut after the store: the holder holds the file", opener: "builder", how: "whole", storeHolds: "file", want: "dropped"}, + {name: "a cut before the store: the holder holds the parts, not the file", opener: "builder", how: "whole", storeHolds: "parts", want: "stored"}, {name: "another caller while the opener sends", opener: "other", how: "lacked", after: time.Minute, want: "another caller, whose last part came 1m0s ago"}, {name: "another caller once the last part is two minutes old", opener: "other", how: "lacked", after: TakeOverAfter, want: "lacks"}, {name: "the receiver's own takeover bound", opener: "other", how: "lacked", after: time.Minute, takeOver: 30 * time.Second, want: "lacks"}, } { pkgLog = nil pkg.TakeOver = c.takeOver - pkg.Holds = nil - if c.storeHolds { + pkg.Holds, pkg.HoldsWhole = nil, nil + switch c.storeHolds { + case "parts": pkg.Holds = func(Transfer[packageShape], string) bool { return true } + case "file": + pkg.HoldsWhole = func(tr Transfer[packageShape]) bool { return tr.Shape == packageShape{"m", "1"} } } cut("c1", c.how) now = now.Add(c.after)