@@ -11,6 +11,7 @@ import (
11
11
"github.com/0chain/blobber/code/go/0chain.net/core/common"
12
12
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
13
13
"go.uber.org/zap"
14
+ "gorm.io/gorm"
14
15
)
15
16
16
17
type ReferencePath struct {
@@ -238,70 +239,43 @@ func GetObjectTree(ctx context.Context, allocationID, path string) (*Ref, error)
238
239
// To retrieve refs efficiently form pagination index is created in postgresql on path column so it can be used to paginate refs
239
240
// very easily and effectively; Same case for offsetDate.
240
241
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
+ )
243
247
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 )
269
254
}
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 )
290
261
}
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 )
297
264
}
298
- }
299
265
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
+ }
300
275
refs = & pRefs
301
276
if len (pRefs ) > 0 {
302
277
newOffsetPath = pRefs [len (pRefs )- 1 ].Path
303
278
}
304
- totalPages = int (math .Ceil (float64 (totalRows ) / float64 (pageLimit )))
305
279
return
306
280
}
307
281
0 commit comments