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

org.apache.flink.runtime.state.gemini.engine.page.bmap.GBinarySplitHashMap 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.core.memory.DataInputView;
import org.apache.flink.runtime.state.gemini.engine.exceptions.GeminiRuntimeException;
import org.apache.flink.runtime.state.gemini.engine.memstore.GSValue;
import org.apache.flink.runtime.state.gemini.engine.page.DataPage;
import org.apache.flink.runtime.state.gemini.engine.page.GValueType;
import org.apache.flink.runtime.state.gemini.engine.rm.GByteBuffer;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.flink.runtime.state.gemini.engine.page.bmap.AbstractGRoutingValue.getHeaderGRoutingType;

/**
 * GBinarySplitHashMap is to support KV split and Map split when value is a map.
 * GBinarySplitHashMap: k->user map{mk->mv}, and user map is split to a set of GBinaryHashMap or GBinarySortedMap.
 */
public class GBinarySplitHashMap extends GBinaryHashMap {

	private GBufferAddressMapping mapping;

	public GBinarySplitHashMap(
		GBinaryHashMap gBinaryHashMap, GBufferAddressMapping mapping) {
		super(gBinaryHashMap.getPageHelper(),
			gBinaryHashMap.data,
			gBinaryHashMap.getKeyTypeSerializer(),
			gBinaryHashMap.getOriginChecksum());
		this.mapping = mapping;
	}

	public GBinarySplitHashMap(
		GByteBuffer data, TypeSerializer keyTypeSerializer) {
		super(data, keyTypeSerializer);
	}

	public GBinarySplitHashMap(GByteBuffer data, TypeSerializer keyTypeSerializer, int originCheckSum) {
		super(data, keyTypeSerializer, originCheckSum);
	}

	public GBinarySplitHashMap(
		GHashHeader pageHelper, GByteBuffer data, TypeSerializer keyTypeSerializer) {
		this(pageHelper, data, keyTypeSerializer, genOriginCheckSum(data.getByteBuffer()));
	}

	public GBinarySplitHashMap(
		GHashHeader pageHelper, GByteBuffer data, TypeSerializer keyTypeSerializer, int originCheckSum) {
		super(pageHelper, data, keyTypeSerializer, originCheckSum);
	}

	public void setMapping(GBufferAddressMapping mapping) {
		this.mapping = mapping;
	}

	public GBufferAddressMapping getBufferAddressMapping() {
		return mapping;
	}

	@Override
	BinaryValue getBinaryValue(
		int keyCount, int indexLen, int baseValueOffset, int slotNum, AtomicReference uncompressValueData) {
		BinaryValue binaryValue = super.getBinaryValue(keyCount,
			indexLen,
			baseValueOffset,
			slotNum,
			uncompressValueData);
		if (isSplitBinaryValue(binaryValue)) {
			return new BinaryValueForSplit(binaryValue, mapping);
		}
		return binaryValue;
	}

	private boolean isSplitBinaryValue(BinaryValue binaryValue) {
		if (binaryValue.getBb() != null && (getHeaderGRoutingType(binaryValue) == DataPage.DataPageType.KSplitHashRouting.getCode() || getHeaderGRoutingType(
			binaryValue) == DataPage.DataPageType.KSplitSortedRouting.getCode())) {
			return true;
		}
		return false;
	}

	@Override
	public  Map> toPOJOMap(Map> result, TypeSerializer valueTypeSerializer) {
		try {
			int keyCount = keyCount();
			if (keyCount == 0) {
				return result;
			}
			int indexLen = indexCount();
			int baseKeyOffset = GHashHeaderImpl.getHeaderKeyOffset(data.getByteBuffer());
			int baseValueOffset = GHashHeaderImpl.getHeaderValueOffset(data.getByteBuffer());
			AtomicReference uncompressData = new AtomicReference<>();
			AtomicReference uncompressValueData = new AtomicReference<>();

			for (int i = 0; i < keyCount; i++) {
				K k = getKeyBySlot(keyCount, baseKeyOffset, baseValueOffset, i, uncompressData);
				BinaryValue binaryValue = getBinaryValue(keyCount, indexLen, baseValueOffset, i, uncompressValueData);
				if (binaryValue.getGValueType() == GValueType.Delete) {
					result.put(k, new GSValue<>(null, binaryValue.getGValueType(), binaryValue.getSeqID()));
				} else {
					if (GHashRoutingValue.isGHashRoutingValue(binaryValue) || GSortedRoutingValue.isGSortedRoutingValue(binaryValue)) {
						List bufferList = AbstractGRoutingValue.getAllSubMapByteBuffer(binaryValue);
						Map mergeMap = new HashMap();
						for (GByteBuffer subMapBB : bufferList) {

							DataInputView byteBufferDataInputView = new ByteBufferDataInputView(subMapBB.getByteBuffer(),
								0,
								subMapBB.capacity());

							V value = valueTypeSerializer.deserialize(byteBufferDataInputView);
							if (!(value instanceof  Map)) {
								throw new GeminiRuntimeException("BUG, GBinarySplitHashMap just for KMap.");
							}

							mergeMap.putAll((Map) value);
						}
						result.put(k, new GSValue(mergeMap, binaryValue.getGValueType(), binaryValue.getSeqID()));
					} else {
						DataInputView byteBufferDataInputView = new ByteBufferDataInputView(binaryValue.getBb(),
							binaryValue.getValueOffset(),
							binaryValue.getValueLen());
						V value = valueTypeSerializer.deserialize(byteBufferDataInputView);
						result.put(k, new GSValue(value, binaryValue.getGValueType(), binaryValue.getSeqID()));
					}
				}
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw new GeminiRuntimeException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy