This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch data-refactoring
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git

commit b36482425365657d4a28e4f13e169264d890d5dc
Author: Bertil Chapuis <[email protected]>
AuthorDate: Sat Apr 5 11:38:27 2025 +0200

    Use the builder pattern for AppendOnlyLog
---
 .../apache/baremaps/workflow/WorkflowContext.java  | 28 +++++----
 .../baremaps/data/collection/AppendOnlyLog.java    | 73 ++++++++++++++++++++--
 2 files changed, 84 insertions(+), 17 deletions(-)

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 bf9c03c10..584e46c3e 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
@@ -73,7 +73,7 @@ public class WorkflowContext {
 
   private <T> DataMap<Long, T> getMemoryAlignedDataMap(String name, 
FixedSizeDataType<T> dataType)
       throws IOException {
-    var coordinateDir = Files.createDirectories(cacheDir.resolve(name));
+    Path coordinateDir = Files.createDirectories(cacheDir.resolve(name));
     return new MemoryAlignedDataMap<>(
         dataType,
         new MemoryMappedDirectory(coordinateDir));
@@ -81,22 +81,26 @@ public class WorkflowContext {
 
   private <T> DataMap<Long, T> getMonotonicDataMap(String name, DataType<T> 
dataType)
       throws IOException {
-    var mapDir = Files.createDirectories(cacheDir.resolve(name));
-    var keysDir = Files.createDirectories(mapDir.resolve("keys"));
-    var valuesDir = Files.createDirectories(mapDir.resolve("values"));
-    return new MonotonicDataMap<>(
-        new MemoryAlignedDataList<>(
-            new PairDataType<>(new LongDataType(), new LongDataType()),
-            new MemoryMappedDirectory(keysDir)),
-        new AppendOnlyLog<>(
-            dataType,
-            new MemoryMappedDirectory(valuesDir)));
+    Path mapDir = Files.createDirectories(cacheDir.resolve(name));
+    Path keysDir = Files.createDirectories(mapDir.resolve("keys"));
+    Path valuesDir = Files.createDirectories(mapDir.resolve("values"));
+    MemoryAlignedDataList<Pair<Long, Long>> keys = new MemoryAlignedDataList<>(
+        new PairDataType<>(new LongDataType(), new LongDataType()),
+        new MemoryMappedDirectory(keysDir));
+    AppendOnlyLog<T> values = AppendOnlyLog.<T>builder()
+        .dataType(dataType)
+        .values(new MemoryMappedDirectory(valuesDir))
+        .build();
+    return MonotonicDataMap.<T>builder()
+        .keys(keys)
+        .values(values)
+        .build();
   }
 
   private DataMap<Long, Coordinate> getMonotonicPairedDataMap(String name,
       DataType<Coordinate> dataType)
       throws IOException {
-    var mapDir = Files.createDirectories(cacheDir.resolve(name));
+    Path mapDir = Files.createDirectories(cacheDir.resolve(name));
     return new MonotonicPairedDataMap<>(
         new MemoryAlignedDataList<>(
             new PairDataType<>(new LongDataType(), new LonLatDataType()),
diff --git 
a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/AppendOnlyLog.java
 
b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/AppendOnlyLog.java
index 44ecc2d1c..826da936d 100644
--- 
a/baremaps-data/src/main/java/org/apache/baremaps/data/collection/AppendOnlyLog.java
+++ 
b/baremaps-data/src/main/java/org/apache/baremaps/data/collection/AppendOnlyLog.java
@@ -47,12 +47,75 @@ public class AppendOnlyLog<E> implements DataCollection<E> {
   private final Lock lock = new ReentrantLock();
 
   /**
-   * Constructs an {@link AppendOnlyLog}.
+   * Static factory method to create a new builder.
    *
-   * @param dataType the data type
+   * @param <E> the type of elements
+   * @return a new builder
    */
-  public AppendOnlyLog(DataType<E> dataType) {
-    this(dataType, new OffHeapMemory());
+  public static <E> Builder<E> builder() {
+    return new Builder<>();
+  }
+
+  /**
+   * Builder for {@link AppendOnlyLog}.
+   *
+   * @param <E> the type of elements
+   */
+  public static class Builder<E> {
+    private DataType<E> dataType;
+    private Memory<?> memory;
+
+    /**
+     * Sets the data type for the log.
+     *
+     * @param dataType the data type
+     * @return this builder
+     */
+    public <T extends E> Builder<E> dataType(DataType<T> dataType) {
+      @SuppressWarnings("unchecked")
+      DataType<E> castedDataType = (DataType<E>) dataType;
+      this.dataType = castedDataType;
+      return this;
+    }
+
+    /**
+     * Sets the memory for the log.
+     *
+     * @param memory the memory
+     * @return this builder
+     */
+    public Builder<E> memory(Memory<?> memory) {
+      this.memory = memory;
+      return this;
+    }
+
+    /**
+     * Sets the values memory for the log. This is an alias for {@link 
#memory(Memory)}.
+     *
+     * @param memory the memory
+     * @return this builder
+     */
+    public Builder<E> values(Memory<?> memory) {
+      return memory(memory);
+    }
+
+    /**
+     * Builds a new {@link AppendOnlyLog}.
+     *
+     * @return a new AppendOnlyLog
+     * @throws IllegalStateException if required parameters are missing
+     */
+    public AppendOnlyLog<E> build() {
+      if (dataType == null) {
+        throw new IllegalStateException("Data type must be specified");
+      }
+
+      if (memory == null) {
+        memory = new OffHeapMemory();
+      }
+
+      return new AppendOnlyLog<>(dataType, memory);
+    }
   }
 
   /**
@@ -61,7 +124,7 @@ public class AppendOnlyLog<E> implements DataCollection<E> {
    * @param dataType the data type
    * @param memory the memory
    */
-  public AppendOnlyLog(DataType<E> dataType, Memory<?> memory) {
+  private AppendOnlyLog(DataType<E> dataType, Memory<?> memory) {
     this.dataType = dataType;
     this.memory = memory;
     this.segmentSize = memory.segmentSize();

Reply via email to