![JAR search and dependency download from the Maven repository](/logo.png)
org.dinky.shaded.paimon.data.BinaryMap 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.dinky.shaded.paimon.data;
import org.dinky.shaded.paimon.annotation.Public;
import org.dinky.shaded.paimon.memory.MemorySegment;
import org.dinky.shaded.paimon.memory.MemorySegmentUtils;
import org.dinky.shaded.paimon.types.DataType;
import java.util.HashMap;
import java.util.Map;
import static org.dinky.shaded.paimon.utils.Preconditions.checkArgument;
/**
* [4 byte(keyArray size in bytes)] + [Key BinaryArray] + [Value BinaryArray].
*
* {@code BinaryMap} are influenced by Apache Spark UnsafeMapData.
*
* @since 0.4.0
*/
@Public
public final class BinaryMap extends BinarySection implements InternalMap {
private static final long serialVersionUID = 1L;
private transient BinaryArray keys;
private transient BinaryArray values;
public BinaryMap() {
keys = new BinaryArray();
values = new BinaryArray();
}
public int size() {
return keys.size();
}
@Override
public void pointTo(MemorySegment[] segments, int offset, int sizeInBytes) {
// Read the numBytes of key array from the first 4 bytes.
final int keyArrayBytes = MemorySegmentUtils.getInt(segments, offset);
assert keyArrayBytes >= 0 : "keyArraySize (" + keyArrayBytes + ") should >= 0";
final int valueArrayBytes = sizeInBytes - keyArrayBytes - 4;
assert valueArrayBytes >= 0 : "valueArraySize (" + valueArrayBytes + ") should >= 0";
// see BinarySection.readObject, on this call stack, keys and values are not initialized
if (keys == null) {
keys = new BinaryArray();
}
keys.pointTo(segments, offset + 4, keyArrayBytes);
if (values == null) {
values = new BinaryArray();
}
values.pointTo(segments, offset + 4 + keyArrayBytes, valueArrayBytes);
assert keys.size() == values.size();
this.segments = segments;
this.offset = offset;
this.sizeInBytes = sizeInBytes;
}
public BinaryArray keyArray() {
return keys;
}
public BinaryArray valueArray() {
return values;
}
public Map, ?> toJavaMap(DataType keyType, DataType valueType) {
Object[] keyArray = keys.toObjectArray(keyType);
Object[] valueArray = values.toObjectArray(valueType);
Map