Skip to content

Commit

Permalink
/go/libraries/doltcore/{doltdb,env}: another optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeegoddd committed Jan 17, 2025
1 parent 3a055eb commit c067fd2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
19 changes: 16 additions & 3 deletions go/libraries/doltcore/doltdb/doltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ type DoltDB struct {
databaseName string
}

type TagRefWithMeta struct {
TagRef ref.TagRef
Meta *datas.TagMeta
CommitAddr hash.Hash
MaybeHeadAddr hash.Hash
}

// DoltDBFromCS creates a DoltDB from a noms chunks.ChunkStore
func DoltDBFromCS(cs chunks.ChunkStore, databaseName string) *DoltDB {
vrw := types.NewValueStore(cs)
Expand Down Expand Up @@ -594,8 +601,13 @@ func (ddb *DoltDB) ResolveTag(ctx context.Context, tagRef ref.TagRef) (*Tag, err
return NewTag(ctx, tagRef.GetPath(), ds, ddb.vrw, ddb.ns)
}

// ResolveTag takes a TagRef and returns the corresponding Tag object.
func (ddb *DoltDB) ResolveTagFromTagRefWithMeta(ctx context.Context, tagRefWithMeta *TagRefWithMeta) (*Tag, error) {
return NewTagFromTagRefWithMeta(ctx, tagRefWithMeta.TagRef.GetPath(), tagRefWithMeta, ddb.vrw, ddb.ns)
}

// ResolveTagMeta takes a TagRef and returns the corresponding TagMeta object.
func (ddb *DoltDB) ResolveTagMeta(ctx context.Context, tagRef ref.TagRef) (*datas.TagMeta, error) {
func (ddb *DoltDB) ResolveTagMeta(ctx context.Context, tagRef ref.TagRef) (*TagRefWithMeta, error) {
ds, err := ddb.db.GetDataset(ctx, tagRef.String())
if err != nil {
return nil, ErrTagNotFound
Expand All @@ -609,12 +621,13 @@ func (ddb *DoltDB) ResolveTagMeta(ctx context.Context, tagRef ref.TagRef) (*data
return nil, fmt.Errorf("tagRef head is not a tag")
}

meta, _, err := ds.HeadTag()
meta, commitAddr, err := ds.HeadTag()
if err != nil {
return nil, err
}

return meta, nil
addr, _ := ds.MaybeHeadAddr()
return &TagRefWithMeta{TagRef: tagRef, Meta: meta, CommitAddr: commitAddr, MaybeHeadAddr: addr}, nil
}

// ResolveWorkingSet takes a WorkingSetRef and returns the corresponding WorkingSet object.
Expand Down
25 changes: 25 additions & 0 deletions go/libraries/doltcore/doltdb/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ type Tag struct {
Commit *Commit
}

// NewTagFromTagRefWithMeta creates a new Tag object from a TagRefWithMeta.
func NewTagFromTagRefWithMeta(ctx context.Context, name string, tagRefWithMeta *TagRefWithMeta, vrw types.ValueReadWriter, ns tree.NodeStore) (*Tag, error) {
dc, err := datas.LoadCommitAddr(ctx, vrw, tagRefWithMeta.CommitAddr)
if err != nil {
return nil, err
}

if dc.IsGhost() {
return nil, ErrGhostCommitEncountered
}

commit, err := NewCommit(ctx, vrw, ns, dc)
if err != nil {
return nil, err
}

return &Tag{
Name: name,
vrw: vrw,
addr: tagRefWithMeta.MaybeHeadAddr,
Meta: tagRefWithMeta.Meta,
Commit: commit,
}, nil
}

// NewTag creates a new Tag object.
func NewTag(ctx context.Context, name string, ds datas.Dataset, vrw types.ValueReadWriter, ns tree.NodeStore) (*Tag, error) {
meta, commitAddr, err := ds.HeadTag()
Expand Down
13 changes: 4 additions & 9 deletions go/libraries/doltcore/env/actions/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ func IterResolvedTags(ctx context.Context, ddb *doltdb.DoltDB, cb func(tag *dolt
return nil
}

type TagRefWithMeta struct {
TagRef ref.TagRef
Meta *datas.TagMeta
}

const DefaultPageSize = 100

// IterResolvedTagsPaginated iterates over tags in dEnv.DoltDB from newest to oldest, resolving the tag to a commit and calling cb().
Expand All @@ -155,17 +150,17 @@ func IterResolvedTagsPaginated(ctx context.Context, ddb *doltdb.DoltDB, startTag
}

// for each tag, get the meta
tagMetas := make([]*TagRefWithMeta, 0, len(tagRefs))
tagMetas := make([]*doltdb.TagRefWithMeta, 0, len(tagRefs))
for _, r := range tagRefs {
tr, ok := r.(ref.TagRef)
if !ok {
return "", fmt.Errorf("DoltDB.GetTags() returned non-tag DoltRef")
}
meta, err := ddb.ResolveTagMeta(ctx, tr)
tm, err := ddb.ResolveTagMeta(ctx, tr)
if err != nil {
return "", err
}
tagMetas = append(tagMetas, &TagRefWithMeta{TagRef: tr, Meta: meta})
tagMetas = append(tagMetas, tm)
}

// sort by meta timestamp
Expand Down Expand Up @@ -194,7 +189,7 @@ func IterResolvedTagsPaginated(ctx context.Context, ddb *doltdb.DoltDB, startTag

// resolve tags for this page
for _, tm := range pageTagMetas {
tag, err := ddb.ResolveTag(ctx, tm.TagRef)
tag, err := ddb.ResolveTagFromTagRefWithMeta(ctx, tm)
if err != nil {
return "", err
}
Expand Down
1 change: 0 additions & 1 deletion go/libraries/doltcore/env/actions/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func TestVisitResolvedTag(t *testing.T) {
})
require.Equal(t, doltdb.ErrTagNotFound, err)
}

func TestIterResolvedTagsPaginated(t *testing.T) {
dEnv, _ := createTestEnv()
ctx := context.Background()
Expand Down

0 comments on commit c067fd2

Please sign in to comment.