Skip to content

Commit 76261a0

Browse files
committed
use single transaction in get regular refs
1 parent 33f0a4c commit 76261a0

File tree

2 files changed

+30
-56
lines changed

2 files changed

+30
-56
lines changed

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

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/0chain/blobber/code/go/0chain.net/core/common"
1212
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
1313
"go.uber.org/zap"
14+
"gorm.io/gorm"
1415
)
1516

1617
type ReferencePath struct {
@@ -238,70 +239,43 @@ func GetObjectTree(ctx context.Context, allocationID, path string) (*Ref, error)
238239
// To retrieve refs efficiently form pagination index is created in postgresql on path column so it can be used to paginate refs
239240
// very easily and effectively; Same case for offsetDate.
240241
func GetRefs(ctx context.Context, allocationID, path, offsetPath, _type string, level, pageLimit int) (refs *[]PaginatedRef, totalPages int, newOffsetPath string, err error) {
241-
var totalRows int64
242-
var pRefs []PaginatedRef
242+
var (
243+
pRefs = make([]PaginatedRef, 0, pageLimit/4)
244+
dbError error
245+
dbQuery *gorm.DB
246+
)
243247
path = filepath.Clean(path)
244-
245-
wg := sync.WaitGroup{}
246-
wg.Add(2)
247-
errChan := make(chan error, 2)
248-
go func() {
249-
err1 := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
250-
tx := datastore.GetStore().GetTransaction(ctx)
251-
db1 := tx.Model(&Ref{}).Where("allocation_id = ? AND (path=? OR path LIKE ?)", allocationID, path, path+"%")
252-
if _type != "" {
253-
db1 = db1.Where("type = ?", _type)
254-
}
255-
if level != 0 {
256-
db1 = db1.Where("level = ?", level)
257-
}
258-
259-
db1 = db1.Where("path > ?", offsetPath)
260-
261-
db1 = db1.Order("path")
262-
err1 := db1.Limit(pageLimit).Find(&pRefs).Error
263-
wg.Done()
264-
265-
return err1
266-
})
267-
if err1 != nil {
268-
errChan <- err1
248+
tx := datastore.GetStore().GetTransaction(ctx)
249+
pathLevel := len(strings.Split(strings.TrimSuffix(path, "/"), "/")) + 1
250+
if pathLevel == level {
251+
dbQuery = tx.Model(&Ref{}).Where("allocation_id = ? AND parent_path = ? and level = ?", allocationID, path, level)
252+
if _type != "" {
253+
dbQuery = dbQuery.Where("type = ?", _type)
269254
}
270-
271-
}()
272-
273-
go func() {
274-
err2 := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
275-
tx := datastore.GetStore().GetTransaction(ctx)
276-
db2 := tx.Model(&Ref{}).Where("allocation_id = ? AND (path=? OR path LIKE ?)", allocationID, path, path+"%")
277-
if _type != "" {
278-
db2 = db2.Where("type = ?", _type)
279-
}
280-
if level != 0 {
281-
db2 = db2.Where("level = ?", level)
282-
}
283-
err2 := db2.Count(&totalRows).Error
284-
wg.Done()
285-
286-
return err2
287-
})
288-
if err2 != nil {
289-
errChan <- err2
255+
dbQuery = dbQuery.Where("path > ?", offsetPath)
256+
dbQuery = dbQuery.Order("path")
257+
} else {
258+
dbQuery = tx.Model(&Ref{}).Where("allocation_id = ? AND (path=? OR path LIKE ?)", allocationID, path, path+"%")
259+
if _type != "" {
260+
dbQuery = dbQuery.Where("type = ?", _type)
290261
}
291-
}()
292-
wg.Wait()
293-
close(errChan)
294-
for err := range errChan {
295-
if err != nil {
296-
return nil, 0, "", err
262+
if level != 0 {
263+
dbQuery = dbQuery.Where("level = ?", level)
297264
}
298-
}
299265

266+
dbQuery = dbQuery.Where("path > ?", offsetPath)
267+
268+
dbQuery = dbQuery.Order("path")
269+
}
270+
dbError = dbQuery.Limit(pageLimit).Find(&pRefs).Error
271+
if dbError != nil && dbError != gorm.ErrRecordNotFound {
272+
err = dbError
273+
return
274+
}
300275
refs = &pRefs
301276
if len(pRefs) > 0 {
302277
newOffsetPath = pRefs[len(pRefs)-1].Path
303278
}
304-
totalPages = int(math.Ceil(float64(totalRows) / float64(pageLimit)))
305279
return
306280
}
307281

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-- +goose Up
22
-- +goose StatementBegin
3-
DROP INDEX idx_created_at,idx_updated_at,idx_lookup_hash,path_idx,idx_path_gin_trgm,idx_name_gin,idx_allocation_changes_lookup_hash;
3+
DROP INDEX idx_created_at,idx_updated_at,idx_lookup_hash,idx_path_gin_trgm,idx_name_gin,idx_allocation_changes_lookup_hash;
44
-- +goose StatementEnd

0 commit comments

Comments
 (0)