org.apache.flink.runtime.state.gemini.engine.page.bmap.GHashRoutingValue 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.api.common.typeutils.TypeSerializer;
import org.apache.flink.runtime.state.gemini.engine.page.DataPage;
import org.apache.flink.runtime.state.gemini.engine.page.PageAddress;
import org.apache.flink.runtime.state.gemini.engine.rm.GByteBuffer;
import org.apache.flink.util.Preconditions;
import java.util.ArrayList;
import java.util.List;
/**
* GHashRoutingValue.
*/
public class GHashRoutingValue extends AbstractGRoutingValue {
public static boolean isGHashRoutingValue(BinaryValue binaryValue) {
return binaryValue instanceof BinaryValueForSplit &&
getHeaderGRoutingType(binaryValue) == DataPage.DataPageType.KSplitHashRouting.getCode();
}
public static GBinaryHashMap getSubGBinaryHashMap(
BinaryValue binaryValue, MK mapKey, TypeSerializer mkTypeSerializer) {
int subMapCount = getSubMapCount(binaryValue);
int indexSlot = mapKey.hashCode() & (subMapCount - 1);
return getSubGBinaryHashMapWithKey(mapKey, indexSlot, binaryValue, mkTypeSerializer);
}
public static List> getAllSubGBinaryHashMap(
BinaryValue binaryValue, TypeSerializer mkTypeSerializer) {
Preconditions.checkState(binaryValue instanceof BinaryValueForSplit);
int subMapCount = getSubMapCount(binaryValue);
List> subMapList = 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) {
subMapList.add(new GBinaryHashMap<>(bb, mkTypeSerializer));
}
}
return subMapList;
}
public static GBinaryHashMap getSubGBinaryHashMapWithKey(
MK mapKey, int hashIndex, BinaryValue binaryValue, TypeSerializer mkTypeSerializer) {
Preconditions.checkState(binaryValue instanceof BinaryValueForSplit);
int subMapId = ByteBufferUtils.toInt(binaryValue.getBb(),
binaryValue.getValueOffset() + getGRoutingBaseValueOffset(binaryValue) + hashIndex * Integer.BYTES);
GByteBuffer bb = binaryValue.getPageMapping().getGByteBuffer(subMapId, mapKey);
return new GBinaryHashMap<>(bb, mkTypeSerializer);
}
public static PageAddress getSubMapPageAddress(int hashIndex, BinaryValue binaryValue) {
Preconditions.checkState(binaryValue instanceof BinaryValueForSplit);
int subMapId = ByteBufferUtils.toInt(binaryValue.getBb(),
binaryValue.getValueOffset() + getGRoutingBaseValueOffset(binaryValue) + hashIndex * Integer.BYTES);
return binaryValue.getPageMapping().getGByteBufferAddress(subMapId);
}
}