@@ -21,32 +21,38 @@ open Builtin.Shortcuts
2121let packageOpTypeName =
2222 FQTypeName.fqPackage PackageIDs.Type.LanguageTools.ProgramTypes.packageOp
2323
24+ let packageOpBatchTypeName =
25+ FQTypeName.fqPackage PackageIDs.Type.LanguageTools.ProgramTypes.packageOpBatch
26+
2427
2528// TODO: review/reconsider the accessibility of these fns
2629let fns : List < BuiltInFn > =
2730 [ { name = fn " scmAddOps" 0
2831 typeParams = []
2932 parameters =
30- [ Param.make " branchID" ( TypeReference.option TUuid) " "
33+ [ Param.make " instanceID" ( TypeReference.option TUuid) " "
34+ Param.make " branchID" ( TypeReference.option TUuid) " "
3135 Param.make " ops" ( TList( TCustomType( Ok packageOpTypeName, []))) " " ]
3236 returnType = TypeReference.result TInt64 TString
3337 description =
3438 " Add package ops to the database and apply them to projections.
35- Returns Ok(insertedCount) on success (duplicates are skipped), or Error with message on failure."
39+ Pass None for instanceID for local ops, or Some(uuid) for ops from remote instances.
40+ Returns the number of inserted ops on success (duplicates are skipped), or an error message on failure."
3641 fn =
3742 let resultOk = Dval.resultOk KTInt64 KTString
3843 let resultError = Dval.resultError KTInt64 KTString
3944 ( function
40- | _, _, _, [ branchID; DList(_ vtTODO, ops) ] ->
45+ | _, _, _, [ instanceID ; branchID; DList(_ vtTODO, ops) ] ->
4146 uply {
4247 try
4348 // Deserialize dvals
4449 let branchID = C2DT.Option.fromDT D.uuid branchID
50+ let instanceID = C2DT.Option.fromDT D.uuid instanceID
4551 let ops = ops |> List.choose PT2DT.PackageOp.fromDT
4652
4753 // Insert ops with deduplication, get count of actually inserted ops
4854 let! insertedCount =
49- LibPackageManager.Inserts.insertAndApplyOps branchID ops
55+ LibPackageManager.Inserts.insertAndApplyOps instanceID branchID ops
5056
5157 return resultOk ( DInt64 insertedCount)
5258 with ex ->
@@ -110,23 +116,49 @@ let fns : List<BuiltInFn> =
110116 { name = fn " scmGetOpsSince" 0
111117 typeParams = []
112118 parameters =
113- [ Param.make " branchID " ( TypeReference.option TUuid) " "
119+ [ Param.make " targetInstanceID " ( TypeReference.option TUuid) " "
114120 Param.make " since" TDateTime " " ]
115- returnType = TList( TCustomType( Ok packageOpTypeName, []))
116- description = " Get package ops created since the given timestamp."
121+ returnType = TList( TCustomType( Ok packageOpBatchTypeName, []))
122+ description =
123+ " Get all package ops (from ALL branches) created since the given timestamp, grouped by branch and instance.
124+ Optionally filters for a target instance (pass None to get all ops, or Some(uuid) to exclude ops from that target instance).
125+ Returns a list of PackageOpBatch, where each batch contains ops from one branch with the same instanceID."
117126 fn =
118127 function
119- | _, _, _, [ branchID ; DDateTime since ] ->
128+ | _, _, _, [ targetInstanceID ; DDateTime since ] ->
120129 uply {
121- let branchID = C2DT.Option.fromDT D.uuid branchID
122-
123- let! ops = LibPackageManager.Queries.getOpsSince branchID since
124-
125- return
126- DList(
127- VT.customType PT2DT.PackageOp.typeName [],
128- ops |> List.map PT2DT.PackageOp.toDT
129- )
130+ let targetID = C2DT.Option.fromDT D.uuid targetInstanceID
131+
132+ let! opsWithMetadata =
133+ LibPackageManager.Queries.getAllOpsSince targetID since
134+
135+ // Group by (branchID, instanceID)
136+ let grouped =
137+ opsWithMetadata
138+ |> List.groupBy ( fun ( _ , branchID , instanceID ) ->
139+ ( branchID, instanceID))
140+ |> Map.toList
141+
142+ // Convert each group to a PackageOpBatch record
143+ let batches =
144+ grouped
145+ |> List.map ( fun (( branchID , instanceID ), ops ) ->
146+ let opsList =
147+ ops
148+ |> List.map ( fun ( op , _ , _ ) -> PT2DT.PackageOp.toDT op)
149+ |> fun opDvals ->
150+ DList( VT.customType packageOpTypeName [], opDvals)
151+
152+ let fields =
153+ [ ( " branchID" , branchID |> Option.map DUuid |> Dval.option KTUuid)
154+ ( " instanceID" ,
155+ instanceID |> Option.map DUuid |> Dval.option KTUuid)
156+ ( " ops" , opsList) ]
157+ |> Map
158+
159+ DRecord( packageOpBatchTypeName, packageOpBatchTypeName, [], fields))
160+
161+ return DList( VT.customType packageOpBatchTypeName [], batches)
130162 }
131163 | _ -> incorrectArgs ()
132164 sqlSpec = NotQueryable
0 commit comments