Skip to content

Commit

Permalink
Merge pull request #9 from mstg/page-count
Browse files Browse the repository at this point in the history
Allow fetching the count from GetPage
  • Loading branch information
mstg authored Mar 20, 2024
2 parents fb9b56f + cdd9dd1 commit c97a81e
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 39 deletions.
2 changes: 1 addition & 1 deletion parser/gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package parser
Expand Down
6 changes: 4 additions & 2 deletions pika.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down Expand Up @@ -155,7 +155,9 @@ type QuerySet[T any] interface {
AIP160(filter string, options AIPFilterOptions) (QuerySet[T], error)

// Page token functionality for gRPC
GetPage(paginatable Paginatable, options AIPFilterOptions) ([]*T, string, error)
// The count is optional and returns the total number of rows for the query.
// It is implemented as a variadic function to not break existing code.
GetPage(paginatable Paginatable, options AIPFilterOptions, count ...*int) ([]*T, string, error)

// Join table
InnerJoin(modelFirst, modelSecond interface{}, keyFirst, keySecond string) QuerySet[T]
Expand Down
2 changes: 1 addition & 1 deletion pika_aip_filter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down
2 changes: 1 addition & 1 deletion pika_aip_filter_proto.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down
2 changes: 1 addition & 1 deletion pika_aip_filter_proto_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down
2 changes: 1 addition & 1 deletion pika_aip_filter_psql_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down
4 changes: 2 additions & 2 deletions pika_page_token.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down Expand Up @@ -57,7 +57,7 @@ func (p *PageToken[T]) Encode() (string, error) {
return base64.URLEncoding.EncodeToString(data), nil
}

// ParsePageToken constructs a PageToken from a base64-encoded string
// Decode constructs a PageToken from a base64-encoded string
func (p *PageToken[T]) Decode(s string) error {
data, err := base64.URLEncoding.DecodeString(s)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions pika_psql.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down Expand Up @@ -542,7 +542,11 @@ func (b *basePsql[T]) AIP160(filter string, options AIPFilterOptions) (QuerySet[
}

// Page tokens for gRPC
func (b *basePsql[T]) GetPage(paginatable Paginatable, options AIPFilterOptions) ([]*T, string, error) {
func (b *basePsql[T]) GetPage(paginatable Paginatable, options AIPFilterOptions, countPointer ...*int) ([]*T, string, error) {
if len(countPointer) > 1 {
return nil, "", fmt.Errorf("too many arguments (count should be one pointer or none)")
}

if b.err != nil {
return nil, "", b.err
}
Expand Down Expand Up @@ -579,6 +583,10 @@ func (b *basePsql[T]) GetPage(paginatable Paginatable, options AIPFilterOptions)
return nil, "", fmt.Errorf("getting count: %w", err)
}

if len(countPointer) > 0 {
*countPointer[0] = count
}

// If no more results after this page, return empty page token
if b.PageToken.Offset >= uint(count) {
return result, "", nil
Expand Down
2 changes: 1 addition & 1 deletion pika_psql_experimental.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand Down
21 changes: 20 additions & 1 deletion pika_psql_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

package pika
Expand All @@ -16,6 +16,7 @@ import (
"github.com/lib/pq"
"github.com/stretchr/testify/require"
orderedmap "github.com/wk8/go-ordered-map/v2"
pikatestpb "go.ciq.dev/pika/testproto"
)

var pgInstance *embeddedpostgres.EmbeddedPostgres
Expand Down Expand Up @@ -1735,3 +1736,21 @@ func TestFilterMissingArgs(t *testing.T) {
_, err = qs.Count()
requireError(err)
}

func TestGetPageCount(t *testing.T) {
psql := newPsql(t)
createTestEntries(t, psql)
qs := Q[simpleModel1](psql)

aipOptions := ProtoReflect(&pikatestpb.SimpleModel1{})
req := &pikatestpb.TestRequest1{
PageSize: int32(1),
}
var count int
page, nt, err := qs.GetPage(req, aipOptions, &count)
require.Nil(t, err)

require.Equal(t, 1, len(page))
require.NotEmpty(t, nt)
require.Equal(t, 3, count)
}
2 changes: 1 addition & 1 deletion testproto/gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright (c) 2023, Ctrl IQ, Inc. All rights reserved
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, Ctrl IQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0

//go:generate protoc --go_opt=paths=source_relative --go_out=. test.proto
Expand Down
Loading

0 comments on commit c97a81e

Please sign in to comment.