You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@baremaps.apache.org by bc...@apache.org on 2023/07/31 20:49:52 UTC
[incubator-baremaps] 06/14: Add new collections and improve naming
This is an automated email from the ASF dual-hosted git repository.
bchapuis pushed a commit to branch 681-handle-the-out-of-memory-errors
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit fc86ea00008cf7fb02bab0acf0ccffc44eedc946
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Tue Jul 11 16:30:24 2023 +0200
Add new collections and improve naming
---
.../baremaps/benchmarks/DataMapBenchmark.java | 37 +-
.../baremaps/benchmarks/MemoryBenchmark.java | 8 +-
.../OpenStreetMapGeometriesBenchmark.java | 7 +-
.../baremaps/database/algorithm/BinarySearch.java | 16 +-
.../database/algorithm/ExternalMergeSort.java | 40 +-
.../collection/AbstractDataCollection.java | 3 +-
.../{DataList.java => AbstractDataList.java} | 6 +-
.../{DataMap.java => AbstractDataMap.java} | 31 +-
.../database/collection/AppendOnlyBuffer.java | 9 +-
.../database/collection/DataCollection.java | 4 +-
.../database/collection/DataCollectionAdapter.java | 4 +-
.../baremaps/database/collection/DataList.java | 72 +-
.../baremaps/database/collection/DataMap.java | 113 +-
.../database/collection/FixedSizeDataList.java | 22 +-
.../database/collection/IndexedDataList.java | 19 +-
...Map.java => Long2LongMemoryAlignedDataMap.java} | 6 +-
.../collection/Long2LongOpenHashDataMap.java | 1491 +++++++++++++++++++
.../collection/Long2LongPackedOpenHashDataMap.java | 1502 ++++++++++++++++++++
...DataMap.java => Long2ObjectIndexedDataMap.java} | 6 +-
...dDataMap.java => Long2ObjectJaggedDataMap.java} | 6 +-
...p.java => Long2ObjectMemoryAlignedDataMap.java} | 4 +-
...taMap.java => Long2ObjectMonotonicDataMap.java} | 31 +-
.../collection/Long2ObjectOpenHashDataMap.java | 1370 ++++++++++++++++++
.../database/collection/MemoryAlignedDataList.java | 12 +-
.../database/memory/MemoryMappedDirectory.java | 2 +-
.../baremaps/database/memory/MemoryMappedFile.java | 4 +-
.../baremaps/database/memory/OffHeapMemory.java | 4 +-
.../baremaps/database/schema/DataTableAdapter.java | 4 +-
.../baremaps/database/schema/DataTableImpl.java | 4 +-
.../postgres/PostgresCoordinateMap.java | 3 +-
.../postgres/PostgresReferenceMap.java | 4 +-
.../storage/flatgeobuf/FlatGeoBufDataTable.java | 5 +-
.../storage/geopackage/GeoPackageDataTable.java | 2 +-
.../storage/postgres/PostgresDataTable.java | 2 +-
.../storage/shapefile/ShapefileDataTable.java | 2 +-
.../workflow/tasks/ImportOpenStreetMap.java | 60 +-
.../org/apache/baremaps/database/DataListTest.java | 11 +-
.../org/apache/baremaps/database/DataMapTest.java | 29 +-
.../baremaps/database/SizedDataListTest.java | 3 +-
.../database/sort/ExternalMergeSortTest.java | 12 +-
.../baremaps/openstreetmap/store/MockDataMap.java | 4 +-
.../org/apache/baremaps/storage/MockDataTable.java | 2 +-
.../flatgeobuf/FlatGeoBufDataTableTest.java | 2 +-
.../geopackage/GeoPackageDataSchemaTest.java | 2 +-
.../geopackage/GeoPackageToPostgresTest.java | 2 +-
.../storage/postgres/PostgresDataTableTest.java | 4 +-
.../baremaps/workflow/tasks/ImportMonacoTest.java | 8 +-
.../workflow/tasks/ImportUpdateDataTest.java | 8 +-
.../tasks/ImportUpdateLiechtensteinTest.java | 8 +-
pom.xml | 2 +-
50 files changed, 4634 insertions(+), 378 deletions(-)
diff --git a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/DataMapBenchmark.java b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/DataMapBenchmark.java
index 91d8318c..e84c3764 100644
--- a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/DataMapBenchmark.java
+++ b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/DataMapBenchmark.java
@@ -16,9 +16,9 @@ package org.apache.baremaps.benchmarks;
import java.util.concurrent.TimeUnit;
import org.apache.baremaps.database.collection.*;
-import org.apache.baremaps.database.collection.JaggedDataMap;
import org.apache.baremaps.database.memory.OffHeapMemory;
import org.apache.baremaps.database.type.LongDataType;
+import org.apache.baremaps.database.type.PairDataType;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
@@ -48,7 +48,7 @@ public class DataMapBenchmark {
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void simpleDataMap() {
- benchmark(new JaggedDataMap<>(new AppendOnlyBuffer<>(new LongDataType())), N);
+ benchmark(new Long2ObjectJaggedDataMap<>(new AppendOnlyBuffer<>(new LongDataType())), N);
}
@Benchmark
@@ -56,7 +56,7 @@ public class DataMapBenchmark {
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void indexedDataMap() {
- benchmark(new IndexedDataMap<>(new AppendOnlyBuffer<>(new LongDataType())), N);
+ benchmark(new Long2ObjectIndexedDataMap<>(new AppendOnlyBuffer<>(new LongDataType())), N);
}
@Benchmark
@@ -64,7 +64,7 @@ public class DataMapBenchmark {
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void memoryAlignedDataMap() {
- benchmark(new MemoryAlignedDataMap<>(new LongDataType(), new OffHeapMemory()), N);
+ benchmark(new Long2ObjectMemoryAlignedDataMap<>(new LongDataType(), new OffHeapMemory()), N);
}
@Benchmark
@@ -72,7 +72,34 @@ public class DataMapBenchmark {
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void monotonicDataMap() {
- benchmark(new MonotonicDataMap<>(new AppendOnlyBuffer<>(new LongDataType())), N);
+ benchmark(new Long2ObjectMonotonicDataMap<>(new AppendOnlyBuffer<>(new LongDataType())), N);
+ }
+
+ @Benchmark
+ @BenchmarkMode(Mode.SingleShotTime)
+ @Warmup(iterations = 2)
+ @Measurement(iterations = 5)
+ public void openHashDataMap() {
+ benchmark(new Long2ObjectOpenHashDataMap<>(10_000_000L, 0.75f,
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new LongDataType(),
+ new OffHeapMemory()),
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new LongDataType(),
+ new OffHeapMemory())),
+ N);
+ }
+
+ @Benchmark
+ @BenchmarkMode(Mode.SingleShotTime)
+ @Warmup(iterations = 2)
+ @Measurement(iterations = 5)
+ public void packedHashDataMap() {
+ benchmark(new Long2LongPackedOpenHashDataMap(100_000_000L, 0.75f,
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new PairDataType<>(new LongDataType(), new LongDataType()),
+ new OffHeapMemory())),
+ N);
}
public static void main(String[] args) throws RunnerException {
diff --git a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/MemoryBenchmark.java b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/MemoryBenchmark.java
index e069ba2e..3decea87 100644
--- a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/MemoryBenchmark.java
+++ b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/MemoryBenchmark.java
@@ -20,7 +20,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import org.apache.baremaps.database.collection.DataMap;
-import org.apache.baremaps.database.collection.MemoryAlignedDataMap;
+import org.apache.baremaps.database.collection.Long2ObjectMemoryAlignedDataMap;
import org.apache.baremaps.database.memory.MemoryMappedFile;
import org.apache.baremaps.database.memory.OffHeapMemory;
import org.apache.baremaps.database.memory.OnHeapMemory;
@@ -62,7 +62,7 @@ public class MemoryBenchmark {
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void onHeap() {
- benchmark(new MemoryAlignedDataMap<>(new LongDataType(), new OnHeapMemory()), N);
+ benchmark(new Long2ObjectMemoryAlignedDataMap<>(new LongDataType(), new OnHeapMemory()), N);
}
@Benchmark
@@ -70,7 +70,7 @@ public class MemoryBenchmark {
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void offHeap() {
- benchmark(new MemoryAlignedDataMap<>(new LongDataType(), new OffHeapMemory()), N);
+ benchmark(new Long2ObjectMemoryAlignedDataMap<>(new LongDataType(), new OffHeapMemory()), N);
}
@Benchmark
@@ -79,7 +79,7 @@ public class MemoryBenchmark {
@Measurement(iterations = 5)
public void onDisk() throws IOException {
Path file = Files.createTempFile(Paths.get("."), "baremaps_", ".tmp");
- benchmark(new MemoryAlignedDataMap<>(new LongDataType(), new MemoryMappedFile(file)),
+ benchmark(new Long2ObjectMemoryAlignedDataMap<>(new LongDataType(), new MemoryMappedFile(file)),
N);
Files.delete(file);
}
diff --git a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java
index d517f463..c49c9e9f 100644
--- a/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java
+++ b/baremaps-benchmark/src/main/java/org/apache/baremaps/benchmarks/OpenStreetMapGeometriesBenchmark.java
@@ -27,7 +27,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.baremaps.database.collection.AppendOnlyBuffer;
import org.apache.baremaps.database.collection.DataMap;
-import org.apache.baremaps.database.collection.IndexedDataMap;
+import org.apache.baremaps.database.collection.Long2ObjectIndexedDataMap;
import org.apache.baremaps.database.memory.MemoryMappedFile;
import org.apache.baremaps.database.memory.OnHeapMemory;
import org.apache.baremaps.database.type.LongListDataType;
@@ -75,10 +75,11 @@ public class OpenStreetMapGeometriesBenchmark {
@Measurement(iterations = 3)
public void store() throws IOException {
Path file = Files.createTempFile(Paths.get("."), "baremaps_", ".tmp");
- DataMap<Coordinate> coordinateMap = new IndexedDataMap<>(
+ DataMap<Coordinate> coordinateMap = new Long2ObjectIndexedDataMap<>(
new AppendOnlyBuffer<>(new CoordinateDataType(), new MemoryMappedFile(file)));
DataMap<List<Long>> referenceMap =
- new IndexedDataMap<>(new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
+ new Long2ObjectIndexedDataMap<>(
+ new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
AtomicLong nodes = new AtomicLong(0);
AtomicLong ways = new AtomicLong(0);
AtomicLong relations = new AtomicLong(0);
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/BinarySearch.java b/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/BinarySearch.java
index e3193387..f6f68380 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/BinarySearch.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/BinarySearch.java
@@ -16,7 +16,7 @@ package org.apache.baremaps.database.algorithm;
import java.util.Comparator;
import java.util.function.Function;
-import org.apache.baremaps.database.collection.DataList;
+import org.apache.baremaps.database.collection.AbstractDataList;
/**
* A binary search algorithm.
@@ -32,8 +32,9 @@ public class BinarySearch {
* @return the index of the search key
* @param <E> the type of the elements in the list
*/
- public static <E> Long binarySearch(DataList<E> list, E value, Comparator<E> comparator) {
- return binarySearch(list, value, comparator, 0, list.sizeAsLong() - 1l);
+ public static <E> Long binarySearch(AbstractDataList<E> list, E value,
+ Comparator<E> comparator) {
+ return binarySearch(list, value, comparator, 0, list.size64() - 1l);
}
/**
@@ -47,7 +48,8 @@ public class BinarySearch {
* @return the index of the search key
* @param <E> the type of the elements in the list
*/
- public static <E> Long binarySearch(DataList<E> list, E value, Comparator<E> comparator,
+ public static <E> Long binarySearch(AbstractDataList<E> list, E value,
+ Comparator<E> comparator,
long fromIndex, long toIndex) {
long lo = fromIndex;
long hi = toIndex;
@@ -77,12 +79,12 @@ public class BinarySearch {
* @param <E> the type of the elements in the list
*/
public static <E, A> E binarySearchAttribute(
- DataList<E> list,
+ AbstractDataList<E> list,
Function<E, A> extractor,
A value,
Comparator<A> comparator) {
long lo = 0;
- long hi = list.sizeAsLong() - 1l;
+ long hi = list.size64() - 1l;
while (lo <= hi) {
long mi = (lo + hi) >>> 1;
E e = list.get(mi);
@@ -112,7 +114,7 @@ public class BinarySearch {
* @param <E> the type of the elements in the list
*/
public static <E, A> E binarySearchAttribute(
- DataList<E> list,
+ AbstractDataList<E> list,
Function<E, A> extractor,
A value,
Comparator<A> comparator,
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/ExternalMergeSort.java b/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/ExternalMergeSort.java
index 36973d01..747d9e4f 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/ExternalMergeSort.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/algorithm/ExternalMergeSort.java
@@ -18,7 +18,7 @@ import java.io.IOException;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Stream;
-import org.apache.baremaps.database.collection.DataList;
+import org.apache.baremaps.database.collection.AbstractDataList;
/**
* An external merge sort algorithm adapted from
@@ -41,10 +41,10 @@ public class ExternalMergeSort {
* @throws IOException
*/
public static <T> void sort(
- DataList<T> input,
- DataList<T> output,
+ AbstractDataList<T> input,
+ AbstractDataList<T> output,
final Comparator<T> comparator,
- Supplier<DataList<T>> tempLists,
+ Supplier<AbstractDataList<T>> tempLists,
long batchSize,
boolean distinct,
boolean parallel) throws IOException {
@@ -64,16 +64,16 @@ public class ExternalMergeSort {
* @throws IOException
*/
private static <T> long mergeSortedBatches(
- List<DataList<T>> batches,
- DataList<T> output,
+ List<AbstractDataList<T>> batches,
+ AbstractDataList<T> output,
Comparator<T> comparator,
boolean distinct) throws IOException {
PriorityQueue<DataStack<T>> queue =
new PriorityQueue<>(batches.size(), (i, j) -> comparator.compare(i.peek(), j.peek()));
- for (DataList<T> input : batches) {
- if (input.sizeAsLong() == 0) {
+ for (AbstractDataList<T> input : batches) {
+ if (input.size64() == 0) {
continue;
}
DataStack stack = new DataStack(input);
@@ -125,7 +125,7 @@ public class ExternalMergeSort {
}
}
- for (DataList<T> batch : batches) {
+ for (AbstractDataList<T> batch : batches) {
batch.clear();
}
@@ -144,14 +144,14 @@ public class ExternalMergeSort {
* @param <T>
* @return the sorted batches
*/
- public static <T> List<DataList<T>> sortInBatch(
- final DataList<T> input,
+ public static <T> List<AbstractDataList<T>> sortInBatch(
+ final AbstractDataList<T> input,
final Comparator<T> comparator,
- Supplier<DataList<T>> supplier,
+ Supplier<AbstractDataList<T>> supplier,
long batchSize,
final boolean distinct,
final boolean parallel) {
- List<DataList<T>> batches = new ArrayList<>();
+ List<AbstractDataList<T>> batches = new ArrayList<>();
List<T> batch = new ArrayList<>();
var iterator = input.iterator();
@@ -179,13 +179,13 @@ public class ExternalMergeSort {
* @param <T>
* @return the sorted batch
*/
- public static <T> DataList<T> sortBatch(
+ public static <T> AbstractDataList<T> sortBatch(
List<T> batch,
Comparator<T> comparator,
- Supplier<DataList<T>> supplier,
+ Supplier<AbstractDataList<T>> supplier,
boolean distinct,
boolean parallel) {
- DataList<T> output = supplier.get();
+ AbstractDataList<T> output = supplier.get();
Stream<T> tmpStream = batch.stream().sorted(comparator);
if (parallel) {
tmpStream = tmpStream.parallel();
@@ -198,25 +198,25 @@ public class ExternalMergeSort {
}
/**
- * A wrapper on top of a {@link DataList} which keeps the last data record in memory.
+ * A wrapper on top of a {@link AbstractDataList} which keeps the last data record in memory.
*
* @param <T>
*/
static final class DataStack<T> implements AutoCloseable {
- private DataList<T> list;
+ private AbstractDataList<T> list;
private Long index = 0l;
private T cache;
- public DataStack(DataList<T> list) {
+ public DataStack(AbstractDataList<T> list) {
this.list = list;
reload();
}
public boolean empty() {
- return this.index > list.sizeAsLong();
+ return this.index > list.size64();
}
public T peek() {
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataCollection.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataCollection.java
index 8ca22c3e..7df2805b 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataCollection.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataCollection.java
@@ -24,13 +24,12 @@ import java.util.AbstractCollection;
public abstract class AbstractDataCollection<E> extends AbstractCollection<E>
implements DataCollection<E> {
-
/**
* {@inheritDoc}
*/
@Override
public int size() {
- return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ return (int) Math.min(size64(), Integer.MAX_VALUE);
}
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataList.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataList.java
similarity index 92%
copy from baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataList.java
copy to baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataList.java
index 8a81858c..14da556a 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataList.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataList.java
@@ -20,9 +20,9 @@ import java.util.NoSuchElementException;
/**
* An abstract list of data elements that can hold a large number of elements.
*
- * @param <E> The type of the data.
+ * @param <E> The type of the elements.
*/
-public abstract class DataList<E> extends AbstractDataCollection<E> {
+public abstract class AbstractDataList<E> extends AbstractDataCollection<E> {
/**
* Appends a value to the list and returns its index.
@@ -68,7 +68,7 @@ public abstract class DataList<E> extends AbstractDataCollection<E> {
private long index = 0;
- private long size = sizeAsLong();
+ private long size = size64();
@Override
public boolean hasNext() {
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataMap.java
similarity index 76%
copy from baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataMap.java
copy to baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataMap.java
index 7720bc51..136ba024 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AbstractDataMap.java
@@ -14,7 +14,6 @@ package org.apache.baremaps.database.collection;
-import com.google.common.collect.Streams;
import java.util.*;
/**
@@ -22,7 +21,7 @@ import java.util.*;
*
* @param <E> The type of the elements.
*/
-public abstract class DataMap<E> implements Map<Long, E> {
+public abstract class AbstractDataMap<E> implements DataMap<E> {
/** {@inheritDoc} */
@Override
@@ -30,34 +29,12 @@ public abstract class DataMap<E> implements Map<Long, E> {
m.forEach(this::put);
}
- /**
- * Returns the value associated with the specified key or null if the key is not present.
- *
- * @param keys the keys
- * @return the values
- */
- public List<E> getAll(List<Long> keys) {
- return Streams.stream(keys).map(this::get).toList();
- }
-
/** {@inheritDoc} */
@Override
public boolean isEmpty() {
return size() == 0;
}
- /**
- * Returns the size of the map as a long.
- *
- * @return the size of the map
- */
- public abstract long sizeAsLong();
-
- /** {@inheritDoc} */
- public int size() {
- return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
- }
-
/**
* Returns an iterator over the keys of the map.
*
@@ -79,7 +56,7 @@ public abstract class DataMap<E> implements Map<Long, E> {
@Override
public int size() {
- return DataMap.this.size();
+ return AbstractDataMap.this.size();
}
}
@@ -104,7 +81,7 @@ public abstract class DataMap<E> implements Map<Long, E> {
@Override
public int size() {
- return DataMap.this.size();
+ return AbstractDataMap.this.size();
}
}
@@ -129,7 +106,7 @@ public abstract class DataMap<E> implements Map<Long, E> {
@Override
public int size() {
- return DataMap.this.size();
+ return AbstractDataMap.this.size();
}
}
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyBuffer.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyBuffer.java
index 8f324210..bb741110 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyBuffer.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyBuffer.java
@@ -26,8 +26,9 @@ import org.apache.baremaps.database.type.DataType;
/**
* A buffer of elements backed by a {@link DataType} and a {@link Memory}. Elements are appended to
- * the buffer and can be accessed by their position in the {@link Memory}. Appending elements to the
- * buffer is thread-safe.
+ * the buffer and can be accessed by their position in the {@link Memory}. In other words, it
+ * implements the Collection interface but is not intended to be used as such. Appending elements to
+ * the buffer is thread-safe.
*
* @param <E> The type of the data.
*/
@@ -119,7 +120,7 @@ public class AppendOnlyBuffer<E> extends AbstractDataCollection<E> {
}
/** {@inheritDoc} */
- public long sizeAsLong() {
+ public long size64() {
return size;
}
@@ -142,7 +143,7 @@ public class AppendOnlyBuffer<E> extends AbstractDataCollection<E> {
*/
@Override
public BufferIterator iterator() {
- final long size = sizeAsLong();
+ final long size = size64();
return new BufferIterator(size);
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollection.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollection.java
index 5dd7bea7..e91c61f9 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollection.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollection.java
@@ -21,11 +21,11 @@ public interface DataCollection<E> extends Collection<E> {
*
* @return the number of values
*/
- long sizeAsLong();
+ long size64();
/** {@inheritDoc} */
default int size() {
- return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ return (int) Math.min(size64(), Integer.MAX_VALUE);
}
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollectionAdapter.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollectionAdapter.java
index a4546962..403a7c50 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollectionAdapter.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataCollectionAdapter.java
@@ -48,7 +48,7 @@ public class DataCollectionAdapter<S, T> extends AbstractDataCollection<T> {
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
- return collection.sizeAsLong();
+ public long size64() {
+ return collection.size64();
}
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataList.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataList.java
index 8a81858c..c5fd7faf 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataList.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataList.java
@@ -12,76 +12,6 @@
package org.apache.baremaps.database.collection;
+public interface DataList {
-
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-/**
- * An abstract list of data elements that can hold a large number of elements.
- *
- * @param <E> The type of the data.
- */
-public abstract class DataList<E> extends AbstractDataCollection<E> {
-
- /**
- * Appends a value to the list and returns its index.
- *
- * @param value the value
- * @return the index of the value.
- */
- public abstract long addIndexed(E value);
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean add(E value) {
- addIndexed(value);
- return true;
- }
-
- /**
- * Sets the value at the specified index.
- *
- * @param index the index
- * @param value the value
- */
- public abstract void set(long index, E value);
-
- /**
- * Returns the value at the specified index.
- *
- * @param index the index
- * @return the value
- */
- public abstract E get(long index);
-
- /** {@inheritDoc} */
- @Override
- public abstract void clear();
-
- /** {@inheritDoc} */
- @Override
- public Iterator<E> iterator() {
- return new Iterator<>() {
-
- private long index = 0;
-
- private long size = sizeAsLong();
-
- @Override
- public boolean hasNext() {
- return index < size;
- }
-
- @Override
- public E next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- return get(index++);
- }
- };
- }
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataMap.java
index 7720bc51..e37e52f3 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataMap.java
@@ -12,124 +12,39 @@
package org.apache.baremaps.database.collection;
-
-
import com.google.common.collect.Streams;
-import java.util.*;
+import java.util.List;
+import java.util.Map;
/**
- * An abstract map of data elements that can hold a large number of elements.
+ * A map of longs to values that can be stored in memory or on disk and has a size that can exceed
+ * the maximum value of an integer.
*
- * @param <E> The type of the elements.
+ * @param <E>
*/
-public abstract class DataMap<E> implements Map<Long, E> {
-
- /** {@inheritDoc} */
- @Override
- public void putAll(Map<? extends Long, ? extends E> m) {
- m.forEach(this::put);
- }
-
- /**
- * Returns the value associated with the specified key or null if the key is not present.
- *
- * @param keys the keys
- * @return the values
- */
- public List<E> getAll(List<Long> keys) {
- return Streams.stream(keys).map(this::get).toList();
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean isEmpty() {
- return size() == 0;
- }
+public interface DataMap<E> extends Map<Long, E> {
/**
* Returns the size of the map as a long.
*
* @return the size of the map
*/
- public abstract long sizeAsLong();
-
- /** {@inheritDoc} */
- public int size() {
- return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
- }
-
- /**
- * Returns an iterator over the keys of the map.
- *
- * @return an iterator
- */
- protected abstract Iterator<Long> keyIterator();
+ long sizeAsLong();
/** {@inheritDoc} */
@Override
- public Set<Long> keySet() {
- return new KeySet();
- }
-
- private class KeySet extends AbstractSet<Long> {
- @Override
- public Iterator<Long> iterator() {
- return keyIterator();
- }
-
- @Override
- public int size() {
- return DataMap.this.size();
- }
- }
-
- /**
- * Returns an iterator over the values of the map.
- *
- * @return an iterator
- */
- protected abstract Iterator<E> valueIterator();
-
- /** {@inheritDoc} */
- @Override
- public Collection<E> values() {
- return new ValueCollection();
- }
-
- private class ValueCollection extends AbstractCollection<E> {
- @Override
- public Iterator<E> iterator() {
- return valueIterator();
- }
-
- @Override
- public int size() {
- return DataMap.this.size();
- }
+ default int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
}
/**
- * Returns an iterator over the entries of the map.
+ * Returns the value associated with the specified key or null if the key is not present.
*
- * @return an iterator
+ * @param keys the keys
+ * @return the values
*/
- protected abstract Iterator<Entry<Long, E>> entryIterator();
-
- /** {@inheritDoc} */
- @Override
- public Set<Entry<Long, E>> entrySet() {
- return new EntrySet();
+ default List<E> getAll(List<Long> keys) {
+ return Streams.stream(keys).map(this::get).toList();
}
- private class EntrySet extends AbstractSet<Entry<Long, E>> {
- @Override
- public Iterator<Entry<Long, E>> iterator() {
- return entryIterator();
- }
-
- @Override
- public int size() {
- return DataMap.this.size();
- }
- }
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/FixedSizeDataList.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/FixedSizeDataList.java
index 0fc73ac1..f5ed2f06 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/FixedSizeDataList.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/FixedSizeDataList.java
@@ -22,13 +22,13 @@ import org.apache.baremaps.database.memory.OffHeapMemory;
import org.apache.baremaps.database.type.FixedSizeDataType;
/**
- * A list that can hold a large number of fixed size data elements.
+ * A collection that can hold a large number of fixed size data elements.
*
- * This list is backed by a memory that can be either heap, off-heap, or memory mapped.
+ * This collection is backed by a memory that can be either heap, off-heap, or memory mapped.
*
* @param <E> The type of the elements.
*/
-public class FixedSizeDataList<E> extends DataList<E> {
+public class FixedSizeDataList<E> extends AbstractDataList<E> {
private final FixedSizeDataType<E> dataType;
@@ -37,7 +37,7 @@ public class FixedSizeDataList<E> extends DataList<E> {
private AtomicLong size;
/**
- * Constructs a list.
+ * Constructs a collection.
*
* @param dataType the data type
*/
@@ -46,7 +46,7 @@ public class FixedSizeDataList<E> extends DataList<E> {
}
/**
- * Constructs a list.
+ * Constructs a collection.
*
* @param dataType the data type
* @param memory the memory
@@ -62,8 +62,8 @@ public class FixedSizeDataList<E> extends DataList<E> {
private void write(long index, E value) {
long position = index * dataType.size();
- int segmentIndex = (int) (position / dataType.size());
- int segmentOffset = (int) (position % dataType.size());
+ int segmentIndex = (int) (position / memory.segmentSize());
+ int segmentOffset = (int) (position % memory.segmentSize());
ByteBuffer segment = memory.segment(segmentIndex);
dataType.write(segment, segmentOffset, value);
}
@@ -83,7 +83,7 @@ public class FixedSizeDataList<E> extends DataList<E> {
*/
@Override
public void set(long index, E value) {
- if (index >= sizeAsLong()) {
+ if (index >= size64()) {
throw new IndexOutOfBoundsException();
}
write(index, value);
@@ -95,8 +95,8 @@ public class FixedSizeDataList<E> extends DataList<E> {
@Override
public E get(long index) {
long position = index * dataType.size();
- int segmentIndex = (int) (position / dataType.size());
- int segmentOffset = (int) (position % dataType.size());
+ int segmentIndex = (int) (position / memory.segmentSize());
+ int segmentOffset = (int) (position % memory.segmentSize());
ByteBuffer segment = memory.segment(segmentIndex);
return dataType.read(segment, segmentOffset);
}
@@ -105,7 +105,7 @@ public class FixedSizeDataList<E> extends DataList<E> {
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
+ public long size64() {
return size.get();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/IndexedDataList.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/IndexedDataList.java
index 198f8bff..b68f88b8 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/IndexedDataList.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/IndexedDataList.java
@@ -17,21 +17,22 @@ package org.apache.baremaps.database.collection;
import org.apache.baremaps.database.type.LongDataType;
/**
- * A list that can hold a large number of variable size data elements.
+ * A collection that can hold a large number of variable size data elements.
*
- * This list is backed by an index and a buffer that can be either heap, off-heap, or memory mapped.
+ * This collection is backed by an index and a buffer that can be either heap, off-heap, or memory
+ * mapped.
*
* @param <E> The type of the elements.
*/
-public class IndexedDataList<E> extends DataList<E> {
+public class IndexedDataList<E> extends AbstractDataList<E> {
- private final DataList<Long> index;
+ private final AbstractDataList<Long> index;
private final AppendOnlyBuffer<E> values;
/**
- * Constructs a list.
+ * Constructs a collection.
*
* @param values the values
*/
@@ -40,12 +41,12 @@ public class IndexedDataList<E> extends DataList<E> {
}
/**
- * Constructs a list.
+ * Constructs a collection.
*
* @param index the index
* @param values the values
*/
- public IndexedDataList(DataList<Long> index, AppendOnlyBuffer<E> values) {
+ public IndexedDataList(AbstractDataList<Long> index, AppendOnlyBuffer<E> values) {
this.index = index;
this.values = values;
}
@@ -81,8 +82,8 @@ public class IndexedDataList<E> extends DataList<E> {
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
- return index.sizeAsLong();
+ public long size64() {
+ return index.size64();
}
/**
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/LongDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongMemoryAlignedDataMap.java
similarity index 83%
rename from baremaps-core/src/main/java/org/apache/baremaps/database/collection/LongDataMap.java
rename to baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongMemoryAlignedDataMap.java
index 186d99fb..23b0cf4d 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/LongDataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongMemoryAlignedDataMap.java
@@ -21,12 +21,12 @@ import org.apache.baremaps.database.type.LongDataType;
/**
* A list of longs.
*/
-public class LongDataMap extends MemoryAlignedDataMap<Long> {
+public class Long2LongMemoryAlignedDataMap extends Long2ObjectMemoryAlignedDataMap<Long> {
/**
* Constructs a list.
*/
- public LongDataMap() {
+ public Long2LongMemoryAlignedDataMap() {
this(new OffHeapMemory());
}
@@ -35,7 +35,7 @@ public class LongDataMap extends MemoryAlignedDataMap<Long> {
*
* @param memory the memory
*/
- public LongDataMap(Memory memory) {
+ public Long2LongMemoryAlignedDataMap(Memory memory) {
super(new LongDataType(), memory);
}
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongOpenHashDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongOpenHashDataMap.java
new file mode 100644
index 00000000..ef8bc548
--- /dev/null
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongOpenHashDataMap.java
@@ -0,0 +1,1491 @@
+/*
+ * Licensed 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.baremaps.database.collection;
+
+import static it.unimi.dsi.fastutil.HashCommon.*;
+
+import it.unimi.dsi.fastutil.Hash;
+import it.unimi.dsi.fastutil.HashCommon;
+import it.unimi.dsi.fastutil.longs.*;
+import it.unimi.dsi.fastutil.objects.*;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+import java.util.function.LongConsumer;
+import java.util.function.Supplier;
+
+public class Long2LongOpenHashDataMap extends AbstractLong2LongMap
+ implements DataMap<Long>, Hash {
+
+ protected Supplier<AbstractDataList<Long>> keySupplier;
+
+ protected Supplier<AbstractDataList<Long>> valueSupplier;
+
+ /**
+ * The array of keys.
+ */
+ protected transient AbstractDataList<Long> key;
+ /**
+ * The array of values.
+ */
+ protected transient AbstractDataList<Long> value;
+ /**
+ * The mask for wrapping a position counter.
+ */
+ protected transient long mask;
+ /**
+ * Whether this map contains the key zero.
+ */
+ protected transient boolean containsNullKey;
+ /**
+ * The current table size.
+ */
+ protected transient long n;
+ /**
+ * Threshold after which we rehash. It must be the table size times {@link #f}.
+ */
+ protected transient long maxFill;
+ /**
+ * We never resize below this threshold, which is the construction-time {#n}.
+ */
+ protected final transient long minN;
+ /**
+ * Number of entries in the set (including the key zero, if present).
+ */
+ protected AtomicLong size = new AtomicLong();
+ /**
+ * The acceptable load factor.
+ */
+ protected final float f;
+ /**
+ * Cached set of entries.
+ */
+ protected transient FastEntrySet entries;
+
+ /**
+ * Cached set of keys.
+ */
+ protected transient LongSet keys;
+ /**
+ * Cached collection of values.
+ */
+ protected transient LongCollection values;
+
+ private Supplier<AbstractDataList<Long>> listSupplier;
+
+ /**
+ * Creates a new hash map.
+ *
+ * <p>
+ * The actual table size will be the least power of two greater than {@code expected}/{@code f}.
+ *
+ * @param expected the expected number of elements in the hash map.
+ * @param f the load factor.
+ */
+ public Long2LongOpenHashDataMap(
+ long expected,
+ float f,
+ Supplier<AbstractDataList<Long>> keySupplier,
+ Supplier<AbstractDataList<Long>> valueSupplier) {
+ if (f <= 0 || f >= 1)
+ throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than 1");
+ if (expected < 0)
+ throw new IllegalArgumentException("The expected number of elements must be nonnegative");
+ this.f = f;
+ this.minN = n = bigArraySize(expected, f);
+ this.mask = n - 1;
+ this.maxFill = maxFill(n, f);
+ this.keySupplier = keySupplier;
+ this.valueSupplier = valueSupplier;
+ this.key = keySupplier.get();
+ this.value = valueSupplier.get();
+ }
+
+ private long realSize() {
+ return containsNullKey ? size.get() - 1 : size.get();
+ }
+
+ private void ensureCapacity(final long capacity) {
+ final long needed = bigArraySize(capacity, f);
+ if (needed > n)
+ rehash(needed);
+ }
+
+ private void tryCapacity(final long capacity) {
+ final long needed =
+ Math.min(1 << 30, Math.max(2, HashCommon.nextPowerOfTwo((long) Math.ceil(capacity / f))));
+ if (needed > n)
+ rehash(needed);
+ }
+
+ private long removeEntry(final long pos) {
+ final long oldValue = value.get(pos);
+ size.decrementAndGet();
+ shiftKeys(pos);
+ if (n > minN && size.get() < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
+ rehash(n / 2);
+ return oldValue;
+ }
+
+ private long removeNullEntry() {
+ containsNullKey = false;
+ final long oldValue = value.get(n);
+ size.decrementAndGet();
+ if (n > minN && size.get() < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
+ rehash(n / 2);
+ return oldValue;
+ }
+
+ @Override
+ public void putAll(Map<? extends Long, ? extends Long> m) {
+ if (f <= .5)
+ ensureCapacity(m.size()); // The resulting map will be sized for m.size() elements
+ else
+ tryCapacity(size() + m.size()); // The resulting map will be tentatively sized for size() +
+ // m.size()
+ // elements
+ super.putAll(m);
+ }
+
+ private long find(final long k) {
+ if (((k) == (0)))
+ return containsNullKey ? n : -(n + 1);
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = HashCommon.mix((k)) & mask)) == (0)))
+ return -(pos + 1);
+ if (((k) == (curr)))
+ return pos;
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return -(pos + 1);
+ if (((k) == (curr)))
+ return pos;
+ }
+ }
+
+ private void insert(final long pos, final long k, final long v) {
+ if (pos == n)
+ containsNullKey = true;
+ key.set(pos, k);
+ value.set(pos, v);
+ if (size.getAndIncrement() >= maxFill)
+ rehash(bigArraySize(size.get() + 1, f));
+ }
+
+ @Override
+ public long put(final long k, final long v) {
+ final long pos = find(k);
+ if (pos < 0) {
+ insert(-pos - 1, k, v);
+ return defRetValue;
+ }
+ final long oldValue = value.get(pos);
+ value.set(pos, v);
+ return oldValue;
+ }
+
+ private long addToValue(final long pos, final long incr) {
+ final long oldValue = value.get(pos);
+ value.set(pos, oldValue + incr);
+ return oldValue;
+ }
+
+ /**
+ * Adds an increment to value currently associated with a key.
+ *
+ * <p>
+ * Note that this method respects the {@linkplain #defaultReturnValue() default return value}
+ * semantics: when called with a key that does not currently appears in the map, the key will be
+ * associated with the default return value plus the given increment.
+ *
+ * @param k the key.
+ * @param incr the increment.
+ * @return the old value, or the {@linkplain #defaultReturnValue() default return value} if no
+ * value was present for the given key.
+ */
+ public long addTo(final long k, final long incr) {
+ long pos;
+ if (((k) == (0))) {
+ if (containsNullKey)
+ return addToValue(n, incr);
+ pos = n;
+ containsNullKey = true;
+ } else {
+ long curr;
+ // The starting point.
+ if (!((curr = key.get(pos = HashCommon.mix((k)) & mask)) == (0))) {
+ if (((curr) == (k)))
+ return addToValue(pos, incr);
+ while (!((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ if (((curr) == (k)))
+ return addToValue(pos, incr);
+ }
+ }
+ key.set(pos, k);
+ value.set(pos, defRetValue + incr);
+ if (size.incrementAndGet() >= maxFill)
+ rehash(bigArraySize(size.get() + 1, f));
+ return defRetValue;
+ }
+
+ /**
+ * Shifts left entries with the specified hash code, starting at the specified position, and
+ * empties the resulting free entry.
+ *
+ * @param pos a starting position.
+ */
+ protected final void shiftKeys(long pos) {
+ // Shift entries with the same hash.
+ long last, slot;
+ long curr;
+ for (;;) {
+ pos = ((last = pos) + 1) & mask;
+ for (;;) {
+ if (((curr = key.get(pos)) == (0))) {
+ key.set(last, 0L);
+ return;
+ }
+ slot = HashCommon.mix((curr)) & mask;
+ if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
+ break;
+ pos = (pos + 1) & mask;
+ }
+ key.set(last, curr);
+ value.set(last, value.get(pos));
+ }
+ }
+
+ @Override
+
+ public long remove(final long k) {
+ if (((k) == (0))) {
+ if (containsNullKey)
+ return removeNullEntry();
+ return defRetValue;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = HashCommon.mix((k)) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return removeEntry(pos);
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return removeEntry(pos);
+ }
+ }
+
+ @Override
+
+ public long get(final long k) {
+ if (((k) == (0)))
+ return containsNullKey ? value.get(n) : defRetValue;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = HashCommon.mix((k)) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ }
+ }
+
+ @Override
+
+ public boolean containsKey(final long k) {
+ if (((k) == (0)))
+ return containsNullKey;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return true;
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return true;
+ }
+ }
+
+ @Override
+ public boolean containsValue(final long v) {
+ if (containsNullKey && ((value.get(n)) == (v)))
+ return true;
+ for (long i = n; i-- != 0;)
+ if (!((key.get(i)) == (0)) && ((value.get(i)) == (v)))
+ return true;
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+
+ public long getOrDefault(final long k, final long defaultValue) {
+ if (((k) == (0)))
+ return containsNullKey ? value.get(n) : defaultValue;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = HashCommon.mix((k)) & mask)) == (0)))
+ return defaultValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return defaultValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long putIfAbsent(final long k, final long v) {
+ final long pos = find(k);
+ if (pos >= 0)
+ return value.get(pos);
+ insert(-pos - 1, k, v);
+ return defRetValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+
+ public boolean remove(final long k, final long v) {
+ if (((k) == (0))) {
+ if (containsNullKey && ((v) == (value.get(n)))) {
+ removeNullEntry();
+ return true;
+ }
+ return false;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)) && ((v) == (value.get(pos)))) {
+ removeEntry(pos);
+ return true;
+ }
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)) && ((v) == (value.get(pos)))) {
+ removeEntry(pos);
+ return true;
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean replace(final long k, final long oldValue, final long v) {
+ final long pos = find(k);
+ if (pos < 0 || !((oldValue) == (value.get(pos))))
+ return false;
+ value.set(pos, v);
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long replace(final long k, final long v) {
+ final long pos = find(k);
+ if (pos < 0)
+ return defRetValue;
+ final long oldValue = value.get(pos);
+ value.set(pos, v);
+ return oldValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfAbsent(final long k,
+ final java.util.function.LongUnaryOperator mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(k);
+ if (pos >= 0)
+ return value.get(pos);
+ final long newValue = mappingFunction.applyAsLong(k);
+ insert(-pos - 1, k, newValue);
+ return newValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfAbsent(final long key, final Long2LongFunction mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(key);
+ if (pos >= 0)
+ return value.get(pos);
+ if (!mappingFunction.containsKey(key))
+ return defRetValue;
+ final long newValue = mappingFunction.get(key);
+ insert(-pos - 1, key, newValue);
+ return newValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfAbsentNullable(final long k,
+ final java.util.function.LongFunction<? extends Long> mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(k);
+ if (pos >= 0)
+ return value.get(pos);
+ final Long newValue = mappingFunction.apply(k);
+ if (newValue == null)
+ return defRetValue;
+ final long v = (newValue).longValue();
+ insert(-pos - 1, k, v);
+ return v;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfPresent(final long k,
+ final java.util.function.BiFunction<? super Long, ? super Long, ? extends Long> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+ final long pos = find(k);
+ if (pos < 0)
+ return defRetValue;
+ final Long newValue = remappingFunction.apply(Long.valueOf(k), Long.valueOf(value.get(pos)));
+ if (newValue == null) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ return defRetValue;
+ }
+ long newVal = (newValue).longValue();
+ value.set(pos, newVal);
+ return newVal;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long compute(final long k,
+ final java.util.function.BiFunction<? super Long, ? super Long, ? extends Long> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+ final long pos = find(k);
+ final Long newValue =
+ remappingFunction.apply(Long.valueOf(k), pos >= 0 ? Long.valueOf(value.get(pos)) : null);
+ if (newValue == null) {
+ if (pos >= 0) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ }
+ return defRetValue;
+ }
+ long newVal = (newValue).longValue();
+ if (pos < 0) {
+ insert(-pos - 1, k, newVal);
+ return newVal;
+ }
+ value.set(pos, newVal);
+ return newVal;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long merge(final long k, final long v,
+ final java.util.function.BiFunction<? super Long, ? super Long, ? extends Long> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+
+ final long pos = find(k);
+ if (pos < 0) {
+ if (pos < 0)
+ insert(-pos - 1, k, v);
+ else
+ value.set(pos, v);
+ return v;
+ }
+ final Long newValue = remappingFunction.apply(Long.valueOf(value.get(pos)), Long.valueOf(v));
+ if (newValue == null) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ return defRetValue;
+ }
+ long newVal = (newValue).longValue();
+ value.set(pos, newVal);
+ return newVal;
+ }
+
+ /*
+ * Removes all elements from this map.
+ *
+ * <p>To increase object reuse, this method does not change the table size. If you want to reduce
+ * the table size, you must use {@link #trim()}.
+ *
+ */
+ @Override
+ public void clear() {
+ if (size.get() == 0)
+ return;
+ size.set(0);
+ containsNullKey = false;
+ // TODO: Arrays.fill(key, (0));
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return size.get() == 0;
+ }
+
+ @Override
+ public long sizeAsLong() {
+ return size.get();
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ /**
+ * The entry class for a hash map does not record key and value, but rather the position in the
+ * hash table of the corresponding entry. This is necessary so that calls to
+ * {@link java.util.Map.Entry#setValue(Object)} are reflected in the map
+ */
+ final class MapEntry implements Long2LongMap.Entry, Map.Entry<Long, Long>, LongLongPair {
+ // The table index this entry refers to, or -1 if this entry has been deleted.
+ long index;
+
+ MapEntry(final long index) {
+ this.index = index;
+ }
+
+ MapEntry() {}
+
+ @Override
+ public long getLongKey() {
+ return key.get(index);
+ }
+
+ @Override
+ public long leftLong() {
+ return key.get(index);
+ }
+
+ @Override
+ public long getLongValue() {
+ return value.get(index);
+ }
+
+ @Override
+ public long rightLong() {
+ return value.get(index);
+ }
+
+ @Override
+ public long setValue(final long v) {
+ final long oldValue = value.get(index);
+ value.set(index, v);
+ return oldValue;
+ }
+
+ @Override
+ public LongLongPair right(final long v) {
+ value.set(index, v);
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Please use the corresponding type-specific method instead.
+ */
+ @Deprecated
+ @Override
+ public Long getKey() {
+ return Long.valueOf(key.get(index));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Please use the corresponding type-specific method instead.
+ */
+ @Deprecated
+ @Override
+ public Long getValue() {
+ return Long.valueOf(value.get(index));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Please use the corresponding type-specific method instead.
+ */
+ @Deprecated
+ @Override
+ public Long setValue(final Long v) {
+ return Long.valueOf(setValue((v).longValue()));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ Map.Entry<Long, Long> e = (Map.Entry<Long, Long>) o;
+ return ((key.get(index)) == ((e.getKey()).longValue()))
+ && ((value.get(index)) == ((e.getValue()).longValue()));
+ }
+
+ @Override
+ public int hashCode() {
+ return HashCommon.long2int(key.get(index)) ^ HashCommon.long2int(value.get(index));
+ }
+
+ @Override
+ public String toString() {
+ return key.get(index) + "=>" + value.get(index);
+ }
+ }
+
+ /**
+ * An iterator over a hash map.
+ */
+ private abstract class MapIterator<ConsumerType> {
+ /**
+ * The index of the last entry returned, if positive or zero; initially, {@link #n}. If
+ * negative, the last entry returned was that of the key of index {@code - pos - 1} from the
+ * {@link #wrapped} list.
+ */
+ long pos = n;
+ /**
+ * The index of the last entry that has been returned (more precisely, the value of {@link #pos}
+ * if {@link #pos} is positive, or {@link Integer#MIN_VALUE} if {@link #pos} is negative). It is
+ * -1 if either we did not return an entry yet, or the last returned entry has been removed.
+ */
+ long last = -1;
+ /**
+ * A downward counter measuring how many entries must still be returned.
+ */
+ long c = size.get();
+ /**
+ * A boolean telling us whether we should return the entry with the null key.
+ */
+ boolean mustReturnNullKey = Long2LongOpenHashDataMap.this.containsNullKey;
+ /**
+ * A lazily allocated list containing keys of entries that have wrapped around the table because
+ * of removals.
+ */
+ LongArrayList wrapped;
+
+ @SuppressWarnings("unused")
+ abstract void acceptOnIndex(final ConsumerType action, final long index);
+
+ public boolean hasNext() {
+ return c != 0;
+ }
+
+ public long nextEntry() {
+ if (!hasNext())
+ throw new NoSuchElementException();
+ c--;
+ if (mustReturnNullKey) {
+ mustReturnNullKey = false;
+ return last = n;
+ }
+ for (;;) {
+ if (--pos < 0) {
+ // We are just enumerating elements from the wrapped list.
+ last = Integer.MIN_VALUE;
+ final long k = wrapped.getLong((int) -pos - 1); // TODO: check if -pos - 1 is correct
+ long p = (int) HashCommon.mix((k)) & mask;
+ while (!((k) == (key.get(p))))
+ p = (p + 1) & mask;
+ return p;
+ }
+ if (!((key.get(pos)) == (0)))
+ return last = pos;
+ }
+ }
+
+ public void forEachRemaining(final ConsumerType action) {
+ if (mustReturnNullKey) {
+ mustReturnNullKey = false;
+ acceptOnIndex(action, last = n);
+ c--;
+ }
+ while (c != 0) {
+ if (--pos < 0) {
+ // We are just enumerating elements from the wrapped list.
+ last = Integer.MIN_VALUE;
+ final long k = wrapped.getLong((int) -pos - 1); // TODO: check if -pos - 1 is correct
+ long p = (int) HashCommon.mix((k)) & mask;
+ while (!((k) == (key.get(p))))
+ p = (p + 1) & mask;
+ acceptOnIndex(action, p);
+ c--;
+ } else if (!((key.get(pos)) == (0))) {
+ acceptOnIndex(action, last = pos);
+ c--;
+ }
+ }
+ }
+
+ /**
+ * Shifts left entries with the specified hash code, starting at the specified position, and
+ * empties the resulting free entry.
+ *
+ * @param pos a starting position.
+ */
+ private void shiftKeys(long pos) {
+ // Shift entries with the same hash.
+ long last, slot;
+ long curr;
+ for (;;) {
+ pos = ((last = pos) + 1) & mask;
+ for (;;) {
+ if (((curr = key.get(pos)) == (0))) {
+ key.set(last, 0L);
+ return;
+ }
+ slot = (int) HashCommon.mix((curr)) & mask;
+ if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
+ break;
+ pos = (pos + 1) & mask;
+ }
+ if (pos < last) { // Wrapped entry.
+ if (wrapped == null)
+ wrapped = new LongArrayList(2);
+ wrapped.add(key.get(pos));
+ }
+ key.set(last, curr);
+ value.set(last, value.get(pos));
+ }
+ }
+
+ public void remove() {
+ if (last == -1)
+ throw new IllegalStateException();
+ if (last == n) {
+ containsNullKey = false;
+ } else if (pos >= 0)
+ shiftKeys(last);
+ else {
+ // We're removing wrapped entries.
+ Long2LongOpenHashDataMap.this.remove(wrapped.getLong((int) -pos - 1)); // TODO: check if
+ // -pos - 1
+ // is correct
+ last = -1; // Note that we must not decrement size
+ return;
+ }
+ size.decrementAndGet();
+ last = -1; // You can no longer remove this entry.
+ }
+
+ public int skip(final int n) {
+ int i = n;
+ while (i-- != 0 && hasNext())
+ nextEntry();
+ return n - i - 1;
+ }
+ }
+
+ private final class EntryIterator
+ extends Long2LongOpenHashDataMap.MapIterator<Consumer<? super Long2LongMap.Entry>>
+ implements ObjectIterator<Long2LongMap.Entry> {
+ private Long2LongOpenHashDataMap.MapEntry entry;
+
+ @Override
+ public Long2LongOpenHashDataMap.MapEntry next() {
+ return entry = new Long2LongOpenHashDataMap.MapEntry(nextEntry());
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2LongMap.Entry> action, final long index) {
+ action.accept(entry = new Long2LongOpenHashDataMap.MapEntry(index));
+ }
+
+ @Override
+ public void remove() {
+ super.remove();
+ entry.index = -1; // You cannot use a deleted entry.
+ }
+ }
+
+ private final class FastEntryIterator
+ extends Long2LongOpenHashDataMap.MapIterator<Consumer<? super Long2LongMap.Entry>>
+ implements ObjectIterator<Long2LongMap.Entry> {
+ private final Long2LongOpenHashDataMap.MapEntry entry = new Long2LongOpenHashDataMap.MapEntry();
+
+ @Override
+ public Long2LongOpenHashDataMap.MapEntry next() {
+ entry.index = nextEntry();
+ return entry;
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2LongMap.Entry> action, final long index) {
+ entry.index = index;
+ action.accept(entry);
+ }
+ }
+
+ private abstract class MapSpliterator<ConsumerType, SplitType extends Long2LongOpenHashDataMap.MapSpliterator<ConsumerType, SplitType>> {
+ /**
+ * The index (which bucket) of the next item to give to the action. Unlike {@code SetIterator},
+ * this counts up instead of down.
+ */
+ long pos = 0;
+ /**
+ * The maximum bucket (exclusive) to iterate to
+ */
+ long max = n;
+ /**
+ * An upwards counter counting how many we have given
+ */
+ long c = 0;
+ /**
+ * A boolean telling us whether we should return the null key.
+ */
+ boolean mustReturnNull = Long2LongOpenHashDataMap.this.containsNullKey;
+ boolean hasSplit = false;
+
+ MapSpliterator() {}
+
+ MapSpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ this.pos = pos;
+ this.max = max;
+ this.mustReturnNull = mustReturnNull;
+ this.hasSplit = hasSplit;
+ }
+
+ abstract void acceptOnIndex(final ConsumerType action, final long index);
+
+ abstract SplitType makeForSplit(long pos, long max, boolean mustReturnNull);
+
+ public boolean tryAdvance(final ConsumerType action) {
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++c;
+ acceptOnIndex(action, n);
+ return true;
+ }
+ while (pos < max) {
+ if (!((key.get(pos)) == (0))) {
+ ++c;
+ acceptOnIndex(action, pos++);
+ return true;
+ }
+ ++pos;
+ }
+ return false;
+ }
+
+ public void forEachRemaining(final ConsumerType action) {
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++c;
+ acceptOnIndex(action, n);
+ }
+ while (pos < max) {
+ if (!((key.get(pos)) == (0))) {
+ acceptOnIndex(action, pos);
+ ++c;
+ }
+ ++pos;
+ }
+ }
+
+ public long estimateSize() {
+ if (!hasSplit) {
+ // Root spliterator; we know how many are remaining.
+ return size.get() - c;
+ } else {
+ // After we split, we can no longer know exactly how many we have (or at least not
+ // efficiently).
+ // (size / n) * (max - pos) aka currentTableDensity * numberOfBucketsLeft seems like a good
+ // estimate.
+ return Math.min(size.get() - c,
+ (long) (((double) realSize() / n) * (max - pos)) + (mustReturnNull ? 1 : 0));
+ }
+ }
+
+ public SplitType trySplit() {
+ if (pos >= max - 1)
+ return null;
+ long retLen = (max - pos) >> 1;
+ if (retLen <= 1)
+ return null;
+ long myNewPos = pos + retLen;
+ long retPos = pos;
+ long retMax = myNewPos;
+ // Since null is returned first, and the convention is that the returned split is the prefix
+ // of
+ // elements,
+ // the split will take care of returning null (if needed), and we won't return it anymore.
+ SplitType split = makeForSplit(retPos, retMax, mustReturnNull);
+ this.pos = myNewPos;
+ this.mustReturnNull = false;
+ this.hasSplit = true;
+ return split;
+ }
+
+ public long skip(long n) {
+ if (n < 0)
+ throw new IllegalArgumentException("Argument must be nonnegative: " + n);
+ if (n == 0)
+ return 0;
+ long skipped = 0;
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++skipped;
+ --n;
+ }
+ while (pos < max && n > 0) {
+ if (!((key.get(pos++)) == (0))) {
+ ++skipped;
+ --n;
+ }
+ }
+ return skipped;
+ }
+ }
+
+ private final class EntrySpliterator extends
+ Long2LongOpenHashDataMap.MapSpliterator<Consumer<? super Long2LongMap.Entry>, Long2LongOpenHashDataMap.EntrySpliterator>
+ implements ObjectSpliterator<Long2LongMap.Entry> {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ ObjectSpliterators.SET_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ EntrySpliterator() {}
+
+ EntrySpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : ObjectSpliterators.SET_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2LongMap.Entry> action, final long index) {
+ action.accept(new Long2LongOpenHashDataMap.MapEntry(index));
+ }
+
+ @Override
+ final Long2LongOpenHashDataMap.EntrySpliterator makeForSplit(long pos, long max,
+ boolean mustReturnNull) {
+ return new Long2LongOpenHashDataMap.EntrySpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ private final class MapEntrySet extends AbstractObjectSet<Long2LongMap.Entry>
+ implements FastEntrySet {
+ @Override
+ public ObjectIterator<Long2LongMap.Entry> iterator() {
+ return new Long2LongOpenHashDataMap.EntryIterator();
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public ObjectIterator<Long2LongMap.Entry> fastIterator() {
+ return new Long2LongOpenHashDataMap.FastEntryIterator();
+ }
+
+ @Override
+ public ObjectSpliterator<Long2LongMap.Entry> spliterator() {
+ return new Long2LongOpenHashDataMap.EntrySpliterator();
+ }
+
+ //
+ @Override
+
+ public boolean contains(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
+ if (e.getKey() == null || !(e.getKey() instanceof Long))
+ return false;
+ if (e.getValue() == null || !(e.getValue() instanceof Long))
+ return false;
+ final long k = ((Long) (e.getKey())).longValue();
+ final long v = ((Long) (e.getValue())).longValue();
+ if (((k) == (0)))
+ return Long2LongOpenHashDataMap.this.containsNullKey && ((value.get(n)) == (v));
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (int) HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return ((value.get(pos)) == (v));
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return ((value.get(pos)) == (v));
+ }
+ }
+
+ @Override
+
+ public boolean remove(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
+ if (e.getKey() == null || !(e.getKey() instanceof Long))
+ return false;
+ if (e.getValue() == null || !(e.getValue() instanceof Long))
+ return false;
+ final long k = ((Long) (e.getKey())).longValue();
+ final long v = ((Long) (e.getValue())).longValue();
+ if (((k) == (0))) {
+ if (containsNullKey && ((value.get(n)) == (v))) {
+ removeNullEntry();
+ return true;
+ }
+ return false;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (int) HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((curr) == (k))) {
+ if (((value.get(pos)) == (v))) {
+ removeEntry(pos);
+ return true;
+ }
+ return false;
+ }
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((curr) == (k))) {
+ if (((value.get(pos)) == (v))) {
+ removeEntry(pos);
+ return true;
+ }
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ Long2LongOpenHashDataMap.this.clear();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void forEach(final Consumer<? super Long2LongMap.Entry> consumer) {
+ if (containsNullKey)
+ consumer.accept(new AbstractLong2LongMap.BasicEntry(key.get(n), value.get(n)));
+ for (long pos = n; pos-- != 0;)
+ if (!((key.get(pos)) == (0)))
+ consumer.accept(new AbstractLong2LongMap.BasicEntry(key.get(pos), value.get(pos)));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void fastForEach(final Consumer<? super Long2LongMap.Entry> consumer) {
+ if (containsNullKey) {
+ consumer.accept(new AbstractLong2LongMap.BasicEntry(key.get(n), value.get(n)));
+ }
+ for (long pos = n; pos-- != 0;)
+ if (!((key.get(pos)) == (0))) {
+ consumer.accept(new AbstractLong2LongMap.BasicEntry(key.get(pos), value.get(pos)));
+ }
+ }
+ }
+
+ @Override
+ public FastEntrySet long2LongEntrySet() {
+ if (entries == null)
+ entries = new Long2LongOpenHashDataMap.MapEntrySet();
+ return entries;
+ }
+
+ /**
+ * An iterator on keys.
+ *
+ * <p>
+ * We simply override the
+ * {@link java.util.ListIterator#next()}/{@link java.util.ListIterator#previous()} methods (and
+ * possibly their type-specific counterparts) so that they return keys instead of entries.
+ */
+ private final class KeyIterator extends Long2LongOpenHashDataMap.MapIterator<LongConsumer>
+ implements LongIterator {
+ public KeyIterator() {
+ super();
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ // Despite the superclass declared with generics, the way Java inherits and generates bridge
+ // methods
+ // avoids the boxing/unboxing
+ @Override
+ final void acceptOnIndex(final java.util.function.LongConsumer action, final long index) {
+ action.accept(key.get(index));
+ }
+
+ @Override
+ public long nextLong() {
+ return key.get(nextEntry());
+ }
+ }
+
+ private final class KeySpliterator
+ extends
+ Long2LongOpenHashDataMap.MapSpliterator<LongConsumer, Long2LongOpenHashDataMap.KeySpliterator>
+ implements LongSpliterator {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ LongSpliterators.SET_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ KeySpliterator() {}
+
+ KeySpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : LongSpliterators.SET_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final java.util.function.LongConsumer action, final long index) {
+ action.accept(key.get(index));
+ }
+
+ @Override
+ final Long2LongOpenHashDataMap.KeySpliterator makeForSplit(long pos, long max,
+ boolean mustReturnNull) {
+ return new Long2LongOpenHashDataMap.KeySpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ private final class KeySet extends AbstractLongSet {
+ @Override
+ public LongIterator iterator() {
+ return new Long2LongOpenHashDataMap.KeyIterator();
+ }
+
+ @Override
+ public LongSpliterator spliterator() {
+ return new Long2LongOpenHashDataMap.KeySpliterator();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void forEach(final java.util.function.LongConsumer consumer) {
+ if (containsNullKey)
+ consumer.accept(key.get(n));
+ for (long pos = n; pos-- != 0;) {
+ final long k = key.get(pos);
+ if (!((k) == (0)))
+ consumer.accept(k);
+ }
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public boolean contains(long k) {
+ return containsKey(k);
+ }
+
+ @Override
+ public boolean remove(long k) {
+ final long oldSize = size.get();
+ Long2LongOpenHashDataMap.this.remove(k);
+ return size.get() != oldSize;
+ }
+
+ @Override
+ public void clear() {
+ Long2LongOpenHashDataMap.this.clear();
+ }
+ }
+
+ @Override
+ public LongSet keySet() {
+ if (keys == null)
+ keys = new Long2LongOpenHashDataMap.KeySet();
+ return keys;
+ }
+
+ /**
+ * An iterator on values.
+ *
+ * <p>
+ * We simply override the
+ * {@link java.util.ListIterator#next()}/{@link java.util.ListIterator#previous()} methods (and
+ * possibly their type-specific counterparts) so that they return values instead of entries.
+ */
+ private final class ValueIterator extends Long2LongOpenHashDataMap.MapIterator<LongConsumer>
+ implements LongIterator {
+ public ValueIterator() {
+ super();
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ // Despite the superclass declared with generics, the way Java inherits and generates bridge
+ // methods
+ // avoids the boxing/unboxing
+ @Override
+ final void acceptOnIndex(final java.util.function.LongConsumer action, final long index) {
+ action.accept(value.get(index));
+ }
+
+ @Override
+ public long nextLong() {
+ return value.get(nextEntry());
+ }
+ }
+
+ private final class ValueSpliterator
+ extends
+ Long2LongOpenHashDataMap.MapSpliterator<LongConsumer, Long2LongOpenHashDataMap.ValueSpliterator>
+ implements LongSpliterator {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ LongSpliterators.COLLECTION_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ ValueSpliterator() {}
+
+ ValueSpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : LongSpliterators.COLLECTION_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final java.util.function.LongConsumer action, final long index) {
+ action.accept(value.get(index));
+ }
+
+ @Override
+ final Long2LongOpenHashDataMap.ValueSpliterator makeForSplit(long pos, long max,
+ boolean mustReturnNull) {
+ return new Long2LongOpenHashDataMap.ValueSpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ @Override
+ public LongCollection values() {
+ if (values == null)
+ values = new AbstractLongCollection() {
+ @Override
+ public LongIterator iterator() {
+ return new Long2LongOpenHashDataMap.ValueIterator();
+ }
+
+ @Override
+ public LongSpliterator spliterator() {
+ return new Long2LongOpenHashDataMap.ValueSpliterator();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void forEach(final java.util.function.LongConsumer consumer) {
+ if (containsNullKey)
+ consumer.accept(value.get(n));
+ for (long pos = n; pos-- != 0;)
+ if (!((key.get(pos)) == (0)))
+ consumer.accept(value.get(pos));
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public boolean contains(long v) {
+ return containsValue(v);
+ }
+
+ @Override
+ public void clear() {
+ Long2LongOpenHashDataMap.this.clear();
+ }
+ };
+ return values;
+ }
+
+ /**
+ * Rehashes the map, making the table as small as possible.
+ *
+ * <p>
+ * This method rehashes the table to the smallest size satisfying the load factor. It can be used
+ * when the set will not be changed anymore, so to optimize access speed and size.
+ *
+ * <p>
+ * If the table size is already the minimum possible, this method does nothing.
+ *
+ * @return true if there was enough memory to trim the map.
+ */
+ public boolean trim() {
+ return trim(size.get());
+ }
+
+ /**
+ * Rehashes this map if the table is too large.
+ *
+ * <p>
+ * Let <var>N</var> be the smallest table size that can hold <code>max(n,{@link #size()})</code>
+ * entries, still satisfying the load factor. If the current table size is smaller than or equal
+ * to <var>N</var>, this method does nothing. Otherwise, it rehashes this map in a table of size
+ * <var>N</var>.
+ *
+ * <p>
+ * This method is useful when reusing maps. {@linkplain #clear() Clearing a map} leaves the table
+ * size untouched. If you are reusing a map many times, you can call this method with a typical
+ * size to avoid keeping around a very large table just because of a few large transient maps.
+ *
+ * @param n the threshold for the trimming.
+ * @return true if there was enough memory to trim the map.
+ * @see #trim()
+ */
+ public boolean trim(final long n) {
+ final int l = HashCommon.nextPowerOfTwo((int) Math.ceil(n / f));
+ if (l >= this.n || size.get() > maxFill(l, f))
+ return true;
+ try {
+ rehash(l);
+ } catch (OutOfMemoryError cantDoIt) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Rehashes the map.
+ *
+ * <p>
+ * This method implements the basic rehashing strategy, and may be overridden by subclasses
+ * implementing different rehashing strategies (e.g., disk-based rehashing). However, you should
+ * not override this method unless you understand the internal workings of this class.
+ *
+ * @param newN the new size
+ */
+ protected void rehash(final long newN) {
+ final long mask = newN - 1; // Note that this is used by the hashing macro
+ final AbstractDataList<Long> newKey = keySupplier.get();
+ final AbstractDataList<Long> newValue = valueSupplier.get();
+ long i = n, pos;
+ for (long j = realSize(); j-- != 0;) {
+ while (((key.get(--i)) == (0)));
+ if (!((newKey
+ .get(pos = (int) mix((key.get(i))) & mask)) == (0)))
+ while (!((newKey.get(pos = (pos + 1) & mask)) == (0)));
+ newKey.set(pos, key.get(i));
+ newValue.set(pos, value.get(i));
+ }
+ newValue.set(n, value.get(n));
+ this.n = newN;
+ this.mask = mask;
+ this.maxFill = maxFill(n, f);
+ this.key = newKey;
+ this.value = newValue;
+ }
+
+ /**
+ * Returns a hash code for this map.
+ * <p>
+ * This method overrides the generic method provided by the superclass. Since {@code equals()} is
+ * not overriden, it is important that the value returned by this method is the same value as the
+ * one returned by the overriden method.
+ *
+ * @return a hash code for this map.
+ */
+ @Override
+ public int hashCode() {
+ int h = 0;
+ for (long j = realSize(), i = 0, t = 0; j-- != 0;) {
+ while (((key.get(i)) == (0)))
+ i++;
+ t = HashCommon.long2int(key.get(i));
+ t ^= HashCommon.long2int(value.get(i));
+ h += t;
+ i++;
+ }
+ // Zero / null keys have hash zero.
+ if (containsNullKey)
+ h += HashCommon.long2int(value.get(n));
+ return h;
+ }
+}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongPackedOpenHashDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongPackedOpenHashDataMap.java
new file mode 100644
index 00000000..cda78863
--- /dev/null
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2LongPackedOpenHashDataMap.java
@@ -0,0 +1,1502 @@
+/*
+ * Licensed 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.baremaps.database.collection;
+
+import static it.unimi.dsi.fastutil.HashCommon.bigArraySize;
+import static it.unimi.dsi.fastutil.HashCommon.maxFill;
+
+import it.unimi.dsi.fastutil.Hash;
+import it.unimi.dsi.fastutil.HashCommon;
+import it.unimi.dsi.fastutil.longs.*;
+import it.unimi.dsi.fastutil.objects.*;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+import java.util.function.LongConsumer;
+import java.util.function.Supplier;
+import org.apache.baremaps.database.type.PairDataType.Pair;
+
+public class Long2LongPackedOpenHashDataMap extends AbstractLong2LongMap
+ implements DataMap<Long>, Hash {
+
+ private final Supplier<DataMap<Pair<Long, Long>>> indexSupplier;
+
+ /**
+ * The hash index.
+ */
+ protected DataMap<Pair<Long, Long>> index;
+ /**
+ * The mask for wrapping a position counter.
+ */
+ protected long mask;
+ /**
+ * Whether this map contains the key zero.
+ */
+ protected boolean containsNullKey;
+ /**
+ * The current table size.
+ */
+ protected long n;
+ /**
+ * Threshold after which we rehash. It must be the table size times {@link #f}.
+ */
+ protected long maxFill;
+ /**
+ * We never resize below this threshold, which is the construction-time {#n}.
+ */
+ protected final long minN;
+ /**
+ * Number of entries in the set (including the key zero, if present).
+ */
+ protected AtomicLong size = new AtomicLong();
+ /**
+ * The acceptable load factor.
+ */
+ protected final float f;
+ /**
+ * Cached set of entries.
+ */
+ protected FastEntrySet entries;
+ /**
+ * Cached set of keys.
+ */
+ protected LongSet keys;
+ /**
+ * Cached collection of values.
+ */
+ protected LongCollection values;
+
+ private Supplier<AbstractDataList<Long>> listSupplier;
+
+ /**
+ * Creates a new hash map.
+ *
+ * <p>
+ * The actual table size will be the least power of two greater than {@code expected}/{@code f}.
+ *
+ * @param expected the expected number of elements in the hash map.
+ * @param f the load factor.
+ */
+ public Long2LongPackedOpenHashDataMap(final long expected, final float f,
+ final Supplier<DataMap<Pair<Long, Long>>> indexSupplier) {
+ if (f <= 0 || f >= 1)
+ throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than 1");
+ if (expected < 0)
+ throw new IllegalArgumentException("The expected number of elements must be nonnegative");
+ this.f = f;
+ this.minN = n = bigArraySize(expected, f);
+ this.mask = n - 1;
+ this.maxFill = maxFill(n, f);
+ this.indexSupplier = indexSupplier;
+ this.index = indexSupplier.get();
+ }
+
+ private long realSize() {
+ return containsNullKey ? size.get() - 1 : size.get();
+ }
+
+ private void ensureCapacity(final long capacity) {
+ final long needed = bigArraySize(capacity, f);
+ if (needed > n)
+ rehash(needed);
+ }
+
+ private void tryCapacity(final long capacity) {
+ final long needed =
+ Math.min(1 << 30, Math.max(2, HashCommon.nextPowerOfTwo((long) Math.ceil(capacity / f))));
+ if (needed > n)
+ rehash(needed);
+ }
+
+ private long removeEntry(final long pos) {
+ final long oldValue = index.get(pos).right();
+ size.decrementAndGet();
+ shiftKeys(pos);
+ if (n > minN && size.get() < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
+ rehash(n / 2);
+ return oldValue;
+ }
+
+ private long removeNullEntry() {
+ containsNullKey = false;
+ final long oldValue = index.get(n).right();
+ size.decrementAndGet();
+ if (n > minN && size.get() < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
+ rehash(n / 2);
+ return oldValue;
+ }
+
+ @Override
+ public void putAll(Map<? extends Long, ? extends Long> m) {
+ if (f <= .5)
+ ensureCapacity(m.size()); // The resulting map will be sized for m.size() elements
+ else
+ tryCapacity(size() + m.size()); // The resulting map will be tentatively sized for size() +
+ // m.size()
+ // elements
+ super.putAll(m);
+ }
+
+ private long find(final long k) {
+ if (((k) == (0)))
+ return containsNullKey ? n : -(n + 1);
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = HashCommon.mix((k)) & mask).left()) == (0)))
+ return -(pos + 1);
+ if (((k) == (curr)))
+ return pos;
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return -(pos + 1);
+ if (((k) == (curr)))
+ return pos;
+ }
+ }
+
+ private void insert(final long pos, final long k, final long v) {
+ if (pos == n)
+ containsNullKey = true;
+ index.put(pos, new Pair<>(k, v));
+ if (size.getAndIncrement() >= maxFill)
+ rehash(bigArraySize(size.get() + 1, f));
+ }
+
+ @Override
+ public long put(final long k, final long v) {
+ final long pos = find(k);
+ if (pos < 0) {
+ insert(-pos - 1, k, v);
+ return defRetValue;
+ }
+ final long oldValue = index.get(pos).right();
+ index.put(pos, new Pair<>(pos, v));
+ return oldValue;
+ }
+
+ private long addToValue(final long pos, final long incr) {
+ final long oldValue = index.get(pos).right();
+ index.put(pos, new Pair<>(pos, oldValue + incr));
+ return oldValue;
+ }
+
+ /**
+ * Adds an increment to value currently associated with a key.
+ *
+ * <p>
+ * Note that this method respects the {@linkplain #defaultReturnValue() default return value}
+ * semantics: when called with a key that does not currently appears in the map, the key will be
+ * associated with the default return value plus the given increment.
+ *
+ * @param k the key.
+ * @param incr the increment.
+ * @return the old value, or the {@linkplain #defaultReturnValue() default return value} if no
+ * value was present for the given key.
+ */
+ public long addTo(final long k, final long incr) {
+ long pos;
+ if (((k) == (0))) {
+ if (containsNullKey)
+ return addToValue(n, incr);
+ pos = n;
+ containsNullKey = true;
+ } else {
+ long curr;
+ // The starting point.
+ if (!((curr = index.get(pos = HashCommon.mix((k)) & mask).left()) == (0))) {
+ if (((curr) == (k)))
+ return addToValue(pos, incr);
+ while (!((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ if (((curr) == (k)))
+ return addToValue(pos, incr);
+ }
+ }
+ index.put(pos, new Pair<>(k, defRetValue + incr));
+ if (size.incrementAndGet() >= maxFill)
+ rehash(bigArraySize(size.get() + 1, f));
+ return defRetValue;
+ }
+
+ /**
+ * Shifts left entries with the specified hash code, starting at the specified position, and
+ * empties the resulting free entry.
+ *
+ * @param pos a starting position.
+ */
+ protected final void shiftKeys(long pos) {
+ // Shift entries with the same hash.
+ long last, slot;
+ long curr;
+ for (;;) {
+ pos = ((last = pos) + 1) & mask;
+ for (;;) {
+ if (((curr = index.get(pos).left()) == (0))) {
+ index.put(last, new Pair<>(curr, 0L));
+ return;
+ }
+ slot = HashCommon.mix((curr)) & mask;
+ if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
+ break;
+ pos = (pos + 1) & mask;
+ }
+ index.put(last, new Pair<>(curr, index.get(pos).right()));
+ }
+ }
+
+ @Override
+
+ public long remove(final long k) {
+ if (((k) == (0))) {
+ if (containsNullKey)
+ return removeNullEntry();
+ return defRetValue;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = HashCommon.mix((k)) & mask).left()) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return removeEntry(pos);
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return removeEntry(pos);
+ }
+ }
+
+ @Override
+
+ public long get(final long k) {
+ if (((k) == (0)))
+ return containsNullKey ? index.get(n).right() : defRetValue;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = HashCommon.mix((k)) & mask).left()) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return index.get(pos).right();
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return index.get(pos).right();
+ }
+ }
+
+ @Override
+
+ public boolean containsKey(final long k) {
+ if (((k) == (0)))
+ return containsNullKey;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = HashCommon.mix((k)) & mask).left()) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return true;
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return true;
+ }
+ }
+
+ @Override
+ public boolean containsValue(final long v) {
+ if (containsNullKey && ((index.get(n).right()) == (v)))
+ return true;
+ for (long i = n; i-- != 0;)
+ if (!((index.get(i).left()) == (0)) && ((index.get(i).right()) == (v)))
+ return true;
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+
+ public long getOrDefault(final long k, final long defaultValue) {
+ if (((k) == (0)))
+ return containsNullKey ? index.get(n).right() : defaultValue;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = HashCommon.mix((k)) & mask).left()) == (0)))
+ return defaultValue;
+ if (((k) == (curr)))
+ return index.get(pos).right();
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return defaultValue;
+ if (((k) == (curr)))
+ return index.get(pos).right();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long putIfAbsent(final long k, final long v) {
+ final long pos = find(k);
+ if (pos >= 0)
+ return index.get(pos).right();
+ insert(-pos - 1, k, v);
+ return defRetValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+
+ public boolean remove(final long k, final long v) {
+ if (((k) == (0))) {
+ if (containsNullKey && ((v) == (index.get(n).right()))) {
+ removeNullEntry();
+ return true;
+ }
+ return false;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = HashCommon.mix((k)) & mask).left()) == (0)))
+ return false;
+ if (((k) == (curr)) && ((v) == (index.get(pos).right()))) {
+ removeEntry(pos);
+ return true;
+ }
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return false;
+ if (((k) == (curr)) && ((v) == (index.get(pos).right()))) {
+ removeEntry(pos);
+ return true;
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean replace(final long k, final long oldValue, final long v) {
+ final long pos = find(k);
+ if (pos < 0 || !((oldValue) == (index.get(pos).right())))
+ return false;
+ index.put(pos, new Pair<>(k, v));
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long replace(final long k, final long v) {
+ final long pos = find(k);
+ if (pos < 0)
+ return defRetValue;
+ final long oldValue = index.get(pos).right();
+ index.put(pos, new Pair<>(k, v));
+ return oldValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfAbsent(final long k,
+ final java.util.function.LongUnaryOperator mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(k);
+ if (pos >= 0)
+ return index.get(pos).right();
+ final long newValue = mappingFunction.applyAsLong(k);
+ insert(-pos - 1, k, newValue);
+ return newValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfAbsent(final long key, final Long2LongFunction mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(key);
+ if (pos >= 0)
+ return index.get(pos).right();
+ if (!mappingFunction.containsKey(key))
+ return defRetValue;
+ final long newValue = mappingFunction.get(key);
+ insert(-pos - 1, key, newValue);
+ return newValue;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfAbsentNullable(final long k,
+ final java.util.function.LongFunction<? extends Long> mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(k);
+ if (pos >= 0)
+ return index.get(pos).right();
+ final Long newValue = mappingFunction.apply(k);
+ if (newValue == null)
+ return defRetValue;
+ final long v = (newValue).longValue();
+ insert(-pos - 1, k, v);
+ return v;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long computeIfPresent(final long k,
+ final java.util.function.BiFunction<? super Long, ? super Long, ? extends Long> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+ final long pos = find(k);
+ if (pos < 0)
+ return defRetValue;
+ final Long newValue =
+ remappingFunction.apply(Long.valueOf(k), Long.valueOf(index.get(pos).right()));
+ if (newValue == null) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ return defRetValue;
+ }
+ long newVal = (newValue).longValue();
+ index.put(pos, new Pair<>(k, newVal));
+ return newVal;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long compute(final long k,
+ final java.util.function.BiFunction<? super Long, ? super Long, ? extends Long> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+ final long pos = find(k);
+ final Long newValue =
+ remappingFunction.apply(Long.valueOf(k),
+ pos >= 0 ? Long.valueOf(index.get(pos).right()) : null);
+ if (newValue == null) {
+ if (pos >= 0) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ }
+ return defRetValue;
+ }
+ long newVal = (newValue).longValue();
+ if (pos < 0) {
+ insert(-pos - 1, k, newVal);
+ return newVal;
+ }
+ index.put(pos, new Pair<>(k, newVal));
+ return newVal;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long merge(final long k, final long v,
+ final java.util.function.BiFunction<? super Long, ? super Long, ? extends Long> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+
+ final long pos = find(k);
+ if (pos < 0) {
+ if (pos < 0)
+ insert(-pos - 1, k, v);
+ else
+ index.put(pos, new Pair<>(k, v));
+ return v;
+ }
+ final Long newValue =
+ remappingFunction.apply(Long.valueOf(index.get(pos).right()), Long.valueOf(v));
+ if (newValue == null) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ return defRetValue;
+ }
+ long newVal = (newValue).longValue();
+ index.put(pos, new Pair<>(k, newVal));
+ return newVal;
+ }
+
+ /*
+ * Removes all elements from this map.
+ *
+ * <p>To increase object reuse, this method does not change the table size. If you want to reduce
+ * the table size, you must use {@link #trim()}.
+ *
+ */
+ @Override
+ public void clear() {
+ if (size.get() == 0)
+ return;
+ size.set(0);
+ containsNullKey = false;
+ // TODO: Arrays.fill(key, (0));
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return size.get() == 0;
+ }
+
+ @Override
+ public long sizeAsLong() {
+ return size.get();
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ /**
+ * The entry class for a hash map does not record key and value, but rather the position in the
+ * hash table of the corresponding entry. This is necessary so that calls to
+ * {@link Map.Entry#setValue(Object)} are reflected in the map
+ */
+ final class MapEntry implements Long2LongMap.Entry, Map.Entry<Long, Long>, LongLongPair {
+ // The table index this entry refers to, or -1 if this entry has been deleted.
+ long index;
+
+ MapEntry(final long index) {
+ this.index = index;
+ }
+
+ MapEntry() {}
+
+ @Override
+ public long getLongKey() {
+ return Long2LongPackedOpenHashDataMap.this.index.get(index).left();
+ }
+
+ @Override
+ public long leftLong() {
+ return Long2LongPackedOpenHashDataMap.this.index.get(index).left();
+ }
+
+ @Override
+ public long getLongValue() {
+ return Long2LongPackedOpenHashDataMap.this.index.get(index).right();
+ }
+
+ @Override
+ public long rightLong() {
+ return Long2LongPackedOpenHashDataMap.this.index.get(index).right();
+ }
+
+ @Override
+ public long setValue(final long v) {
+ final long oldValue = Long2LongPackedOpenHashDataMap.this.index.get(index).right();
+ Long2LongPackedOpenHashDataMap.this.index.put(index,
+ new Pair<>(Long2LongPackedOpenHashDataMap.this.index.get(index).left(), v));
+ return oldValue;
+ }
+
+ @Override
+ public LongLongPair right(final long v) {
+ Long2LongPackedOpenHashDataMap.this.index.put(index,
+ new Pair<>(Long2LongPackedOpenHashDataMap.this.index.get(index).left(), v));
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Please use the corresponding type-specific method instead.
+ */
+ @Deprecated
+ @Override
+ public Long getKey() {
+ return Long.valueOf(Long2LongPackedOpenHashDataMap.this.index.get(index).left());
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Please use the corresponding type-specific method instead.
+ */
+ @Deprecated
+ @Override
+ public Long getValue() {
+ return Long.valueOf(Long2LongPackedOpenHashDataMap.this.index.get(index).right());
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Please use the corresponding type-specific method instead.
+ */
+ @Deprecated
+ @Override
+ public Long setValue(final Long v) {
+ return Long.valueOf(setValue((v).longValue()));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ Map.Entry<Long, Long> e = (Map.Entry<Long, Long>) o;
+ var pair = Long2LongPackedOpenHashDataMap.this.index.get(index);
+ return ((pair.left()) == ((e.getKey()).longValue()))
+ && ((pair.right()) == ((e.getValue()).longValue()));
+ }
+
+ @Override
+ public int hashCode() {
+ var pair = Long2LongPackedOpenHashDataMap.this.index.get(index);
+ return HashCommon.long2int(pair.left()) ^ HashCommon.long2int(pair.right());
+ }
+
+ @Override
+ public String toString() {
+ var pair = Long2LongPackedOpenHashDataMap.this.index.get(index);
+ return pair.left() + "=>" + pair.right();
+ }
+ }
+
+ /**
+ * An iterator over a hash map.
+ */
+ private abstract class MapIterator<ConsumerType> {
+ /**
+ * The index of the last entry returned, if positive or zero; initially, {@link #n}. If
+ * negative, the last entry returned was that of the key of index {@code - pos - 1} from the
+ * {@link #wrapped} list.
+ */
+ long pos = n;
+ /**
+ * The index of the last entry that has been returned (more precisely, the value of {@link #pos}
+ * if {@link #pos} is positive, or {@link Integer#MIN_VALUE} if {@link #pos} is negative). It is
+ * -1 if either we did not return an entry yet, or the last returned entry has been removed.
+ */
+ long last = -1;
+ /**
+ * A downward counter measuring how many entries must still be returned.
+ */
+ long c = size.get();
+ /**
+ * A boolean telling us whether we should return the entry with the null key.
+ */
+ boolean mustReturnNullKey = Long2LongPackedOpenHashDataMap.this.containsNullKey;
+ /**
+ * A lazily allocated list containing keys of entries that have wrapped around the table because
+ * of removals.
+ */
+ LongArrayList wrapped;
+
+ @SuppressWarnings("unused")
+ abstract void acceptOnIndex(final ConsumerType action, final long index);
+
+ public boolean hasNext() {
+ return c != 0;
+ }
+
+ public long nextEntry() {
+ if (!hasNext())
+ throw new NoSuchElementException();
+ c--;
+ if (mustReturnNullKey) {
+ mustReturnNullKey = false;
+ return last = n;
+ }
+ for (;;) {
+ if (--pos < 0) {
+ // We are just enumerating elements from the wrapped list.
+ last = Integer.MIN_VALUE;
+ final long k = wrapped.getLong((int) -pos - 1); // TODO: check if -pos - 1 is correct
+ long p = (int) HashCommon.mix((k)) & mask;
+ while (!((k) == (index.get(p).left())))
+ p = (p + 1) & mask;
+ return p;
+ }
+ if (!((index.get(pos).left()) == (0)))
+ return last = pos;
+ }
+ }
+
+ public void forEachRemaining(final ConsumerType action) {
+ if (mustReturnNullKey) {
+ mustReturnNullKey = false;
+ acceptOnIndex(action, last = n);
+ c--;
+ }
+ while (c != 0) {
+ if (--pos < 0) {
+ // We are just enumerating elements from the wrapped list.
+ last = Integer.MIN_VALUE;
+ final long k = wrapped.getLong((int) -pos - 1); // TODO: check if -pos - 1 is correct
+ long p = (int) HashCommon.mix((k)) & mask;
+ while (!((k) == (index.get(p).left())))
+ p = (p + 1) & mask;
+ acceptOnIndex(action, p);
+ c--;
+ } else if (!((index.get(pos).left()) == (0))) {
+ acceptOnIndex(action, last = pos);
+ c--;
+ }
+ }
+ }
+
+ /**
+ * Shifts left entries with the specified hash code, starting at the specified position, and
+ * empties the resulting free entry.
+ *
+ * @param pos a starting position.
+ */
+ private void shiftKeys(long pos) {
+ // Shift entries with the same hash.
+ long last, slot;
+ long curr;
+ for (;;) {
+ pos = ((last = pos) + 1) & mask;
+ for (;;) {
+ if (((curr = index.get(pos).left()) == (0))) {
+ index.put(last, new Pair<>(curr, 0L));
+ return;
+ }
+ slot = (int) HashCommon.mix((curr)) & mask;
+ if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
+ break;
+ pos = (pos + 1) & mask;
+ }
+ var pair = index.get(pos);
+ if (pos < last) { // Wrapped entry.
+ if (wrapped == null)
+ wrapped = new LongArrayList(2);
+ wrapped.add(pair.left());
+ }
+ index.put(last, new Pair<>(curr, pair.right()));
+ }
+ }
+
+ public void remove() {
+ if (last == -1)
+ throw new IllegalStateException();
+ if (last == n) {
+ containsNullKey = false;
+ } else if (pos >= 0)
+ shiftKeys(last);
+ else {
+ // We're removing wrapped entries.
+ Long2LongPackedOpenHashDataMap.this.remove(wrapped.getLong((int) -pos - 1)); // TODO: check
+ // if -pos - 1
+ // is correct
+ last = -1; // Note that we must not decrement size
+ return;
+ }
+ size.decrementAndGet();
+ last = -1; // You can no longer remove this entry.
+ }
+
+ public int skip(final int n) {
+ int i = n;
+ while (i-- != 0 && hasNext())
+ nextEntry();
+ return n - i - 1;
+ }
+ }
+
+ private final class EntryIterator
+ extends Long2LongPackedOpenHashDataMap.MapIterator<Consumer<? super Long2LongMap.Entry>>
+ implements ObjectIterator<Long2LongMap.Entry> {
+ private Long2LongPackedOpenHashDataMap.MapEntry entry;
+
+ @Override
+ public Long2LongPackedOpenHashDataMap.MapEntry next() {
+ return entry = new Long2LongPackedOpenHashDataMap.MapEntry(nextEntry());
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2LongMap.Entry> action, final long index) {
+ action.accept(entry = new Long2LongPackedOpenHashDataMap.MapEntry(index));
+ }
+
+ @Override
+ public void remove() {
+ super.remove();
+ entry.index = -1; // You cannot use a deleted entry.
+ }
+ }
+
+ private final class FastEntryIterator
+ extends Long2LongPackedOpenHashDataMap.MapIterator<Consumer<? super Long2LongMap.Entry>>
+ implements ObjectIterator<Long2LongMap.Entry> {
+ private final Long2LongPackedOpenHashDataMap.MapEntry entry =
+ new Long2LongPackedOpenHashDataMap.MapEntry();
+
+ @Override
+ public Long2LongPackedOpenHashDataMap.MapEntry next() {
+ entry.index = nextEntry();
+ return entry;
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2LongMap.Entry> action, final long index) {
+ entry.index = index;
+ action.accept(entry);
+ }
+ }
+
+ private abstract class MapSpliterator<ConsumerType, SplitType extends Long2LongPackedOpenHashDataMap.MapSpliterator<ConsumerType, SplitType>> {
+ /**
+ * The index (which bucket) of the next item to give to the action. Unlike {@code SetIterator},
+ * this counts up instead of down.
+ */
+ long pos = 0;
+ /**
+ * The maximum bucket (exclusive) to iterate to
+ */
+ long max = n;
+ /**
+ * An upwards counter counting how many we have given
+ */
+ long c = 0;
+ /**
+ * A boolean telling us whether we should return the null key.
+ */
+ boolean mustReturnNull = Long2LongPackedOpenHashDataMap.this.containsNullKey;
+ boolean hasSplit = false;
+
+ MapSpliterator() {}
+
+ MapSpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ this.pos = pos;
+ this.max = max;
+ this.mustReturnNull = mustReturnNull;
+ this.hasSplit = hasSplit;
+ }
+
+ abstract void acceptOnIndex(final ConsumerType action, final long index);
+
+ abstract SplitType makeForSplit(long pos, long max, boolean mustReturnNull);
+
+ public boolean tryAdvance(final ConsumerType action) {
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++c;
+ acceptOnIndex(action, n);
+ return true;
+ }
+ while (pos < max) {
+ if (!((index.get(pos).left()) == (0))) {
+ ++c;
+ acceptOnIndex(action, pos++);
+ return true;
+ }
+ ++pos;
+ }
+ return false;
+ }
+
+ public void forEachRemaining(final ConsumerType action) {
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++c;
+ acceptOnIndex(action, n);
+ }
+ while (pos < max) {
+ if (!((index.get(pos).left()) == (0))) {
+ acceptOnIndex(action, pos);
+ ++c;
+ }
+ ++pos;
+ }
+ }
+
+ public long estimateSize() {
+ if (!hasSplit) {
+ // Root spliterator; we know how many are remaining.
+ return size.get() - c;
+ } else {
+ // After we split, we can no longer know exactly how many we have (or at least not
+ // efficiently).
+ // (size / n) * (max - pos) aka currentTableDensity * numberOfBucketsLeft seems like a good
+ // estimate.
+ return Math.min(size.get() - c,
+ (long) (((double) realSize() / n) * (max - pos)) + (mustReturnNull ? 1 : 0));
+ }
+ }
+
+ public SplitType trySplit() {
+ if (pos >= max - 1)
+ return null;
+ long retLen = (max - pos) >> 1;
+ if (retLen <= 1)
+ return null;
+ long myNewPos = pos + retLen;
+ long retPos = pos;
+ long retMax = myNewPos;
+ // Since null is returned first, and the convention is that the returned split is the prefix
+ // of
+ // elements,
+ // the split will take care of returning null (if needed), and we won't return it anymore.
+ SplitType split = makeForSplit(retPos, retMax, mustReturnNull);
+ this.pos = myNewPos;
+ this.mustReturnNull = false;
+ this.hasSplit = true;
+ return split;
+ }
+
+ public long skip(long n) {
+ if (n < 0)
+ throw new IllegalArgumentException("Argument must be nonnegative: " + n);
+ if (n == 0)
+ return 0;
+ long skipped = 0;
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++skipped;
+ --n;
+ }
+ while (pos < max && n > 0) {
+ if (!((index.get(pos++).left()) == (0))) {
+ ++skipped;
+ --n;
+ }
+ }
+ return skipped;
+ }
+ }
+
+ private final class EntrySpliterator extends
+ Long2LongPackedOpenHashDataMap.MapSpliterator<Consumer<? super Long2LongMap.Entry>, Long2LongPackedOpenHashDataMap.EntrySpliterator>
+ implements ObjectSpliterator<Long2LongMap.Entry> {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ ObjectSpliterators.SET_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ EntrySpliterator() {}
+
+ EntrySpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : ObjectSpliterators.SET_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2LongMap.Entry> action, final long index) {
+ action.accept(new Long2LongPackedOpenHashDataMap.MapEntry(index));
+ }
+
+ @Override
+ final Long2LongPackedOpenHashDataMap.EntrySpliterator makeForSplit(long pos, long max,
+ boolean mustReturnNull) {
+ return new Long2LongPackedOpenHashDataMap.EntrySpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ private final class MapEntrySet extends AbstractObjectSet<Long2LongMap.Entry>
+ implements FastEntrySet {
+ @Override
+ public ObjectIterator<Long2LongMap.Entry> iterator() {
+ return new Long2LongPackedOpenHashDataMap.EntryIterator();
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public ObjectIterator<Long2LongMap.Entry> fastIterator() {
+ return new Long2LongPackedOpenHashDataMap.FastEntryIterator();
+ }
+
+ @Override
+ public ObjectSpliterator<Long2LongMap.Entry> spliterator() {
+ return new Long2LongPackedOpenHashDataMap.EntrySpliterator();
+ }
+
+ //
+ @Override
+
+ public boolean contains(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
+ if (e.getKey() == null || !(e.getKey() instanceof Long))
+ return false;
+ if (e.getValue() == null || !(e.getValue() instanceof Long))
+ return false;
+ final long k = ((Long) (e.getKey())).longValue();
+ final long v = ((Long) (e.getValue())).longValue();
+ if (((k) == (0)))
+ return Long2LongPackedOpenHashDataMap.this.containsNullKey
+ && ((index.get(n).right()) == (v));
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = (int) HashCommon.mix((k)) & mask).left()) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return ((index.get(pos).right()) == (v));
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return ((index.get(pos).right()) == (v));
+ }
+ }
+
+ @Override
+
+ public boolean remove(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
+ if (e.getKey() == null || !(e.getKey() instanceof Long))
+ return false;
+ if (e.getValue() == null || !(e.getValue() instanceof Long))
+ return false;
+ final long k = ((Long) (e.getKey())).longValue();
+ final long v = ((Long) (e.getValue())).longValue();
+ if (((k) == (0))) {
+ if (containsNullKey && ((index.get(n).right()) == (v))) {
+ removeNullEntry();
+ return true;
+ }
+ return false;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = index.get(pos = (int) HashCommon.mix((k)) & mask).left()) == (0)))
+ return false;
+ if (((curr) == (k))) {
+ if (((index.get(pos).right()) == (v))) {
+ removeEntry(pos);
+ return true;
+ }
+ return false;
+ }
+ while (true) {
+ if (((curr = index.get(pos = (pos + 1) & mask).left()) == (0)))
+ return false;
+ if (((curr) == (k))) {
+ if (((index.get(pos).right()) == (v))) {
+ removeEntry(pos);
+ return true;
+ }
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ Long2LongPackedOpenHashDataMap.this.clear();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void forEach(final Consumer<? super Long2LongMap.Entry> consumer) {
+ if (containsNullKey) {
+ var pair = index.get(n);
+ consumer.accept(new BasicEntry(pair.left(), pair.right()));
+ }
+ for (long pos = n; pos-- != 0;) {
+ var pair = index.get(pos);
+ if (!((pair.left()) == (0)))
+ consumer.accept(new BasicEntry(pair.left(), pair.right()));
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void fastForEach(final Consumer<? super Long2LongMap.Entry> consumer) {
+ if (containsNullKey) {
+ var pair = index.get(n);
+ consumer.accept(new BasicEntry(pair.left(), pair.right()));
+ }
+ for (long pos = n; pos-- != 0;) {
+ var pair = index.get(pos);
+ if (!((pair.left()) == (0))) {
+ consumer.accept(new BasicEntry(pair.left(), pair.right()));
+ }
+ }
+ }
+ }
+
+ @Override
+ public FastEntrySet long2LongEntrySet() {
+ if (entries == null) {
+ entries = new Long2LongPackedOpenHashDataMap.MapEntrySet();
+ }
+ return entries;
+ }
+
+ /**
+ * An iterator on keys.
+ *
+ * <p>
+ * We simply override the
+ * {@link java.util.ListIterator#next()}/{@link java.util.ListIterator#previous()} methods (and
+ * possibly their type-specific counterparts) so that they return keys instead of entries.
+ */
+ private final class KeyIterator extends Long2LongPackedOpenHashDataMap.MapIterator<LongConsumer>
+ implements LongIterator {
+ public KeyIterator() {
+ super();
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ // Despite the superclass declared with generics, the way Java inherits and generates bridge
+ // methods
+ // avoids the boxing/unboxing
+ @Override
+ final void acceptOnIndex(final LongConsumer action, final long index) {
+ action.accept(Long2LongPackedOpenHashDataMap.this.index.get(index).left());
+ }
+
+ @Override
+ public long nextLong() {
+ return index.get(nextEntry()).left();
+ }
+ }
+
+ private final class KeySpliterator
+ extends
+ Long2LongPackedOpenHashDataMap.MapSpliterator<LongConsumer, Long2LongPackedOpenHashDataMap.KeySpliterator>
+ implements LongSpliterator {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ LongSpliterators.SET_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ KeySpliterator() {}
+
+ KeySpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : LongSpliterators.SET_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final LongConsumer action, final long index) {
+ action.accept(Long2LongPackedOpenHashDataMap.this.index.get(index).left());
+ }
+
+ @Override
+ final Long2LongPackedOpenHashDataMap.KeySpliterator makeForSplit(long pos, long max,
+ boolean mustReturnNull) {
+ return new Long2LongPackedOpenHashDataMap.KeySpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ private final class KeySet extends AbstractLongSet {
+ @Override
+ public LongIterator iterator() {
+ return new Long2LongPackedOpenHashDataMap.KeyIterator();
+ }
+
+ @Override
+ public LongSpliterator spliterator() {
+ return new Long2LongPackedOpenHashDataMap.KeySpliterator();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void forEach(final LongConsumer consumer) {
+ if (containsNullKey)
+ consumer.accept(index.get(n).left());
+ for (long pos = n; pos-- != 0;) {
+ final long k = index.get(pos).left();
+ if (!((k) == (0)))
+ consumer.accept(k);
+ }
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public boolean contains(long k) {
+ return containsKey(k);
+ }
+
+ @Override
+ public boolean remove(long k) {
+ final long oldSize = size.get();
+ Long2LongPackedOpenHashDataMap.this.remove(k);
+ return size.get() != oldSize;
+ }
+
+ @Override
+ public void clear() {
+ Long2LongPackedOpenHashDataMap.this.clear();
+ }
+ }
+
+ @Override
+ public LongSet keySet() {
+ if (keys == null) {
+ keys = new Long2LongPackedOpenHashDataMap.KeySet();
+ }
+ return keys;
+ }
+
+ /**
+ * An iterator on values.
+ *
+ * <p>
+ * We simply override the
+ * {@link java.util.ListIterator#next()}/{@link java.util.ListIterator#previous()} methods (and
+ * possibly their type-specific counterparts) so that they return values instead of entries.
+ */
+ private final class ValueIterator extends Long2LongPackedOpenHashDataMap.MapIterator<LongConsumer>
+ implements LongIterator {
+ public ValueIterator() {
+ super();
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ // Despite the superclass declared with generics, the way Java inherits and generates bridge
+ // methods
+ // avoids the boxing/unboxing
+ @Override
+ final void acceptOnIndex(final LongConsumer action, final long index) {
+ action.accept(Long2LongPackedOpenHashDataMap.this.index.get(index).right());
+ }
+
+ @Override
+ public long nextLong() {
+ return index.get(nextEntry()).right();
+ }
+ }
+
+ private final class ValueSpliterator
+ extends
+ Long2LongPackedOpenHashDataMap.MapSpliterator<LongConsumer, Long2LongPackedOpenHashDataMap.ValueSpliterator>
+ implements LongSpliterator {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ LongSpliterators.COLLECTION_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ ValueSpliterator() {}
+
+ ValueSpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : LongSpliterators.COLLECTION_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final java.util.function.LongConsumer action, final long index) {
+ action.accept(Long2LongPackedOpenHashDataMap.this.index.get(index).right());
+ }
+
+ @Override
+ final Long2LongPackedOpenHashDataMap.ValueSpliterator makeForSplit(long pos, long max,
+ boolean mustReturnNull) {
+ return new Long2LongPackedOpenHashDataMap.ValueSpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ @Override
+ public LongCollection values() {
+ if (values == null)
+ values = new AbstractLongCollection() {
+ @Override
+ public LongIterator iterator() {
+ return new Long2LongPackedOpenHashDataMap.ValueIterator();
+ }
+
+ @Override
+ public LongSpliterator spliterator() {
+ return new Long2LongPackedOpenHashDataMap.ValueSpliterator();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void forEach(final java.util.function.LongConsumer consumer) {
+ if (containsNullKey) {
+ consumer.accept(index.get(n).right());
+ }
+ for (long pos = n; pos-- != 0;) {
+ var pair = index.get(pos);
+ if (!((pair.left()) == (0))) {
+ consumer.accept(pair.right());
+ }
+ }
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public boolean contains(long v) {
+ return containsValue(v);
+ }
+
+ @Override
+ public void clear() {
+ Long2LongPackedOpenHashDataMap.this.clear();
+ }
+ };
+ return values;
+ }
+
+ /**
+ * Rehashes the map, making the table as small as possible.
+ *
+ * <p>
+ * This method rehashes the table to the smallest size satisfying the load factor. It can be used
+ * when the set will not be changed anymore, so to optimize access speed and size.
+ *
+ * <p>
+ * If the table size is already the minimum possible, this method does nothing.
+ *
+ * @return true if there was enough memory to trim the map.
+ */
+ public boolean trim() {
+ return trim(size.get());
+ }
+
+ /**
+ * Rehashes this map if the table is too large.
+ *
+ * <p>
+ * Let <var>N</var> be the smallest table size that can hold <code>max(n,{@link #size()})</code>
+ * entries, still satisfying the load factor. If the current table size is smaller than or equal
+ * to <var>N</var>, this method does nothing. Otherwise, it rehashes this map in a table of size
+ * <var>N</var>.
+ *
+ * <p>
+ * This method is useful when reusing maps. {@linkplain #clear() Clearing a map} leaves the table
+ * size untouched. If you are reusing a map many times, you can call this method with a typical
+ * size to avoid keeping around a very large table just because of a few large transient maps.
+ *
+ * @param n the threshold for the trimming.
+ * @return true if there was enough memory to trim the map.
+ * @see #trim()
+ */
+ public boolean trim(final long n) {
+ final int l = HashCommon.nextPowerOfTwo((int) Math.ceil(n / f));
+ if (l >= this.n || size.get() > maxFill(l, f))
+ return true;
+ try {
+ rehash(l);
+ } catch (OutOfMemoryError cantDoIt) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Rehashes the map.
+ *
+ * <p>
+ * This method implements the basic rehashing strategy, and may be overridden by subclasses
+ * implementing different rehashing strategies (e.g., disk-based rehashing). However, you should
+ * not override this method unless you understand the internal workings of this class.
+ *
+ * @param newN the new size
+ */
+ protected void rehash(final long newN) {
+ final long mask = newN - 1; // Note that this is used by the hashing macro
+ final DataMap<Pair<Long, Long>> newIndex = indexSupplier.get();
+ long i = n, pos;
+ for (long j = realSize(); j-- != 0;) {
+ while (index.get(--i).left() == (0));
+ if (!(newIndex.get(pos = HashCommon.mix(index.get(i).left()) & mask).left() == 0L)) {
+ while (!(newIndex.get(pos = (pos + 1) & mask).left() == 0L));
+ }
+ Pair<Long, Long> pair = index.get(i);
+ newIndex.put(pos, pair);
+ }
+ Pair<Long, Long> pair = index.get(n);
+ newIndex.put(newN, new Pair<>(pair.left(), n));
+ this.n = newN;
+ this.mask = mask;
+ this.maxFill = maxFill(n, f);
+ this.index = newIndex;
+ }
+
+ /**
+ * Returns a hash code for this map.
+ * <p>
+ * This method overrides the generic method provided by the superclass. Since {@code equals()} is
+ * not overriden, it is important that the value returned by this method is the same value as the
+ * one returned by the overriden method.
+ *
+ * @return a hash code for this map.
+ */
+ @Override
+ public int hashCode() {
+ int h = 0;
+ for (long j = realSize(), i = 0, t = 0; j-- != 0;) {
+ Pair<Long, Long> pair;
+ while (((pair = index.get(i)).left() == 0)) {
+ i++;
+ }
+ t = HashCommon.long2int(pair.left());
+ t ^= HashCommon.long2int(pair.right());
+ h += t;
+ i++;
+ }
+ // Zero / null keys have hash zero.
+ if (containsNullKey)
+ h += HashCommon.long2int(index.get(n).right());
+ return h;
+ }
+}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/IndexedDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectIndexedDataMap.java
similarity index 93%
rename from baremaps-core/src/main/java/org/apache/baremaps/database/collection/IndexedDataMap.java
rename to baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectIndexedDataMap.java
index 394e569e..e5c426fd 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/IndexedDataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectIndexedDataMap.java
@@ -26,7 +26,7 @@ import java.util.Map;
*
* @param <E> The type of the elements.
*/
-public class IndexedDataMap<E> extends DataMap<E> {
+public class Long2ObjectIndexedDataMap<E> extends AbstractDataMap<E> {
private final Map<Long, Long> index;
@@ -37,7 +37,7 @@ public class IndexedDataMap<E> extends DataMap<E> {
*
* @param values the values
*/
- public IndexedDataMap(AppendOnlyBuffer<E> values) {
+ public Long2ObjectIndexedDataMap(AppendOnlyBuffer<E> values) {
this(new Long2LongOpenHashMap(), values);
}
@@ -47,7 +47,7 @@ public class IndexedDataMap<E> extends DataMap<E> {
* @param index the index
* @param values the values
*/
- public IndexedDataMap(Map<Long, Long> index, AppendOnlyBuffer<E> values) {
+ public Long2ObjectIndexedDataMap(Map<Long, Long> index, AppendOnlyBuffer<E> values) {
this.index = index;
this.values = values;
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/JaggedDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectJaggedDataMap.java
similarity index 96%
rename from baremaps-core/src/main/java/org/apache/baremaps/database/collection/JaggedDataMap.java
rename to baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectJaggedDataMap.java
index d831e9fb..d1d2c076 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/JaggedDataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectJaggedDataMap.java
@@ -20,12 +20,12 @@ import java.util.stream.IntStream;
import org.apache.baremaps.stream.StreamUtils;
/**
- * A map that stores data in a jagged array, using the 40 leas significant bits of the key to index
+ * A map that stores data in a jagged array, using the 40 least significant bits of the key to index
* the array. The capacity of the map is 2^40, which is sufficient to store 1 trillion entries.
*
* @param <E>
*/
-public class JaggedDataMap<E> extends DataMap<E> {
+public class Long2ObjectJaggedDataMap<E> extends AbstractDataMap<E> {
private static final int L_BYTES = 8;
private static final int L_SIZE = 1 << L_BYTES;
@@ -60,7 +60,7 @@ public class JaggedDataMap<E> extends DataMap<E> {
*
* @param values the values
*/
- public JaggedDataMap(AppendOnlyBuffer<E> values) {
+ public Long2ObjectJaggedDataMap(AppendOnlyBuffer<E> values) {
this.index = new long[I_SIZE][][][];
this.values = values;
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MemoryAlignedDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectMemoryAlignedDataMap.java
similarity index 96%
rename from baremaps-core/src/main/java/org/apache/baremaps/database/collection/MemoryAlignedDataMap.java
rename to baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectMemoryAlignedDataMap.java
index f21331d3..be22e632 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MemoryAlignedDataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectMemoryAlignedDataMap.java
@@ -27,7 +27,7 @@ import org.apache.baremaps.database.type.FixedSizeDataType;
* <p>
* This code has been adapted from Planetiler (Apache license).
*/
-public class MemoryAlignedDataMap<E> extends DataMap<E> {
+public class Long2ObjectMemoryAlignedDataMap<E> extends AbstractDataMap<E> {
private final FixedSizeDataType<E> dataType;
@@ -45,7 +45,7 @@ public class MemoryAlignedDataMap<E> extends DataMap<E> {
* @param dataType the data type
* @param memory the memory
*/
- public MemoryAlignedDataMap(FixedSizeDataType<E> dataType, Memory memory) {
+ public Long2ObjectMemoryAlignedDataMap(FixedSizeDataType<E> dataType, Memory memory) {
if (dataType.size() > memory.segmentSize()) {
throw new DataCollectionException("The segment size is too small for the data type");
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectMonotonicDataMap.java
similarity index 82%
rename from baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicDataMap.java
rename to baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectMonotonicDataMap.java
index 4d0e32e2..055560e0 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicDataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectMonotonicDataMap.java
@@ -31,10 +31,10 @@ import org.apache.baremaps.database.type.PairDataType.Pair;
* <p>
* Copyright (c) Planetiler.
*/
-public class MonotonicDataMap<E> extends DataMap<E> {
+public class Long2ObjectMonotonicDataMap<E> extends AbstractDataMap<E> {
- private final DataList<Long> offsets;
- private final DataList<Pair<Long, Long>> keys;
+ private final AbstractDataList<Long> offsets;
+ private final AbstractDataList<Pair<Long, Long>> keys;
private final AppendOnlyBuffer<E> values;
private long lastChunk = -1;
@@ -44,10 +44,11 @@ public class MonotonicDataMap<E> extends DataMap<E> {
*
* @param values the buffer of values
*/
- public MonotonicDataMap(AppendOnlyBuffer<E> values) {
+ public Long2ObjectMonotonicDataMap(AppendOnlyBuffer<E> values) {
this(
new MemoryAlignedDataList<>(new LongDataType()),
- new MemoryAlignedDataList<>(new PairDataType<>(new LongDataType(), new LongDataType())),
+ new MemoryAlignedDataList<>(
+ new PairDataType<>(new LongDataType(), new LongDataType())),
values);
}
@@ -57,7 +58,8 @@ public class MonotonicDataMap<E> extends DataMap<E> {
* @param keys the list of keys
* @param values the buffer of values
*/
- public MonotonicDataMap(DataList<Pair<Long, Long>> keys, AppendOnlyBuffer<E> values) {
+ public Long2ObjectMonotonicDataMap(AbstractDataList<Pair<Long, Long>> keys,
+ AppendOnlyBuffer<E> values) {
this(
new MemoryAlignedDataList<>(new LongDataType()),
keys,
@@ -71,7 +73,8 @@ public class MonotonicDataMap<E> extends DataMap<E> {
* @param keys the list of keys
* @param values the buffer of values
*/
- public MonotonicDataMap(DataList<Long> offsets, DataList<Pair<Long, Long>> keys,
+ public Long2ObjectMonotonicDataMap(AbstractDataList<Long> offsets,
+ AbstractDataList<Pair<Long, Long>> keys,
AppendOnlyBuffer<E> values) {
this.offsets = offsets;
this.keys = keys;
@@ -80,10 +83,10 @@ public class MonotonicDataMap<E> extends DataMap<E> {
/** {@inheritDoc} */
public E put(Long key, E value) {
- long index = keys.sizeAsLong();
+ long index = keys.size64();
long chunk = key >>> 8;
if (chunk != lastChunk) {
- while (offsets.sizeAsLong() <= chunk) {
+ while (offsets.size64() <= chunk) {
offsets.add(index);
}
lastChunk = chunk;
@@ -97,15 +100,15 @@ public class MonotonicDataMap<E> extends DataMap<E> {
public E get(Object keyObject) {
long key = (long) keyObject;
long chunk = key >>> 8;
- if (chunk >= offsets.sizeAsLong()) {
+ if (chunk >= offsets.size64()) {
return null;
}
long lo = offsets.get(chunk);
long hi =
Math.min(
- keys.sizeAsLong(),
- chunk >= offsets.sizeAsLong() - 1
- ? keys.sizeAsLong()
+ keys.size64(),
+ chunk >= offsets.size64() - 1
+ ? keys.size64()
: offsets.get(chunk + 1))
- 1;
while (lo <= hi) {
@@ -147,7 +150,7 @@ public class MonotonicDataMap<E> extends DataMap<E> {
/** {@inheritDoc} */
@Override
public long sizeAsLong() {
- return keys.sizeAsLong();
+ return keys.size64();
}
/** {@inheritDoc} */
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectOpenHashDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectOpenHashDataMap.java
new file mode 100644
index 00000000..b030572b
--- /dev/null
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/Long2ObjectOpenHashDataMap.java
@@ -0,0 +1,1370 @@
+/*
+ * Licensed 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.baremaps.database.collection;
+
+import static it.unimi.dsi.fastutil.HashCommon.*;
+
+import it.unimi.dsi.fastutil.Hash;
+import it.unimi.dsi.fastutil.HashCommon;
+import it.unimi.dsi.fastutil.longs.*;
+import it.unimi.dsi.fastutil.objects.AbstractObjectCollection;
+import it.unimi.dsi.fastutil.objects.AbstractObjectSet;
+import it.unimi.dsi.fastutil.objects.ObjectCollection;
+import it.unimi.dsi.fastutil.objects.ObjectIterator;
+import it.unimi.dsi.fastutil.objects.ObjectSpliterator;
+import it.unimi.dsi.fastutil.objects.ObjectSpliterators;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+/**
+ * A type-specific hash map with a fast, small-footprint implementation.
+ *
+ * <p>
+ * Instances of this class use a hash table to represent a map. The table is filled up to a
+ * specified <em>load factor</em>, and then doubled in size to accommodate new entries. If the table
+ * is emptied below <em>one fourth</em> of the load factor, it is halved in size; however, the table
+ * is never reduced to a size smaller than that at creation time: this approach makes it possible to
+ * create maps with a large capacity in which insertions and deletions do not cause immediately
+ * rehashing. Moreover, halving is not performed when deleting entries from an iterator, as it would
+ * interfere with the iteration process.
+ *
+ * <p>
+ * Note that {@link #clear()} does not modify the hash table size. Rather, a family of
+ * {@linkplain #trim() trimming methods} lets you control the size of the table; this is
+ * particularly useful if you reuse instances of this class.
+ *
+ * <p>
+ * Entries returned by the type-specific {@link #entrySet()} method implement the suitable
+ * type-specific {@link it.unimi.dsi.fastutil.Pair Pair} interface; only values are mutable.
+ *
+ * @see Hash
+ * @see HashCommon
+ */
+public class Long2ObjectOpenHashDataMap<V> extends AbstractLong2ObjectMap<V>
+ implements DataMap<V>, Hash {
+
+ private Supplier<DataMap<Long>> keySupplier;
+ private Supplier<DataMap<V>> valueSupplier;
+
+ /** The array of keys. */
+ protected transient DataMap<Long> key;
+ /** The array of values. */
+ protected transient DataMap<V> value;
+ /** The mask for wrapping a position counter. */
+ protected transient long mask;
+ /** Whether this map contains the key zero. */
+ protected transient boolean containsNullKey;
+ /** The current table size. */
+ protected transient long n;
+ /** Threshold after which we rehash. It must be the table size times {@link #f}. */
+ protected transient long maxFill;
+ /** We never resize below this threshold, which is the construction-time {#n}. */
+ protected final transient long minN;
+ /** Number of entries in the set (including the key zero, if present). */
+ protected AtomicLong size = new AtomicLong();;
+ /** The acceptable load factor. */
+ protected final float f;
+ /** Cached set of entries. */
+ protected transient FastEntrySet<V> entries;
+ /** Cached set of keys. */
+ protected transient LongSet keys;
+ /** Cached collection of values. */
+ protected transient ObjectCollection<V> values;
+
+ /**
+ * Creates a new hash map.
+ *
+ * <p>
+ * The actual table size will be the least power of two greater than {@code expected}/{@code f}.
+ *
+ * @param expected the expected number of elements in the hash map.
+ * @param f the load factor.
+ */
+ @SuppressWarnings("unchecked")
+ public Long2ObjectOpenHashDataMap(
+ final long expected,
+ final float f,
+ final Supplier<DataMap<Long>> keySupplier,
+ final Supplier<DataMap<V>> valueSupplier) {
+ if (f <= 0 || f >= 1)
+ throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than 1");
+ if (expected < 0)
+ throw new IllegalArgumentException("The expected number of elements must be nonnegative");
+ this.f = f;
+ this.minN = n = bigArraySize(expected, f);
+ this.mask = n - 1;
+ this.maxFill = maxFill(n, f);
+ this.keySupplier = keySupplier;
+ this.valueSupplier = valueSupplier;
+ this.key = keySupplier.get();
+ this.value = valueSupplier.get();
+ }
+
+ private long realSize() {
+ return containsNullKey ? size.get() - 1 : size.get();
+ }
+
+ private void ensureCapacity(final long capacity) {
+ final long needed = bigArraySize(capacity, f);
+ if (needed > n)
+ rehash(needed);
+ }
+
+ private void tryCapacity(final long capacity) {
+ final long needed = (long) Math.min(1 << 30,
+ Math.max(2, HashCommon.nextPowerOfTwo((long) Math.ceil(capacity / f))));
+ if (needed > n)
+ rehash(needed);
+ }
+
+ private V removeEntry(final long pos) {
+ final V oldValue = value.get(pos);
+ value.put(pos, null);
+ size.decrementAndGet();
+ shiftKeys(pos);
+ if (n > minN && size.get() < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
+ rehash(n / 2);
+ return oldValue;
+ }
+
+ private V removeNullEntry() {
+ containsNullKey = false;
+ final V oldValue = value.get(n);
+ value.put(n, null);
+ size.decrementAndGet();
+ if (n > minN && size.get() < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
+ rehash(n / 2);
+ return oldValue;
+ }
+
+ @Override
+ public void putAll(Map<? extends Long, ? extends V> m) {
+ if (f <= .5)
+ ensureCapacity(m.size()); // The resulting map will be sized for m.size() elements
+ else
+ tryCapacity(size() + m.size()); // The resulting map will be tentatively sized for size() +
+ // m.size()
+ // elements
+ super.putAll(m);
+ }
+
+ private long find(final long k) {
+ if (((k) == (0)))
+ return containsNullKey ? n : -(n + 1);
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return -(pos + 1);
+ if (((k) == (curr)))
+ return pos;
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return -(pos + 1);
+ if (((k) == (curr)))
+ return pos;
+ }
+ }
+
+ private void insert(final long pos, final long k, final V v) {
+ if (pos == n)
+ containsNullKey = true;
+ key.put(pos, k);
+ value.put(pos, v);
+ if (size.getAndIncrement() >= maxFill)
+ rehash(bigArraySize(size.get() + 1, f));
+ }
+
+ @Override
+ public V put(final long k, final V v) {
+ final long pos = find(k);
+ if (pos < 0) {
+ insert(-pos - 1, k, v);
+ return defRetValue;
+ }
+ final V oldValue = value.get(pos);
+ value.put(pos, v);
+ return oldValue;
+ }
+
+ /**
+ * Shifts left entries with the specified hash code, starting at the specified position, and
+ * empties the resulting free entry.
+ *
+ * @param pos a starting position.
+ */
+ protected final void shiftKeys(long pos) {
+ // Shift entries with the same hash.
+ long last, slot;
+ long curr;
+ for (;;) {
+ pos = ((last = pos) + 1) & mask;
+ for (;;) {
+ if (((curr = key.get(pos)) == (0))) {
+ key.put(last, 0L);
+ value.put(last, null);
+ return;
+ }
+ slot = (long) HashCommon.mix((curr)) & mask;
+ if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
+ break;
+ pos = (pos + 1) & mask;
+ }
+ key.put(last, curr);
+ value.put(last, value.get(pos));
+ }
+ }
+
+ @Override
+
+ public V remove(final long k) {
+ if (((k) == (0))) {
+ if (containsNullKey)
+ return removeNullEntry();
+ return defRetValue;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return removeEntry(pos);
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return removeEntry(pos);
+ }
+ }
+
+ @Override
+
+ public V get(final long k) {
+ if (((k) == (0)))
+ return containsNullKey ? value.get(n) : defRetValue;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return defRetValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ }
+ }
+
+ @Override
+
+ public boolean containsKey(final long k) {
+ if (((k) == (0)))
+ return containsNullKey;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return true;
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return true;
+ }
+ }
+
+ @Override
+ public boolean containsValue(final Object v) {
+ if (containsNullKey && java.util.Objects.equals(value.get(n), v))
+ return true;
+ for (long i = n; i-- != 0;)
+ if (!((key.get(i)) == (0)) && java.util.Objects.equals(value.get(i), v))
+ return true;
+ return false;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+
+ public V getOrDefault(final long k, final V defaultValue) {
+ if (((k) == (0)))
+ return containsNullKey ? value.get(n) : defaultValue;
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return defaultValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return defaultValue;
+ if (((k) == (curr)))
+ return value.get(pos);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public V putIfAbsent(final long k, final V v) {
+ final long pos = find(k);
+ if (pos >= 0)
+ return value.get(pos);
+ insert(-pos - 1, k, v);
+ return defRetValue;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+
+ public boolean remove(final long k, final Object v) {
+ if (((k) == (0))) {
+ if (containsNullKey && java.util.Objects.equals(v, value.get(n))) {
+ removeNullEntry();
+ return true;
+ }
+ return false;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)) && java.util.Objects.equals(v, value.get(pos))) {
+ removeEntry(pos);
+ return true;
+ }
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)) && java.util.Objects.equals(v, value.get(pos))) {
+ removeEntry(pos);
+ return true;
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public boolean replace(final long k, final V oldValue, final V v) {
+ final long pos = find(k);
+ if (pos < 0 || !java.util.Objects.equals(oldValue, value.get(pos)))
+ return false;
+ value.put(pos, v);
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public V replace(final long k, final V v) {
+ final long pos = find(k);
+ if (pos < 0)
+ return defRetValue;
+ final V oldValue = value.get(pos);
+ value.put(pos, v);
+ return oldValue;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public V computeIfAbsent(final long k,
+ final java.util.function.LongFunction<? extends V> mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(k);
+ if (pos >= 0)
+ return value.get(pos);
+ final V newValue = mappingFunction.apply(k);
+ insert(-pos - 1, k, newValue);
+ return newValue;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public V computeIfAbsent(final long key, final Long2ObjectFunction<? extends V> mappingFunction) {
+ java.util.Objects.requireNonNull(mappingFunction);
+ final long pos = find(key);
+ if (pos >= 0)
+ return value.get(pos);
+ if (!mappingFunction.containsKey(key))
+ return defRetValue;
+ final V newValue = mappingFunction.get(key);
+ insert(-pos - 1, key, newValue);
+ return newValue;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public V computeIfPresent(final long k,
+ final java.util.function.BiFunction<? super Long, ? super V, ? extends V> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+ final long pos = find(k);
+ if (pos < 0)
+ return defRetValue;
+ if (value.get(pos) == null)
+ return defRetValue;
+ final V newValue = remappingFunction.apply(Long.valueOf(k), (value.get(pos)));
+ if (newValue == null) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ return defRetValue;
+ }
+ value.put(pos, newValue);
+ return newValue;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public V compute(final long k,
+ final java.util.function.BiFunction<? super Long, ? super V, ? extends V> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+ final long pos = find(k);
+ final V newValue = remappingFunction.apply(Long.valueOf(k), pos >= 0 ? (value.get(pos)) : null);
+ if (newValue == null) {
+ if (pos >= 0) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ }
+ return defRetValue;
+ }
+ V newVal = (newValue);
+ if (pos < 0) {
+ insert(-pos - 1, k, newVal);
+ return newVal;
+ }
+ value.put(pos, newVal);
+ return newVal;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public V merge(final long k, final V v,
+ final java.util.function.BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
+ java.util.Objects.requireNonNull(remappingFunction);
+ java.util.Objects.requireNonNull(v);
+ final long pos = find(k);
+ if (pos < 0 || value.get(pos) == null) {
+ if (pos < 0)
+ insert(-pos - 1, k, v);
+ else
+ value.put(pos, v);
+ return v;
+ }
+ final V newValue = remappingFunction.apply((value.get(pos)), (v));
+ if (newValue == null) {
+ if (((k) == (0)))
+ removeNullEntry();
+ else
+ removeEntry(pos);
+ return defRetValue;
+ }
+ value.put(pos, newValue);
+ return newValue;
+ }
+
+ /*
+ * Removes all elements from this map.
+ *
+ * <p>To increase object reuse, this method does not change the table size. If you want to reduce
+ * the table size, you must use {@link #trim()}.
+ *
+ */
+ @Override
+ public void clear() {
+ if (size.get() == 0)
+ return;
+ size.set(0);
+ containsNullKey = false;
+ // TODO: Arrays.fill(key, (0));
+ // TODO: Arrays.fill(value, null);
+ }
+
+ @Override
+ public long sizeAsLong() {
+ return size.get();
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(sizeAsLong(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return sizeAsLong() == 0;
+ }
+
+ /**
+ * The entry class for a hash map does not record key and value, but rather the position in the
+ * hash table of the corresponding entry. This is necessary so that calls to
+ * {@link java.util.Map.Entry#setValue(Object)} are reflected in the map
+ */
+ final class MapEntry implements Long2ObjectMap.Entry<V>, Map.Entry<Long, V>, LongObjectPair<V> {
+ // The table index this entry refers to, or -1 if this entry has been deleted.
+ long index;
+
+ MapEntry(final long index) {
+ this.index = index;
+ }
+
+ MapEntry() {}
+
+ @Override
+ public long getLongKey() {
+ return key.get(index);
+ }
+
+ @Override
+ public long leftLong() {
+ return key.get(index);
+ }
+
+ @Override
+ public V getValue() {
+ return value.get(index);
+ }
+
+ @Override
+ public V right() {
+ return value.get(index);
+ }
+
+ @Override
+ public V setValue(final V v) {
+ final V oldValue = value.get(index);
+ value.put(index, v);
+ return oldValue;
+ }
+
+ @Override
+ public LongObjectPair<V> right(final V v) {
+ value.put(index, v);
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Please use the corresponding type-specific method instead.
+ */
+ @Deprecated
+ @Override
+ public Long getKey() {
+ return Long.valueOf(key.get(index));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ Map.Entry<Long, V> e = (Map.Entry<Long, V>) o;
+ return ((key.get(index)) == ((e.getKey()).longValue()))
+ && java.util.Objects.equals(value.get(index), (e.getValue()));
+ }
+
+ @Override
+ public int hashCode() {
+ return HashCommon.long2int(key.get(index))
+ ^ ((value.get(index)) == null ? 0 : (value.get(index)).hashCode());
+ }
+
+ @Override
+ public String toString() {
+ return key.get(index) + "=>" + value.get(index);
+ }
+ }
+
+ /** An iterator over a hash map. */
+ private abstract class MapIterator<ConsumerType> {
+ /**
+ * The index of the last entry returned, if positive or zero; initially, {@link #n}. If
+ * negative, the last entry returned was that of the key of index {@code - pos - 1} from the
+ * {@link #wrapped} list.
+ */
+ long pos = n;
+ /**
+ * The index of the last entry that has been returned (more precisely, the value of {@link #pos}
+ * if {@link #pos} is positive, or {@link Integer#MIN_VALUE} if {@link #pos} is negative). It is
+ * -1 if either we did not return an entry yet, or the last returned entry has been removed.
+ */
+ long last = -1;
+ /** A downward counter measuring how many entries must still be returned. */
+ long c = size.get();
+ /** A boolean telling us whether we should return the entry with the null key. */
+ boolean mustReturnNullKey = Long2ObjectOpenHashDataMap.this.containsNullKey;
+ /**
+ * A lazily allocated list containing keys of entries that have wrapped around the table because
+ * of removals.
+ */
+ LongArrayList wrapped;
+
+ @SuppressWarnings("unused")
+ abstract void acceptOnIndex(final ConsumerType action, final long index);
+
+ public boolean hasNext() {
+ return c != 0;
+ }
+
+ public long nextEntry() {
+ if (!hasNext())
+ throw new NoSuchElementException();
+ c--;
+ if (mustReturnNullKey) {
+ mustReturnNullKey = false;
+ return last = n;
+ }
+ for (;;) {
+ if (--pos < 0) {
+ // We are just enumerating elements from the wrapped list.
+ last = Integer.MIN_VALUE;
+ final long k = wrapped.getLong((int) -pos - 1); // TODO: check if -pos - 1 is correct
+ long p = (long) HashCommon.mix((k)) & mask;
+ while (!((k) == (key.get(p))))
+ p = (p + 1) & mask;
+ return p;
+ }
+ if (!((key.get(pos)) == (0)))
+ return last = pos;
+ }
+ }
+
+ public void forEachRemaining(final ConsumerType action) {
+ if (mustReturnNullKey) {
+ mustReturnNullKey = false;
+ acceptOnIndex(action, last = n);
+ c--;
+ }
+ while (c != 0) {
+ if (--pos < 0) {
+ // We are just enumerating elements from the wrapped list.
+ last = Integer.MIN_VALUE;
+ final long k = wrapped.getLong((int) -pos - 1); // TODO: check if -pos - 1 is correct
+ long p = (long) HashCommon.mix((k)) & mask;
+ while (!((k) == (key.get(p))))
+ p = (p + 1) & mask;
+ acceptOnIndex(action, p);
+ c--;
+ } else if (!((key.get(pos)) == (0))) {
+ acceptOnIndex(action, last = pos);
+ c--;
+ }
+ }
+ }
+
+ /**
+ * Shifts left entries with the specified hash code, starting at the specified position, and
+ * empties the resulting free entry.
+ *
+ * @param pos a starting position.
+ */
+ private void shiftKeys(long pos) {
+ // Shift entries with the same hash.
+ long last, slot;
+ long curr;
+ for (;;) {
+ pos = ((last = pos) + 1) & mask;
+ for (;;) {
+ if (((curr = key.get(pos)) == (0))) {
+ key.put(last, 0L);
+ value.put(last, null);
+ return;
+ }
+ slot = (long) HashCommon.mix((curr)) & mask;
+ if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
+ break;
+ pos = (pos + 1) & mask;
+ }
+ if (pos < last) { // Wrapped entry.
+ if (wrapped == null)
+ wrapped = new LongArrayList(2);
+ wrapped.add(key.get(pos));
+ }
+ key.put(last, curr);
+ value.put(last, value.get(pos));
+ }
+ }
+
+ public void remove() {
+ if (last == -1)
+ throw new IllegalStateException();
+ if (last == n) {
+ containsNullKey = false;
+ value.put(n, null);
+ } else if (pos >= 0)
+ shiftKeys(last);
+ else {
+ // We're removing wrapped entries.
+ Long2ObjectOpenHashDataMap.this.remove(wrapped.getLong((int) -pos - 1)); // TODO: check if
+ // -pos -
+ // 1 is correct
+ last = -1; // Note that we must not decrement size
+ return;
+ }
+ size.decrementAndGet();
+ last = -1; // You can no longer remove this entry.
+ }
+
+ public long skip(final long n) {
+ long i = n;
+ while (i-- != 0 && hasNext())
+ nextEntry();
+ return n - i - 1;
+ }
+ }
+
+ private final class EntryIterator extends MapIterator<Consumer<? super Long2ObjectMap.Entry<V>>>
+ implements ObjectIterator<Long2ObjectMap.Entry<V>> {
+ private MapEntry entry;
+
+ @Override
+ public MapEntry next() {
+ return entry = new MapEntry(nextEntry());
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2ObjectMap.Entry<V>> action,
+ final long index) {
+ action.accept(entry = new MapEntry(index));
+ }
+
+ @Override
+ public void remove() {
+ super.remove();
+ entry.index = -1; // You cannot use a deleted entry.
+ }
+ }
+
+ private final class FastEntryIterator
+ extends MapIterator<Consumer<? super Long2ObjectMap.Entry<V>>>
+ implements ObjectIterator<Long2ObjectMap.Entry<V>> {
+ private final MapEntry entry = new MapEntry();
+
+ @Override
+ public MapEntry next() {
+ entry.index = nextEntry();
+ return entry;
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2ObjectMap.Entry<V>> action,
+ final long index) {
+ entry.index = index;
+ action.accept(entry);
+ }
+ }
+
+ private abstract class MapSpliterator<ConsumerType, SplitType extends MapSpliterator<ConsumerType, SplitType>> {
+ /**
+ * The index (which bucket) of the next item to give to the action. Unlike {@code SetIterator},
+ * this counts up instead of down.
+ */
+ long pos = 0;
+ /** The maximum bucket (exclusive) to iterate to */
+ long max = n;
+ /** An upwards counter counting how many we have given */
+ long c = 0;
+ /** A boolean telling us whether we should return the null key. */
+ boolean mustReturnNull = Long2ObjectOpenHashDataMap.this.containsNullKey;
+ boolean hasSplit = false;
+
+ MapSpliterator() {}
+
+ MapSpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ this.pos = pos;
+ this.max = max;
+ this.mustReturnNull = mustReturnNull;
+ this.hasSplit = hasSplit;
+ }
+
+ abstract void acceptOnIndex(final ConsumerType action, final long index);
+
+ abstract SplitType makeForSplit(long pos, long max, boolean mustReturnNull);
+
+ public boolean tryAdvance(final ConsumerType action) {
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++c;
+ acceptOnIndex(action, n);
+ return true;
+ }
+ while (pos < max) {
+ if (!((key.get(pos)) == (0))) {
+ ++c;
+ acceptOnIndex(action, pos++);
+ return true;
+ }
+ ++pos;
+ }
+ return false;
+ }
+
+ public void forEachRemaining(final ConsumerType action) {
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++c;
+ acceptOnIndex(action, n);
+ }
+ while (pos < max) {
+ if (!((key.get(pos)) == (0))) {
+ acceptOnIndex(action, pos);
+ ++c;
+ }
+ ++pos;
+ }
+ }
+
+ public long estimateSize() {
+ if (!hasSplit) {
+ // Root spliterator; we know how many are remaining.
+ return size.get() - c;
+ } else {
+ // After we split, we can no longer know exactly how many we have (or at least not
+ // efficiently).
+ // (size / n) * (max - pos) aka currentTableDensity * numberOfBucketsLeft seems like a good
+ // estimate.
+ return Math.min(size.get() - c,
+ (long) (((double) realSize() / n) * (max - pos)) + (mustReturnNull ? 1 : 0));
+ }
+ }
+
+ public SplitType trySplit() {
+ if (pos >= max - 1)
+ return null;
+ long retLen = (max - pos) >> 1;
+ if (retLen <= 1)
+ return null;
+ long myNewPos = pos + retLen;
+ long retPos = pos;
+ long retMax = myNewPos;
+ // Since null is returned first, and the convention is that the returned split is the prefix
+ // of
+ // elements,
+ // the split will take care of returning null (if needed), and we won't return it anymore.
+ SplitType split = makeForSplit(retPos, retMax, mustReturnNull);
+ this.pos = myNewPos;
+ this.mustReturnNull = false;
+ this.hasSplit = true;
+ return split;
+ }
+
+ public long skip(long n) {
+ if (n < 0)
+ throw new IllegalArgumentException("Argument must be nonnegative: " + n);
+ if (n == 0)
+ return 0;
+ long skipped = 0;
+ if (mustReturnNull) {
+ mustReturnNull = false;
+ ++skipped;
+ --n;
+ }
+ while (pos < max && n > 0) {
+ if (!((key.get(pos++)) == (0))) {
+ ++skipped;
+ --n;
+ }
+ }
+ return skipped;
+ }
+ }
+
+ private final class EntrySpliterator
+ extends MapSpliterator<Consumer<? super Long2ObjectMap.Entry<V>>, EntrySpliterator>
+ implements ObjectSpliterator<Long2ObjectMap.Entry<V>> {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ ObjectSpliterators.SET_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ EntrySpliterator() {}
+
+ EntrySpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : ObjectSpliterators.SET_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final Consumer<? super Long2ObjectMap.Entry<V>> action,
+ final long index) {
+ action.accept(new MapEntry(index));
+ }
+
+ @Override
+ final EntrySpliterator makeForSplit(long pos, long max, boolean mustReturnNull) {
+ return new EntrySpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ private final class MapEntrySet extends AbstractObjectSet<Long2ObjectMap.Entry<V>>
+ implements FastEntrySet<V> {
+ @Override
+ public ObjectIterator<Long2ObjectMap.Entry<V>> iterator() {
+ return new EntryIterator();
+ }
+
+ @Override
+ public ObjectIterator<Long2ObjectMap.Entry<V>> fastIterator() {
+ return new FastEntryIterator();
+ }
+
+ @Override
+ public ObjectSpliterator<Long2ObjectMap.Entry<V>> spliterator() {
+ return new EntrySpliterator();
+ }
+
+ //
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean contains(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
+ if (e.getKey() == null || !(e.getKey() instanceof Long))
+ return false;
+ final long k = ((Long) (e.getKey())).longValue();
+ final V v = ((V) e.getValue());
+ if (((k) == (0)))
+ return Long2ObjectOpenHashDataMap.this.containsNullKey
+ && java.util.Objects.equals(value.get(n), v);
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return java.util.Objects.equals(value.get(pos), v);
+ // There's always an unused entry.
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((k) == (curr)))
+ return java.util.Objects.equals(value.get(pos), v);
+ }
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean remove(final Object o) {
+ if (!(o instanceof Map.Entry))
+ return false;
+ final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
+ if (e.getKey() == null || !(e.getKey() instanceof Long))
+ return false;
+ final long k = ((Long) (e.getKey())).longValue();
+ final V v = ((V) e.getValue());
+ if (((k) == (0))) {
+ if (containsNullKey && java.util.Objects.equals(value.get(n), v)) {
+ removeNullEntry();
+ return true;
+ }
+ return false;
+ }
+ long curr;
+ long pos;
+ // The starting point.
+ if (((curr = key.get(pos = (long) HashCommon.mix((k)) & mask)) == (0)))
+ return false;
+ if (((curr) == (k))) {
+ if (java.util.Objects.equals(value.get(pos), v)) {
+ removeEntry(pos);
+ return true;
+ }
+ return false;
+ }
+ while (true) {
+ if (((curr = key.get(pos = (pos + 1) & mask)) == (0)))
+ return false;
+ if (((curr) == (k))) {
+ if (java.util.Objects.equals(value.get(pos), v)) {
+ removeEntry(pos);
+ return true;
+ }
+ }
+ }
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(size.get(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public void clear() {
+ Long2ObjectOpenHashDataMap.this.clear();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void forEach(final Consumer<? super Long2ObjectMap.Entry<V>> consumer) {
+ if (containsNullKey)
+ consumer.accept(new AbstractLong2ObjectMap.BasicEntry<V>(key.get(n), value.get(n)));
+ for (long pos = n; pos-- != 0;)
+ if (!((key.get(pos)) == (0)))
+ consumer.accept(new AbstractLong2ObjectMap.BasicEntry<V>(key.get(pos), value.get(pos)));
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void fastForEach(final Consumer<? super Long2ObjectMap.Entry<V>> consumer) {
+ if (containsNullKey) {
+ consumer.accept(new AbstractLong2ObjectMap.BasicEntry<>(key.get(n), value.get(n)));
+ }
+ for (long pos = n; pos-- != 0;)
+ if (!((key.get(pos)) == (0))) {
+ consumer.accept(new AbstractLong2ObjectMap.BasicEntry<>(key.get(pos), value.get(pos)));
+ }
+ }
+ }
+
+ @Override
+ public FastEntrySet<V> long2ObjectEntrySet() {
+ if (entries == null)
+ entries = new MapEntrySet();
+ return entries;
+ }
+
+ /**
+ * An iterator on keys.
+ *
+ * <p>
+ * We simply override the
+ * {@link java.util.ListIterator#next()}/{@link java.util.ListIterator#previous()} methods (and
+ * possibly their type-specific counterparts) so that they return keys instead of entries.
+ */
+ private final class KeyIterator extends MapIterator<java.util.function.LongConsumer>
+ implements LongIterator {
+ public KeyIterator() {
+ super();
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ // Despite the superclass declared with generics, the way Java inherits and generates bridge
+ // methods
+ // avoids the boxing/unboxing
+ @Override
+ final void acceptOnIndex(final java.util.function.LongConsumer action, final long index) {
+ action.accept(key.get(index));
+ }
+
+ @Override
+ public long nextLong() {
+ return key.get(nextEntry());
+ }
+ }
+
+ private final class KeySpliterator extends
+ MapSpliterator<java.util.function.LongConsumer, KeySpliterator> implements LongSpliterator {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ LongSpliterators.SET_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ KeySpliterator() {}
+
+ KeySpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : LongSpliterators.SET_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final java.util.function.LongConsumer action, final long index) {
+ action.accept(key.get(index));
+ }
+
+ @Override
+ final KeySpliterator makeForSplit(long pos, long max, boolean mustReturnNull) {
+ return new KeySpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ private final class KeySet extends AbstractLongSet {
+ @Override
+ public LongIterator iterator() {
+ return new KeyIterator();
+ }
+
+ @Override
+ public LongSpliterator spliterator() {
+ return new KeySpliterator();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void forEach(final java.util.function.LongConsumer consumer) {
+ if (containsNullKey)
+ consumer.accept(key.get(n));
+ for (long pos = n; pos-- != 0;) {
+ final long k = key.get(pos);
+ if (!((k) == (0)))
+ consumer.accept(k);
+ }
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(size.get(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public boolean contains(long k) {
+ return containsKey(k);
+ }
+
+ @Override
+ public boolean remove(long k) {
+ final long oldSize = size.get();
+ Long2ObjectOpenHashDataMap.this.remove(k);
+ return size.get() != oldSize;
+ }
+
+ @Override
+ public void clear() {
+ Long2ObjectOpenHashDataMap.this.clear();
+ }
+ }
+
+ @Override
+ public LongSet keySet() {
+ if (keys == null)
+ keys = new KeySet();
+ return keys;
+ }
+
+ /**
+ * An iterator on values.
+ *
+ * <p>
+ * We simply override the
+ * {@link java.util.ListIterator#next()}/{@link java.util.ListIterator#previous()} methods (and
+ * possibly their type-specific counterparts) so that they return values instead of entries.
+ */
+ private final class ValueIterator extends MapIterator<Consumer<? super V>>
+ implements ObjectIterator<V> {
+ public ValueIterator() {
+ super();
+ }
+
+ // forEachRemaining inherited from MapIterator superclass.
+ // Despite the superclass declared with generics, the way Java inherits and generates bridge
+ // methods
+ // avoids the boxing/unboxing
+ @Override
+ final void acceptOnIndex(final Consumer<? super V> action, final long index) {
+ action.accept(value.get(index));
+ }
+
+ @Override
+ public V next() {
+ return value.get(nextEntry());
+ }
+ }
+
+ private final class ValueSpliterator extends MapSpliterator<Consumer<? super V>, ValueSpliterator>
+ implements ObjectSpliterator<V> {
+ private static final int POST_SPLIT_CHARACTERISTICS =
+ ObjectSpliterators.COLLECTION_SPLITERATOR_CHARACTERISTICS & ~java.util.Spliterator.SIZED;
+
+ ValueSpliterator() {}
+
+ ValueSpliterator(long pos, long max, boolean mustReturnNull, boolean hasSplit) {
+ super(pos, max, mustReturnNull, hasSplit);
+ }
+
+ @Override
+ public int characteristics() {
+ return hasSplit ? POST_SPLIT_CHARACTERISTICS
+ : ObjectSpliterators.COLLECTION_SPLITERATOR_CHARACTERISTICS;
+ }
+
+ @Override
+ final void acceptOnIndex(final Consumer<? super V> action, final long index) {
+ action.accept(value.get(index));
+ }
+
+ @Override
+ final ValueSpliterator makeForSplit(long pos, long max, boolean mustReturnNull) {
+ return new ValueSpliterator(pos, max, mustReturnNull, true);
+ }
+ }
+
+ @Override
+ public ObjectCollection<V> values() {
+ if (values == null)
+ values = new AbstractObjectCollection<V>() {
+ @Override
+ public ObjectIterator<V> iterator() {
+ return new ValueIterator();
+ }
+
+ @Override
+ public ObjectSpliterator<V> spliterator() {
+ return new ValueSpliterator();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void forEach(final Consumer<? super V> consumer) {
+ if (containsNullKey)
+ consumer.accept(value.get(n));
+ for (long pos = n; pos-- != 0;)
+ if (!((key.get(pos)) == (0)))
+ consumer.accept(value.get(pos));
+ }
+
+ @Override
+ public int size() {
+ return (int) Math.min(size.get(), Integer.MAX_VALUE);
+ }
+
+ @Override
+ public boolean contains(Object v) {
+ return containsValue(v);
+ }
+
+ @Override
+ public void clear() {
+ Long2ObjectOpenHashDataMap.this.clear();
+ }
+ };
+ return values;
+ }
+
+ /**
+ * Rehashes the map, making the table as small as possible.
+ *
+ * <p>
+ * This method rehashes the table to the smallest size satisfying the load factor. It can be used
+ * when the set will not be changed anymore, so to optimize access speed and size.
+ *
+ * <p>
+ * If the table size is already the minimum possible, this method does nothing.
+ *
+ * @return true if there was enough memory to trim the map.
+ */
+ public boolean trim() {
+ return trim(size.get());
+ }
+
+ /**
+ * Rehashes this map if the table is too large.
+ *
+ * <p>
+ * Let <var>N</var> be the smallest table size that can hold <code>max(n,{@link #size()})</code>
+ * entries, still satisfying the load factor. If the current table size is smaller than or equal
+ * to <var>N</var>, this method does nothing. Otherwise, it rehashes this map in a table of size
+ * <var>N</var>.
+ *
+ * <p>
+ * This method is useful when reusing maps. {@linkplain #clear() Clearing a map} leaves the table
+ * size untouched. If you are reusing a map many times, you can call this method with a typical
+ * size to avoid keeping around a very large table just because of a few large transient maps.
+ *
+ * @param n the threshold for the trimming.
+ * @return true if there was enough memory to trim the map.
+ * @see #trim()
+ */
+ public boolean trim(final long n) {
+ final long l = HashCommon.nextPowerOfTwo((long) Math.ceil(n / f));
+ if (l >= this.n || size.get() > maxFill(l, f))
+ return true;
+ try {
+ rehash(l);
+ } catch (OutOfMemoryError cantDoIt) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Rehashes the map.
+ *
+ * <p>
+ * This method implements the basic rehashing strategy, and may be overridden by subclasses
+ * implementing different rehashing strategies (e.g., disk-based rehashing). However, you should
+ * not override this method unless you understand the internal workings of this class.
+ *
+ * @param newN the new size
+ */
+ @SuppressWarnings("unchecked")
+ protected void rehash(final long newN) {
+ final long mask = newN - 1; // Note that this is used by the hashing macro
+ final DataMap<Long> newKey = keySupplier.get();
+ final DataMap<V> newValue = valueSupplier.get();
+ long i = n, pos;
+ for (long j = realSize(); j-- != 0;) {
+ while ((key.get(--i) == 0));
+ if (!((newKey.get(pos = (long) mix((key.get(i))) & mask)) == (0))) {
+ while (!(newKey.get(pos = (pos + 1) & mask) == 0));
+ }
+ newKey.put(pos, key.get(i));
+ newValue.put(pos, value.get(i));
+ }
+ newValue.put(newN, value.get(n));
+ this.n = newN;
+ this.mask = mask;
+ this.maxFill = maxFill(n, f);
+ this.key = newKey;
+ this.value = newValue;
+ }
+
+ /**
+ * Returns a hash code for this map.
+ *
+ * This method overrides the generic method provided by the superclass. Since {@code equals()} is
+ * not overriden, it is important that the value returned by this method is the same value as the
+ * one returned by the overriden method.
+ *
+ * @return a hash code for this map.
+ */
+ @Override
+ public int hashCode() {
+ int h = 0;
+ for (long j = realSize(), i = 0, t = 0; j-- != 0;) {
+ while (((key.get(i)) == (0)))
+ i++;
+ t = HashCommon.long2int(key.get(i));
+ if (this != value.get(i))
+ t ^= ((value.get(i)) == null ? 0 : (value.get(i)).hashCode());
+ h += t;
+ i++;
+ }
+ // Zero / null keys have hash zero.
+ if (containsNullKey)
+ h += ((value.get(n)) == null ? 0 : (value.get(n)).hashCode());
+ return h;
+ }
+}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MemoryAlignedDataList.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MemoryAlignedDataList.java
index 683d8563..4e117b71 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MemoryAlignedDataList.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MemoryAlignedDataList.java
@@ -22,13 +22,13 @@ import org.apache.baremaps.database.memory.OffHeapMemory;
import org.apache.baremaps.database.type.FixedSizeDataType;
/**
- * A list that can hold a large number of fixed-size memory-aligned data elements.
+ * A collection that can hold a large number of fixed-size memory-aligned data elements.
*
- * This list is backed by a memory that can be either heap, off-heap, or memory mapped.
+ * This collection is backed by a memory that can be either heap, off-heap, or memory mapped.
*
* @param <E> The type of the elements.
*/
-public class MemoryAlignedDataList<E> extends DataList<E> {
+public class MemoryAlignedDataList<E> extends AbstractDataList<E> {
private final FixedSizeDataType<E> dataType;
@@ -43,7 +43,7 @@ public class MemoryAlignedDataList<E> extends DataList<E> {
private AtomicLong size;
/**
- * Constructs a list.
+ * Constructs a collection.
*
* @param dataType the data type
*/
@@ -52,7 +52,7 @@ public class MemoryAlignedDataList<E> extends DataList<E> {
}
/**
- * Constructs a list.
+ * Constructs a collection.
*
* @param dataType the data type
* @param memory the memory
@@ -109,7 +109,7 @@ public class MemoryAlignedDataList<E> extends DataList<E> {
/** {@inheritDoc} */
@Override
- public long sizeAsLong() {
+ public long size64() {
return size.get();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedDirectory.java b/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedDirectory.java
index 88593038..9c563011 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedDirectory.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedDirectory.java
@@ -35,7 +35,7 @@ public class MemoryMappedDirectory extends Memory<MappedByteBuffer> {
* @param directory the directory that stores the data
*/
public MemoryMappedDirectory(Path directory) {
- this(directory, 1 << 30);
+ this(directory, 1 << 28);
}
/**
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedFile.java b/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedFile.java
index cdbfdd51..05b70bd1 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedFile.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/memory/MemoryMappedFile.java
@@ -29,12 +29,12 @@ public class MemoryMappedFile extends Memory<MappedByteBuffer> {
private final Path file;
/**
- * Constructs an {@link MemoryMappedFile} with a custom file and a default segment size of 1gb.
+ * Constructs an {@link MemoryMappedFile} with a custom file and a default segment size of 256mb.
*
* @param file the file that stores the data
*/
public MemoryMappedFile(Path file) {
- this(file, 1 << 30);
+ this(file, 1 << 28);
}
/**
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/memory/OffHeapMemory.java b/baremaps-core/src/main/java/org/apache/baremaps/database/memory/OffHeapMemory.java
index e9beaf7f..824fdca9 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/memory/OffHeapMemory.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/memory/OffHeapMemory.java
@@ -20,9 +20,9 @@ import java.nio.ByteBuffer;
/** A memory that stores segments off-heap using direct byte buffers. */
public class OffHeapMemory extends Memory<ByteBuffer> {
- /** Constructs an {@link OffHeapMemory} with a default segment size of 1mb. */
+ /** Constructs an {@link OffHeapMemory} with a default segment size of 16mb. */
public OffHeapMemory() {
- this(1 << 20);
+ this(1 << 24);
}
/**
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableAdapter.java b/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableAdapter.java
index 96580b91..6d78b23c 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableAdapter.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableAdapter.java
@@ -57,7 +57,7 @@ public class DataTableAdapter extends AbstractDataCollection<DataRow> implements
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
- return table.sizeAsLong();
+ public long size64() {
+ return table.size64();
}
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableImpl.java b/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableImpl.java
index b56d0030..465f4fbc 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableImpl.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/schema/DataTableImpl.java
@@ -65,9 +65,9 @@ public class DataTableImpl extends AbstractDataCollection<DataRow> implements Da
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
+ public long size64() {
if (rows instanceof DataCollection<DataRow>dataCollection) {
- return dataCollection.sizeAsLong();
+ return dataCollection.size64();
} else {
return rows.size();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresCoordinateMap.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresCoordinateMap.java
index 9da22933..92920b74 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresCoordinateMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresCoordinateMap.java
@@ -17,6 +17,7 @@ package org.apache.baremaps.openstreetmap.postgres;
import java.sql.*;
import java.util.*;
import javax.sql.DataSource;
+import org.apache.baremaps.database.collection.AbstractDataMap;
import org.apache.baremaps.database.collection.DataCollectionException;
import org.apache.baremaps.database.collection.DataMap;
import org.locationtech.jts.geom.Coordinate;
@@ -24,7 +25,7 @@ import org.locationtech.jts.geom.Coordinate;
/**
* A read-only {@link DataMap} for coordinates baked by OpenStreetMap nodes stored in PostgreSQL.
*/
-public class PostgresCoordinateMap extends DataMap<Coordinate> {
+public class PostgresCoordinateMap extends AbstractDataMap<Coordinate> {
public static final String SELECT_CONTAINS_KEY = """
SELECT 1
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresReferenceMap.java b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresReferenceMap.java
index 77c175e5..a5827730 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresReferenceMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/postgres/PostgresReferenceMap.java
@@ -21,13 +21,13 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import javax.sql.DataSource;
+import org.apache.baremaps.database.collection.AbstractDataMap;
import org.apache.baremaps.database.collection.DataCollectionException;
-import org.apache.baremaps.database.collection.DataMap;
/**
* A read-only {@code LongDataMap} for references baked by OpenStreetMap ways stored in Postgres.
*/
-public class PostgresReferenceMap extends DataMap<List<Long>> {
+public class PostgresReferenceMap extends AbstractDataMap<List<Long>> {
public static final String SELECT_CONTAINS_KEY = """
SELECT 1
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTable.java b/baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTable.java
index 1401b29d..2a9fdddf 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTable.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTable.java
@@ -117,7 +117,7 @@ public class FlatGeoBufDataTable extends AbstractDataTable {
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
+ public long size64() {
try (var channel = FileChannel.open(file, StandardOpenOption.READ)) {
var buffer = ByteBuffer.allocate(1 << 20).order(ByteOrder.LITTLE_ENDIAN);
HeaderMeta headerMeta = readHeaderMeta(channel, buffer);
@@ -161,9 +161,10 @@ public class FlatGeoBufDataTable extends AbstractDataTable {
headerMeta.indexNodeSize = 16;
headerMeta.srid = 3857;
headerMeta.featuresCount =
- features instanceof AbstractDataCollection<DataRow>c ? c.sizeAsLong() : features.size();
+ features instanceof AbstractDataCollection<DataRow>c ? c.size64() : features.size();
headerMeta.name = rowType.name();
headerMeta.columns = FlatGeoBufTypeConversion.asColumns(rowType.columns());
+
HeaderMeta.write(headerMeta, outputStream, bufferBuilder);
var indexSize =
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/storage/geopackage/GeoPackageDataTable.java b/baremaps-core/src/main/java/org/apache/baremaps/storage/geopackage/GeoPackageDataTable.java
index 03fc5f13..9b550fc7 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/storage/geopackage/GeoPackageDataTable.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/storage/geopackage/GeoPackageDataTable.java
@@ -72,7 +72,7 @@ public class GeoPackageDataTable extends AbstractDataCollection<DataRow> impleme
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
+ public long size64() {
return featureDao.count();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/storage/postgres/PostgresDataTable.java b/baremaps-core/src/main/java/org/apache/baremaps/storage/postgres/PostgresDataTable.java
index 0f196ff1..194ebb19 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/storage/postgres/PostgresDataTable.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/storage/postgres/PostgresDataTable.java
@@ -71,7 +71,7 @@ public class PostgresDataTable extends AbstractDataTable {
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
+ public long size64() {
var countQuery = count(rowType);
try (var connection = dataSource.getConnection();
var statement = connection.prepareStatement(countQuery);
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/storage/shapefile/ShapefileDataTable.java b/baremaps-core/src/main/java/org/apache/baremaps/storage/shapefile/ShapefileDataTable.java
index f413ad95..5f357c0c 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/storage/shapefile/ShapefileDataTable.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/storage/shapefile/ShapefileDataTable.java
@@ -68,7 +68,7 @@ public class ShapefileDataTable extends AbstractDataTable {
* {@inheritDoc}
*/
@Override
- public long sizeAsLong() {
+ public long size64() {
return Long.MAX_VALUE;
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ImportOpenStreetMap.java b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ImportOpenStreetMap.java
index b7053a70..e227dbf6 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ImportOpenStreetMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/ImportOpenStreetMap.java
@@ -18,7 +18,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.apache.baremaps.database.collection.*;
-import org.apache.baremaps.database.memory.MemoryMappedFile;
+import org.apache.baremaps.database.memory.MemoryMappedDirectory;
import org.apache.baremaps.database.type.LongDataType;
import org.apache.baremaps.database.type.LongListDataType;
import org.apache.baremaps.database.type.PairDataType;
@@ -34,7 +34,6 @@ import org.apache.baremaps.openstreetmap.postgres.PostgresWayRepository;
import org.apache.baremaps.openstreetmap.repository.*;
import org.apache.baremaps.openstreetmap.repository.BlockImporter;
import org.apache.baremaps.stream.StreamUtils;
-import org.apache.baremaps.utils.FileUtils;
import org.apache.baremaps.workflow.Task;
import org.apache.baremaps.workflow.WorkflowContext;
import org.locationtech.jts.geom.Coordinate;
@@ -69,35 +68,40 @@ public record ImportOpenStreetMap(Path file, String database, Integer databaseSr
var cacheDir = Files.createTempDirectory(Paths.get("."), "cache_");
- DataMap<Coordinate> coordinateMap;
- if (Files.size(path) > 1 << 30) {
- var coordinatesFile = Files.createFile(cacheDir.resolve("coordinates"));
- coordinateMap = new MemoryAlignedDataMap<>(
- new LonLatDataType(),
- new MemoryMappedFile(coordinatesFile));
- } else {
- var coordinatesKeysFile = Files.createFile(cacheDir.resolve("coordinates_keys"));
- var coordinatesValsFile = Files.createFile(cacheDir.resolve("coordinates_vals"));
- coordinateMap =
- new MonotonicDataMap<>(
- new MemoryAlignedDataList<>(
- new PairDataType<>(new LongDataType(), new LongDataType()),
- new MemoryMappedFile(coordinatesKeysFile)),
- new AppendOnlyBuffer<>(
- new LonLatDataType(),
- new MemoryMappedFile(coordinatesValsFile)));
- }
+ var coordinatesKeysFile = Files.createDirectory(cacheDir.resolve("coordinates_keys"));
+ var coordinatesValsFile = Files.createDirectory(cacheDir.resolve("coordinates_vals"));
+ var coordinateMap =
+ new Long2ObjectOpenHashDataMap<>(100_000_000L, 0.9f,
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new LongDataType(),
+ new MemoryMappedDirectory(coordinatesKeysFile)),
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new LonLatDataType(),
+ new MemoryMappedDirectory(coordinatesValsFile)));
+
+ var referencesKeysDir = Files.createDirectory(cacheDir.resolve("references_keys"));
+ var referencesValuesDir = Files.createDirectory(cacheDir.resolve("references_vals"));
- var referencesKeysDir = Files.createFile(cacheDir.resolve("references_keys"));
- var referencesValuesDir = Files.createFile(cacheDir.resolve("references_vals"));
var referenceMap =
- new MonotonicDataMap<>(
- new MemoryAlignedDataList<>(
- new PairDataType<>(new LongDataType(), new LongDataType()),
- new MemoryMappedFile(referencesKeysDir)),
+ new Long2ObjectIndexedDataMap<>(
+ new Long2LongPackedOpenHashDataMap(
+ 10_000_000L,
+ 0.9f,
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new PairDataType<>(new LongDataType(), new LongDataType()),
+ new MemoryMappedDirectory(referencesKeysDir))),
new AppendOnlyBuffer<>(
new LongListDataType(),
- new MemoryMappedFile(referencesValuesDir)));
+ new MemoryMappedDirectory(referencesValuesDir)));
+
+ // var referenceMap =
+ // new MonotonicDataMap<>(
+ // new MemoryAlignedDataList<>(
+ // new PairDataType<>(new LongDataType(), new LongDataType()),
+ // new MemoryMappedDirectory(referencesKeysDir)),
+ // new AppendOnlyBuffer<>(
+ // new LongListDataType(),
+ // new MemoryMappedDirectory(referencesValuesDir)));
execute(
path,
@@ -109,7 +113,7 @@ public record ImportOpenStreetMap(Path file, String database, Integer databaseSr
relationRepository,
databaseSrid);
- FileUtils.deleteRecursively(cacheDir);
+ // FileUtils.deleteRecursively(cacheDir);
}
public static void execute(
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/database/DataListTest.java b/baremaps-core/src/test/java/org/apache/baremaps/database/DataListTest.java
index db69b09c..9d90ba6b 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/database/DataListTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/database/DataListTest.java
@@ -25,7 +25,7 @@ class DataListTest {
@ParameterizedTest
@MethodSource("listProvider")
- void addSetGet(DataList<Long> list) {
+ void addSetGet(AbstractDataList<Long> list) {
for (long i = 0; i < 1000; i++) {
list.add(i);
}
@@ -42,7 +42,7 @@ class DataListTest {
@ParameterizedTest
@MethodSource("listProvider")
- void containsValue(DataList<Long> list) {
+ void containsValue(AbstractDataList<Long> list) {
for (long i = 0; i < 1000; i++) {
assertFalse(list.contains(i));
}
@@ -56,7 +56,7 @@ class DataListTest {
@ParameterizedTest
@MethodSource("listProvider")
- void clear(DataList<Long> list) {
+ void clear(AbstractDataList<Long> list) {
for (long i = 0; i < 1000; i++) {
list.add(i);
}
@@ -71,8 +71,9 @@ class DataListTest {
static Stream<Arguments> listProvider() {
return Stream.of(
Arguments.of(new FixedSizeDataList<>(new LongDataType())),
- Arguments.of(new IndexedDataList<>(new MemoryAlignedDataList<>(new LongDataType()),
- new AppendOnlyBuffer<>(new LongDataType()))),
+ Arguments
+ .of(new IndexedDataList<>(new MemoryAlignedDataList<>(new LongDataType()),
+ new AppendOnlyBuffer<>(new LongDataType()))),
Arguments.of(new MemoryAlignedDataList<>(new LongDataType())));
}
}
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/database/DataMapTest.java b/baremaps-core/src/test/java/org/apache/baremaps/database/DataMapTest.java
index 2109e745..5d4de369 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/database/DataMapTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/database/DataMapTest.java
@@ -158,12 +158,35 @@ class DataMapTest {
return Stream
.of(
Arguments.of(
- new JaggedDataMap<>(
+ new Long2LongOpenHashDataMap(10000, 0.75f,
+ () -> new MemoryAlignedDataList<>(
+ new LongDataType(),
+ new OffHeapMemory()),
+ () -> new MemoryAlignedDataList<>(
+ new LongDataType(),
+ new OffHeapMemory()))),
+ Arguments.of(
+ new Long2ObjectOpenHashDataMap<>(10000, 0.75f,
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new LongDataType(),
+ new OffHeapMemory()),
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new LongDataType(),
+ new OffHeapMemory()))),
+ Arguments.of(
+ new Long2LongPackedOpenHashDataMap(10000, 0.75f,
+ () -> new Long2ObjectMemoryAlignedDataMap<>(
+ new PairDataType<>(
+ new LongDataType(),
+ new LongDataType()),
+ new OffHeapMemory()))),
+ Arguments.of(
+ new Long2ObjectJaggedDataMap<>(
new AppendOnlyBuffer<>(new LongDataType(), new OffHeapMemory()))),
Arguments.of(
- new IndexedDataMap<>(
+ new Long2ObjectIndexedDataMap<>(
new AppendOnlyBuffer<>(new LongDataType(), new OffHeapMemory()))),
- Arguments.of(new MonotonicDataMap<>(
+ Arguments.of(new Long2ObjectMonotonicDataMap<>(
new MemoryAlignedDataList<>(
new PairDataType<>(new LongDataType(), new LongDataType()),
new OffHeapMemory()),
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/database/SizedDataListTest.java b/baremaps-core/src/test/java/org/apache/baremaps/database/SizedDataListTest.java
index 45a4cb8d..35f5cabc 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/database/SizedDataListTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/database/SizedDataListTest.java
@@ -31,7 +31,8 @@ class SizedDataListTest {
void segmentsTooSmall() {
var dataType = new LongDataType();
var memory = new OffHeapMemory(4);
- assertThrows(DataCollectionException.class, () -> new FixedSizeDataList<>(dataType, memory));
+ assertThrows(DataCollectionException.class,
+ () -> new FixedSizeDataList<>(dataType, memory));
}
@ParameterizedTest
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/database/sort/ExternalMergeSortTest.java b/baremaps-core/src/test/java/org/apache/baremaps/database/sort/ExternalMergeSortTest.java
index 3d365773..9224cd29 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/database/sort/ExternalMergeSortTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/database/sort/ExternalMergeSortTest.java
@@ -22,8 +22,8 @@ import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import org.apache.baremaps.database.algorithm.ExternalMergeSort;
+import org.apache.baremaps.database.collection.AbstractDataList;
import org.apache.baremaps.database.collection.AppendOnlyBuffer;
-import org.apache.baremaps.database.collection.DataList;
import org.apache.baremaps.database.collection.IndexedDataList;
import org.apache.baremaps.database.collection.MemoryAlignedDataList;
import org.apache.baremaps.database.memory.OnHeapMemory;
@@ -38,9 +38,9 @@ class ExternalMergeSortTest {
List<String> stringsAsc = strings.stream().sorted(Comparator.naturalOrder()).toList();
List<String> stringsDsc = strings.stream().sorted(Comparator.reverseOrder()).toList();;
List<String> stringsDistinct = stringsAsc.stream().distinct().toList();
- Supplier<DataList<String>> supplier;
- DataList<String> input;
- DataList<String> output;
+ Supplier<AbstractDataList<String>> supplier;
+ AbstractDataList<String> input;
+ AbstractDataList<String> output;
@BeforeEach
void before() {
@@ -54,9 +54,9 @@ class ExternalMergeSortTest {
}
}
- public List<String> stringList(DataList<String> list) {
+ public List<String> stringList(AbstractDataList<String> list) {
var l = new ArrayList<String>();
- for (long i = 0; i < list.sizeAsLong(); i++) {
+ for (long i = 0; i < list.size64(); i++) {
l.add(list.get(i));
}
return l;
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/store/MockDataMap.java b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/store/MockDataMap.java
index 01fba4d6..28ed0399 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/store/MockDataMap.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/store/MockDataMap.java
@@ -16,10 +16,10 @@ package org.apache.baremaps.openstreetmap.store;
import java.util.Iterator;
import java.util.Map;
-import org.apache.baremaps.database.collection.DataMap;
+import org.apache.baremaps.database.collection.AbstractDataMap;
import org.jetbrains.annotations.Nullable;
-public class MockDataMap<T> extends DataMap<T> {
+public class MockDataMap<T> extends AbstractDataMap<T> {
private final Map<Long, T> values;
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/storage/MockDataTable.java b/baremaps-core/src/test/java/org/apache/baremaps/storage/MockDataTable.java
index c9e2c673..3fc46d33 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/storage/MockDataTable.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/storage/MockDataTable.java
@@ -52,7 +52,7 @@ public class MockDataTable extends AbstractDataTable {
}
@Override
- public long sizeAsLong() {
+ public long size64() {
return rows.size();
}
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTableTest.java b/baremaps-core/src/test/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTableTest.java
index 08b352ef..d24ac398 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTableTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTableTest.java
@@ -32,7 +32,7 @@ class FlatGeoBufDataTableTest {
@Test
void read() throws IOException {
var table = new FlatGeoBufDataTable(TestFiles.resolve("countries.fgb"));
- assertEquals(179, table.sizeAsLong());
+ assertEquals(179, table.size64());
assertEquals(179, table.stream().count());
}
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageDataSchemaTest.java b/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageDataSchemaTest.java
index b01b8f0c..e9621177 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageDataSchemaTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageDataSchemaTest.java
@@ -32,7 +32,7 @@ class GeoPackageDataSchemaTest {
void read() {
var geoPackageStore = new GeoPackageDataSchema(TestFiles.resolve("countries.gpkg"));
var table = geoPackageStore.get("countries");
- assertEquals(179, table.sizeAsLong());
+ assertEquals(179, table.size64());
assertEquals(179, table.stream().count());
}
}
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageToPostgresTest.java b/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageToPostgresTest.java
index 81223761..444e66e4 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageToPostgresTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/storage/geopackage/GeoPackageToPostgresTest.java
@@ -37,7 +37,7 @@ public class GeoPackageToPostgresTest extends PostgresContainerTest {
var postgresTable = postgresStore.get("countries");
assertEquals("countries", postgresTable.rowType().name());
assertEquals(4, postgresTable.rowType().columns().size());
- assertEquals(179l, postgresTable.sizeAsLong());
+ assertEquals(179l, postgresTable.size64());
assertEquals(179l, postgresTable.stream().count());
}
}
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/storage/postgres/PostgresDataTableTest.java b/baremaps-core/src/test/java/org/apache/baremaps/storage/postgres/PostgresDataTableTest.java
index b8ddeebb..06320692 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/storage/postgres/PostgresDataTableTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/storage/postgres/PostgresDataTableTest.java
@@ -44,9 +44,9 @@ class PostgresDataTableTest extends PostgresContainerTest {
@Test
@Tag("integration")
- void sizeAsLong() {
+ void size64() {
var table = schema.get("mock");
- assertEquals(5, table.sizeAsLong());
+ assertEquals(5, table.size64());
}
@Test
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportMonacoTest.java b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportMonacoTest.java
index 4ac44277..9955ea28 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportMonacoTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportMonacoTest.java
@@ -18,7 +18,7 @@ import java.time.LocalDateTime;
import java.util.List;
import org.apache.baremaps.database.collection.AppendOnlyBuffer;
import org.apache.baremaps.database.collection.DataMap;
-import org.apache.baremaps.database.collection.IndexedDataMap;
+import org.apache.baremaps.database.collection.Long2ObjectIndexedDataMap;
import org.apache.baremaps.database.memory.OnHeapMemory;
import org.apache.baremaps.database.type.LongListDataType;
import org.apache.baremaps.database.type.geometry.CoordinateDataType;
@@ -47,9 +47,11 @@ class ImportMonacoTest extends PostgresRepositoryTest {
PostgresRelationRepository relationRepository = new PostgresRelationRepository(dataSource());
DataMap<Coordinate> coordinateMap =
- new IndexedDataMap<>(new AppendOnlyBuffer<>(new CoordinateDataType(), new OnHeapMemory()));
+ new Long2ObjectIndexedDataMap<>(
+ new AppendOnlyBuffer<>(new CoordinateDataType(), new OnHeapMemory()));
DataMap<List<Long>> referenceMap =
- new IndexedDataMap<>(new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
+ new Long2ObjectIndexedDataMap<>(
+ new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
// Import data
ImportOpenStreetMap.execute(TestFiles.resolve("monaco/monaco-210801.osm.pbf"), coordinateMap,
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateDataTest.java b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateDataTest.java
index 42b767a9..ce62b64b 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateDataTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateDataTest.java
@@ -21,7 +21,7 @@ import java.time.LocalDateTime;
import java.util.List;
import org.apache.baremaps.database.collection.AppendOnlyBuffer;
import org.apache.baremaps.database.collection.DataMap;
-import org.apache.baremaps.database.collection.IndexedDataMap;
+import org.apache.baremaps.database.collection.Long2ObjectIndexedDataMap;
import org.apache.baremaps.database.memory.OnHeapMemory;
import org.apache.baremaps.database.type.LongListDataType;
import org.apache.baremaps.database.type.geometry.CoordinateDataType;
@@ -51,9 +51,11 @@ class ImportUpdateDataTest extends PostgresRepositoryTest {
PostgresRelationRepository relationRepository = new PostgresRelationRepository(dataSource());
DataMap<Coordinate> coordinateMap =
- new IndexedDataMap<>(new AppendOnlyBuffer<>(new CoordinateDataType(), new OnHeapMemory()));
+ new Long2ObjectIndexedDataMap<>(
+ new AppendOnlyBuffer<>(new CoordinateDataType(), new OnHeapMemory()));
DataMap<List<Long>> referenceMap =
- new IndexedDataMap<>(new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
+ new Long2ObjectIndexedDataMap<>(
+ new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
// Import data
ImportOpenStreetMap.execute(SIMPLE_DATA_OSM_PBF, coordinateMap, referenceMap, headerRepository,
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateLiechtensteinTest.java b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateLiechtensteinTest.java
index b255ee5b..697967cf 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateLiechtensteinTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateLiechtensteinTest.java
@@ -20,7 +20,7 @@ import java.time.LocalDateTime;
import java.util.List;
import org.apache.baremaps.database.collection.AppendOnlyBuffer;
import org.apache.baremaps.database.collection.DataMap;
-import org.apache.baremaps.database.collection.IndexedDataMap;
+import org.apache.baremaps.database.collection.Long2ObjectIndexedDataMap;
import org.apache.baremaps.database.memory.OnHeapMemory;
import org.apache.baremaps.database.type.LongListDataType;
import org.apache.baremaps.database.type.geometry.CoordinateDataType;
@@ -48,9 +48,11 @@ class ImportUpdateLiechtensteinTest extends PostgresRepositoryTest {
PostgresRelationRepository relationRepository = new PostgresRelationRepository(dataSource());
DataMap<Coordinate> coordinateMap =
- new IndexedDataMap<>(new AppendOnlyBuffer<>(new CoordinateDataType(), new OnHeapMemory()));
+ new Long2ObjectIndexedDataMap<>(
+ new AppendOnlyBuffer<>(new CoordinateDataType(), new OnHeapMemory()));
DataMap<List<Long>> referenceMap =
- new IndexedDataMap<>(new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
+ new Long2ObjectIndexedDataMap<>(
+ new AppendOnlyBuffer<>(new LongListDataType(), new OnHeapMemory()));
// Import data
ImportOpenStreetMap.execute(LIECHTENSTEIN_OSM_PBF, coordinateMap, referenceMap,
diff --git a/pom.xml b/pom.xml
index fa029f47..0fb71dcb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -82,7 +82,7 @@
<version.lib.caffeine>3.1.1</version.lib.caffeine>
<version.lib.calcite>1.34.0</version.lib.calcite>
<version.lib.commons-compress>1.21</version.lib.commons-compress>
- <version.lib.fastutil>8.5.9</version.lib.fastutil>
+ <version.lib.fastutil>8.5.12</version.lib.fastutil>
<version.lib.flatgeobuf>3.24.0</version.lib.flatgeobuf>
<version.lib.geopackage>6.5.0</version.lib.geopackage>
<version.lib.graalvm>22.2.0</version.lib.graalvm>