mirror of
https://github.com/go-gitea/gitea
synced 2025-03-10 04:34:26 +00:00
Small refactor to reduce unnecessary database queries and remove duplicated functions (#33779)
This commit is contained in:
parent
75e85c25c1
commit
6c8fb8d455
@ -43,13 +43,10 @@ func init() {
|
|||||||
// GetSchedulesMapByIDs returns the schedules by given id slice.
|
// GetSchedulesMapByIDs returns the schedules by given id slice.
|
||||||
func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
|
func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
|
||||||
schedules := make(map[int64]*ActionSchedule, len(ids))
|
schedules := make(map[int64]*ActionSchedule, len(ids))
|
||||||
return schedules, db.GetEngine(ctx).In("id", ids).Find(&schedules)
|
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.
|
// CreateScheduleTask creates new schedule task.
|
||||||
|
@ -32,7 +32,7 @@ func (specs SpecList) LoadSchedules(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
repoIDs := specs.GetRepoIDs()
|
repoIDs := specs.GetRepoIDs()
|
||||||
repos, err := GetReposMapByIDs(ctx, repoIDs)
|
repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -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
|
// 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
|
// Timestamps of the entities won't be updated
|
||||||
func DecrByIDs(ctx context.Context, ids []int64, decrCol string, bean any) error {
|
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)
|
_, err := GetEngine(ctx).Decr(decrCol).In("id", ids).NoAutoCondition().NoAutoTime().Update(bean)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -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.
|
// 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) {
|
func GetIssuesByIDs(ctx context.Context, issueIDs []int64, keepOrder ...bool) (IssueList, error) {
|
||||||
issues := make([]*Issue, 0, len(issueIDs))
|
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 {
|
if err := db.GetEngine(ctx).In("id", issueIDs).Find(&issues); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -299,6 +299,9 @@ func GetLabelByID(ctx context.Context, labelID int64) (*Label, error) {
|
|||||||
// GetLabelsByIDs returns a list of labels by IDs
|
// GetLabelsByIDs returns a list of labels by IDs
|
||||||
func GetLabelsByIDs(ctx context.Context, labelIDs []int64, cols ...string) ([]*Label, error) {
|
func GetLabelsByIDs(ctx context.Context, labelIDs []int64, cols ...string) ([]*Label, error) {
|
||||||
labels := make([]*Label, 0, len(labelIDs))
|
labels := make([]*Label, 0, len(labelIDs))
|
||||||
|
if len(labelIDs) == 0 {
|
||||||
|
return labels, nil
|
||||||
|
}
|
||||||
return labels, db.GetEngine(ctx).Table("label").
|
return labels, db.GetEngine(ctx).Table("label").
|
||||||
In("id", labelIDs).
|
In("id", labelIDs).
|
||||||
Asc("name").
|
Asc("name").
|
||||||
@ -375,6 +378,9 @@ func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
|
|||||||
// it silently ignores label IDs that do not belong to the repository.
|
// it silently ignores label IDs that do not belong to the repository.
|
||||||
func GetLabelsInRepoByIDs(ctx context.Context, repoID int64, labelIDs []int64) ([]*Label, error) {
|
func GetLabelsInRepoByIDs(ctx context.Context, repoID int64, labelIDs []int64) ([]*Label, error) {
|
||||||
labels := make([]*Label, 0, len(labelIDs))
|
labels := make([]*Label, 0, len(labelIDs))
|
||||||
|
if len(labelIDs) == 0 {
|
||||||
|
return labels, nil
|
||||||
|
}
|
||||||
return labels, db.GetEngine(ctx).
|
return labels, db.GetEngine(ctx).
|
||||||
Where("repo_id = ?", repoID).
|
Where("repo_id = ?", repoID).
|
||||||
In("id", labelIDs).
|
In("id", labelIDs).
|
||||||
@ -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.
|
// it silently ignores label IDs that do not belong to the organization.
|
||||||
func GetLabelsInOrgByIDs(ctx context.Context, orgID int64, labelIDs []int64) ([]*Label, error) {
|
func GetLabelsInOrgByIDs(ctx context.Context, orgID int64, labelIDs []int64) ([]*Label, error) {
|
||||||
labels := make([]*Label, 0, len(labelIDs))
|
labels := make([]*Label, 0, len(labelIDs))
|
||||||
|
if len(labelIDs) == 0 {
|
||||||
|
return labels, nil
|
||||||
|
}
|
||||||
return labels, db.GetEngine(ctx).
|
return labels, db.GetEngine(ctx).
|
||||||
Where("org_id = ?", orgID).
|
Where("org_id = ?", orgID).
|
||||||
In("id", labelIDs).
|
In("id", labelIDs).
|
||||||
|
@ -133,5 +133,8 @@ func GetTeamsByOrgIDs(ctx context.Context, orgIDs []int64) (TeamList, error) {
|
|||||||
|
|
||||||
func GetTeamsByIDs(ctx context.Context, teamIDs []int64) (map[int64]*Team, error) {
|
func GetTeamsByIDs(ctx context.Context, teamIDs []int64) (map[int64]*Team, error) {
|
||||||
teams := make(map[int64]*Team, len(teamIDs))
|
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)
|
return teams, db.GetEngine(ctx).Where(builder.In("`id`", teamIDs)).Find(&teams)
|
||||||
}
|
}
|
||||||
|
@ -336,6 +336,9 @@ func UpdateColumnSorting(ctx context.Context, cl ColumnList) error {
|
|||||||
|
|
||||||
func GetColumnsByIDs(ctx context.Context, projectID int64, columnsIDs []int64) (ColumnList, error) {
|
func GetColumnsByIDs(ctx context.Context, projectID int64, columnsIDs []int64) (ColumnList, error) {
|
||||||
columns := make([]*Column, 0, 5)
|
columns := make([]*Column, 0, 5)
|
||||||
|
if len(columnsIDs) == 0 {
|
||||||
|
return columns, nil
|
||||||
|
}
|
||||||
if err := db.GetEngine(ctx).
|
if err := db.GetEngine(ctx).
|
||||||
Where("project_id =?", projectID).
|
Where("project_id =?", projectID).
|
||||||
In("id", columnsIDs).
|
In("id", columnsIDs).
|
||||||
|
@ -839,6 +839,9 @@ func GetRepositoryByID(ctx context.Context, id int64) (*Repository, error) {
|
|||||||
// GetRepositoriesMapByIDs returns the repositories by given id slice.
|
// GetRepositoriesMapByIDs returns the repositories by given id slice.
|
||||||
func GetRepositoriesMapByIDs(ctx context.Context, ids []int64) (map[int64]*Repository, error) {
|
func GetRepositoriesMapByIDs(ctx context.Context, ids []int64) (map[int64]*Repository, error) {
|
||||||
repos := make(map[int64]*Repository, len(ids))
|
repos := make(map[int64]*Repository, len(ids))
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return repos, nil
|
||||||
|
}
|
||||||
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
|
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,11 +21,6 @@ import (
|
|||||||
"xorm.io/builder"
|
"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
|
// RepositoryListDefaultPageSize is the default number of repositories
|
||||||
// to load in memory when running administrative tasks on all (or almost
|
// to load in memory when running administrative tasks on all (or almost
|
||||||
// all) of them.
|
// all) of them.
|
||||||
|
@ -11,6 +11,10 @@ import (
|
|||||||
|
|
||||||
func GetUsersMapByIDs(ctx context.Context, userIDs []int64) (map[int64]*User, error) {
|
func GetUsersMapByIDs(ctx context.Context, userIDs []int64) (map[int64]*User, error) {
|
||||||
userMaps := make(map[int64]*User, len(userIDs))
|
userMaps := make(map[int64]*User, len(userIDs))
|
||||||
|
if len(userIDs) == 0 {
|
||||||
|
return userMaps, nil
|
||||||
|
}
|
||||||
|
|
||||||
left := len(userIDs)
|
left := len(userIDs)
|
||||||
for left > 0 {
|
for left > 0 {
|
||||||
limit := db.DefaultMaxInSize
|
limit := db.DefaultMaxInSize
|
||||||
|
Loading…
x
Reference in New Issue
Block a user