mirror of https://github.com/go-gitea/gitea.git
Optimization for user.GetRepositoryAccesses to reduce db query times (#495)
* optimization for user.GetRepositoryAccesses to reduce db query times * fix missing cache
This commit is contained in:
parent
9fae9f0dc2
commit
c463b1b8cb
|
@ -4,11 +4,7 @@
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AccessMode specifies the users access mode
|
// AccessMode specifies the users access mode
|
||||||
type AccessMode int
|
type AccessMode int
|
||||||
|
@ -103,26 +99,39 @@ func HasAccess(user *User, repo *Repository, testMode AccessMode) (bool, error)
|
||||||
// GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
|
// GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
|
||||||
func (user *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
|
func (user *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
|
||||||
accesses := make([]*Access, 0, 10)
|
accesses := make([]*Access, 0, 10)
|
||||||
if err := x.Find(&accesses, &Access{UserID: user.ID}); err != nil {
|
type RepoAccess struct {
|
||||||
return nil, err
|
Access `xorm:"extends"`
|
||||||
|
Repository `xorm:"extends"`
|
||||||
}
|
}
|
||||||
|
|
||||||
repos := make(map[*Repository]AccessMode, len(accesses))
|
rows, err := x.
|
||||||
for _, access := range accesses {
|
Join("INNER", "repository", "respository.id = access.repo_id").
|
||||||
repo, err := GetRepositoryByID(access.RepoID)
|
Where("access.user_id = ?", user.ID).
|
||||||
|
And("repository.owner_id <> ?", user.ID).
|
||||||
|
Rows(new(RepoAccess))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var repos = make(map[*Repository]AccessMode, len(accesses))
|
||||||
|
var ownerCache = make(map[int64]*User, len(accesses))
|
||||||
|
for rows.Next() {
|
||||||
|
var repo RepoAccess
|
||||||
|
err = rows.Scan(&repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if IsErrRepoNotExist(err) {
|
return nil, err
|
||||||
log.Error(4, "GetRepositoryByID: %v", err)
|
}
|
||||||
continue
|
|
||||||
|
var ok bool
|
||||||
|
if repo.Owner, ok = ownerCache[repo.OwnerID]; !ok {
|
||||||
|
if err = repo.GetOwner(); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return nil, err
|
ownerCache[repo.OwnerID] = repo.Owner
|
||||||
}
|
}
|
||||||
if err = repo.GetOwner(); err != nil {
|
|
||||||
return nil, err
|
repos[&repo.Repository] = repo.Access.Mode
|
||||||
} else if repo.OwnerID == user.ID {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
repos[repo] = access.Mode
|
|
||||||
}
|
}
|
||||||
return repos, nil
|
return repos, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue