Skip to content

Commit

Permalink
is: Set default page limit in the store
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaplots committed Oct 21, 2024
1 parent 0376d91 commit 086ca94
Show file tree
Hide file tree
Showing 28 changed files with 356 additions and 84 deletions.
4 changes: 2 additions & 2 deletions pkg/identityserver/application_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (is *IdentityServer) listApplicationAPIKeys(
}
ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -398,7 +398,7 @@ func (is *IdentityServer) listApplicationCollaborators(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/application_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (is *IdentityServer) listApplications( // nolint:gocyclo
}
ctx = store.WithOrder(ctx, req.Order)
var total uint64
paginateCtx := store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (is *IdentityServer) listClientCollaborators(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/client_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (is *IdentityServer) listClients(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
paginateCtx := store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/end_device_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (is *IdentityServer) listEndDevices(ctx context.Context, req *ttnpb.ListEnd
}
ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
4 changes: 2 additions & 2 deletions pkg/identityserver/gateway_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (is *IdentityServer) listGatewayAPIKeys(
}
ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -389,7 +389,7 @@ func (is *IdentityServer) listGatewayCollaborators(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/gateway_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (is *IdentityServer) listGateways( // nolint:gocyclo

ctx = store.WithOrder(ctx, req.Order)
var total uint64
paginateCtx := store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/invitation_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (is *IdentityServer) listInvitations(ctx context.Context, req *ttnpb.ListIn
return nil, errNoInviteRights.New()
}
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/notification_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (is *IdentityServer) listNotifications(ctx context.Context, req *ttnpb.List
res := &ttnpb.ListNotificationsResponse{}
err := is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) {
var total uint64
paginateCtx := store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
4 changes: 2 additions & 2 deletions pkg/identityserver/oauth_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (is *IdentityServer) listOAuthClientAuthorizations(ctx context.Context, req
}
ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -60,7 +60,7 @@ func (is *IdentityServer) listOAuthAccessTokens(ctx context.Context, req *ttnpb.
}
ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
4 changes: 2 additions & 2 deletions pkg/identityserver/organization_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (is *IdentityServer) listOrganizationAPIKeys(
}
ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -402,7 +402,7 @@ func (is *IdentityServer) listOrganizationCollaborators(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
2 changes: 1 addition & 1 deletion pkg/identityserver/organization_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (is *IdentityServer) listOrganizations(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
paginateCtx := store.WithPagination(ctx, store.WithLimit(req.Limit, is.config.Pagination.DefaultLimit), req.Page, &total)
paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down
24 changes: 12 additions & 12 deletions pkg/identityserver/registry_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (rs *registrySearch) SearchApplications(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, rs.IdentityServer.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -101,7 +101,7 @@ func (rs *registrySearch) SearchApplications(
return err
}
}
ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindApplications).
ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindApplications).
res.Applications, err = st.FindApplications(ctx, entityIDs, req.FieldMask.GetPaths())
if err != nil {
return err
Expand Down Expand Up @@ -176,7 +176,7 @@ func (rs *registrySearch) SearchClients(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, rs.IdentityServer.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -204,7 +204,7 @@ func (rs *registrySearch) SearchClients(
return err
}
}
ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindClients).
ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindClients).
res.Clients, err = st.FindClients(ctx, entityIDs, req.FieldMask.GetPaths())
if err != nil {
return err
Expand Down Expand Up @@ -282,7 +282,7 @@ func (rs *registrySearch) SearchGateways(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, rs.IdentityServer.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -310,7 +310,7 @@ func (rs *registrySearch) SearchGateways(
return err
}
}
ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindGateways).
ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindGateways).
res.Gateways, err = st.FindGateways(ctx, entityIDs, req.FieldMask.GetPaths())
if err != nil {
return err
Expand Down Expand Up @@ -391,7 +391,7 @@ func (rs *registrySearch) SearchOrganizations(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, rs.IdentityServer.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand Down Expand Up @@ -419,7 +419,7 @@ func (rs *registrySearch) SearchOrganizations(
return err
}
}
ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindOrganizations).
ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindOrganizations).
res.Organizations, err = st.FindOrganizations(ctx, entityIDs, req.FieldMask.GetPaths())
if err != nil {
return err
Expand Down Expand Up @@ -492,7 +492,7 @@ func (rs *registrySearch) SearchUsers(

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, rs.IdentityServer.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand All @@ -516,7 +516,7 @@ func (rs *registrySearch) SearchUsers(
if len(ids) == 0 {
return nil
}
ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindUsers).
ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindUsers).
res.Users, err = st.FindUsers(ctx, ids, req.FieldMask.GetPaths())
if err != nil {
return err
Expand Down Expand Up @@ -606,7 +606,7 @@ func (rs *registrySearch) SearchEndDevices(ctx context.Context, req *ttnpb.Searc

ctx = store.WithOrder(ctx, req.Order)
var total uint64
ctx = store.WithPagination(ctx, store.WithLimit(req.Limit, rs.IdentityServer.config.Pagination.DefaultLimit), req.Page, &total)
ctx = store.WithPagination(ctx, req.Limit, req.Page, &total)
defer func() {
if err == nil {
setTotalHeader(ctx, total)
Expand All @@ -622,7 +622,7 @@ func (rs *registrySearch) SearchEndDevices(ctx context.Context, req *ttnpb.Searc
if len(ids) == 0 {
return nil
}
ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindEndDevices).
ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindEndDevices).
res.EndDevices, err = st.FindEndDevices(ctx, ids, req.FieldMask.GetPaths())
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/identityserver/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ import (
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
bunstore "go.thethings.network/lorawan-stack/v3/pkg/identityserver/bunstore"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/store"
"go.thethings.network/lorawan-stack/v3/pkg/log"
storeutil "go.thethings.network/lorawan-stack/v3/pkg/util/store"
)

func (is *IdentityServer) setupStore() error {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: is.config.Pagination.DefaultLimit,
})
return is.setupBunStore()
}

Expand Down
20 changes: 12 additions & 8 deletions pkg/identityserver/store/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import (
"strings"
)

// PaginationDefaults sets default values for paginations options within the IS store.
type PaginationDefaults struct {
DefaultLimit uint32
}

var paginationDefaults = PaginationDefaults{}

func SetPaginationDefaults(d PaginationDefaults) { paginationDefaults = d }

Check failure on line 30 in pkg/identityserver/store/pagination.go

View workflow job for this annotation

GitHub Actions / Code Quality

exported: exported function SetPaginationDefaults should have comment or be unexported (revive)

type paginationOptionsKeyType struct{}

var paginationOptionsKey paginationOptionsKeyType
Expand All @@ -31,20 +40,15 @@ type PaginationOptions struct {
total *uint64
}

// WithLimit checks if the limit is set, and returns the set limit or the default.
func WithLimit(set, def uint32) uint32 {
if set == 0 {
return def
}
return set
}

// WithPagination instructs the store to paginate the results, and set the total
// number of results into total.
func WithPagination(ctx context.Context, limit, page uint32, total *uint64) context.Context {
if page == 0 {
page = 1
}
if limit == 0 {
limit = paginationDefaults.DefaultLimit
}
return context.WithValue(ctx, paginationOptionsKey, PaginationOptions{
limit: limit,
offset: (page - 1) * limit,
Expand Down
34 changes: 31 additions & 3 deletions pkg/identityserver/storetest/api_key_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (st *StoreTest) TestAPIKeyStorePagination(t *T) {
app1 := st.population.NewApplication(nil)

var all []*ttnpb.APIKey
for i := 0; i < 102; i++ {
for i := 0; i < 7; i++ {
_, key := st.population.NewAPIKey(app1.GetEntityIdentifiers(), ttnpb.Right_RIGHT_APPLICATION_ALL)
key.Name = fmt.Sprintf("Key %d", i)
all = append(all, key)
Expand Down Expand Up @@ -253,15 +253,43 @@ func (st *StoreTest) TestAPIKeyStorePagination(t *T) {

got, err := s.FindAPIKeys(paginateCtx, app1.GetEntityIdentifiers())
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 2)
if page == 4 {
a.So(got, should.HaveLength, 1)
} else {
a.So(got, should.HaveLength, 2)
}
for i, e := range got {
a.So(e.Name, should.Equal, all[i+2*int(page-1)].Name)
}
}

a.So(total, should.Equal, 102)
a.So(total, should.Equal, 7)
}
})
}

func (st *StoreTest) TestAPIKeyStorePaginationDefaults(t *T) {

Check failure on line 271 in pkg/identityserver/storetest/api_key_store.go

View workflow job for this annotation

GitHub Actions / Code Quality

exported: exported method StoreTest.TestAPIKeyStorePaginationDefaults should have comment or be unexported (revive)
app1 := st.population.NewApplication(nil)

var all []*ttnpb.APIKey
for i := 0; i < 102; i++ {
_, key := st.population.NewAPIKey(app1.GetEntityIdentifiers(), ttnpb.Right_RIGHT_APPLICATION_ALL)
key.Name = fmt.Sprintf("Key %d", i)
all = append(all, key)
}

sort.Sort(apiKeysByID(all))

s, ok := st.PrepareDB(t).(interface {
Store

is.APIKeyStore
})
defer st.DestroyDB(t, false)
if !ok {
t.Skip("Store does not implement APIKeyStore")
}
defer s.Close()

t.Run("FindAPIKeys_PageLimit", func(t *T) {
a, ctx := test.New(t)
Expand Down
Loading

0 comments on commit 086ca94

Please sign in to comment.