All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.state.gemini.engine.page.bmap.GSortedRoutingValue Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * 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.exceptions.GeminiRuntimeException;
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.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.apache.flink.runtime.state.gemini.engine.page.bmap.GBinarySortedMap.EMPTY_G_BINARY_SORTEDMAP;

/**
 * GSortedRoutingValue.
 */
public class GSortedRoutingValue extends AbstractGRoutingValue {

	public static boolean isGSortedRoutingValue(BinaryValue binaryValue) {
		return binaryValue instanceof BinaryValueForSplit &&
			getHeaderGRoutingType(binaryValue) == DataPage.DataPageType.KSplitSortedRouting.getCode();
	}

	public static  GBinarySortedMap getSubGBinarySortedMapWithKey(
		MK mapKey, int keySlot, BinaryValue binaryValue, TypeSerializer mkTypeSerializer, GComparator gComparator) {

		Preconditions.checkState(binaryValue instanceof  BinaryValueForSplit);
		int subMapId = ByteBufferUtils.toInt(binaryValue.getBb(),
			binaryValue.getValueOffset() + getGRoutingBaseValueOffset(binaryValue) + keySlot * Integer.BYTES);
		GByteBuffer bb = binaryValue.getPageMapping().getGByteBuffer(subMapId, mapKey);
		if (bb == null || bb.capacity() == 0) {
			return EMPTY_G_BINARY_SORTEDMAP;
		}

		return new GBinarySortedMap<>(bb.getByteBuffer(), mkTypeSerializer, gComparator);
	}

	public static PageAddress getSubMapPageAddress(int keySlot, BinaryValue binaryValue) {
		Preconditions.checkState(binaryValue instanceof  BinaryValueForSplit);
		int subMapId = ByteBufferUtils.toInt(binaryValue.getBb(),
			binaryValue.getValueOffset() + getGRoutingBaseValueOffset(binaryValue) + keySlot * Integer.BYTES);

		return binaryValue.getPageMapping().getGByteBufferAddress(subMapId);
	}

	public static  GBinarySortedMap getSubGBinarySortedMap(
		MK mapKey, BinaryValue binaryValue, TypeSerializer mkTypeSerializer, GComparator gComparator) {

		Preconditions.checkState(binaryValue instanceof BinaryValueForSplit);
		List keyList = getKeyIndexArray(binaryValue);
		int mapKeySlot = findKeySlot(keyList, gComparator, mapKey);

		return getSubGBinarySortedMapWithKey(mapKey, mapKeySlot, binaryValue, mkTypeSerializer, gComparator);
	}

	private static  int findKeySlot(List keyList, GComparator gComparator, MK mapKey) {
		int low = 0, high = keyList.size() - 1;
		while (low < high) {
			int mid = (low + high) >> 1;
			if (gComparator.compare(mapKey, keyList.get(mid)) == 0) {
				return mid;
			} else if (gComparator.compare(mapKey, keyList.get(mid)) > 0) {
				low = mid + 1;
			} else {
				high = mid;
			}
		}
		return high;
	}

	private static int getKeyPositionBySlot(int slot, BinaryValue binaryValue) {
		return ByteBufferUtils.toInt(binaryValue.getBb(),
			binaryValue.getValueOffset() + GROUTING_INFO_HEADER_LENGTH + slot * Integer.BYTES);
	}

	public static List getKeyIndexArray(BinaryValue binaryValue) {

		int subMapCount = getSubMapCount(binaryValue);
		List keyIndex = new ArrayList<>(subMapCount);
		int lastKeyPosition = binaryValue.getValueOffset() + GROUTING_INFO_HEADER_LENGTH + subMapCount * Integer.BYTES;
		for (int i = 0; i < subMapCount; i++) {
			int curKeyPosition = binaryValue.getValueOffset() + GROUTING_INFO_HEADER_LENGTH + getKeyPositionBySlot(i, binaryValue);
			keyIndex.add(new BinaryKey(binaryValue.getBb(), lastKeyPosition, curKeyPosition - lastKeyPosition, 0));
			lastKeyPosition = curKeyPosition;
		}

		return keyIndex;
	}

	public static BinaryKey getKeyIndexBySlot(BinaryValue binaryValue, int slot) {
		int lastKeyPosition;
		if (slot == 0) {
			int subMapCount = getSubMapCount(binaryValue);
			lastKeyPosition = binaryValue.getValueOffset() + GROUTING_INFO_HEADER_LENGTH + subMapCount * Integer.BYTES;
		} else {
			lastKeyPosition =  binaryValue.getValueOffset() + GROUTING_INFO_HEADER_LENGTH + getKeyPositionBySlot(slot - 1, binaryValue);
		}
		int curKeyPosition = binaryValue.getValueOffset() + GROUTING_INFO_HEADER_LENGTH + getKeyPositionBySlot(slot, binaryValue);

		return new BinaryKey(binaryValue.getBb(), lastKeyPosition, curKeyPosition - lastKeyPosition, 0);
	}

	public static  MK getKeyIndexBySlot(BinaryValue binaryValue, int slot, TypeSerializer keyTypeSerializer) {

		try {
			BinaryKey binaryKey = getKeyIndexBySlot(binaryValue, slot);
			ByteBufferDataInputView byteBufferDataInputView = new ByteBufferDataInputView(binaryKey.getBb(),
				binaryKey.getKeyOffset(),
				binaryKey.getKeyLen());
			return keyTypeSerializer.deserialize(byteBufferDataInputView);
		} catch (IOException e) {
			throw new GeminiRuntimeException("Exception when deserialize BinaryKey", e);
		}
	}

	public static  List> getAllSubGBinarySortedMap(
		BinaryValue binaryValue, TypeSerializer mkTypeSerializer, GComparator gComparator) {
		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 || bb.capacity() == 0) {
				subMapList.add(EMPTY_G_BINARY_SORTEDMAP);
			} else {
				subMapList.add(new GBinarySortedMap<>(bb.getByteBuffer(), mkTypeSerializer, gComparator));
			}
		}

		return subMapList;
	}

	public static  GBinarySortedMap getFirstOrLastSubGBinarySortedMap(
		BinaryValue binaryValue,
		TypeSerializer mkTypeSerializer,
		GComparator gComparator,
		boolean first) {

		Preconditions.checkState(binaryValue instanceof BinaryValueForSplit);
		int subMapCount = getSubMapCount(binaryValue);
		int keySlot = first ? 0 : subMapCount - 1;
		int subMapId = ByteBufferUtils.toInt(binaryValue.getBb(),
			binaryValue.getValueOffset() + getGRoutingBaseValueOffset(binaryValue) + keySlot * Integer.BYTES);
		GByteBuffer bb = binaryValue.getPageMapping().getGByteBuffer(subMapId, null);

		return new GBinarySortedMap<>(bb.getByteBuffer(), mkTypeSerializer, gComparator);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy