priyeshkaratha commented on code in PR #8566: URL: https://github.com/apache/ozone/pull/8566#discussion_r2189093209
########## hadoop-hdds/framework/src/main/java/org/apache/hadoop/ozone/common/DeletedBlockGroup.java: ########## @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.common; + +import java.util.ArrayList; +import java.util.List; +import org.apache.hadoop.hdds.client.BlockID; +import org.apache.hadoop.hdds.client.DeletedBlock; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos; +import org.apache.hadoop.hdds.protocol.proto.ScmBlockLocationProtocolProtos.DeletedKeyBlocks; +import org.apache.hadoop.hdds.protocol.proto.ScmBlockLocationProtocolProtos.KeyBlocks; + +/** + * A group of blocks relations relevant, e.g belong to a certain object key. + */ +public final class DeletedBlockGroup { + + private String groupID; + private List<DeletedBlock> blocks; + + private DeletedBlockGroup(String groupID, List<DeletedBlock> blocks) { + this.groupID = groupID; + this.blocks = blocks; + } + + public List<DeletedBlock> getAllBlocks() { + return blocks; + } + + public String getGroupID() { + return groupID; + } + + public DeletedKeyBlocks getProto() { + DeletedKeyBlocks.Builder kbb = DeletedKeyBlocks.newBuilder(); + for (DeletedBlock block : blocks) { + kbb.addBlocks(block.getProtobuf()); + } + return kbb.setKey(groupID).build(); + } + + public static DeletedBlockGroup getFromProto(DeletedKeyBlocks proto) { + List<DeletedBlock> blocks = new ArrayList<>(); + for (HddsProtos.DeletedBlock block : proto.getBlocksList()) { + HddsProtos.ContainerBlockID containerBlockId = block.getBlockId().getContainerBlockID(); + blocks.add(new DeletedBlock(new BlockID(containerBlockId.getContainerID(), containerBlockId.getLocalID()), + block.getUsedBytes())); + } + return DeletedBlockGroup.newBuilder().setKeyName(proto.getKey()) + .addAllBlocks(blocks).build(); + } + + /** ------------------------------------------------------------------ + * Build the *legacy* wire format (KeyBlocks) — used when talking to an + * older SCM that doesn’t understand usedBytes. Each BlockID is copied + * exactly, but the size field is absent. + * ------------------------------------------------------------------ */ + public KeyBlocks getLegacyProto() { + KeyBlocks.Builder kb = KeyBlocks.newBuilder().setKey(groupID); + for (DeletedBlock b : blocks) { + kb.addBlocks(b.getBlockID().getProtobuf()); + } + return kb.build(); + } + + /** ------------------------------------------------------------------ + * Convert a legacy KeyBlocks message (which has no usedBytes) into the + * richer DeletedBlockGroup representation by defaulting size to 0. + * ------------------------------------------------------------------ */ + public static DeletedBlockGroup fromLegacy(KeyBlocks proto) { + List<DeletedBlock> list = new ArrayList<>(); + for (HddsProtos.BlockID bid : proto.getBlocksList()) { + list.add(new DeletedBlock(BlockID.getFromProtobuf(bid), 0L)); Review Comment: Addressed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
