import Base import ./execution_composer_layout.bend as Layout import ./execution_reservation_bundle.bend as Bundle import ./execution_reservation_materialization.bend as Materialize import ./execution_command_permission.bend as Permission import ./restore_data.bend as R import ./entity_key.bend as Key import ./world.bend as W import ./change_tick.bend as Tick type Row<-layout:Layout.Layout> is Type:Row{saved:Key.Key,bundle:Bundle.Payload(layout)} type Receipt<-S:Data,-layout:Layout.Layout> is Type: Materialized{saved:Key.Key,handle:W.Handle,previous:Bundle.Payload(layout)} AllocationRefused{saved:Key.Key,bundle:Bundle.Payload(layout)} PublicationRefused{saved:Key.Key,handle:W.Handle,bundle:Bundle.Payload(layout),reason:Bundle.Refusal} type Published<-S:Data,-layout:Layout.Layout,-Resource:Type,-E:Data> is Type: Published{world:W.World,mapping:List<&2,R.Binding>,receipts:List>,complete:Bool} def materialized(~S:Data,~layout:Layout.Layout,~Resource:Type,~E:Data,saved:Key.Key,result:Bundle.Publication) -> W.World & Receipt: match result: case Bundle.Materialized{world,handle,previous}:(world,Materialized{saved,handle,previous}) case Bundle.RefusedPublication{world,handle,bundle,reason}:(world,PublicationRefused{saved,handle,bundle,reason}) def reserved(~S:Data,~layout:Layout.Layout,~Resource:Type,~E:Data,saved:Key.Key,bundle:Bundle.Payload(layout),tick:Tick.Tick,result:W.World & W.Reservation) -> W.World & Receipt: match result: case (world,W.ReservationRejected{_}):(world,AllocationRefused{saved,bundle}) case (world,W.Reserved{+handle}):materialized(~S,~layout,~Resource,~E,saved,Materialize.apply_at(~S,~Resource,~E,~layout,Permission.EntityStructural{},W.mark_pending(~S,~Layout.columns(~S,layout),~Resource,~E,world,handle),handle,bundle,tick)) def returned(~S:Data,~layout:Layout.Layout,~Resource:Type,~E:Data,next:W.World -> List<&2,R.Binding> -> List> -> Bool -> Published,mapping:List<&2,R.Binding>,receipts:List>,complete:Bool,result:W.World & Receipt) -> Published: match result: case (world,Materialized{+saved,+handle,previous}):next(world,R.Binding{saved,handle} <> mapping,Materialized{saved,handle,previous} <> receipts,complete) case (world,receipt):next(world,mapping,receipt <> receipts,False{}) def rows(~S:Data,~layout:Layout.Layout,~Resource:Type,~E:Data,values:List>,world:W.World,+tick:Tick.Tick,mapping:List<&2,R.Binding>,receipts:List>,complete:Bool) -> Published: match values: case []:Published{world,List.reverse(&2,R.Binding,mapping),List.reverse(&1,Receipt,receipts),complete} case Row{saved,bundle} <> rest:returned(~S,~layout,~Resource,~E,nextWorld => nextMapping => nextReceipts => nextComplete => rows(~S,~layout,~Resource,~E,rest,nextWorld,tick,nextMapping,nextReceipts,nextComplete),mapping,receipts,complete,reserved(~S,~layout,~Resource,~E,saved,bundle,tick,W.reserve_id(~S,~Layout.columns(~S,layout),~Resource,~E,world))) # All incoming owners finish either in the receiver or an explicit refusal # receipt. A failed row marks the whole result partial; later rows are retained # and attempted once. Only canonical fresh receiver handles enter the map. def publish(~S:Data,~layout:Layout.Layout,~Resource:Type,~E:Data,values:List>,world:W.World,tick:Tick.Tick) -> Published: rows(~S,~layout,~Resource,~E,values,world,tick,[],[],True{})