unistore: split ListIterator and ListHistory in StorageBackend (#105654)

split listIterator from ListHistory
This commit is contained in:
Georges Chaudy
2025-05-23 15:00:18 +02:00
committed by GitHub
parent 392c1a71c9
commit 04d39cbbc6
7 changed files with 50 additions and 45 deletions
+10 -5
View File
@@ -569,10 +569,6 @@ func (b *backend) ListIterator(ctx context.Context, req *resourcepb.ListRequest,
return 0, fmt.Errorf("missing group or resource")
}
if req.Source != resourcepb.ListRequest_STORE {
return b.getHistory(ctx, req, cb)
}
// TODO: think about how to handler VersionMatch. We should be able to use latest for the first page (only).
// TODO: add support for RemainingItemCount
@@ -583,6 +579,13 @@ func (b *backend) ListIterator(ctx context.Context, req *resourcepb.ListRequest,
return b.listLatest(ctx, req, cb)
}
func (b *backend) ListHistory(ctx context.Context, req *resourcepb.ListRequest, cb func(resource.ListIterator) error) (int64, error) {
ctx, span := b.tracer.Start(ctx, tracePrefix+"ListHistory")
defer span.End()
return b.getHistory(ctx, req, cb)
}
// listLatest fetches the resources from the resource table.
func (b *backend) listLatest(ctx context.Context, req *resourcepb.ListRequest, cb func(resource.ListIterator) error) (int64, error) {
ctx, span := b.tracer.Start(ctx, tracePrefix+"listLatest")
@@ -733,7 +736,9 @@ func (b *backend) getHistory(ctx context.Context, req *resourcepb.ListRequest, c
// for Unset (default) and Exact matching.
listReq.SortAscending = req.GetVersionMatchV2() == resourcepb.ResourceVersionMatchV2_NotOlderThan
iter := &listIter{}
iter := &listIter{
useCurrentRV: true, // use the current RV for the continue token instead of the listRV
}
if req.NextPageToken != "" {
continueToken, err := resource.GetContinueToken(req.NextPageToken)
if err != nil {
+8 -8
View File
@@ -8,10 +8,11 @@ import (
var _ resource.ListIterator = (*listIter)(nil)
type listIter struct {
rows db.Rows
offset int64
listRV int64
sortAsc bool
rows db.Rows
offset int64
listRV int64
sortAsc bool
useCurrentRV bool
// any error
err error
@@ -29,13 +30,12 @@ type listIter struct {
// ContinueToken implements resource.ListIterator.
func (l *listIter) ContinueToken() string {
if l.useCurrentRV {
return resource.ContinueToken{ResourceVersion: l.rv, StartOffset: l.offset, SortAscending: l.sortAsc}.String()
}
return resource.ContinueToken{ResourceVersion: l.listRV, StartOffset: l.offset, SortAscending: l.sortAsc}.String()
}
func (l *listIter) ContinueTokenWithCurrentRV() string {
return resource.ContinueToken{ResourceVersion: l.rv, StartOffset: l.offset, SortAscending: l.sortAsc}.String()
}
func (l *listIter) Error() error {
return l.err
}
@@ -219,7 +219,7 @@ func TestListIter(t *testing.T) {
require.Equal(t, expected, actual)
})
t.Run("ContinueTokenWithCurrentRV uses current row's RV", func(t *testing.T) {
t.Run("ContinueToken uses the current row's RV", func(t *testing.T) {
listReq := sqlResourceListRequest{
SQLTemplate: sqltemplate.New(dialect),
Request: new(resourcepb.ListRequest),
@@ -229,14 +229,15 @@ func TestListIter(t *testing.T) {
require.NoError(t, err)
iter := &listIter{
rows: rows,
listRV: 300,
sortAsc: true,
rows: rows,
listRV: 300,
sortAsc: true,
useCurrentRV: true, // use the current RV for the continue token instead of the listRV
}
require.True(t, iter.Next())
token := iter.ContinueTokenWithCurrentRV()
token := iter.ContinueToken()
var actual resource.ContinueToken
b, err := base64.StdEncoding.DecodeString(token)