Skip to content

Commit

Permalink
Merge pull request #1126 from zacYL/issue_1125
Browse files Browse the repository at this point in the history
feat: 当历史版本过多的情况下会导致拉取数据量过大,新增接口,不需要历史版本字段的业务可选用该接口查询包信息 #1125
  • Loading branch information
owenlxu authored Sep 6, 2023
2 parents e8f4664 + 833c931 commit 251a300
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ import org.springframework.stereotype.Repository
@Repository
class PackageDao : SimpleMongoDao<TPackage>() {

/**
* 当历史版本过多的情况下会导致拉取数据量过大,针对不需要历史版本字段的业务可选用该接口查询包信息
*/
fun findByKeyExcludeHistoryVersion(projectId: String, repoName: String, key: String): TPackage? {
if (key.isBlank()) {
return null
}
val query = PackageQueryHelper.packageQuery(projectId, repoName, key)
query.fields().exclude(HISTORY_VERSION)
return this.findOne(query)
}

fun checkExist(projectId: String, repoName: String, key: String): Boolean {
if (key.isBlank()) {
return false
}
return this.exists(PackageQueryHelper.packageQuery(projectId, repoName, key))
}

fun findByKey(projectId: String, repoName: String, key: String): TPackage? {
if (key.isBlank()) {
return null
Expand Down Expand Up @@ -102,4 +121,8 @@ class PackageDao : SimpleMongoDao<TPackage>() {
val update = Update().set(TPackage::latest.name, latestVersion)
this.updateFirst(query, update)
}

companion object {
private const val HISTORY_VERSION = "historyVersion"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class PackageDependentsServiceImpl(
}

protected fun checkPackage(projectId: String, repoName: String, packageKey: String): TPackage? {
return packageDao.findByKey(projectId, repoName, packageKey)
return packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class PackageDownloadsServiceImpl(
eqDate = date
)
val query = Query(criteria)
if (packageDownloadsDao.findOne(query) != null) {
if (packageDownloadsDao.exists(query)) {
return
}
val downloads = TPackageDownloads(
Expand Down Expand Up @@ -271,7 +271,7 @@ class PackageDownloadsServiceImpl(
* 查找包,不存在则抛异常
*/
private fun checkPackage(projectId: String, repoName: String, packageKey: String): TPackage {
return packageDao.findByKey(projectId, repoName, packageKey)
return packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)
?: throw ErrorCodeException(ArtifactMessageCode.PACKAGE_NOT_FOUND, packageKey)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class PackageServiceImpl(
packageKey: String,
versionName: String
): PackageVersion? {
val packageId = packageDao.findByKey(projectId, repoName, packageKey)?.id ?: return null
val packageId = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)?.id ?: return null
return convert(packageVersionDao.findByName(packageId, versionName))
}

Expand All @@ -107,7 +107,8 @@ class PackageServiceImpl(
packageKey: String,
tag: String
): String? {
val versionTag = packageDao.findByKey(projectId, repoName, packageKey)?.versionTag ?: return null
val versionTag = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)?.versionTag
?: return null
return versionTag[tag]
}

Expand All @@ -116,7 +117,7 @@ class PackageServiceImpl(
repoName: String,
packageKey: String
): PackageVersion? {
val packageId = packageDao.findByKey(projectId, repoName, packageKey)?.id ?: return null
val packageId = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)?.id ?: return null
return convert(packageVersionDao.findLatest(packageId))
}

Expand Down Expand Up @@ -156,7 +157,7 @@ class PackageServiceImpl(
Preconditions.checkArgument(pageSize >= 0, "pageSize")
val stageTag = option.stageTag?.split(StringPool.COMMA)
val pageRequest = Pages.ofRequest(pageNumber, pageSize)
val tPackage = packageDao.findByKey(projectId, repoName, packageKey)
val tPackage = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)
return if (tPackage == null) {
Pages.ofResponse(pageRequest, 0, emptyList())
} else {
Expand All @@ -180,7 +181,7 @@ class PackageServiceImpl(
option: VersionListOption
): List<PackageVersion> {
val stageTag = option.stageTag?.split(StringPool.COMMA)
val tPackage = packageDao.findByKey(projectId, repoName, packageKey) ?: return emptyList()
val tPackage = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey) ?: return emptyList()
val query = PackageQueryHelper.versionListQuery(
packageId = tPackage.id!!,
name = option.version,
Expand Down Expand Up @@ -248,7 +249,7 @@ class PackageServiceImpl(
}

override fun deletePackage(projectId: String, repoName: String, packageKey: String, realIpAddress: String?) {
val tPackage = packageDao.findByKey(projectId, repoName, packageKey) ?: return
val tPackage = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey) ?: return
packageVersionDao.deleteByPackageId(tPackage.id!!)
packageDao.deleteByKey(projectId, repoName, packageKey)
publishEvent(
Expand All @@ -273,7 +274,7 @@ class PackageServiceImpl(
versionName: String,
realIpAddress: String?
) {
var tPackage = packageDao.findByKey(projectId, repoName, packageKey) ?: return
var tPackage = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey) ?: return
val packageId = tPackage.id!!
val tPackageVersion = packageVersionDao.findByName(packageId, versionName) ?: return
checkCluster(tPackageVersion)
Expand Down Expand Up @@ -330,7 +331,7 @@ class PackageServiceImpl(
} else {
null
}
val tPackage = checkPackage(projectId, repoName, packageKey)
val tPackage = findPackageExcludeHistoryVersion(projectId, repoName, packageKey)
val packageId = tPackage.id.orEmpty()
val tPackageVersion = checkPackageVersion(packageId, versionName).apply {
checkCluster(this)
Expand Down Expand Up @@ -363,7 +364,7 @@ class PackageServiceImpl(
versionName: String,
realIpAddress: String?
) {
val tPackage = checkPackage(projectId, repoName, packageKey)
val tPackage = findPackageExcludeHistoryVersion(projectId, repoName, packageKey)
val tPackageVersion = checkPackageVersion(tPackage.id!!, versionName)
if (tPackageVersion.artifactPath.isNullOrBlank()) {
throw ErrorCodeException(CommonMessageCode.METHOD_NOT_ALLOWED, "artifactPath is null")
Expand Down Expand Up @@ -415,7 +416,7 @@ class PackageServiceImpl(
packageKey: String,
packageVersionList: List<String>
): List<String> {
val tPackage = packageDao.findByKey(projectId, repoName, packageKey) ?: return emptyList()
val tPackage = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey) ?: return emptyList()
val versionQuery = PackageQueryHelper.versionQuery(tPackage.id!!, packageVersionList)
return packageVersionDao.find(versionQuery).map { it.name }
}
Expand Down Expand Up @@ -465,6 +466,15 @@ class PackageServiceImpl(
?: throw ErrorCodeException(ArtifactMessageCode.PACKAGE_NOT_FOUND, packageKey)
}

/**
* 查找包,不存在则抛异常
*/
private fun findPackageExcludeHistoryVersion(projectId: String, repoName: String, packageKey: String): TPackage {
return packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)
?: throw ErrorCodeException(ArtifactMessageCode.PACKAGE_NOT_FOUND, packageKey)
}


/**
* 查找版本,不存在则抛异常
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class StageServiceImpl(
packageKey: String,
version: String
): TPackageVersion {
val tPackage = packageDao.findByKey(projectId, repoName, packageKey)
val tPackage = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)
?: throw ErrorCodeException(CommonMessageCode.RESOURCE_NOT_FOUND, packageKey)
return packageVersionDao.findByName(tPackage.id!!, version)
?: throw ErrorCodeException(CommonMessageCode.RESOURCE_NOT_FOUND, version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CommitEdgeCenterPackageDependentsServiceImpl(
) {
override fun checkPackage(projectId: String, repoName: String, packageKey: String): TPackage? {
return packageDao
.findByKey(projectId, repoName, packageKey)
.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey)
?.also { ClusterUtils.checkIsSrcCluster(it.clusterNames) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class CommitEdgeCenterPackageServiceImpl(
}

override fun deletePackage(projectId: String, repoName: String, packageKey: String, realIpAddress: String?) {
val tPackage = packageDao.findByKey(projectId, repoName, packageKey) ?: return
val tPackage = packageDao.findByKeyExcludeHistoryVersion(projectId, repoName, packageKey) ?: return
val srcCluster = srcCluster()

if (ClusterUtils.isUniqueSrcCluster(tPackage.clusterNames)) {
Expand Down

0 comments on commit 251a300

Please sign in to comment.