Skip to content

Small refactor to reduce unnecessary database queries and remove duplicated functions #33779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions models/actions/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ func init() {
// GetSchedulesMapByIDs returns the schedules by given id slice.
func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
schedules := make(map[int64]*ActionSchedule, len(ids))
if len(ids) == 0 {
return schedules, nil
}
return schedules, db.GetEngine(ctx).In("id", ids).Find(&schedules)
}

// GetReposMapByIDs returns the repos by given id slice.
func GetReposMapByIDs(ctx context.Context, ids []int64) (map[int64]*repo_model.Repository, error) {
repos := make(map[int64]*repo_model.Repository, len(ids))
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
}

// CreateScheduleTask creates new schedule task.
func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
// Return early if there are no rows to insert
Expand Down
2 changes: 1 addition & 1 deletion models/actions/schedule_spec_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (specs SpecList) LoadSchedules(ctx context.Context) error {
}

repoIDs := specs.GetRepoIDs()
repos, err := GetReposMapByIDs(ctx, repoIDs)
repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions models/db/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([
// DecrByIDs decreases the given column for entities of the "bean" type with one of the given ids by one
// Timestamps of the entities won't be updated
func DecrByIDs(ctx context.Context, ids []int64, decrCol string, bean any) error {
if len(ids) == 0 {
return nil
}
_, err := GetEngine(ctx).Decr(decrCol).In("id", ids).NoAutoCondition().NoAutoTime().Update(bean)
return err
}
Expand Down
3 changes: 3 additions & 0 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ func GetIssueByID(ctx context.Context, id int64) (*Issue, error) {
// If keepOrder is true, the order of the returned issues will be the same as the given IDs.
func GetIssuesByIDs(ctx context.Context, issueIDs []int64, keepOrder ...bool) (IssueList, error) {
issues := make([]*Issue, 0, len(issueIDs))
if len(issueIDs) == 0 {
return issues, nil
}

if err := db.GetEngine(ctx).In("id", issueIDs).Find(&issues); err != nil {
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions models/issues/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ func GetLabelByID(ctx context.Context, labelID int64) (*Label, error) {
// GetLabelsByIDs returns a list of labels by IDs
func GetLabelsByIDs(ctx context.Context, labelIDs []int64, cols ...string) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
if len(labelIDs) == 0 {
return labels, nil
}
return labels, db.GetEngine(ctx).Table("label").
In("id", labelIDs).
Asc("name").
Expand Down Expand Up @@ -375,6 +378,9 @@ func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
// it silently ignores label IDs that do not belong to the repository.
func GetLabelsInRepoByIDs(ctx context.Context, repoID int64, labelIDs []int64) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
if len(labelIDs) == 0 {
return labels, nil
}
return labels, db.GetEngine(ctx).
Where("repo_id = ?", repoID).
In("id", labelIDs).
Expand Down Expand Up @@ -447,6 +453,9 @@ func GetLabelInOrgByID(ctx context.Context, orgID, labelID int64) (*Label, error
// it silently ignores label IDs that do not belong to the organization.
func GetLabelsInOrgByIDs(ctx context.Context, orgID int64, labelIDs []int64) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
if len(labelIDs) == 0 {
return labels, nil
}
return labels, db.GetEngine(ctx).
Where("org_id = ?", orgID).
In("id", labelIDs).
Expand Down
3 changes: 3 additions & 0 deletions models/organization/team_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,8 @@ func GetTeamsByOrgIDs(ctx context.Context, orgIDs []int64) (TeamList, error) {

func GetTeamsByIDs(ctx context.Context, teamIDs []int64) (map[int64]*Team, error) {
teams := make(map[int64]*Team, len(teamIDs))
if len(teamIDs) == 0 {
return teams, nil
}
return teams, db.GetEngine(ctx).Where(builder.In("`id`", teamIDs)).Find(&teams)
}
3 changes: 3 additions & 0 deletions models/project/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ func UpdateColumnSorting(ctx context.Context, cl ColumnList) error {

func GetColumnsByIDs(ctx context.Context, projectID int64, columnsIDs []int64) (ColumnList, error) {
columns := make([]*Column, 0, 5)
if len(columnsIDs) == 0 {
return columns, nil
}
if err := db.GetEngine(ctx).
Where("project_id =?", projectID).
In("id", columnsIDs).
Expand Down
3 changes: 3 additions & 0 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,9 @@ func GetRepositoryByID(ctx context.Context, id int64) (*Repository, error) {
// GetRepositoriesMapByIDs returns the repositories by given id slice.
func GetRepositoriesMapByIDs(ctx context.Context, ids []int64) (map[int64]*Repository, error) {
repos := make(map[int64]*Repository, len(ids))
if len(ids) == 0 {
return repos, nil
}
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
}

Expand Down
5 changes: 0 additions & 5 deletions models/repo/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import (
"xorm.io/builder"
)

// FindReposMapByIDs find repos as map
func FindReposMapByIDs(ctx context.Context, repoIDs []int64, res map[int64]*Repository) error {
return db.GetEngine(ctx).In("id", repoIDs).Find(&res)
}

// RepositoryListDefaultPageSize is the default number of repositories
// to load in memory when running administrative tasks on all (or almost
// all) of them.
Expand Down
4 changes: 4 additions & 0 deletions models/user/user_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (

func GetUsersMapByIDs(ctx context.Context, userIDs []int64) (map[int64]*User, error) {
userMaps := make(map[int64]*User, len(userIDs))
if len(userIDs) == 0 {
return userMaps, nil
}

left := len(userIDs)
for left > 0 {
limit := db.DefaultMaxInSize
Expand Down