Skip to content
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

Optimization for user.GetRepositoryAccesses to reduce db query times #495

Merged
Merged
Changes from 1 commit
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
Next Next commit
optimization for user.GetRepositoryAccesses to reduce db query times
  • Loading branch information
lunny committed Dec 27, 2016
commit 3c2fd7b705f29eb19baae68dd3d7e0195d00b032
44 changes: 26 additions & 18 deletions models/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

package models

import (
"fmt"

"code.gitea.io/gitea/modules/log"
)
import "fmt"

// AccessMode specifies the users access mode
type AccessMode int
Expand Down Expand Up @@ -103,26 +99,38 @@ 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.
func (user *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
accesses := make([]*Access, 0, 10)
if err := x.Find(&accesses, &Access{UserID: user.ID}); err != nil {
type RepoAccess struct {
Access `xorm:"extends"`
Repository `xorm:"extends"`
}

rows, err := x.
Join("INNER", "repository", "respository.id = access.repo_id").
Where("access.user_id = ?", user.ID).
And("repository.owner_id <> ?", user.ID).
Rows(new(RepoAccess))
if err != nil {
return nil, err
}
defer rows.Close()

repos := make(map[*Repository]AccessMode, len(accesses))
for _, access := range accesses {
repo, err := GetRepositoryByID(access.RepoID)
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 IsErrRepoNotExist(err) {
log.Error(4, "GetRepositoryByID: %v", err)
continue
}
return nil, err
}
if err = repo.GetOwner(); err != nil {
return nil, err
} else if repo.OwnerID == user.ID {
continue

var ok bool
if repo.Owner, ok = ownerCache[repo.OwnerID]; !ok {
if err = repo.GetOwner(); err != nil {
return nil, err
}
}
repos[repo] = access.Mode

repos[&repo.Repository] = repo.Access.Mode
}
return repos, nil
}
Expand Down