From 833c93128f4a7d31d0e4e208807549da18056d16 Mon Sep 17 00:00:00 2001 From: zacyanliu Date: Tue, 5 Sep 2023 15:55:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BD=93=E5=8E=86=E5=8F=B2=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=BF=87=E5=A4=9A=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B?= =?UTF-8?q?=E4=BC=9A=E5=AF=BC=E8=87=B4=E6=8B=89=E5=8F=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=87=8F=E8=BF=87=E5=A4=A7=EF=BC=8C=E6=96=B0=E5=A2=9E=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E4=B8=8D=E9=9C=80=E8=A6=81=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=AD=97=E6=AE=B5=E7=9A=84=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E7=94=A8=E8=AF=A5=E6=8E=A5=E5=8F=A3=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8C=85=E4=BF=A1=E6=81=AF=20#1125?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bkrepo/repository/dao/PackageDao.kt | 23 ++++++++++++++ .../impl/PackageDependentsServiceImpl.kt | 2 +- .../impl/PackageDownloadsServiceImpl.kt | 4 +-- .../packages/impl/PackageServiceImpl.kt | 30 ++++++++++++------- .../service/packages/impl/StageServiceImpl.kt | 2 +- ...tEdgeCenterPackageDependentsServiceImpl.kt | 2 +- .../CommitEdgeCenterPackageServiceImpl.kt | 2 +- 7 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/dao/PackageDao.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/dao/PackageDao.kt index 6d5f0f0807..53ce5319ed 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/dao/PackageDao.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/dao/PackageDao.kt @@ -48,6 +48,25 @@ import org.springframework.stereotype.Repository @Repository class PackageDao : SimpleMongoDao() { + /** + * 当历史版本过多的情况下会导致拉取数据量过大,针对不需要历史版本字段的业务可选用该接口查询包信息 + */ + 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 @@ -102,4 +121,8 @@ class PackageDao : SimpleMongoDao() { val update = Update().set(TPackage::latest.name, latestVersion) this.updateFirst(query, update) } + + companion object { + private const val HISTORY_VERSION = "historyVersion" + } } diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDependentsServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDependentsServiceImpl.kt index afdb146432..e49c8cde7c 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDependentsServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDependentsServiceImpl.kt @@ -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 { diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDownloadsServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDownloadsServiceImpl.kt index fe9345dec5..5b128d0ca5 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDownloadsServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageDownloadsServiceImpl.kt @@ -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( @@ -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) } diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageServiceImpl.kt index 3ef62f1b75..8fc6d82b2b 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/PackageServiceImpl.kt @@ -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)) } @@ -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] } @@ -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)) } @@ -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 { @@ -180,7 +181,7 @@ class PackageServiceImpl( option: VersionListOption ): List { 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, @@ -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( @@ -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) @@ -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) @@ -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") @@ -415,7 +416,7 @@ class PackageServiceImpl( packageKey: String, packageVersionList: List ): List { - 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 } } @@ -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) + } + + /** * 查找版本,不存在则抛异常 */ diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/StageServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/StageServiceImpl.kt index ed3ee041f7..6a8f17d46b 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/StageServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/StageServiceImpl.kt @@ -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) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageDependentsServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageDependentsServiceImpl.kt index 3750550d10..c073eb107f 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageDependentsServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageDependentsServiceImpl.kt @@ -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) } } } diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageServiceImpl.kt index 3e6a2e43e3..fd94447693 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/packages/impl/center/CommitEdgeCenterPackageServiceImpl.kt @@ -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)) {