Skip to content

Commit

Permalink
Use sequence instead of list to utilize lazy transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
CamaradeRoman committed May 23, 2024
1 parent f20cd5b commit 76f7b43
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class RepositoryControllerOld(
@RequestHeader(Constants.DEFERRED_RESULT_HEADER, required = false) requestId: String?
) = processJob(requestId ?: UUID.randomUUID().toString()) {
log.info("Get commits ({},{}] in `{}` repository", (from ?: fromDate?.toString()).orEmpty(), to, vcsPath)
val commits = vcsManager.getCommits(vcsPath, from, fromDate, to).map { it.toOld() }
RepositoryResponse(commits)
RepositoryResponse(vcsManager.getCommits(vcsPath, from, fromDate, to).map { it.toOld() })
}.data

@Deprecated(
Expand Down Expand Up @@ -100,11 +99,10 @@ class RepositoryControllerOld(
"Find issue keys in commits ({},{}] in `{}` repository",
(from ?: fromDate?.toString()).orEmpty(), to, vcsPath
)
val issues = vcsManager.getCommits(vcsPath, from, fromDate, to)
.map { it.toOld() }
.flatMap { IssueKeyParser.findIssueKeys(it.message) }
.distinct()
RepositoryResponse(issues)
RepositoryResponse(
vcsManager.getCommits(vcsPath, from, fromDate, to)
.flatMap { IssueKeyParser.findIssueKeys(it.message) }.distinct()
)
}.data

@GetMapping("issues/{issueKey}", produces = [MediaType.APPLICATION_JSON_VALUE])
Expand All @@ -113,8 +111,7 @@ class RepositoryControllerOld(
@RequestHeader(Constants.DEFERRED_RESULT_HEADER, required = false) requestId: String?
) = processJob(requestId ?: UUID.randomUUID().toString()) {
log.info("Find commits by issue key {}", issueKey)
val commits = vcsManager.findCommits(issueKey).map { it.toOld() }
RepositoryResponse(commits)
RepositoryResponse(vcsManager.findCommits(issueKey).map { it.toOld() })
}.data


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package org.octopusden.octopus.vcsfacade.dto

import org.octopusden.octopus.vcsfacade.client.common.dto.VcsFacadeResponse

data class RepositoryResponse<T>(val data: List<T>) : VcsFacadeResponse
data class RepositoryResponse<T>(val data: Sequence<T>) : VcsFacadeResponse
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ import org.octopusden.octopus.vcsfacade.dto.VcsServiceType
interface OpenSearchService {
fun findRepositoriesInfoByRepositoryType(type: VcsServiceType): Set<RepositoryInfoDocument>
fun findRepositoryInfoById(repositoryId: String): RepositoryInfoDocument?
fun saveRepositoriesInfo(repositoriesInfo: List<RepositoryInfoDocument>)
fun saveRepositoriesInfo(repositoriesInfo: Sequence<RepositoryInfoDocument>)
fun deleteRepositoryInfoById(repositoryId: String)
fun findRefsByRepositoryId(repositoryId: String): Set<RefDocument>
fun saveRefs(refs: List<RefDocument>)
fun deleteRefsByIds(refsIds: List<String>)
fun saveRefs(refs: Sequence<RefDocument>)
fun deleteRefsByIds(refsIds: Sequence<String>)
fun deleteRefsByRepositoryId(repositoryId: String)
fun findCommitsByRepositoryId(repositoryId: String): Set<CommitDocument>
fun saveCommits(commits: List<CommitDocument>)
fun deleteCommitsByIds(commitsIds: List<String>)
fun saveCommits(commits: Sequence<CommitDocument>)
fun deleteCommitsByIds(commitsIds: Sequence<String>)
fun deleteCommitsByRepositoryId(repositoryId: String)
fun findPullRequestsByRepositoryId(repositoryId: String): Set<PullRequestDocument>
fun savePullRequests(pullRequests: List<PullRequestDocument>)
fun deletePullRequestsByIds(pullRequestsIds: List<String>)
fun savePullRequests(pullRequests: Sequence<PullRequestDocument>)
fun deletePullRequestsByIds(pullRequestsIds: Sequence<String>)
fun deletePullRequestsByRepositoryId(repositoryId: String)
fun findBranchesByIssueKey(issueKey: String): List<RefDocument>
fun findCommitsByIssueKey(issueKey: String): List<CommitDocument>
fun findPullRequestsByIssueKey(issueKey: String): List<PullRequestDocument>
fun findBranchesByIssueKey(issueKey: String): Sequence<RefDocument>
fun findCommitsByIssueKey(issueKey: String): Sequence<CommitDocument>
fun findPullRequestsByIssueKey(issueKey: String): Sequence<PullRequestDocument>
fun findByIssueKey(issueKey: String): SearchSummary

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import org.octopusden.octopus.vcsfacade.client.common.dto.SearchSummary
import org.octopusden.octopus.vcsfacade.client.common.dto.Tag

interface VcsManager {
fun getTags(sshUrl: String): List<Tag>
fun getCommits(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String): List<Commit>
fun getCommitsWithFiles(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String): List<CommitWithFiles>
fun getTags(sshUrl: String): Sequence<Tag>
fun getCommits(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String): Sequence<Commit>
fun getCommitsWithFiles(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String): Sequence<CommitWithFiles>
fun getCommit(sshUrl: String, hashOrRef: String): Commit
fun getCommitWithFiles(sshUrl: String, hashOrRef: String): CommitWithFiles
fun createPullRequest(sshUrl: String, createPullRequest: CreatePullRequest): PullRequest
fun searchIssuesInRanges(searchRequest: SearchIssuesInRangesRequest): SearchIssueInRangesResponse
fun findBranches(issueKey: String): List<Branch>
fun findCommits(issueKey: String): List<Commit>
fun findCommitsWithFiles(issueKey: String): List<CommitWithFiles>
fun findPullRequests(issueKey: String): List<PullRequest>
fun findBranches(issueKey: String): Sequence<Branch>
fun findCommits(issueKey: String): Sequence<Commit>
fun findCommitsWithFiles(issueKey: String): Sequence<CommitWithFiles>
fun findPullRequests(issueKey: String): Sequence<PullRequest>
fun find(issueKey: String): SearchSummary
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ abstract class VcsService(vcsProperties: VcsConfig.VcsProperties) {
fun parse(sshUrl: String) =
sshUrlRegex.find(sshUrl.lowercase())!!.destructured.let { it.component1().trimEnd('/') to it.component2() }

abstract fun getBranches(group: String, repository: String): List<Branch>
abstract fun getTags(group: String, repository: String): List<Tag>
abstract fun getCommits(group: String, repository: String, from: HashOrRefOrDate<String, Date>?, toHashOrRef: String): List<Commit>
abstract fun getCommitsWithFiles(group: String, repository: String, from: HashOrRefOrDate<String, Date>?, toHashOrRef: String): List<CommitWithFiles>
abstract fun getBranches(group: String, repository: String): Sequence<Branch>
abstract fun getTags(group: String, repository: String): Sequence<Tag>
abstract fun getCommits(group: String, repository: String, from: HashOrRefOrDate<String, Date>?, toHashOrRef: String): Sequence<Commit>
abstract fun getCommitsWithFiles(group: String, repository: String, from: HashOrRefOrDate<String, Date>?, toHashOrRef: String): Sequence<CommitWithFiles>
abstract fun getCommit(group: String, repository: String, hashOrRef: String): Commit
abstract fun getCommitWithFiles(group: String, repository: String, hashOrRef: String): CommitWithFiles
abstract fun createPullRequest(group: String, repository: String, createPullRequest: CreatePullRequest): PullRequest
abstract fun getPullRequest(group: String, repository: String, index: Long): PullRequest
abstract fun findCommits(group: String, repository: String, hashes: Set<String>): List<Commit>
abstract fun findPullRequests(group: String, repository: String, indexes: Set<Long>): List<PullRequest>
abstract fun findBranches(issueKey: String): List<Branch>
abstract fun findCommits(issueKey: String): List<Commit>
abstract fun findCommitsWithFiles(issueKey: String): List<CommitWithFiles>
abstract fun findPullRequests(issueKey: String): List<PullRequest>
abstract fun findCommits(group: String, repository: String, hashes: Set<String>): Sequence<Commit>
abstract fun findPullRequests(group: String, repository: String, indexes: Set<Long>): Sequence<PullRequest>
abstract fun findBranches(issueKey: String): Sequence<Branch>
abstract fun findCommits(issueKey: String): Sequence<Commit>
abstract fun findCommitsWithFiles(issueKey: String): Sequence<CommitWithFiles>
abstract fun findPullRequests(issueKey: String): Sequence<PullRequest>
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,33 @@ class BitbucketService(

override val sshUrlRegex = "(?:ssh://)?git@$host/([^/]+)/([^/]+).git".toRegex()

override fun getBranches(group: String, repository: String): List<Branch> {
override fun getBranches(group: String, repository: String): Sequence<Branch> {
log.trace("=> getBranches({}, {})", group, repository)
return client.getBranches(group, repository).map { it.toBranch(group, repository) }.also {
log.trace("<= getBranches({}, {}): {}", group, repository, it)
}
return client.getBranches(group, repository).asSequence().map {
it.toBranch(group, repository)
}.also { log.trace("<= getBranches({}, {}): {}", group, repository, it) }
}

override fun getTags(group: String, repository: String): List<Tag> {
override fun getTags(group: String, repository: String): Sequence<Tag> {
log.trace("=> getTags({}, {})", group, repository)
return client.getTags(group, repository).map { it.toTag(group, repository) }.also {
log.trace("<= getTags({}, {}): {}", group, repository, it)
}
return client.getTags(group, repository).asSequence().map {
it.toTag(group, repository)
}.also { log.trace("<= getTags({}, {}): {}", group, repository, it) }
}

override fun getCommits(
group: String,
repository: String,
from: HashOrRefOrDate<String, Date>?,
toHashOrRef: String
): List<Commit> {
): Sequence<Commit> {
log.trace("=> getCommits({}, {}, {}, {})", group, repository, from, toHashOrRef)
val commits = if (from is HashOrRefOrDate.HashOrRefValue) {
client.getCommits(group, repository, toHashOrRef, from.value)
} else {
client.getCommits(group, repository, toHashOrRef, (from as? HashOrRefOrDate.DateValue)?.value)
}
return commits.map { it.toCommit(group, repository) }.also {
return commits.asSequence().map { it.toCommit(group, repository) }.also {
log.trace("<= getCommits({}, {}, {}, {}): {}", group, repository, from, toHashOrRef, it)
}
}
Expand All @@ -100,7 +100,7 @@ class BitbucketService(
repository: String,
from: HashOrRefOrDate<String, Date>?,
toHashOrRef: String
): List<CommitWithFiles> {
): Sequence<CommitWithFiles> {
log.trace("=> getCommitsWithFiles({}, {}, {}, {})", group, repository, from, toHashOrRef)
return getCommits(group, repository, from, toHashOrRef).map {
val fileChanges = getCommitChanges(group, repository, it)
Expand Down Expand Up @@ -146,49 +146,47 @@ class BitbucketService(
}
}

override fun findCommits(group: String, repository: String, hashes: Set<String>): List<Commit> {
override fun findCommits(group: String, repository: String, hashes: Set<String>): Sequence<Commit> {
log.trace("=> findCommits({}, {}, {})", group, repository, hashes)
return hashes.mapNotNull {
try {
client.getCommit(group, repository, it).toCommit(group, repository)
} catch (e: NotFoundException) {
null
}
}.also {
}.asSequence().also {
log.trace("<= findCommits({}, {}, {}): {}", group, repository, hashes, it)
}
}

override fun findPullRequests(group: String, repository: String, indexes: Set<Long>): List<PullRequest> {
override fun findPullRequests(group: String, repository: String, indexes: Set<Long>): Sequence<PullRequest> {
log.trace("=> findPullRequests({}, {}, {})", group, repository, indexes)
return indexes.mapNotNull {
try {
client.getPullRequest(group, repository, it).toPullRequest(group, repository)
} catch (e: NotFoundException) {
null
}
}.also {
}.asSequence().also {
log.trace("<= findPullRequests({}, {}, {}): {}", group, repository, indexes, it)
}
}

override fun findBranches(issueKey: String): List<Branch> {
override fun findBranches(issueKey: String): Sequence<Branch> {
log.warn("There is no native implementation of findBranches")
return emptyList()
return emptySequence()
}

override fun findCommits(issueKey: String): List<Commit> {
override fun findCommits(issueKey: String): Sequence<Commit> {
log.trace("=> findCommits({})", issueKey)
return client.getCommits(issueKey).map {
return client.getCommits(issueKey).asSequence().map {
it.toCommit.toCommit(it.repository.project.key.lowercase(), it.repository.slug.lowercase())
}.also {
log.trace("<= findCommits({}): {}", issueKey, it)
}
}.also { log.trace("<= findCommits({}): {}", issueKey, it) }
}

override fun findCommitsWithFiles(issueKey: String): List<CommitWithFiles> {
override fun findCommitsWithFiles(issueKey: String): Sequence<CommitWithFiles> {
log.trace("=> findCommitsWithFiles({})", issueKey)
return client.getCommits(issueKey).map {
return client.getCommits(issueKey).asSequence().map {
val group = it.repository.project.key.lowercase()
val repository = it.repository.slug.lowercase()
val commit = it.toCommit.toCommit(group, repository)
Expand All @@ -197,9 +195,9 @@ class BitbucketService(
}.also { log.trace("<= findCommitsWithFiles({}): {}", issueKey, it) }
}

override fun findPullRequests(issueKey: String): List<PullRequest> {
override fun findPullRequests(issueKey: String): Sequence<PullRequest> {
log.warn("There is no native implementation of findPullRequests")
return emptyList()
return emptySequence()
}

private fun getCommitChanges(group: String, repository: String, commit: Commit): List<FileChange> {
Expand Down
Loading

0 comments on commit 76f7b43

Please sign in to comment.