ibessonov commented on a change in pull request #589:
URL: https://github.com/apache/ignite-3/pull/589#discussion_r793306121



##########
File path: 
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/ConfigurationListenerHolder.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.configuration;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Holder (thread safe) for configuration change listeners.
+ */
+class ConfigurationListenerHolder<L> {
+    private final Collection<Container<L>> containers = new 
CopyOnWriteArrayList<>();

Review comment:
       I don't really understand, why all fields/parameters have to be 
`collections`. This, for example, must be ordered (not sorted). Ordered 
collections are called lists, right? What's the exact motivation? I would 
really like to know.

##########
File path: 
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/ConfigurationListenerHolder.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.configuration;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Holder (thread safe) for configuration change listeners.
+ */
+class ConfigurationListenerHolder<L> {
+    private final Collection<Container<L>> containers = new 
CopyOnWriteArrayList<>();
+
+    /**
+     * Adds a listener.
+     *
+     * @param listener Configuration change listener.
+     * @param notificationNumber Configuration notification listener number 
after which the listener will be called.
+     * @see ConfigurationListenerHolder#listeners
+     */
+    void addListener(L listener, long notificationNumber) {
+        containers.add(new Container<>(listener, notificationNumber));
+    }
+
+    /**
+     * Removes the listener.
+     *
+     * @param listener Configuration change listener.
+     */
+    void removeListener(L listener) {
+        containers.remove(new Container<>(listener, -1) {
+            /** {@inheritDoc} */
+            @Override
+            public boolean equals(Object obj) {
+                return listener == ((Container<L>) obj).listener;
+            }
+        });
+    }
+
+    /**
+     * Returns an iterator of the listeners for the {@code notificationNumber} 
(were added for and before it).
+     *
+     * <p>NOTE: {@link Iterator#remove} - not supported.
+     *
+     * @param notificationNumber Configuration notification listener number.
+     * @see ConfigurationListenerHolder#addListener
+     */
+    Iterator<L> listeners(long notificationNumber) {

Review comment:
       Or, you could return filtered stream and not reinvent the wheel. 

##########
File path: 
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/ConfigurationNode.java
##########
@@ -104,22 +101,24 @@ protected ConfigurationNode(
     /** {@inheritDoc} */
     @Override
     public void listen(ConfigurationListener<VIEWT> listener) {
-        updateListeners.add(listener);
+        updateListeners.addListener(listener, changer.notificationCount() + 1);

Review comment:
       I feel like this statement breaks encapsulation. Should `+ 1` be inside 
of the method, not outside?

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/CollectionUtils.java
##########
@@ -197,13 +202,100 @@ public T next() {
         };
     }
 
+    /**
+     * Create a lazy concatenation of iterators.
+     *
+     * <p>NOTE: {@link Iterator#remove} - not supported.
+     *
+     * @param iterators Iterators.
+     * @param <T> Type of the elements.
+     * @return Concatenation of iterators.
+     */
+    @SafeVarargs
+    public static <T> Iterator<T> concat(@Nullable Iterator<? extends T>... 
iterators) {

Review comment:
       Why don't we use streams?

##########
File path: 
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/ConfigurationListenerHolder.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.configuration;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Holder (thread safe) for configuration change listeners.
+ */
+class ConfigurationListenerHolder<L> {
+    private final Collection<Container<L>> containers = new 
CopyOnWriteArrayList<>();
+
+    /**
+     * Adds a listener.
+     *
+     * @param listener Configuration change listener.
+     * @param notificationNumber Configuration notification listener number 
after which the listener will be called.
+     * @see ConfigurationListenerHolder#listeners
+     */
+    void addListener(L listener, long notificationNumber) {
+        containers.add(new Container<>(listener, notificationNumber));
+    }
+
+    /**
+     * Removes the listener.
+     *
+     * @param listener Configuration change listener.
+     */
+    void removeListener(L listener) {

Review comment:
       You know what's funny? "Nested" removes won't work properly :)

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/CollectionUtils.java
##########
@@ -197,13 +202,100 @@ public T next() {
         };
     }
 
+    /**
+     * Create a lazy concatenation of iterators.
+     *
+     * <p>NOTE: {@link Iterator#remove} - not supported.
+     *
+     * @param iterators Iterators.
+     * @param <T> Type of the elements.
+     * @return Concatenation of iterators.
+     */
+    @SafeVarargs
+    public static <T> Iterator<T> concat(@Nullable Iterator<? extends T>... 
iterators) {
+        if (iterators == null || iterators.length == 0) {
+            return emptyIterator();
+        }
+
+        return new Iterator<>() {
+            /** Current index at {@code iterators}. */
+            int idx = 0;
+
+            /** Current iterator. */
+            Iterator<? extends T> curr = emptyIterator();
+
+            /** {@inheritDoc} */
+            @Override
+            public boolean hasNext() {
+                while (!curr.hasNext() && idx < iterators.length) {
+                    curr = iterators[idx++];
+                }
+
+                return curr.hasNext();
+            }
+
+            /** {@inheritDoc} */
+            @Override
+            public T next() {
+                if (!hasNext()) {
+                    throw new NoSuchElementException();
+                } else {
+                    return curr.next();
+                }
+            }
+        };
+    }
+
+    /**
+     * Create a lazy concatenation of iterators.
+     *
+     * <p>NOTE: {@link Iterator#remove} - not supported.
+     *
+     * @param iterators Iterators.
+     * @param <T> Type of the elements.
+     * @return Concatenation of iterators.
+     */
+    public static <T> Iterator<T> concat(@Nullable Collection<Iterator<? 
extends T>> iterators) {

Review comment:
       Same question, this is literally a `flatMap(Function.id())` operation

##########
File path: 
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/ConfigurationChanger.java
##########
@@ -95,7 +96,10 @@
     private final ConfigurationStorage storage;
 
     /** Storage trees. */
-    private volatile StorageRoots storageRoots;
+    @Nullable private volatile StorageRoots storageRoots;

Review comment:
       It can't be null after `start`, are you sure that `@Nullable` is really 
necessary 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