Skip to content

Commit

Permalink
feat: blockNode抽取至公共模块 TencentBlueKing#2413
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxuwan committed Aug 5, 2024
1 parent 7435eba commit 3b3fceb
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
dependencies {
api(project(":common:common-artifact:artifact-api"))
api(project(":common:common-metadata:metadata-api"))
api(project(":common:common-mongo-reactive"))
api(project(":common:common-storage:storage-api"))
api(project(":repository:api-repository"))

compileOnly(project(":common:common-mongo-reactive"))
compileOnly(project(":common:common-mongo"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tencent.bkrepo.common.metadata.condition

import org.springframework.context.annotation.Condition
import org.springframework.context.annotation.ConditionContext
import org.springframework.core.type.AnnotatedTypeMetadata

class ReactiveCondition : Condition {
override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean {
try {
context.classLoader?.loadClass("com.mongodb.reactivestreams.client.MongoClient")
return true
} catch (e: ClassNotFoundException) {
return false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tencent.bkrepo.common.metadata.condition

import org.springframework.context.annotation.Condition
import org.springframework.context.annotation.ConditionContext
import org.springframework.core.type.AnnotatedTypeMetadata

class SyncCondition : Condition {
override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean {
try {
context.classLoader?.loadClass("com.mongodb.client.MongoClient")
return true
} catch (e: ClassNotFoundException) {
return false
}
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.common.metadata.dao

import com.tencent.bkrepo.common.mongo.reactive.dao.ShardingMongoReactiveDao
import com.tencent.bkrepo.common.metadata.condition.SyncCondition
import com.tencent.bkrepo.common.metadata.model.TBlockNode
import com.tencent.bkrepo.common.mongo.dao.sharding.HashShardingMongoDao
import org.springframework.context.annotation.Conditional
import org.springframework.stereotype.Repository

@Repository
class BlockNodeDao : ShardingMongoReactiveDao<TBlockNode>()
@Conditional(SyncCondition::class)
class BlockNodeDao : HashShardingMongoDao<TBlockNode>()
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@

package com.tencent.bkrepo.common.metadata.dao

import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import

@Configuration
@Import(
BlockNodeDao::class
)
@ComponentScan(basePackages = ["com.tencent.bkrepo.common.metadata.dao"])
class MetadataDaoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.common.metadata.dao

import com.tencent.bkrepo.common.metadata.condition.ReactiveCondition
import com.tencent.bkrepo.common.metadata.model.TBlockNode
import com.tencent.bkrepo.common.mongo.reactive.dao.ShardingMongoReactiveDao
import org.springframework.context.annotation.Conditional
import org.springframework.stereotype.Repository

@Repository
@Conditional(ReactiveCondition::class)
class RBlockNodeDao : ShardingMongoReactiveDao<TBlockNode>()
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ import com.tencent.bkrepo.common.storage.credentials.StorageCredentials
import java.time.LocalDateTime

interface BlockNodeService {

/**
* 查询出范围内的分块
* */
suspend fun listBlocks(
fun listBlocks(
range: Range,
projectId: String,
repoName: String,
Expand All @@ -47,7 +48,7 @@ interface BlockNodeService {
/**
* 创建分块
* */
suspend fun createBlock(
fun createBlock(
blockNode: TBlockNode,
storageCredentials: StorageCredentials?
): TBlockNode
Expand All @@ -59,20 +60,26 @@ interface BlockNodeService {
* @param repoName 仓库名
* @param fullPath 文件路径
* */
suspend fun deleteBlocks(
fun deleteBlocks(
projectId: String,
repoName: String,
fullPath: String
)

suspend fun moveBlocks(
/**
* 移动文件对应分块
*/
fun moveBlocks(
projectId: String,
repoName: String,
fullPath: String,
dstFullPath: String
)

suspend fun restoreBlocks(
/**
* 恢复文件对应分块
*/
fun restoreBlocks(
projectId: String,
repoName: String,
fullPath: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.tencent.bkrepo.common.metadata.service.blocknode

import com.tencent.bkrepo.common.artifact.stream.Range
import com.tencent.bkrepo.common.metadata.model.TBlockNode
import com.tencent.bkrepo.common.storage.credentials.StorageCredentials
import java.time.LocalDateTime

interface RBlockNodeService {
/**
* 查询出范围内的分块
* */
suspend fun listBlocks(
range: Range,
projectId: String,
repoName: String,
fullPath: String,
createdDate: String
): List<TBlockNode>

/**
* 创建分块
* */
suspend fun createBlock(
blockNode: TBlockNode,
storageCredentials: StorageCredentials?
): TBlockNode

/**
* 删除旧分块,即删除非指定的nodeCurrentSha256的分块。
* 如果未指定nodeCurrentSha256,则删除节点所有分块
* @param projectId 项目id
* @param repoName 仓库名
* @param fullPath 文件路径
* */
suspend fun deleteBlocks(
projectId: String,
repoName: String,
fullPath: String
)

/**
* 移动文件对应分块
*/
suspend fun moveBlocks(
projectId: String,
repoName: String,
fullPath: String,
dstFullPath: String
)

/**
* 恢复文件对应分块
*/
suspend fun restoreBlocks(
projectId: String,
repoName: String,
fullPath: String,
nodeCreateDate: LocalDateTime,
nodeDeleteDate: LocalDateTime
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ abstract class AbstractBlockNodeService(
private val blockNodeDao: BlockNodeDao
) : BlockNodeService {

override suspend fun createBlock(
blockNode: TBlockNode,
storageCredentials: StorageCredentials?
): TBlockNode {
override fun createBlock(blockNode: TBlockNode, storageCredentials: StorageCredentials?): TBlockNode {
with(blockNode) {
val bn = blockNodeDao.save(blockNode)
incFileRef(bn.sha256, storageCredentials?.key)
Expand All @@ -63,7 +60,7 @@ abstract class AbstractBlockNodeService(
}
}

override suspend fun listBlocks(
override fun listBlocks(
range: Range,
projectId: String,
repoName: String,
Expand All @@ -83,7 +80,7 @@ abstract class AbstractBlockNodeService(
return blockNodeDao.find(query)
}

override suspend fun deleteBlocks(
override fun deleteBlocks(
projectId: String,
repoName: String,
fullPath: String
Expand All @@ -97,7 +94,7 @@ abstract class AbstractBlockNodeService(
logger.info("Delete node blocks[$projectId/$repoName$fullPath] success.")
}

override suspend fun moveBlocks(projectId: String, repoName: String, fullPath: String, dstFullPath: String) {
override fun moveBlocks(projectId: String, repoName: String, fullPath: String, dstFullPath: String) {
val nodeDetail = getNodeDetail(projectId, repoName, dstFullPath)
if (nodeDetail.folder) {
val criteria = where(TBlockNode::nodeFullPath).regex("^${EscapeUtils.escapeRegex(fullPath)}/")
Expand All @@ -123,7 +120,7 @@ abstract class AbstractBlockNodeService(



override suspend fun restoreBlocks(
override fun restoreBlocks(
projectId: String,
repoName: String,
fullPath: String,
Expand All @@ -140,9 +137,9 @@ abstract class AbstractBlockNodeService(
"between $nodeCreateDate and $nodeDeleteDate success.")
}

abstract suspend fun incFileRef(sha256: String, credentialsKey: String?)
abstract fun incFileRef(sha256: String, credentialsKey: String?)

abstract suspend fun getNodeDetail(projectId: String, repoName: String, dstFullPath: String): NodeDetail
abstract fun getNodeDetail(projectId: String, repoName: String, dstFullPath: String): NodeDetail

companion object {
private val logger = LoggerFactory.getLogger(AbstractBlockNodeService::class.java)
Expand Down
Loading

0 comments on commit 3b3fceb

Please sign in to comment.