denis-chudov commented on code in PR #1807:
URL: https://github.com/apache/ignite-3/pull/1807#discussion_r1146124058


##########
modules/core/src/main/java/org/apache/ignite/internal/causality/BaseVersionedValue.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.causality;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.lang.IgniteStringFormatter.format;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.NavigableMap;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.Supplier;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.util.Lazy;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Base implementation of a {@code VersionedValue} intended to be used by 
other implementations.
+ */
+class BaseVersionedValue<T> implements VersionedValue<T> {
+    private final IgniteLogger log = 
Loggers.forClass(BaseVersionedValue.class);
+
+    /** Token until the value is initialized. */
+    private static final long NOT_INITIALIZED = -1L;
+
+    /** Default history size. */
+    private static final int DEFAULT_MAX_HISTORY_SIZE = 10;
+
+    /** Size of the history of changes to store, including last applied token. 
*/
+    private final int maxHistorySize;
+
+    /** List of completion listeners, see {@link #whenComplete}. */
+    private final List<CompletionListener<T>> completionListeners = new 
CopyOnWriteArrayList<>();
+
+    /** Versioned value storage. */
+    private final ConcurrentNavigableMap<Long, CompletableFuture<T>> history = 
new ConcurrentSkipListMap<>();
+
+    /** Default value that is used when a Versioned Value is completed without 
an explicit value. */
+    @Nullable
+    private final Lazy<T> defaultValue;
+
+    /**
+     * Last applied causality token.
+     *
+     * <p>Multi-threaded access is guarded by the {@link #readWriteLock}.
+     */
+    private long actualToken = NOT_INITIALIZED;
+
+    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+
+    BaseVersionedValue(@Nullable Supplier<T> defaultValueSupplier) {
+        this(DEFAULT_MAX_HISTORY_SIZE, defaultValueSupplier);
+    }
+
+    BaseVersionedValue(int maxHistorySize, @Nullable Supplier<T> 
defaultValueSupplier) {
+        this.maxHistorySize = maxHistorySize;
+        this.defaultValue = defaultValueSupplier == null ? null : new 
Lazy<>(defaultValueSupplier);
+    }
+
+    @Override
+    public CompletableFuture<T> get(long causalityToken) {
+        assert causalityToken > NOT_INITIALIZED;
+
+        readWriteLock.readLock().lock();
+
+        try {
+            if (causalityToken > actualToken) {
+                return history.computeIfAbsent(causalityToken, t -> new 
CompletableFuture<>());
+            }
+
+            Entry<Long, CompletableFuture<T>> histEntry = 
history.floorEntry(causalityToken);
+
+            if (histEntry == null) {
+                throw new OutdatedTokenException(causalityToken, actualToken, 
maxHistorySize);
+            }
+
+            return histEntry.getValue();
+        } finally {
+            readWriteLock.readLock().unlock();
+        }
+    }
+
+    @Override
+    public @Nullable T latest() {
+        for (CompletableFuture<T> fut : history.descendingMap().values()) {
+            if (fut.isDone()) {
+                return fut.join();
+            }
+        }
+
+        return getDefault();
+    }
+
+    /**
+     * Returns the default value.
+     */
+    @Nullable T getDefault() {
+        return defaultValue == null ? null : defaultValue.get();
+    }
+
+    /**
+     * Completes the Versioned Value for the given token. This method will 
look for the previous complete token and complete all registered
+     * futures in the {@code (prevToken, causalityToken]} range. If no {@code 
complete} methods have been called before, all these futures
+     * will be complete with the configured default value.
+     *
+     * <p>Calling this method will trigger the {@link #whenComplete} listeners 
for the given token.
+     *
+     * @param causalityToken Causality token.
+     */
+    void complete(long causalityToken) {

Review Comment:
   ok, no objections here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to