Skip to content

Commit

Permalink
/go/libraries/doltcore/env/actions: fix tag page
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeegoddd committed Jan 17, 2025
1 parent 5001979 commit 3a055eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
35 changes: 14 additions & 21 deletions go/libraries/doltcore/env/actions/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,30 +144,26 @@ type TagRefWithMeta struct {
Meta *datas.TagMeta
}

type RefPageToken struct {
TagName string
}

const DefaultPageSize = 100

// IterResolvedTagsPaginated iterates over tags in dEnv.DoltDB from newest to oldest, resolving the tag to a commit and calling cb().
// Returns a next page token if there are more results available.
func IterResolvedTagsPaginated(ctx context.Context, ddb *doltdb.DoltDB, pageToken *RefPageToken, cb func(tag *doltdb.Tag) (stop bool, err error)) (*RefPageToken, error) {
// Returns the next tag name if there are more results available.
func IterResolvedTagsPaginated(ctx context.Context, ddb *doltdb.DoltDB, startTag string, cb func(tag *doltdb.Tag) (stop bool, err error)) (string, error) {
tagRefs, err := ddb.GetTags(ctx)
if err != nil {
return nil, err
return "", err
}

// for each tag, get the meta
tagMetas := make([]*TagRefWithMeta, 0, len(tagRefs))
for _, r := range tagRefs {
tr, ok := r.(ref.TagRef)
if !ok {
return nil, fmt.Errorf("DoltDB.GetTags() returned non-tag DoltRef")
return "", fmt.Errorf("DoltDB.GetTags() returned non-tag DoltRef")
}
meta, err := ddb.ResolveTagMeta(ctx, tr)
if err != nil {
return nil, err
return "", err
}
tagMetas = append(tagMetas, &TagRefWithMeta{TagRef: tr, Meta: meta})
}
Expand All @@ -177,12 +173,12 @@ func IterResolvedTagsPaginated(ctx context.Context, ddb *doltdb.DoltDB, pageToke
return tagMetas[i].Meta.Timestamp > tagMetas[j].Meta.Timestamp
})

// find starting index based on page token
// find starting index based on start tag
startIdx := 0
if pageToken != nil && pageToken.TagName != "" {
if startTag != "" {
for i, tm := range tagMetas {
if tm.TagRef.GetPath() == pageToken.TagName {
startIdx = i + 1 // start after the token
if tm.TagRef.GetPath() == startTag {
startIdx = i + 1 // start after the given tag
break
}
}
Expand All @@ -200,28 +196,25 @@ func IterResolvedTagsPaginated(ctx context.Context, ddb *doltdb.DoltDB, pageToke
for _, tm := range pageTagMetas {
tag, err := ddb.ResolveTag(ctx, tm.TagRef)
if err != nil {
return nil, err
return "", err
}

stop, err := cb(tag)
if err != nil {
return nil, err
return "", err
}
if stop {
break
}
}

// return next page token if there are more results
var nextPageToken *RefPageToken
// return next tag name if there are more results
if endIdx < len(tagMetas) {
lastTag := pageTagMetas[len(pageTagMetas)-1]
nextPageToken = &RefPageToken{
TagName: lastTag.TagRef.GetPath(),
}
return lastTag.TagRef.GetPath(), nil
}

return nextPageToken, nil
return "", nil
}

// VisitResolvedTag iterates over tags in ddb until the given tag name is found, then calls cb() with the resolved tag.
Expand Down
8 changes: 4 additions & 4 deletions go/libraries/doltcore/env/actions/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func TestIterResolvedTagsPaginated(t *testing.T) {

// Test first page
var foundTags []string
pageToken, err := IterResolvedTagsPaginated(ctx, dEnv.DoltDB, nil, func(tag *doltdb.Tag) (bool, error) {
pageToken, err := IterResolvedTagsPaginated(ctx, dEnv.DoltDB, "", func(tag *doltdb.Tag) (bool, error) {
foundTags = append(foundTags, tag.Name)
return false, nil
})
require.NoError(t, err)
require.NotNil(t, pageToken) // Should have next page
require.NotEmpty(t, pageToken) // Should have next page
require.Equal(t, DefaultPageSize, len(foundTags)) // Default page size tags returned

// Test second page
Expand All @@ -110,7 +110,7 @@ func TestIterResolvedTagsPaginated(t *testing.T) {
return false, nil
})
require.NoError(t, err)
require.Nil(t, nextPageToken) // Should be no more pages
require.Empty(t, nextPageToken) // Should be no more pages
require.Equal(t, DefaultPageSize, len(secondPageTags)) // Remaining tags

// Verify all tags were found
Expand All @@ -120,7 +120,7 @@ func TestIterResolvedTagsPaginated(t *testing.T) {

// Test early termination
var earlyTermTags []string
_, err = IterResolvedTagsPaginated(ctx, dEnv.DoltDB, nil, func(tag *doltdb.Tag) (bool, error) {
_, err = IterResolvedTagsPaginated(ctx, dEnv.DoltDB, "", func(tag *doltdb.Tag) (bool, error) {
earlyTermTags = append(earlyTermTags, tag.Name)
return true, nil // Stop after first tag
})
Expand Down

0 comments on commit 3a055eb

Please sign in to comment.