@@ -3,6 +3,8 @@ package metadata
3
3
import (
4
4
"strings"
5
5
6
+ "sync"
7
+
6
8
"github.com/availproject/avail-go-sdk/interfaces"
7
9
prim "github.com/availproject/avail-go-sdk/primitives"
8
10
)
@@ -161,12 +163,12 @@ func GenericFetch[V any, S StorageT](blockStorage interfaces.BlockStorageT, stor
161
163
return prim .None [V ](), err
162
164
}
163
165
164
- if encoded == "" {
166
+ if encoded . IsNone () || encoded . Unwrap () == "" {
165
167
return prim .None [V ](), nil
166
168
}
167
169
168
170
var t V
169
- decoder := prim .NewDecoder (prim .Hex .FromHex (encoded ), 0 )
171
+ decoder := prim .NewDecoder (prim .Hex .FromHex (encoded . Unwrap () ), 0 )
170
172
if err := decoder .Decode (& t ); err != nil {
171
173
return prim .None [V ](), err
172
174
}
@@ -181,12 +183,12 @@ func GenericMapFetch[V any, K any, S StorageMapT](blockStorage interfaces.BlockS
181
183
return prim .None [StorageEntry [K , V ]](), err
182
184
}
183
185
184
- if encoded == "" {
186
+ if encoded . IsNone () || encoded . Unwrap () == "" {
185
187
return prim .None [StorageEntry [K , V ]](), nil
186
188
}
187
189
188
190
var t V
189
- decoder := prim .NewDecoder (prim .Hex .FromHex (encoded ), 0 )
191
+ decoder := prim .NewDecoder (prim .Hex .FromHex (encoded . Unwrap () ), 0 )
190
192
if err := decoder .Decode (& t ); err != nil {
191
193
return prim .None [StorageEntry [K , V ]](), err
192
194
}
@@ -206,23 +208,62 @@ func GenericMapKeysFetch[V any, K any, S StorageMapT](blockStorage interfaces.Bl
206
208
return nil , nil
207
209
}
208
210
211
+ // Sync mechanism
212
+ errors := []error {}
213
+ mu := sync.Mutex {}
214
+ wg := sync.WaitGroup {}
215
+
209
216
storageEntries := []StorageEntry [K , V ]{}
210
- for i := range storageKeys {
211
- mapKey , err := storageMapKeyDecode [K ](storageKeys [i ], storage )
212
- if err != nil {
213
- return nil , err
217
+ target := len (storageKeys )
218
+ currentIndex := 0
219
+ for {
220
+ for i := 0 ; i < 150 ; i ++ {
221
+ wg .Add (1 )
222
+ go func (i int ) {
223
+ defer wg .Done ()
224
+
225
+ mapKey , err := storageMapKeyDecode [K ](storageKeys [i ], storage )
226
+ if err != nil {
227
+ mu .Lock ()
228
+ errors = append (errors , err )
229
+ mu .Unlock ()
230
+ return
231
+ }
232
+
233
+ value , err := GenericMapFetch [V ](blockStorage , mapKey , storage )
234
+ if err != nil {
235
+ mu .Lock ()
236
+ errors = append (errors , err )
237
+ mu .Unlock ()
238
+ return
239
+ }
240
+
241
+ if value .IsNone () {
242
+ return
243
+ }
244
+
245
+ mu .Lock ()
246
+ storageEntries = append (storageEntries , value .Unwrap ())
247
+ mu .Unlock ()
248
+ }(currentIndex )
249
+
250
+ currentIndex += 1
251
+ if currentIndex >= target {
252
+ break
253
+ }
214
254
}
215
255
216
- value , err := GenericMapFetch [ V ]( blockStorage , mapKey , storage )
217
- if err != nil {
218
- return nil , err
256
+ wg . Wait ( )
257
+ if len ( errors ) > 0 {
258
+ break
219
259
}
220
-
221
- if value .IsNone () {
222
- continue
260
+ if currentIndex >= (target - 1 ) {
261
+ break
223
262
}
263
+ }
224
264
225
- storageEntries = append (storageEntries , value .Unwrap ())
265
+ if len (errors ) > 0 {
266
+ return nil , errors [0 ]
226
267
}
227
268
228
269
return storageEntries , nil
@@ -235,12 +276,12 @@ func GenericDoubleMapFetch[V any, K1 any, K2 any, S StorageDoubleMapT](blockStor
235
276
return prim .None [StorageEntryDoubleMap [K1 , K2 , V ]](), err
236
277
}
237
278
238
- if encoded == "" {
279
+ if encoded . IsNone () || encoded . Unwrap () == "" {
239
280
return prim .None [StorageEntryDoubleMap [K1 , K2 , V ]](), nil
240
281
}
241
282
242
283
var t V
243
- decoder := prim .NewDecoder (prim .Hex .FromHex (encoded ), 0 )
284
+ decoder := prim .NewDecoder (prim .Hex .FromHex (encoded . Unwrap () ), 0 )
244
285
if err := decoder .Decode (& t ); err != nil {
245
286
return prim .None [StorageEntryDoubleMap [K1 , K2 , V ]](), err
246
287
}
@@ -260,23 +301,62 @@ func GenericDoubleMapKeysFetch[V any, K1 any, K2 any, S StorageDoubleMapT](block
260
301
return nil , nil
261
302
}
262
303
304
+ // Sync mechanism
305
+ errors := []error {}
306
+ mu := sync.Mutex {}
307
+ wg := sync.WaitGroup {}
308
+
263
309
storageEntries := []StorageEntryDoubleMap [K1 , K2 , V ]{}
264
- for i := range storageKeys {
265
- mapKey1 , mapKey2 , err := storageDoubleMapKeyDecode [K1 , K2 ](storageKeys [i ], storage )
266
- if err != nil {
267
- return nil , err
310
+ target := len (storageKeys )
311
+ currentIndex := 0
312
+ for {
313
+ for i := 0 ; i < 150 ; i ++ {
314
+ wg .Add (1 )
315
+ go func (i int ) {
316
+ defer wg .Done ()
317
+
318
+ mapKey1 , mapKey2 , err := storageDoubleMapKeyDecode [K1 , K2 ](storageKeys [i ], storage )
319
+ if err != nil {
320
+ mu .Lock ()
321
+ errors = append (errors , err )
322
+ mu .Unlock ()
323
+ return
324
+ }
325
+
326
+ value , err := GenericDoubleMapFetch [V ](blockStorage , mapKey1 , mapKey2 , storage )
327
+ if err != nil {
328
+ mu .Lock ()
329
+ errors = append (errors , err )
330
+ mu .Unlock ()
331
+ return
332
+ }
333
+
334
+ if value .IsNone () {
335
+ return
336
+ }
337
+
338
+ mu .Lock ()
339
+ storageEntries = append (storageEntries , value .Unwrap ())
340
+ mu .Unlock ()
341
+ }(currentIndex )
342
+
343
+ currentIndex += 1
344
+ if currentIndex >= target {
345
+ break
346
+ }
268
347
}
269
348
270
- value , err := GenericDoubleMapFetch [ V ]( blockStorage , mapKey1 , mapKey2 , storage )
271
- if err != nil {
272
- return nil , err
349
+ wg . Wait ( )
350
+ if len ( errors ) > 0 {
351
+ break
273
352
}
274
-
275
- if value .IsNone () {
276
- continue
353
+ if currentIndex >= (target - 1 ) {
354
+ break
277
355
}
356
+ }
278
357
279
- storageEntries = append (storageEntries , value .Unwrap ())
358
+ if len (errors ) > 0 {
359
+ return nil , errors [0 ]
280
360
}
281
361
282
362
return storageEntries , nil
0 commit comments