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 2024/05/09 08:38:41 UTC
(incubator-baremaps) 04/06: Add utility class for conversions
This is an automated email from the ASF dual-hosted git repository.
bchapuis pushed a commit to branch modularization
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit 074b9d3af1ef04b50e64a511a253ff960a6b6770
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Sat May 4 18:34:10 2024 +0200
Add utility class for conversions
---
.../database/collection/AppendOnlyLog.java | 10 +-
.../database/collection/DataCollection.java | 26 +++++
.../database/collection/DataConversions.java | 110 +++++++++++++++++++++
.../baremaps/database/collection/DataMap.java | 70 ++++++++++---
.../database/collection/IndexedDataList.java | 6 ++
.../database/collection/IndexedDataMap.java | 2 +-
.../baremaps/database/collection/MapAdapter.java | 24 -----
.../database/collection/MemoryAlignedDataMap.java | 2 +-
.../database/collection/MonotonicDataMap.java | 2 +-
.../collection/MonotonicFixedSizeDataMap.java | 2 +-
.../collection/MonotonicPairedDataMap.java | 2 +-
.../apache/baremaps/workflow/WorkflowContext.java | 4 +-
.../baremaps/database/AppendOnlyLogTest.java | 1 -
.../org/apache/baremaps/database/DataMapTest.java | 6 +-
.../workflow/tasks/ImportUpdateSampleTest.java | 6 +-
15 files changed, 213 insertions(+), 60 deletions(-)
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyLog.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyLog.java
index ea4cd2db..55664d7e 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyLog.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/AppendOnlyLog.java
@@ -101,6 +101,9 @@ public class AppendOnlyLog<E> implements DataCollection<E> {
return position;
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public boolean add(E e) {
addPositioned(e);
@@ -130,13 +133,6 @@ public class AppendOnlyLog<E> implements DataCollection<E> {
return size;
}
- /**
- * Closes the log.
- */
- public void close() {
- memory.segment(0).putLong(0, size);
- }
-
/**
* Clears the log.
*/
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 bbf1b316..cbc7afc1 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
@@ -33,20 +33,46 @@ public interface DataCollection<E> extends Iterable<E> {
*/
long size();
+ /**
+ * Returns true if the data collection is empty.
+ *
+ * @return true if the data collection is empty
+ */
default boolean isEmpty() {
return size() == 0;
}
+ /**
+ * Returns an iterator over the elements in the data collection.
+ *
+ * @return an iterator
+ */
Iterator<E> iterator();
+ /**
+ * Returns a spliterator over the elements in the data collection.
+ *
+ * @return a spliterator
+ */
default Spliterator<E> spliterator() {
return Spliterators.spliterator(iterator(), size(), Spliterator.ORDERED);
}
+ /**
+ * Returns a stream over the elements in the data collection.
+ *
+ * @return a stream
+ */
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
+ /**
+ * Adds a value to the data collection.
+ *
+ * @param e the value to add
+ * @return true if the data collection has been modified
+ */
default boolean add(E e) {
throw new UnsupportedOperationException();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataConversions.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataConversions.java
new file mode 100644
index 00000000..0d2e6d8e
--- /dev/null
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/DataConversions.java
@@ -0,0 +1,110 @@
+package org.apache.baremaps.database.collection;
+
+import java.util.*;
+import java.util.Map.Entry;
+
+public class DataConversions {
+
+ public static <K, V> Map<K, V> asMap(DataMap<K, V> dataMap) {
+ if (dataMap instanceof DataMapAdapter<K, V> adapter) {
+ return adapter.map;
+ } else {
+ return new MapAdapter<>(dataMap);
+ }
+ }
+
+ public static <K, V> DataMap<K, V> asDataMap(Map<K, V> map) {
+ if (map instanceof MapAdapter<K, V> adapter) {
+ return adapter.map;
+ } else {
+ return new DataMapAdapter<>(map);
+ }
+ }
+
+ public static class MapAdapter<K, V> extends AbstractMap<K, V> {
+
+ private final DataMap<K, V> map;
+ private final int size;
+
+ public MapAdapter(DataMap<K, V> dataMap) {
+ this.map = dataMap;
+ this.size = (int) dataMap.size();
+ }
+
+ @Override
+ public Set<Entry<K, V>> entrySet() {
+ return new AbstractSet<>() {
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return map.entryIterator();
+ }
+
+ @Override
+ public int size() {
+ return size;
+ }
+ };
+ }
+ }
+
+ public static class DataMapAdapter<K, V> implements DataMap<K, V> {
+
+ private final Map<K, V> map;
+
+ public DataMapAdapter(Map<K, V> map) {
+ this.map = map;
+ }
+
+
+ @Override
+ public long size() {
+ return map.size();
+ }
+
+ @Override
+ public V get(Object key) {
+ return map.get(key);
+ }
+
+ @Override
+ public V put(K key, V value) {
+ return map.put(key, value);
+ }
+
+ @Override
+ public V remove(K key) {
+ return map.remove(key);
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return map.containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(V value) {
+ return map.containsValue(value);
+ }
+
+ @Override
+ public void clear() {
+ map.clear();
+ }
+
+ @Override
+ public Iterator<K> keyIterator() {
+ return map.keySet().iterator();
+ }
+
+ @Override
+ public Iterator<V> valueIterator() {
+ return map.values().iterator();
+ }
+
+ @Override
+ public Iterator<Entry<K, V>> entryIterator() {
+ return map.entrySet().iterator();
+ }
+ }
+
+}
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 b2d5258e..3fab0239 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
@@ -29,24 +29,21 @@ import java.util.Map.Entry;
*/
public interface DataMap<K, V> {
+ /**
+ * Returns the number of values stored in the data map.
+ *
+ * @return the number of values
+ */
long size();
+ /**
+ * Returns the value associated with the specified key or null if the key is not present.
+ *
+ * @param key the key
+ * @return the value
+ */
V get(Object key);
- V put(K key, V value);
-
- V remove(Object key);
-
- void clear();
-
- boolean containsKey(Object key);
-
- boolean containsValue(Object value);
-
- default void putAll(Map<? extends K, ? extends V> m) {
- m.forEach(this::put);
- }
-
/**
* Returns the value associated with the specified key or null if the key is not present.
*
@@ -57,7 +54,50 @@ public interface DataMap<K, V> {
return Streams.stream(keys).map(this::get).toList();
}
- /** {@inheritDoc} */
+ /**
+ * Associates the specified value with the specified key in the map.
+ *
+ * @param key the key
+ * @param value the value
+ * @return the previous value associated with the key, or null if there was no mapping for the
+ * key.
+ */
+ V put(K key, V value);
+
+ /**
+ * Removes the mapping for the specified key from the map if present.
+ *
+ * @param key the key
+ * @return the previous value associated with the key, or null if there was no mapping for the
+ * key.
+ */
+ V remove(K key);
+
+ /**
+ * Returns true if the map contains a mapping for the specified key.
+ *
+ * @param key the key
+ * @return true if the map contains a mapping for the key
+ */
+ boolean containsKey(Object key);
+
+ /**
+ * Returns true if the map contains a mapping for the specified value.
+ *
+ * @param value the value
+ * @return true if the map contains a mapping for the value
+ */
+ boolean containsValue(V value);
+
+ /**
+ * Clears the map.
+ */
+ void clear();
+
+ /**
+ * Returns true if the map contains no elements.
+ * @return true if the map contains no elements
+ */
default boolean isEmpty() {
return size() == 0;
}
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 a74dac94..0fe65547 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
@@ -82,11 +82,17 @@ public class IndexedDataList<E> implements DataList<E> {
return values.read(position);
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public long size() {
return index.size();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public void clear() {
index.clear();
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/IndexedDataMap.java
index 82d33018..4e1655db 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/IndexedDataMap.java
@@ -135,7 +135,7 @@ public class IndexedDataMap<E> implements DataMap<Long, E> {
* {@inheritDoc}
*/
@Override
- public E remove(Object key) {
+ public E remove(Long key) {
return values.read(index.remove(key));
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MapAdapter.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MapAdapter.java
index 303c9c3c..fdeef477 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MapAdapter.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MapAdapter.java
@@ -22,28 +22,4 @@ import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;
-public class MapAdapter<K, V> extends AbstractMap<K, V> {
- private final DataMap<K, V> dataMap;
- private final int size;
-
- public MapAdapter(DataMap<K, V> dataMap) {
- this.dataMap = dataMap;
- this.size = (int) dataMap.size();
- }
-
- @Override
- public Set<Entry<K, V>> entrySet() {
- return new AbstractSet<>() {
- @Override
- public Iterator<Entry<K, V>> iterator() {
- return dataMap.entryIterator();
- }
-
- @Override
- public int size() {
- return size;
- }
- };
- }
-}
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/MemoryAlignedDataMap.java
index db439637..3cbd3f4f 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/MemoryAlignedDataMap.java
@@ -92,7 +92,7 @@ public class MemoryAlignedDataMap<E> implements DataMap<Long, E> {
/** {@inheritDoc} */
@Override
- public E remove(Object key) {
+ public E remove(Long key) {
throw new UnsupportedOperationException();
}
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/MonotonicDataMap.java
index 732a63d2..21d22bea 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/MonotonicDataMap.java
@@ -170,7 +170,7 @@ public class MonotonicDataMap<E> implements DataMap<Long, E> {
/** {@inheritDoc} */
@Override
- public E remove(Object key) {
+ public E remove(Long key) {
throw new UnsupportedOperationException();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicFixedSizeDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicFixedSizeDataMap.java
index dfd99409..d686c047 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicFixedSizeDataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicFixedSizeDataMap.java
@@ -110,7 +110,7 @@ public class MonotonicFixedSizeDataMap<E> implements DataMap<Long, E> {
/** {@inheritDoc} */
@Override
- public E remove(Object key) {
+ public E remove(Long key) {
throw new UnsupportedOperationException();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicPairedDataMap.java b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicPairedDataMap.java
index 11394602..45f2ad30 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicPairedDataMap.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/database/collection/MonotonicPairedDataMap.java
@@ -137,7 +137,7 @@ public class MonotonicPairedDataMap<E> implements DataMap<Long, E> {
/** {@inheritDoc} */
@Override
- public E remove(Object key) {
+ public E remove(Long key) {
throw new UnsupportedOperationException();
}
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/workflow/WorkflowContext.java b/baremaps-core/src/main/java/org/apache/baremaps/workflow/WorkflowContext.java
index 66daaac0..541479ec 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/workflow/WorkflowContext.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/workflow/WorkflowContext.java
@@ -65,11 +65,11 @@ public class WorkflowContext {
}
public Map<Long, Coordinate> getCoordinateMap() throws IOException {
- return new MapAdapter<>(getMemoryAlignedDataMap("coordinates", new LonLatDataType()));
+ return DataConversions.asMap(getMemoryAlignedDataMap("coordinates", new LonLatDataType()));
}
public Map<Long, List<Long>> getReferenceMap() throws IOException {
- return new MapAdapter<>(getMonotonicDataMap("references", new LongListDataType()));
+ return DataConversions.asMap(getMonotonicDataMap("references", new LongListDataType()));
}
public <T> DataMap<Long, T> getMemoryAlignedDataMap(String name, FixedSizeDataType<T> dataType)
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/database/AppendOnlyLogTest.java b/baremaps-core/src/test/java/org/apache/baremaps/database/AppendOnlyLogTest.java
index 140b94ce..8b5d14ab 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/database/AppendOnlyLogTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/database/AppendOnlyLogTest.java
@@ -75,7 +75,6 @@ class AppendOnlyLogTest {
for (int i = 0; i < num; i++) {
collection.addPositioned(value);
}
- collection.close();
// read values
int count = 0;
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 d3bdd263..f6e82feb 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
@@ -105,7 +105,7 @@ class DataMapTest {
set.add(i);
map.put(i, i);
}
- var res = new MapAdapter<>(map).keySet();
+ var res = DataConversions.asMap(map).keySet();
assertEquals(set, res);
}
@@ -117,7 +117,7 @@ class DataMapTest {
set.add(i);
map.put(i, i);
}
- assertEquals(set, new HashSet(new MapAdapter<>(map).values()));
+ assertEquals(set, new HashSet(DataConversions.asMap(map).values()));
}
@ParameterizedTest
@@ -156,7 +156,7 @@ class DataMapTest {
assertEquals(15l, map.get(15l));
assertEquals(20l, map.get(20l));
- assertEquals(Set.of(10l, 15l, 20l), new MapAdapter<>(map).keySet());;
+ assertEquals(Set.of(10l, 15l, 20l), DataConversions.asMap(map).keySet());;
}
diff --git a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateSampleTest.java b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateSampleTest.java
index dd019048..cb75d868 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateSampleTest.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/workflow/tasks/ImportUpdateSampleTest.java
@@ -26,8 +26,8 @@ import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import org.apache.baremaps.database.collection.AppendOnlyLog;
+import org.apache.baremaps.database.collection.DataConversions;
import org.apache.baremaps.database.collection.IndexedDataMap;
-import org.apache.baremaps.database.collection.MapAdapter;
import org.apache.baremaps.database.memory.OnHeapMemory;
import org.apache.baremaps.database.type.LongListDataType;
import org.apache.baremaps.database.type.geometry.CoordinateDataType;
@@ -59,9 +59,9 @@ class ImportUpdateSampleTest extends PostgresRepositoryTest {
PostgresRelationRepository relationRepository = new PostgresRelationRepository(dataSource());
// Initialize the data maps
- Map<Long, Coordinate> coordinateMap = new MapAdapter<>(
+ Map<Long, Coordinate> coordinateMap = DataConversions.asMap(
new IndexedDataMap<>(new AppendOnlyLog<>(new CoordinateDataType(), new OnHeapMemory())));
- Map<Long, List<Long>> referenceMap = new MapAdapter<>(
+ Map<Long, List<Long>> referenceMap = DataConversions.asMap(
new IndexedDataMap<>(new AppendOnlyLog<>(new LongListDataType(), new OnHeapMemory())));
// Import the sample data