Git designed for AI agents.
sqlstore: the land identities of a changeset are written with one statement
Federico Eliaa4bb73bb7d23
diff --git a/internal/graft/corestore/sqlstore/landidentity_bench_test.go b/internal/graft/corestore/sqlstore/landidentity_bench_test.gonew file mode 100644index 0000000..ecdc858--- /dev/null+++ b/internal/graft/corestore/sqlstore/landidentity_bench_test.go@@ -0,0 +1,48 @@+package sqlstore_test++import (+ "fmt"+ "testing"++ "github.com/feder-cr/git-ai/internal/graft/changelog"+ "github.com/feder-cr/git-ai/internal/graft/corestore"+ "github.com/feder-cr/git-ai/internal/graft/corestore/coretest"+ "github.com/feder-cr/git-ai/internal/testhelper"+)++// BenchmarkCommitChangesetLands measures the commit of a land's changeset with the identities of+// none or of a batch of its lands (doc/graft/PLAN11.md, 4.1), so that the cost of their rows is the+// difference.+func BenchmarkCommitChangesetLands(b *testing.B) {+ for _, dialect := range coretest.Dialects() {+ for _, lands := range []int{0, 10} {+ b.Run(fmt.Sprintf("%s/%d lands", dialect, lands), func(b *testing.B) {+ ctx := testhelper.Context(b)+ s := openStore(b, ctx, coretest.DatabaseURL(b, dialect))+ repo, _, err := s.RegisterRepository(ctx, testKey("bench"), "sha1", corestore.InitialState{})+ if err != nil {+ b.Fatal(err)+ }++ old := corestore.Target("")+ b.ResetTimer()+ for i := 0; i < b.N; i++ {+ tip := oid(1000000 + i)+ meta := &changelog.Meta{Operation: "land_batch", Actor: "graft-batch"}+ if lands > 0 {+ meta.LandReference = "refs/heads/main"+ meta.LandInvocation = fmt.Sprint("inv-", i)+ for j := 0; j < lands; j++ {+ meta.Lands = append(meta.Lands, changelog.Land{Original: string(oid(2000000 + i*lands + j)), Landed: string(oid(3000000 + i*lands + j))})+ }+ }+ update := corestore.RefUpdate{Name: "refs/heads/main", Expect: corestore.ExpectValue, Old: old, New: tip}+ if _, err := s.CommitChangeset(ctx, corestore.ChangesetInput{RepoID: repo.ID, Updates: []corestore.RefUpdate{update}, Operation: "land_batch", NodeID: "node-a", Changelog: meta}); err != nil {+ b.Fatal(err)+ }+ old = tip+ }+ })+ }+ }+}diff --git a/internal/graft/corestore/sqlstore/landidentity_test.go b/internal/graft/corestore/sqlstore/landidentity_test.goindex 16172eb..9402963 100644--- a/internal/graft/corestore/sqlstore/landidentity_test.go+++ b/internal/graft/corestore/sqlstore/landidentity_test.go@@ -99,6 +99,28 @@ func TestLandIdentities(t *testing.T) {})}+// TestLandIdentitiesOfACommitLandedTwiceInOneEntry verifies that an entry whose lands name a commit+// more than once, which one upsert statement cannot write, records its last land.+func TestLandIdentitiesOfACommitLandedTwiceInOneEntry(t *testing.T) {+ forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {+ repo := register(t, ctx, s, testKey("lands-twice"))+ result, err := s.CommitChangeset(ctx, corestore.ChangesetInput{+ RepoID: repo.ID,+ Updates: []corestore.RefUpdate{create("refs/heads/main", oid(13))},+ Changelog: landMeta("refs/heads/main",+ changelog.Land{Original: string(oid(10)), Landed: string(oid(11))},+ changelog.Land{Original: string(oid(20)), Landed: string(oid(12))},+ changelog.Land{Original: string(oid(10)), Landed: string(oid(13))},+ ),+ })+ require.NoError(t, err)++ seq := result.Changelog[0].Sequence+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(10), changelog.LandRecord{Sequence: seq, Landed: string(oid(13))})+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(20), changelog.LandRecord{Sequence: seq, Landed: string(oid(12))})+ })+}+// TestLandIdentitiesAreWrittenWithTheChangeset verifies that the land record and the changeset are// one transaction: when the record cannot be written, the changeset is not committed either.func TestLandIdentitiesAreWrittenWithTheChangeset(t *testing.T) {diff --git a/internal/graft/corestore/sqlstore/repository.go b/internal/graft/corestore/sqlstore/repository.goindex 2a1ebbc..9a0a105 100644--- a/internal/graft/corestore/sqlstore/repository.go+++ b/internal/graft/corestore/sqlstore/repository.go@@ -6,6 +6,7 @@ import ("encoding/json""errors""fmt"+ "strings""time""github.com/feder-cr/git-ai/internal/graft/changelog"@@ -584,17 +585,40 @@ func insertChangelog(ctx context.Context, s *Store, q querier, repoID int64, ent// original commits on the branch of entry. Every writer of the changelog appends// its entries after the last one (the sequences of an import must be greater),// so the entry is the latest land of its commits and replaces their records.+//+// The lands of an entry are written with one statement, so that a batch costs+// one round trip to the database and not one per member. An upsert may not+// change a row twice, so a commit that the entry lands more than once keeps+// its last land, the one that holds it.func insertLandIdentities(ctx context.Context, s *Store, q querier, repoID int64, entry changelog.Entry) error {- for _, land := range entry.Lands {- if _, err := q.ExecContext(ctx, s.bind(`- INSERT INTO graft_land_identities (repo_id, branch, original_commit, log_seq, landed_commit)- VALUES (?, ?, ?, ?, ?)- ON CONFLICT (repo_id, branch, original_commit) DO UPDATE SET- log_seq = excluded.log_seq,- landed_commit = excluded.landed_commit- `), repoID, bytesArg([]byte(entry.Reference)), land.Original, int64(entry.Sequence), land.Landed); err != nil {- return fmt.Errorf("insert land identity of changelog entry %d: %w", entry.Sequence, err)+ if len(entry.Lands) == 0 {+ return nil+ }++ last := make(map[string]int, len(entry.Lands))+ for i, land := range entry.Lands {+ last[land.Original] = i+ }++ branch := bytesArg([]byte(entry.Reference))+ values := make([]string, 0, len(last))+ args := make([]any, 0, 5*len(last))+ for i, land := range entry.Lands {+ if last[land.Original] != i {+ continue}+ values = append(values, "(?, ?, ?, ?, ?)")+ args = append(args, repoID, branch, land.Original, int64(entry.Sequence), land.Landed)+ }++ if _, err := q.ExecContext(ctx, s.bind(`+ INSERT INTO graft_land_identities (repo_id, branch, original_commit, log_seq, landed_commit)+ VALUES `+strings.Join(values, ", ")+`+ ON CONFLICT (repo_id, branch, original_commit) DO UPDATE SET+ log_seq = excluded.log_seq,+ landed_commit = excluded.landed_commit+ `), args...); err != nil {+ return fmt.Errorf("insert land identity of changelog entry %d: %w", entry.Sequence, err)}return nil}diff --git a/internal/graft/corestore/sqlstore/store_test.go b/internal/graft/corestore/sqlstore/store_test.goindex 3ce4869..0049ba9 100644--- a/internal/graft/corestore/sqlstore/store_test.go+++ b/internal/graft/corestore/sqlstore/store_test.go@@ -36,7 +36,7 @@ func forEachDialect(t *testing.T, fn func(t *testing.T, ctx context.Context, s *var openedDSNs sync.Map// openStore opens a store on dsn and closes it when the test ends.-func openStore(t *testing.T, ctx context.Context, dsn string) *sqlstore.Store {+func openStore(t testing.TB, ctx context.Context, dsn string) *sqlstore.Store {t.Helper()s, err := sqlstore.Open(ctx, dsn)