Skip to content

Commit

Permalink
feat: 增加ddc服务批量操作接口 TencentBlueKing#2776
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl committed Nov 25, 2024
1 parent 7b68f9a commit d5cba2a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ import com.tencent.bkrepo.common.api.message.CommonMessageCode.PARAMETER_INVALID
import com.tencent.bkrepo.common.artifact.api.ArtifactFile
import com.tencent.bkrepo.common.artifact.api.ArtifactPathVariable
import com.tencent.bkrepo.common.artifact.audit.ActionAuditContent
import com.tencent.bkrepo.common.artifact.audit.NODE_CREATE_ACTION
import com.tencent.bkrepo.common.artifact.audit.NODE_DOWNLOAD_ACTION
import com.tencent.bkrepo.common.artifact.audit.NODE_RESOURCE
import com.tencent.bkrepo.common.artifact.audit.NODE_CREATE_ACTION
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo
import com.tencent.bkrepo.ddc.artifact.repository.DdcLocalRepository.Companion.HEADER_NAME_HASH
import com.tencent.bkrepo.ddc.component.PermissionHelper
import com.tencent.bkrepo.ddc.pojo.BatchOps
import com.tencent.bkrepo.ddc.pojo.Operation
import com.tencent.bkrepo.ddc.service.ReferenceArtifactService
import com.tencent.bkrepo.ddc.utils.MEDIA_TYPE_JUPITER_INLINED_PAYLOAD
import com.tencent.bkrepo.ddc.utils.MEDIA_TYPE_UNREAL_COMPACT_BINARY
Expand Down Expand Up @@ -153,6 +155,28 @@ class ReferencesController(
referenceArtifactService.finalize(artifactInfo)
}

@ApiOperation("批量读写")
@PostMapping(
"/{repoName}",
consumes = [MEDIA_TYPE_UNREAL_COMPACT_BINARY],
produces = [MEDIA_TYPE_UNREAL_COMPACT_BINARY],
)
fun batchOp(@PathVariable repoName: String) {
// 检查权限
val ops = BatchOps.deserialize(HttpContextHolder.getRequest().inputStream.use { it.readBytes() })
var requiredPermissionAction = PermissionAction.READ
for (op in ops.ops) {
if (op.op == Operation.PUT.name) {
requiredPermissionAction = PermissionAction.WRITE
break
}
}
permissionHelper.checkPathPermission(requiredPermissionAction)

// 执行操作
referenceArtifactService.batch(ops)
}

private fun getResponseType(format: String?, default: String): String {
if (!format.isNullOrEmpty()) {
return when (format.toLowerCase()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.ddc.pojo

import com.tencent.bkrepo.ddc.serialization.CbField
import com.tencent.bkrepo.ddc.serialization.CbObject
import com.tencent.bkrepo.ddc.utils.isBool
import com.tencent.bkrepo.ddc.utils.isInteger
import com.tencent.bkrepo.ddc.utils.isObject
import com.tencent.bkrepo.ddc.utils.isString
import java.nio.ByteBuffer

/**
* 批量操作
*/
data class BatchOps(
val ops: List<BatchOp>
) {
companion object {
fun deserialize(byteArray: ByteArray): BatchOps {
val ops = CbObject(ByteBuffer.wrap(byteArray))[BatchOps::ops.name].asArray().map {
deserializeBatchOp(BatchOp(), it.asObject())
}
return BatchOps(ops)
}

/**
* 仅支持反序列化最外层的属性,且只支持了BatchOp的字段类型
*/
private fun deserializeBatchOp(obj: BatchOp, cbObject: CbObject): BatchOp {
fun toVal(field: CbField) = when {
field.isBool() -> field.asBool() as Any
field.isString() -> field.asString()
field.isInteger() -> field.asUInt32()
field.isObject() -> field.asObject()
else -> throw RuntimeException("unsupported field type ${field.getType()}")
}
cbObject.forEach { field ->
obj.javaClass.getDeclaredField(field.name).set(obj, toVal(field))
}
return obj
}
}
}

/**
* 操作
*/
data class BatchOp(
var opId: Int = 0,
var bucket: String = "",
var key: String = "",
var op: String = Operation.INVALID.toString(),
/**
* 是否检查ref引用的所有blob是否存在
*/
var resolveAttachments: Boolean = false,
/**
* ref inline blob,op为PUT时有值
*/
var payload: CbObject? = null,
/**
* ref inline blob hash, op为PUT时有值
*/
var payloadHash: String? = null,
)

/**
* 操作类型
*/
enum class Operation {
INVALID,
GET,
PUT,
HEAD;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.tencent.bkrepo.common.artifact.repository.context.ArtifactUploadConte
import com.tencent.bkrepo.common.artifact.repository.core.ArtifactService
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo
import com.tencent.bkrepo.ddc.pojo.BatchOps
import org.springframework.stereotype.Service

@Service
Expand All @@ -58,6 +59,13 @@ class ReferenceArtifactService(
}
}

/**
* 批量操作
*/
fun batch(ops: BatchOps) {
// TODO
}

fun finalize(artifactInfo: ReferenceArtifactInfo) {
with(artifactInfo) {
val ref = referenceService.getReference(
Expand Down

0 comments on commit d5cba2a

Please sign in to comment.