From 413bf3c8484f949cc77b8f3b8085c95b378fdfd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 31 Jul 2024 00:15:09 +0200 Subject: [PATCH] v0.0.491 small optimization in Paginate method --- go.mod | 4 ++-- go.sum | 4 ++++ goextVersion.go | 4 ++-- wmo/queryPaginate.go | 30 +++++++++++++++++++----------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 0acd62c..cbd3ec4 100644 --- a/go.mod +++ b/go.mod @@ -21,8 +21,8 @@ require ( ) require ( - github.com/bytedance/sonic v1.11.9 // indirect - github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/bytedance/sonic v1.12.0 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect diff --git a/go.sum b/go.sum index bc504ca..3e0705b 100644 --- a/go.sum +++ b/go.sum @@ -24,9 +24,13 @@ github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LR github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg= github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls= +github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9BqwBO3pm9H/+HY= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= diff --git a/goextVersion.go b/goextVersion.go index 95a453f..deae0ed 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.490" +const GoextVersion = "0.0.491" -const GoextVersionTimestamp = "2024-07-27T23:44:18+0200" +const GoextVersionTimestamp = "2024-07-31T00:15:09+0200" diff --git a/wmo/queryPaginate.go b/wmo/queryPaginate.go index 5930915..beb65c6 100644 --- a/wmo/queryPaginate.go +++ b/wmo/queryPaginate.go @@ -60,19 +60,27 @@ func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.MongoFilter, page return nil, pag.Pagination{}, exerr.Wrap(err, "failed to all-decode entities").Build() } - cursorTotalCount, err := c.coll.Aggregate(ctx, pipelineTotalCount) - if err != nil { - return nil, pag.Pagination{}, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipelineTotalCount).Str("collection", c.Name()).Build() - } - var tcRes totalCountResult - if cursorTotalCount.Next(ctx) { - err = cursorTotalCount.Decode(&tcRes) - if err != nil { - return nil, pag.Pagination{}, exerr.Wrap(err, "failed to decode mongo-aggregation $count result").Any("pipeline", pipelineTotalCount).Str("collection", c.Name()).Build() - } + + if limit == nil { + // optimization, limit==nil, so we query all entities anyway, just use the array length + tcRes.Count = len(entities) } else { - tcRes.Count = 0 // no entries in DB + + cursorTotalCount, err := c.coll.Aggregate(ctx, pipelineTotalCount) + if err != nil { + return nil, pag.Pagination{}, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipelineTotalCount).Str("collection", c.Name()).Build() + } + + if cursorTotalCount.Next(ctx) { + err = cursorTotalCount.Decode(&tcRes) + if err != nil { + return nil, pag.Pagination{}, exerr.Wrap(err, "failed to decode mongo-aggregation $count result").Any("pipeline", pipelineTotalCount).Str("collection", c.Name()).Build() + } + } else { + tcRes.Count = 0 // no entries in DB + } + } paginationObj := pag.Pagination{