Fix FIXME and remove superfluous queries in models/org #749
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I realize this is long, but I want to give some context for my changes. Please bear with me, or jump to the TL;DR at the bottom if you're impatient.
This PR addresses the following
FIXME
inmodels/org.go, line 474-475
(an identicalFIXME
is also inmodels/action.go, line 661
):org.GetUserRepositories(uid, ..)
first computes the necessary repoIDs, then loads the repos with those repoIDs, then finally computes a count (the second return value, which is ignored above). It would therefore make sense to create a separate function for computing the repoIDs (sayorg.GetUserRepositoryIDs(uid, ..)
) that bothorg.GetUserRepositories(..)
andmodels/org.go, 474-475
can use.There's more: of the several places where
org.GetUserRepositories(..)
is called, the count is ignored in all but one place (routers/user/home.go, line 355
). So most of the time whenorg.GetUserRepositories(..)
is called, the count is calculated (which involves a join), and then the count is immediately thrown away. I therefore decided to not haveorg.GetUserRepositories(..)
compute the count, but instead move that logic to another function (sayorg.GetAccessibleReposCount(uid)
) to avoid wasting work.routers/user/home.go, line 355
would then make two calls: one toorg.GetUserRepositories(..)
and another toorg.GetAccessibleRepoCount(..)
.This seems promising, but there's one problem with this approach: both computing the repoIDs (in
org.GetUserRepositoryIDs(..)
, and by extensionorg.GetUserRepositories(..)
) and computing the count (org.GetAccessibleRepoCount(..)
) require determining which teams the specified user belongs to. Inrouters/user/home.go, line 355
, where I actually care about the count, I will need to compute the teams that a user belongs to twice (once fororg.GetUserRepositories()
, another time fororg.GetAccessibleRepoCount()
). The same problem occurs in places where bothorg.GetUserRepositories()
andorg.GetUserMirrorRepositories()
) are called, (e.g.routers/user/home.go, line 116
).Taking a step back, there are three functions
org.GetUserRepositoryIDs(uid, ..)
org.GetAccessibleRepositoryCount(uid)
org.GetUserMirrorRepositories(uid)
all of which require the same preprocessing, and we would like to avoid doing the preprocessing more than once if we call these functions multiple times with the same
uid
. The solution that occurred to me was to move these functions to aAccessibleRepoEnvironment
interface. The implementation of the interface,accessibleRepoEnv
, would store the results of the preprocessing (i.e. the teamIDs a particular user belongs to), and each function/method would just use those results instead of (re)computing them.Under my changes, to call these functions/method, one can do the following
and no work will be wasted or duplicated 😃. The interface, or at least the idea behind, could also be used in other parts of the codebase.
TL;DR: I got rid of useless/duplicated queries by moving some functions to an interface.
I'm open to suggestions (particularly for naming) and would be happy to hear any feedback on this.