1+ open Belt
2+
3+ type batchCheckpoint = {
4+ checkpointId : bigint ,
5+ chainId : int ,
6+ blockNumber : int ,
7+ // Might be empty if we are not in reorg threshold
8+ // or rollback on reorg is disabled
9+ blockHash ?: string ,
10+ // Might be empty if we it's a reorg guard block
11+ items ?: array <Internal .item >,
12+ }
13+
14+ type chainBeforeBatch = {
15+ chainId : int ,
16+ fetchState : FetchState .t ,
17+ blocks : ChainBlocks .t ,
18+ totalEventsProcessed : int ,
19+ }
20+
21+ type mutChainAcc = {
22+ mutable batchSize : int ,
23+ mutable lastCheckpoint : batchCheckpoint ,
24+ }
25+
126type progressedChain = {
227 chainId : int ,
328 batchSize : int ,
@@ -6,11 +31,12 @@ type progressedChain = {
631}
732
833type t = {
9- items : array <Internal . item >,
34+ checkpoints : array <batchCheckpoint >,
1035 progressedChains : array <progressedChain >,
11- updatedFetchStates : ChainMap .t <FetchState .t >,
12- dcsToStoreByChainId : dict <array <FetchState .indexingContract >>,
13- creationTimeMs : int ,
36+ totalBatchSize : int ,
37+ // updatedFetchStates: ChainMap.t<FetchState.t>,
38+ // dcsToStoreByChainId: dict<array<FetchState.indexingContract>>,
39+ // creationTimeMs: int,
1440}
1541
1642/**
@@ -21,7 +47,7 @@ let getOrderedNextChain = (fetchStates: ChainMap.t<FetchState.t>, ~batchSizePerC
2147 let earliestChainTimestamp = ref (0 )
2248 let chainKeys = fetchStates -> ChainMap .keys
2349 for idx in 0 to chainKeys -> Array .length - 1 {
24- let chain = chainKeys -> Array .get (idx )
50+ let chain = chainKeys -> Array .getUnsafe (idx )
2551 let fetchState = fetchStates -> ChainMap .get (chain )
2652 if fetchState -> FetchState .isActivelyIndexing {
2753 let timestamp = fetchState -> FetchState .getTimestampAt (
@@ -120,41 +146,59 @@ let prepareOrderedBatch = (
120146}
121147
122148let prepareUnorderedBatch = (
149+ ~chains : array <chainBeforeBatch >,
123150 ~batchSizeTarget ,
124- ~fetchStates : ChainMap .t <FetchState .t >,
125- ~mutBatchSizePerChain : dict <int >,
126- ) => {
151+ ~progressCheckpointId ,
152+ ): t => {
153+ let progressCheckpointId = ref (progressCheckpointId )
154+ let accPerChain = Js .Dict .empty ()
155+
127156 let preparedFetchStates =
128- fetchStates
129- -> ChainMap . values
157+ chains
158+ -> Array . map ( chain => chain . fetchState )
130159 -> FetchState .filterAndSortForUnorderedBatch (~batchSizeTarget )
131160
132161 let chainIdx = ref (0 )
133162 let preparedNumber = preparedFetchStates -> Array .length
134- let batchSize = ref (0 )
163+ let totalBatchSize = ref (0 )
135164
136165 let items = []
137166
138167 // Accumulate items for all actively indexing chains
139168 // the way to group as many items from a single chain as possible
140169 // This way the loaders optimisations will hit more often
141- while batchSize .contents < batchSizeTarget && chainIdx .contents < preparedNumber {
170+ while totalBatchSize .contents < batchSizeTarget && chainIdx .contents < preparedNumber {
142171 let fetchState = preparedFetchStates -> Js .Array2 .unsafe_get (chainIdx .contents )
172+ let chainAcc = switch accPerChain -> Utils .Dict .dangerouslyGetByIntNonOption (fetchState .chainId ) {
173+ | Some (chainAcc ) => chainAcc
174+ | None =>
175+ let acc = {
176+ batchSize : 0 ,
177+ lastCheckpoint : %raw (` null ` ) ,
178+ }
179+ accPerChain -> Utils .Dict .setByInt (fetchState .chainId , acc )
180+ acc
181+ }
182+
143183 let chainBatchSize =
144184 fetchState -> FetchState .getReadyItemsCount (
145- ~targetSize = batchSizeTarget - batchSize .contents ,
185+ ~targetSize = batchSizeTarget - totalBatchSize .contents ,
146186 ~fromItem = 0 ,
147187 )
148188 if chainBatchSize > 0 {
149189 for idx in 0 to chainBatchSize - 1 {
150190 items -> Js .Array2 .push (fetchState .buffer -> Belt .Array .getUnsafe (idx ))-> ignore
151191 }
152- batchSize := batchSize .contents + chainBatchSize
153- mutBatchSizePerChain -> Utils . Dict . setByInt ( fetchState . chainId , chainBatchSize )
192+ totalBatchSize := totalBatchSize .contents + chainBatchSize
193+ chainAcc . batchSize = chainBatchSize
154194 }
155195
156196 chainIdx := chainIdx .contents + 1
157197 }
158198
159- items
199+ {
200+ totalBatchSize : totalBatchSize .contents ,
201+ progressedChains : [],
202+ checkpoints : [],
203+ }
160204}
0 commit comments