org.apache.arrow.vector.util.MapWithOrdinal Maven / Gradle / Ivy
Show all versions of arrow-vector Show documentation
/*
* 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.arrow.vector.util;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.arrow.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap;
/**
* An implementation of map that supports constant time look-up by a generic key or an ordinal.
*
* This class extends the functionality a regular {@link Map} with ordinal lookup support.
* Upon insertion an unused ordinal is assigned to the inserted (key, value) tuple.
* Upon update the same ordinal id is re-used while value is replaced.
* Upon deletion of an existing item, its corresponding ordinal is recycled and could be used by another item.
*
*
For any instance with N items, this implementation guarantees that ordinals are in the range of [0, N). However,
* the ordinal assignment is dynamic and may change after an insertion or deletion. Consumers of this class are
* responsible for explicitly checking the ordinal corresponding to a key via
* {@link org.apache.arrow.vector.util.MapWithOrdinal#getOrdinal(Object)} before attempting to execute a lookup
* with an ordinal.
*
* @param key type
* @param value type
*/
public class MapWithOrdinal implements Map {
private static final Logger logger = LoggerFactory.getLogger(MapWithOrdinal.class);
private final Map> primary = new HashMap<>();
private final IntObjectHashMap secondary = new IntObjectHashMap<>();
private final Map delegate = new Map() {
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public int size() {
return primary.size();
}
@Override
public boolean containsKey(Object key) {
return primary.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return primary.containsValue(value);
}
@Override
public V get(Object key) {
Entry pair = primary.get(key);
if (pair != null) {
return pair.getValue();
}
return null;
}
@Override
public V put(K key, V value) {
final Entry oldPair = primary.get(key);
// if key exists try replacing otherwise, assign a new ordinal identifier
final int ordinal = oldPair == null ? primary.size() : oldPair.getKey();
primary.put(key, new AbstractMap.SimpleImmutableEntry<>(ordinal, value));
secondary.put(ordinal, value);
return oldPair == null ? null : oldPair.getValue();
}
@Override
public V remove(Object key) {
final Entry oldPair = primary.remove(key);
if (oldPair != null) {
final int lastOrdinal = secondary.size();
final V last = secondary.get(lastOrdinal);
// normalize mappings so that all numbers until primary.size() is assigned
// swap the last element with the deleted one
secondary.put(oldPair.getKey(), last);
primary.put((K) key, new AbstractMap.SimpleImmutableEntry<>(oldPair.getKey(), last));
}
return oldPair == null ? null : oldPair.getValue();
}
@Override
public void putAll(Map extends K, ? extends V> m) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
primary.clear();
secondary.clear();
}
@Override
public Set keySet() {
return primary.keySet();
}
@Override
public Collection values() {
return StreamSupport.stream(secondary.entries().spliterator(), false)
.map((IntObjectMap.PrimitiveEntry t) -> Preconditions.checkNotNull(t).value())
.collect(Collectors.toList());
}
@Override
public Set> entrySet() {
return primary.entrySet().stream()
.map(entry -> new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), entry.getValue().getValue()))
.collect(Collectors.toSet());
}
};
/**
* Returns the value corresponding to the given ordinal.
*
* @param id ordinal value for lookup
* @return an instance of V
*/
public V getByOrdinal(int id) {
return secondary.get(id);
}
/**
* Returns the ordinal corresponding to the given key.
*
* @param key key for ordinal lookup
* @return ordinal value corresponding to key if it exists or -1
*/
public int getOrdinal(K key) {
Entry pair = primary.get(key);
if (pair != null) {
return pair.getKey();
}
return -1;
}
@Override
public int size() {
return delegate.size();
}
@Override
public boolean isEmpty() {
return delegate.isEmpty();
}
@Override
public V get(Object key) {
return delegate.get(key);
}
/**
* Inserts the tuple (key, value) into the map extending the semantics of {@link Map#put} with automatic ordinal
* assignment. A new ordinal is assigned if key does not exists. Otherwise the same ordinal is re-used but the value
* is replaced.
*
* @see java.util.Map#put
*/
@Override
public V put(K key, V value) {
return delegate.put(key, value);
}
@Override
public Collection values() {
return delegate.values();
}
@Override
public boolean containsKey(Object key) {
return delegate.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return delegate.containsValue(value);
}
/**
* Removes the element corresponding to the key if exists extending the semantics of {@link java.util.Map#remove}
* with ordinal re-cycling. The ordinal corresponding to the given key may be re-assigned to another tuple. It is
* important that consumer checks the ordinal value via
* {@link org.apache.arrow.vector.util.MapWithOrdinal#getOrdinal(Object)} before attempting to look-up by ordinal.
*
* @see java.util.Map#remove
*/
@Override
public V remove(Object key) {
return delegate.remove(key);
}
@Override
public void putAll(Map extends K, ? extends V> m) {
delegate.putAll(m);
}
@Override
public void clear() {
delegate.clear();
}
@Override
public Set keySet() {
return delegate.keySet();
}
/**
* Returns a list of keys in ordinal order.
*/
public List keyList() {
int size = size();
Set keys = keySet();
List children = new ArrayList<>(size);
for (K key : keys) {
children.add(getOrdinal(key), key);
}
return children;
}
@Override
public Set> entrySet() {
return delegate.entrySet();
}
}