summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-10-12 07:18:26 +0200
committerGitHub <noreply@github.com>2022-10-12 13:18:26 +0800
commit0e57ff7eee4ac71d923f970d15889ad4d50f97a9 (patch)
treee5a92c55af5366924bd40ae14b4bf12842239193 /models
parente84558b0931309cf1f4f2767bc47296483b9b3e1 (diff)
downloadgitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.tar.gz
gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.zip
Add generic set type (#21408)
This PR adds a generic set type to get rid of maps used as sets. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'models')
-rw-r--r--models/activities/action_list.go16
-rw-r--r--models/activities/notification.go46
-rw-r--r--models/issues/comment_list.go64
-rw-r--r--models/issues/issue_list.go33
-rw-r--r--models/issues/reaction.go10
-rw-r--r--models/migrate.go5
-rw-r--r--models/migrations/v115.go5
-rw-r--r--models/packages/conan/search.go5
-rw-r--r--models/repo/repo_list.go6
-rw-r--r--models/repo/topic.go7
-rw-r--r--models/repo/user_repo.go31
11 files changed, 87 insertions, 141 deletions
diff --git a/models/activities/action_list.go b/models/activities/action_list.go
index 16fb4bac8c..f349b94ac8 100644
--- a/models/activities/action_list.go
+++ b/models/activities/action_list.go
@@ -18,13 +18,11 @@ import (
type ActionList []*Action
func (actions ActionList) getUserIDs() []int64 {
- userIDs := make(map[int64]struct{}, len(actions))
+ userIDs := make(container.Set[int64], len(actions))
for _, action := range actions {
- if _, ok := userIDs[action.ActUserID]; !ok {
- userIDs[action.ActUserID] = struct{}{}
- }
+ userIDs.Add(action.ActUserID)
}
- return container.KeysInt64(userIDs)
+ return userIDs.Values()
}
func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.User, error) {
@@ -48,13 +46,11 @@ func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.
}
func (actions ActionList) getRepoIDs() []int64 {
- repoIDs := make(map[int64]struct{}, len(actions))
+ repoIDs := make(container.Set[int64], len(actions))
for _, action := range actions {
- if _, ok := repoIDs[action.RepoID]; !ok {
- repoIDs[action.RepoID] = struct{}{}
- }
+ repoIDs.Add(action.RepoID)
}
- return container.KeysInt64(repoIDs)
+ return repoIDs.Values()
}
func (actions ActionList) loadRepositories(ctx context.Context) error {
diff --git a/models/activities/notification.go b/models/activities/notification.go
index 88776db42b..2f21dc74d1 100644
--- a/models/activities/notification.go
+++ b/models/activities/notification.go
@@ -200,7 +200,7 @@ func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID,
func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, notificationAuthorID, receiverID int64) error {
// init
- var toNotify map[int64]struct{}
+ var toNotify container.Set[int64]
notifications, err := getNotificationsByIssueID(ctx, issueID)
if err != nil {
return err
@@ -212,33 +212,27 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
}
if receiverID > 0 {
- toNotify = make(map[int64]struct{}, 1)
- toNotify[receiverID] = struct{}{}
+ toNotify = make(container.Set[int64], 1)
+ toNotify.Add(receiverID)
} else {
- toNotify = make(map[int64]struct{}, 32)
+ toNotify = make(container.Set[int64], 32)
issueWatches, err := issues_model.GetIssueWatchersIDs(ctx, issueID, true)
if err != nil {
return err
}
- for _, id := range issueWatches {
- toNotify[id] = struct{}{}
- }
+ toNotify.AddMultiple(issueWatches...)
if !(issue.IsPull && issues_model.HasWorkInProgressPrefix(issue.Title)) {
repoWatches, err := repo_model.GetRepoWatchersIDs(ctx, issue.RepoID)
if err != nil {
return err
}
- for _, id := range repoWatches {
- toNotify[id] = struct{}{}
- }
+ toNotify.AddMultiple(repoWatches...)
}
issueParticipants, err := issue.GetParticipantIDsByIssue(ctx)
if err != nil {
return err
}
- for _, id := range issueParticipants {
- toNotify[id] = struct{}{}
- }
+ toNotify.AddMultiple(issueParticipants...)
// dont notify user who cause notification
delete(toNotify, notificationAuthorID)
@@ -248,7 +242,7 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
return err
}
for _, id := range issueUnWatches {
- delete(toNotify, id)
+ toNotify.Remove(id)
}
}
@@ -499,16 +493,14 @@ func (nl NotificationList) LoadAttributes() error {
}
func (nl NotificationList) getPendingRepoIDs() []int64 {
- ids := make(map[int64]struct{}, len(nl))
+ ids := make(container.Set[int64], len(nl))
for _, notification := range nl {
if notification.Repository != nil {
continue
}
- if _, ok := ids[notification.RepoID]; !ok {
- ids[notification.RepoID] = struct{}{}
- }
+ ids.Add(notification.RepoID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
// LoadRepos loads repositories from database
@@ -575,16 +567,14 @@ func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error)
}
func (nl NotificationList) getPendingIssueIDs() []int64 {
- ids := make(map[int64]struct{}, len(nl))
+ ids := make(container.Set[int64], len(nl))
for _, notification := range nl {
if notification.Issue != nil {
continue
}
- if _, ok := ids[notification.IssueID]; !ok {
- ids[notification.IssueID] = struct{}{}
- }
+ ids.Add(notification.IssueID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
// LoadIssues loads issues from database
@@ -661,16 +651,14 @@ func (nl NotificationList) Without(failures []int) NotificationList {
}
func (nl NotificationList) getPendingCommentIDs() []int64 {
- ids := make(map[int64]struct{}, len(nl))
+ ids := make(container.Set[int64], len(nl))
for _, notification := range nl {
if notification.CommentID == 0 || notification.Comment != nil {
continue
}
- if _, ok := ids[notification.CommentID]; !ok {
- ids[notification.CommentID] = struct{}{}
- }
+ ids.Add(notification.CommentID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
// LoadComments loads comments from database
diff --git a/models/issues/comment_list.go b/models/issues/comment_list.go
index e3406a5cbe..70105d7ff0 100644
--- a/models/issues/comment_list.go
+++ b/models/issues/comment_list.go
@@ -17,13 +17,11 @@ import (
type CommentList []*Comment
func (comments CommentList) getPosterIDs() []int64 {
- posterIDs := make(map[int64]struct{}, len(comments))
+ posterIDs := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := posterIDs[comment.PosterID]; !ok {
- posterIDs[comment.PosterID] = struct{}{}
- }
+ posterIDs.Add(comment.PosterID)
}
- return container.KeysInt64(posterIDs)
+ return posterIDs.Values()
}
func (comments CommentList) loadPosters(ctx context.Context) error {
@@ -70,13 +68,11 @@ func (comments CommentList) getCommentIDs() []int64 {
}
func (comments CommentList) getLabelIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.LabelID]; !ok {
- ids[comment.LabelID] = struct{}{}
- }
+ ids.Add(comment.LabelID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
@@ -120,13 +116,11 @@ func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
}
func (comments CommentList) getMilestoneIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.MilestoneID]; !ok {
- ids[comment.MilestoneID] = struct{}{}
- }
+ ids.Add(comment.MilestoneID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadMilestones(ctx context.Context) error {
@@ -163,13 +157,11 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
}
func (comments CommentList) getOldMilestoneIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.OldMilestoneID]; !ok {
- ids[comment.OldMilestoneID] = struct{}{}
- }
+ ids.Add(comment.OldMilestoneID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadOldMilestones(ctx context.Context) error {
@@ -206,13 +198,11 @@ func (comments CommentList) loadOldMilestones(ctx context.Context) error {
}
func (comments CommentList) getAssigneeIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.AssigneeID]; !ok {
- ids[comment.AssigneeID] = struct{}{}
- }
+ ids.Add(comment.AssigneeID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadAssignees(ctx context.Context) error {
@@ -259,16 +249,14 @@ func (comments CommentList) loadAssignees(ctx context.Context) error {
// getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
func (comments CommentList) getIssueIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if comment.Issue != nil {
continue
}
- if _, ok := ids[comment.IssueID]; !ok {
- ids[comment.IssueID] = struct{}{}
- }
+ ids.Add(comment.IssueID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
// Issues returns all the issues of comments
@@ -334,16 +322,14 @@ func (comments CommentList) loadIssues(ctx context.Context) error {
}
func (comments CommentList) getDependentIssueIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if comment.DependentIssue != nil {
continue
}
- if _, ok := ids[comment.DependentIssueID]; !ok {
- ids[comment.DependentIssueID] = struct{}{}
- }
+ ids.Add(comment.DependentIssueID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadDependentIssues(ctx context.Context) error {
@@ -439,13 +425,11 @@ func (comments CommentList) loadAttachments(ctx context.Context) (err error) {
}
func (comments CommentList) getReviewIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.ReviewID]; !ok {
- ids[comment.ReviewID] = struct{}{}
- }
+ ids.Add(comment.ReviewID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadReviews(ctx context.Context) error { //nolint
diff --git a/models/issues/issue_list.go b/models/issues/issue_list.go
index 874f2a6368..deadb6a564 100644
--- a/models/issues/issue_list.go
+++ b/models/issues/issue_list.go
@@ -22,16 +22,16 @@ type IssueList []*Issue
// get the repo IDs to be loaded later, these IDs are for issue.Repo and issue.PullRequest.HeadRepo
func (issues IssueList) getRepoIDs() []int64 {
- repoIDs := make(map[int64]struct{}, len(issues))
+ repoIDs := make(container.Set[int64], len(issues))
for _, issue := range issues {
if issue.Repo == nil {
- repoIDs[issue.RepoID] = struct{}{}
+ repoIDs.Add(issue.RepoID)
}
if issue.PullRequest != nil && issue.PullRequest.HeadRepo == nil {
- repoIDs[issue.PullRequest.HeadRepoID] = struct{}{}
+ repoIDs.Add(issue.PullRequest.HeadRepoID)
}
}
- return container.KeysInt64(repoIDs)
+ return repoIDs.Values()
}
func (issues IssueList) loadRepositories(ctx context.Context) ([]*repo_model.Repository, error) {
@@ -79,13 +79,11 @@ func (issues IssueList) LoadRepositories() ([]*repo_model.Repository, error) {
}
func (issues IssueList) getPosterIDs() []int64 {
- posterIDs := make(map[int64]struct{}, len(issues))
+ posterIDs := make(container.Set[int64], len(issues))
for _, issue := range issues {
- if _, ok := posterIDs[issue.PosterID]; !ok {
- posterIDs[issue.PosterID] = struct{}{}
- }
+ posterIDs.Add(issue.PosterID)
}
- return container.KeysInt64(posterIDs)
+ return posterIDs.Values()
}
func (issues IssueList) loadPosters(ctx context.Context) error {
@@ -185,13 +183,11 @@ func (issues IssueList) loadLabels(ctx context.Context) error {
}
func (issues IssueList) getMilestoneIDs() []int64 {
- ids := make(map[int64]struct{}, len(issues))
+ ids := make(container.Set[int64], len(issues))
for _, issue := range issues {
- if _, ok := ids[issue.MilestoneID]; !ok {
- ids[issue.MilestoneID] = struct{}{}
- }
+ ids.Add(issue.MilestoneID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (issues IssueList) loadMilestones(ctx context.Context) error {
@@ -224,14 +220,11 @@ func (issues IssueList) loadMilestones(ctx context.Context) error {
}
func (issues IssueList) getProjectIDs() []int64 {
- ids := make(map[int64]struct{}, len(issues))
+ ids := make(container.Set[int64], len(issues))
for _, issue := range issues {
- projectID := issue.ProjectID()
- if _, ok := ids[projectID]; !ok {
- ids[projectID] = struct{}{}
- }
+ ids.Add(issue.ProjectID())
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (issues IssueList) loadProjects(ctx context.Context) error {
diff --git a/models/issues/reaction.go b/models/issues/reaction.go
index e7295c8af8..ccda10be2c 100644
--- a/models/issues/reaction.go
+++ b/models/issues/reaction.go
@@ -211,7 +211,7 @@ type ReactionOptions struct {
// CreateReaction creates reaction for issue or comment.
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
- if !setting.UI.ReactionsMap[opts.Type] {
+ if !setting.UI.ReactionsLookup.Contains(opts.Type) {
return nil, ErrForbiddenIssueReaction{opts.Type}
}
@@ -316,16 +316,14 @@ func (list ReactionList) GroupByType() map[string]ReactionList {
}
func (list ReactionList) getUserIDs() []int64 {
- userIDs := make(map[int64]struct{}, len(list))
+ userIDs := make(container.Set[int64], len(list))
for _, reaction := range list {
if reaction.OriginalAuthor != "" {
continue
}
- if _, ok := userIDs[reaction.UserID]; !ok {
- userIDs[reaction.UserID] = struct{}{}
- }
+ userIDs.Add(reaction.UserID)
}
- return container.KeysInt64(userIDs)
+ return userIDs.Values()
}
func valuesUser(m map[int64]*user_model.User) []*user_model.User {
diff --git a/models/migrate.go b/models/migrate.go
index f6bceaa019..d842fb967b 100644
--- a/models/migrate.go
+++ b/models/migrate.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/structs"
)
@@ -99,9 +100,9 @@ func InsertIssueComments(comments []*issues_model.Comment) error {
return nil
}
- issueIDs := make(map[int64]bool)
+ issueIDs := make(container.Set[int64])
for _, comment := range comments {
- issueIDs[comment.IssueID] = true
+ issueIDs.Add(comment.IssueID)
}
ctx, committer, err := db.TxContext()
diff --git a/models/migrations/v115.go b/models/migrations/v115.go
index 7708ed5e28..b242ccf238 100644
--- a/models/migrations/v115.go
+++ b/models/migrations/v115.go
@@ -13,6 +13,7 @@ import (
"path/filepath"
"time"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
@@ -39,7 +40,7 @@ func renameExistingUserAvatarName(x *xorm.Engine) error {
}
log.Info("%d User Avatar(s) to migrate ...", count)
- deleteList := make(map[string]struct{})
+ deleteList := make(container.Set[string])
start := 0
migrated := 0
for {
@@ -86,7 +87,7 @@ func renameExistingUserAvatarName(x *xorm.Engine) error {
return fmt.Errorf("[user: %s] user table update: %v", user.LowerName, err)
}
- deleteList[filepath.Join(setting.Avatar.Path, oldAvatar)] = struct{}{}
+ deleteList.Add(filepath.Join(setting.Avatar.Path, oldAvatar))
migrated++
select {
case <-ticker.C:
diff --git a/models/packages/conan/search.go b/models/packages/conan/search.go
index 6a2cfa38f5..39a9000459 100644
--- a/models/packages/conan/search.go
+++ b/models/packages/conan/search.go
@@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/packages"
+ "code.gitea.io/gitea/modules/container"
conan_module "code.gitea.io/gitea/modules/packages/conan"
"xorm.io/builder"
@@ -88,7 +89,7 @@ func SearchRecipes(ctx context.Context, opts *RecipeSearchOptions) ([]string, er
return nil, err
}
- unique := make(map[string]bool)
+ unique := make(container.Set[string])
for _, info := range results {
recipe := fmt.Sprintf("%s/%s", info.Name, info.Version)
@@ -111,7 +112,7 @@ func SearchRecipes(ctx context.Context, opts *RecipeSearchOptions) ([]string, er
}
}
- unique[recipe] = true
+ unique.Add(recipe)
}
recipes := make([]string, 0, len(unique))
diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go
index cc524a417e..0cd0a3c8e3 100644
--- a/models/repo/repo_list.go
+++ b/models/repo/repo_list.go
@@ -68,10 +68,10 @@ func (repos RepositoryList) loadAttributes(ctx context.Context) error {
return nil
}
- set := make(map[int64]struct{})
+ set := make(container.Set[int64])
repoIDs := make([]int64, len(repos))
for i := range repos {
- set[repos[i].OwnerID] = struct{}{}
+ set.Add(repos[i].OwnerID)
repoIDs[i] = repos[i].ID
}
@@ -79,7 +79,7 @@ func (repos RepositoryList) loadAttributes(ctx context.Context) error {
users := make(map[int64]*user_model.User, len(set))
if err := db.GetEngine(ctx).
Where("id > 0").
- In("id", container.KeysInt64(set)).
+ In("id", set.Values()).
Find(&users); err != nil {
return fmt.Errorf("find users: %v", err)
}
diff --git a/models/repo/topic.go b/models/repo/topic.go
index 2a16467215..7ba9a49e89 100644
--- a/models/repo/topic.go
+++ b/models/repo/topic.go
@@ -11,6 +11,7 @@ import (
"strings"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder"
@@ -62,7 +63,7 @@ func ValidateTopic(topic string) bool {
// SanitizeAndValidateTopics sanitizes and checks an array or topics
func SanitizeAndValidateTopics(topics []string) (validTopics, invalidTopics []string) {
validTopics = make([]string, 0)
- mValidTopics := make(map[string]struct{})
+ mValidTopics := make(container.Set[string])
invalidTopics = make([]string, 0)
for _, topic := range topics {
@@ -72,12 +73,12 @@ func SanitizeAndValidateTopics(topics []string) (validTopics, invalidTopics []st
continue
}
// ignore same topic twice
- if _, ok := mValidTopics[topic]; ok {
+ if mValidTopics.Contains(topic) {
continue
}
if ValidateTopic(topic) {
validTopics = append(validTopics, topic)
- mValidTopics[topic] = struct{}{}
+ mValidTopics.Add(topic)
} else {
invalidTopics = append(invalidTopics, topic)
}
diff --git a/models/repo/user_repo.go b/models/repo/user_repo.go
index 6c0a241dc5..e7125f70f8 100644
--- a/models/repo/user_repo.go
+++ b/models/repo/user_repo.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/container"
api "code.gitea.io/gitea/modules/structs"
"xorm.io/builder"
@@ -83,37 +84,19 @@ func GetRepoAssignees(ctx context.Context, repo *Repository) (_ []*user_model.Us
return nil, err
}
- uidMap := map[int64]bool{}
- i := 0
- for _, uid := range userIDs {
- if uidMap[uid] {
- continue
- }
- uidMap[uid] = true
- userIDs[i] = uid
- i++
- }
- userIDs = userIDs[:i]
- userIDs = append(userIDs, additionalUserIDs...)
-
- for _, uid := range additionalUserIDs {
- if uidMap[uid] {
- continue
- }
- userIDs[i] = uid
- i++
- }
- userIDs = userIDs[:i]
+ uniqueUserIDs := make(container.Set[int64])
+ uniqueUserIDs.AddMultiple(userIDs...)
+ uniqueUserIDs.AddMultiple(additionalUserIDs...)
// Leave a seat for owner itself to append later, but if owner is an organization
// and just waste 1 unit is cheaper than re-allocate memory once.
- users := make([]*user_model.User, 0, len(userIDs)+1)
+ users := make([]*user_model.User, 0, len(uniqueUserIDs)+1)
if len(userIDs) > 0 {
- if err = e.In("id", userIDs).OrderBy(user_model.GetOrderByName()).Find(&users); err != nil {
+ if err = e.In("id", uniqueUserIDs.Values()).OrderBy(user_model.GetOrderByName()).Find(&users); err != nil {
return nil, err
}
}
- if !repo.Owner.IsOrganization() && !uidMap[repo.OwnerID] {
+ if !repo.Owner.IsOrganization() && !uniqueUserIDs.Contains(repo.OwnerID) {
users = append(users, repo.Owner)
}