History

Add commit status summary table to reduce query from commit status table (#30223)

This PR adds a new table named commit status summary to reduce queries from the commit status table. After this change, commit status summary table will be used for the final result, commit status table will be for details. ---------

Lunny Xiao committed 2 years ago

Commit 64957411 parent 5a10eec6 files · +162 −19Browse files

Showing the changes to models/git/commit_status.go (1 of 6 files). Show all

Unified · Split

models/git/commit_status.go+9−12
@@ -257,30 +257,27 @@ func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOp · 256 unmodified lines
257}257}
258 258
259// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs259// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
260func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHAs map[int64]string, listOptions db.ListOptions) (map[int64][]*CommitStatus, error) {260func GetLatestCommitStatusForPairs(ctx context.Context, repoSHAs []RepoSHA) (map[int64][]*CommitStatus, error) {
261 type result struct {261 type result struct {
262 Index int64262 Index int64
263 RepoID int64263 RepoID int64
264 SHA string
264 }265 }
265 266
266 results := make([]result, 0, len(repoIDsToLatestCommitSHAs))267 results := make([]result, 0, len(repoSHAs))
267 268
268 getBase := func() *xorm.Session {269 getBase := func() *xorm.Session {
269 return db.GetEngine(ctx).Table(&CommitStatus{})270 return db.GetEngine(ctx).Table(&CommitStatus{})
270 }271 }
271 272
272 // Create a disjunction of conditions for each repoID and SHA pair273 // Create a disjunction of conditions for each repoID and SHA pair
273 conds := make([]builder.Cond, 0, len(repoIDsToLatestCommitSHAs))274 conds := make([]builder.Cond, 0, len(repoSHAs))
274 for repoID, sha := range repoIDsToLatestCommitSHAs {275 for _, repoSHA := range repoSHAs {
275 conds = append(conds, builder.Eq{"repo_id": repoID, "sha": sha})276 conds = append(conds, builder.Eq{"repo_id": repoSHA.RepoID, "sha": repoSHA.SHA})
276 }277 }
277 sess := getBase().Where(builder.Or(conds...)).278 sess := getBase().Where(builder.Or(conds...)).
278 Select("max( `index` ) as `index`, repo_id").279 Select("max( `index` ) as `index`, repo_id, sha").
279 GroupBy("context_hash, repo_id").OrderBy("max( `index` ) desc")280 GroupBy("context_hash, repo_id, sha").OrderBy("max( `index` ) desc")
280
281 if !listOptions.IsListAll() {
282 sess = db.SetSessionPagination(sess, &listOptions)
283 }
284 281
285 err := sess.Find(&results)282 err := sess.Find(&results)
286 if err != nil {283 if err != nil {
@@ -297,7 +294,7 @@ func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHA · 10 unmodified lines
297 cond := builder.Eq{294 cond := builder.Eq{
298 "`index`": result.Index,295 "`index`": result.Index,
299 "repo_id": result.RepoID,296 "repo_id": result.RepoID,
300 "sha": repoIDsToLatestCommitSHAs[result.RepoID],297 "sha": result.SHA,
301 }298 }
302 conds = append(conds, cond)299 conds = append(conds, cond)
303 }300 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t