Skip to content

Commit 4f2674d

Browse files
authored
fix get refs (#1203)
1 parent d14239b commit 4f2674d

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

code/go/0chain.net/blobbercore/reference/referencepath.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ func GetRefs(ctx context.Context, allocationID, path, offsetPath, _type string,
244244

245245
wg := sync.WaitGroup{}
246246
wg.Add(2)
247+
errChan := make(chan error, 2)
247248
go func() {
248-
_ = datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
249+
err1 := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
249250
tx := datastore.GetStore().GetTransaction(ctx)
250-
db1 := tx.Model(&Ref{}).Where("allocation_id = ?", allocationID).
251-
Where("path = ?", path).Or("path LIKE ?", path+"%")
251+
db1 := tx.Model(&Ref{}).Where("allocation_id = ? AND (path=? OR path LIKE ?)", allocationID, path, path+"%")
252252
if _type != "" {
253253
db1 = db1.Where("type = ?", _type)
254254
}
@@ -259,34 +259,42 @@ func GetRefs(ctx context.Context, allocationID, path, offsetPath, _type string,
259259
db1 = db1.Where("path > ?", offsetPath)
260260

261261
db1 = db1.Order("path")
262-
err = db1.Limit(pageLimit).Find(&pRefs).Error
262+
err1 := db1.Limit(pageLimit).Find(&pRefs).Error
263263
wg.Done()
264264

265-
return nil
265+
return err1
266266
})
267+
if err1 != nil {
268+
errChan <- err1
269+
}
267270

268271
}()
269272

270273
go func() {
271-
_ = datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
274+
err2 := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
272275
tx := datastore.GetStore().GetTransaction(ctx)
273-
db2 := tx.Model(&Ref{}).Where("allocation_id = ?", allocationID).
274-
Where("path = ?", path).Or("path LIKE ?", path+"%")
276+
db2 := tx.Model(&Ref{}).Where("allocation_id = ? AND (path=? OR path LIKE ?)", allocationID, path, path+"%")
275277
if _type != "" {
276278
db2 = db2.Where("type = ?", _type)
277279
}
278280
if level != 0 {
279281
db2 = db2.Where("level = ?", level)
280282
}
281-
db2.Count(&totalRows)
283+
err2 := db2.Count(&totalRows).Error
282284
wg.Done()
283285

284-
return nil
286+
return err2
285287
})
288+
if err2 != nil {
289+
errChan <- err2
290+
}
286291
}()
287292
wg.Wait()
288-
if err != nil {
289-
return
293+
close(errChan)
294+
for err := range errChan {
295+
if err != nil {
296+
return nil, 0, "", err
297+
}
290298
}
291299

292300
refs = &pRefs
@@ -313,8 +321,7 @@ func GetUpdatedRefs(ctx context.Context, allocationID, path, offsetPath, _type,
313321
go func() {
314322
err := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
315323
tx := datastore.GetStore().GetTransaction(ctx)
316-
db1 := tx.Model(&Ref{}).Where("allocation_id = ?", allocationID).
317-
Where("path = ?", path).Or("path LIKE ?", path+"%")
324+
db1 := tx.Model(&Ref{}).Where("allocation_id = ? AND (path=? OR path LIKE ?)", allocationID, path, path+"%")
318325
if _type != "" {
319326
db1 = db1.Where("type = ?", _type)
320327
}
@@ -343,8 +350,7 @@ func GetUpdatedRefs(ctx context.Context, allocationID, path, offsetPath, _type,
343350
go func() {
344351
err := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
345352
tx := datastore.GetStore().GetTransaction(ctx)
346-
db2 := tx.Model(&Ref{}).Where("allocation_id = ?", allocationID).
347-
Where("path = ?", path).Or("path LIKE ?", path+"%")
353+
db2 := tx.Model(&Ref{}).Where("allocation_id = ? AND (path=? OR path LIKE ?)", allocationID, path, path+"%")
348354
if _type != "" {
349355
db2 = db2.Where("type > ?", level)
350356
}

0 commit comments

Comments
 (0)