org.apache.flink.runtime.state.gemini.engine.page.bmap.AbstractGRoutingValue Maven / Gradle / Ivy
/*
* 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.flink.runtime.state.gemini.engine.page.bmap;
import org.apache.flink.runtime.state.gemini.engine.rm.GByteBuffer;
import org.apache.flink.util.Preconditions;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* AbstractGRoutingValue.
* GRouting format:
* | Header | hashIndex(keyIndex) | subMapId |
*/
public abstract class AbstractGRoutingValue {
public static final int GROUTING_INFO_HEADER_LENGTH = 16;
private static final int HEADER_ROUTING_TYPE_OFFSET = 0;
private static final int HEADER_SUB_MAP_COUNT_OFFSET = HEADER_ROUTING_TYPE_OFFSET + Byte.BYTES;
private static final int HEADER_SUB_MAP_MAX_SIZE_OFFSET = HEADER_SUB_MAP_COUNT_OFFSET + Integer.BYTES;
private static final int HEADER_BASE_VALUE_OFFSET_OFFSET = HEADER_SUB_MAP_MAX_SIZE_OFFSET + Integer.BYTES;
private static final int HEADER_BASE_KEY_OFFSET = GROUTING_INFO_HEADER_LENGTH;
public static byte getHeaderGRoutingType(BinaryValue binaryValue) {
return ByteBufferUtils.toByte(binaryValue.getBb(), binaryValue.getValueOffset() + HEADER_ROUTING_TYPE_OFFSET);
}
public static byte getHeaderGRoutingType(ByteBuffer headerBuffer) {
return ByteBufferUtils.toByte(headerBuffer, HEADER_ROUTING_TYPE_OFFSET);
}
public static int getSubMapCount(BinaryValue binaryValue) {
return ByteBufferUtils.toInt(binaryValue.getBb(), binaryValue.getValueOffset() + HEADER_SUB_MAP_COUNT_OFFSET);
}
public static int getSubMapCount(ByteBuffer headerBuffer) {
return ByteBufferUtils.toInt(headerBuffer, HEADER_SUB_MAP_COUNT_OFFSET);
}
public static int getSubMapMaxSize(BinaryValue binaryValue) {
return ByteBufferUtils.toInt(binaryValue.getBb(), binaryValue.getValueOffset() + HEADER_SUB_MAP_MAX_SIZE_OFFSET);
}
public static int getGRoutingBaseValueOffset(BinaryValue binaryValue) {
return ByteBufferUtils.toInt(binaryValue.getBb(), binaryValue.getValueOffset() + HEADER_BASE_VALUE_OFFSET_OFFSET);
}
public static int getGRoutingBaseValueOffset(ByteBuffer byteBuffer) {
return ByteBufferUtils.toInt(byteBuffer, HEADER_BASE_VALUE_OFFSET_OFFSET);
}
public static void writeHeaderRoutingType(ByteBuffer headerBuffer, byte type) {
ByteBufferUtils.putByte(headerBuffer, HEADER_ROUTING_TYPE_OFFSET, type);
}
public static void writeHeaderSubMapCount(ByteBuffer headerBuffer, int subMapCount) {
ByteBufferUtils.putInt(headerBuffer, HEADER_SUB_MAP_COUNT_OFFSET, subMapCount);
}
public static void writeHeaderSubMapMaxSize(ByteBuffer headerBuffer, int subMapMaxSize) {
ByteBufferUtils.putInt(headerBuffer, HEADER_SUB_MAP_MAX_SIZE_OFFSET, subMapMaxSize);
}
public static void writeHeaderBaseValueOffset(ByteBuffer headerBuffer, int baseValueOffset) {
ByteBufferUtils.putInt(headerBuffer, HEADER_BASE_VALUE_OFFSET_OFFSET, baseValueOffset);
}
public static List getAllSubMapByteBuffer(BinaryValue binaryValue) {
Preconditions.checkState(binaryValue instanceof BinaryValueForSplit);
int subMapCount = getSubMapCount(binaryValue);
List bufferList = new ArrayList<>(subMapCount);
for (int i = 0; i < subMapCount; i++) {
int subMapId = ByteBufferUtils.toInt(binaryValue.getBb(),
binaryValue.getValueOffset() + getGRoutingBaseValueOffset(binaryValue) + i * Integer.BYTES);
GByteBuffer bb = binaryValue.getPageMapping().getGByteBuffer(subMapId, null);
if (bb != null) {
bufferList.add(bb);
}
}
return bufferList;
}
public static int[] getAllSubMapId(BinaryValue binaryValue) {
Preconditions.checkState(binaryValue instanceof BinaryValueForSplit);
int subMapCount = getSubMapCount(binaryValue);
int[] idList = new int[subMapCount];
for (int i = 0; i < subMapCount; i++) {
idList[i] = ByteBufferUtils.toInt(binaryValue.getBb(),
binaryValue.getValueOffset() + getGRoutingBaseValueOffset(binaryValue) + i * Integer.BYTES);
}
return idList;
}
}