Skip to content

Commit

Permalink
feat(paging): add support for after id parameter in find options
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Aug 21, 2020
1 parent 550966d commit 054e025
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
22 changes: 19 additions & 3 deletions kv/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,34 @@ func (s *Service) findBuckets(ctx context.Context, tx Tx, filter influxdb.Bucket
filter.OrganizationID = &o.ID
}

var offset, limit, count int
var descending bool
var (
offset, limit, count int
descending bool
)

after := func(*influxdb.Bucket) bool {
return true
}

if len(opts) > 0 {
offset = opts[0].Offset
limit = opts[0].Limit
descending = opts[0].Descending
if opts[0].After != nil {
after = func(b *influxdb.Bucket) bool {
if descending {
return b.ID < *opts[0].After
}

return b.ID > *opts[0].After
}
}
}

filterFn := filterBucketsFn(filter)
err := s.forEachBucket(ctx, tx, descending, func(b *influxdb.Bucket) bool {
if filterFn(b) {
if count >= offset {
if count >= offset && after(b) {
bs = append(bs, b)
}
count++
Expand Down
17 changes: 17 additions & 0 deletions paging.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type PagingLinks struct {
type FindOptions struct {
Limit int
Offset int
After *ID
SortBy string
Descending bool
}
Expand All @@ -50,6 +51,18 @@ func DecodeFindOptions(r *http.Request) (*FindOptions, error) {
opts.Offset = o
}

if after := qp.Get("after"); after != "" {
id, err := IDFromString(after)
if err != nil {
return nil, &Error{
Code: EInvalid,
Err: fmt.Errorf("decoding after: %w", err),
}
}

opts.After = id
}

if limit := qp.Get("limit"); limit != "" {
l, err := strconv.Atoi(limit)
if err != nil {
Expand Down Expand Up @@ -109,6 +122,10 @@ func (f FindOptions) QueryParams() map[string][]string {
"offset": {strconv.Itoa(f.Offset)},
}

if f.After != nil {
qp["after"] = []string{f.After.String()}
}

if f.Limit > 0 {
qp["limit"] = []string{strconv.Itoa(f.Limit)}
}
Expand Down
12 changes: 11 additions & 1 deletion tenant/storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,17 @@ func (s *Store) ListBuckets(ctx context.Context, tx kv.Tx, filter BucketFilter,
if o.Descending {
opts = append(opts, kv.WithCursorDirection(kv.CursorDescending))
}
cursor, err := b.ForwardCursor(nil, opts...)

var seek []byte
if o.After != nil {
after := (*o.After) + 1
seek, err = after.Encode()
if err != nil {
return nil, err
}
}

cursor, err := b.ForwardCursor(seek, opts...)
if err != nil {
return nil, err
}
Expand Down
48 changes: 48 additions & 0 deletions testing/bucket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,54 @@ func FindBuckets(
},
},
},
{
name: "find all buckets by after and limit",
fields: BucketFields{
BucketIDs: mock.NewIncrementingIDGenerator(idOne),
Organizations: []*influxdb.Organization{
{
Name: "theorg",
},
},
Buckets: []*influxdb.Bucket{
{
// ID(1)
OrgID: idOne,
Name: "abc",
},
{
// ID(2)
OrgID: idOne,
Name: "def",
},
{
// ID(3)
OrgID: idOne,
Name: "xyz",
},
},
},
args: args{
findOptions: influxdb.FindOptions{
After: idPtr(idOne),
Limit: 2,
},
},
wants: wants{
buckets: []*influxdb.Bucket{
{
ID: idTwo,
OrgID: idOne,
Name: "def",
},
{
ID: idThree,
OrgID: idOne,
Name: "xyz",
},
},
},
},
{
name: "find all buckets by descending",
fields: BucketFields{
Expand Down

0 comments on commit 054e025

Please sign in to comment.