You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "szetszwo (via GitHub)" <gi...@apache.org> on 2023/05/26 16:56:50 UTC

[GitHub] [ozone] szetszwo opened a new pull request, #4782: HDDS-8707. Avoid linear search in DBDefinition implementations.

szetszwo opened a new pull request, #4782:
URL: https://github.com/apache/ozone/pull/4782

   ## What changes were proposed in this pull request?
   
   DBDefinition declares the following method returning an array.
   ```java
   public DBColumnFamilyDefinition[] getColumnFamilies();
   ```
   It forces the callers to perform a linear search for looking up DBColumnFamilyDefinition by names.  The method should return a `Map`.
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/HDDS-8707
   
   ## How was this patch tested?
   
   By existing unit tests.


-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


[GitHub] [ozone] szetszwo commented on pull request #4782: HDDS-8707. Avoid linear search in DBDefinition implementations.

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on PR #4782:
URL: https://github.com/apache/ozone/pull/4782#issuecomment-1573026297

   When merging this, please add 
   ```
   Co-authored-by: Doroszlai, Attila <ad...@apache.org>
   ```


-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


[GitHub] [ozone] adoroszlai merged pull request #4782: HDDS-8707. Avoid linear search in DBDefinition implementations.

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai merged PR #4782:
URL: https://github.com/apache/ozone/pull/4782


-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


[GitHub] [ozone] adoroszlai commented on pull request #4782: HDDS-8707. Avoid linear search in DBDefinition implementations.

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on PR #4782:
URL: https://github.com/apache/ozone/pull/4782#issuecomment-1573145809

   Thanks @szetszwo for updating the patch.


-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


[GitHub] [ozone] szetszwo commented on a diff in pull request #4782: HDDS-8707. Avoid linear search in DBDefinition implementations.

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #4782:
URL: https://github.com/apache/ozone/pull/4782#discussion_r1213811338


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/CollectionUtils.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** Utility methods for Java Collections. */
+public interface CollectionUtils {
+  static <KEY, VALUE> Map<KEY, VALUE> newUnmodifiableMap(
+      List<VALUE> values, Function<VALUE, KEY> getKey,
+      Map<KEY, VALUE> existing) {
+    final Map<KEY, VALUE> map = new HashMap<>(existing);
+    for (VALUE v : values) {
+      final KEY key = getKey.apply(v);
+      final VALUE previous = map.put(key, v);
+      if (previous != null) {
+        throw new IllegalArgumentException("Already exists: " + key
+            + ", previous " + previous.getClass());
+      }
+    }
+    return Collections.unmodifiableMap(map);
+  }
+
+  static <KEY, VALUE> Map<KEY, List<VALUE>> newUnmodifiableMultiMap(
+      List<VALUE> values, Function<VALUE, KEY> getKey) {
+    final Map<KEY, List<VALUE>> map = new HashMap<>();
+    for (VALUE v : values) {
+      final KEY key = getKey.apply(v);
+      map.computeIfAbsent(key, k -> new ArrayList<>()).add(v);
+    }
+    return Collections.unmodifiableMap(map.entrySet().stream()
+        .collect(Collectors.toMap(Map.Entry::getKey,
+            e -> Collections.unmodifiableList(e.getValue()))));
+  }
+
+  static <T> Iterator<T> newIterator(Collection<List<T>> values) {
+    final Iterator<List<T>> listIterator = values.iterator();
+    return new Iterator<T>() {
+      private Iterator<T> i;
+
+      private boolean hasNextItem() {
+        return i != null && i.hasNext();
+      }
+
+      @Override
+      public boolean hasNext() {
+        return listIterator.hasNext() || hasNextItem();
+      }
+
+      @Override
+      public T next() {
+        if (hasNextItem()) {
+          return i.next();
+        }
+        if (listIterator.hasNext()) {
+          i = listIterator.next().iterator();
+          return i.next();
+        }
+        throw new NoSuchElementException();
+      }

Review Comment:
   @adoroszlai , good catch!  You are right that it won't work if there are empty lists.   Thanks for providing the code.  Let me try it.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


[GitHub] [ozone] adoroszlai commented on a diff in pull request #4782: HDDS-8707. Avoid linear search in DBDefinition implementations.

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on code in PR #4782:
URL: https://github.com/apache/ozone/pull/4782#discussion_r1213435493


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/CollectionUtils.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/** Utility methods for Java Collections. */
+public interface CollectionUtils {
+  static <KEY, VALUE> Map<KEY, VALUE> newUnmodifiableMap(
+      List<VALUE> values, Function<VALUE, KEY> getKey,
+      Map<KEY, VALUE> existing) {
+    final Map<KEY, VALUE> map = new HashMap<>(existing);
+    for (VALUE v : values) {
+      final KEY key = getKey.apply(v);
+      final VALUE previous = map.put(key, v);
+      if (previous != null) {
+        throw new IllegalArgumentException("Already exists: " + key
+            + ", previous " + previous.getClass());
+      }
+    }
+    return Collections.unmodifiableMap(map);
+  }
+
+  static <KEY, VALUE> Map<KEY, List<VALUE>> newUnmodifiableMultiMap(
+      List<VALUE> values, Function<VALUE, KEY> getKey) {
+    final Map<KEY, List<VALUE>> map = new HashMap<>();
+    for (VALUE v : values) {
+      final KEY key = getKey.apply(v);
+      map.computeIfAbsent(key, k -> new ArrayList<>()).add(v);
+    }
+    return Collections.unmodifiableMap(map.entrySet().stream()
+        .collect(Collectors.toMap(Map.Entry::getKey,
+            e -> Collections.unmodifiableList(e.getValue()))));
+  }
+
+  static <T> Iterator<T> newIterator(Collection<List<T>> values) {
+    final Iterator<List<T>> listIterator = values.iterator();
+    return new Iterator<T>() {
+      private Iterator<T> i;
+
+      private boolean hasNextItem() {
+        return i != null && i.hasNext();
+      }
+
+      @Override
+      public boolean hasNext() {
+        return listIterator.hasNext() || hasNextItem();
+      }
+
+      @Override
+      public T next() {
+        if (hasNextItem()) {
+          return i.next();
+        }
+        if (listIterator.hasNext()) {
+          i = listIterator.next().iterator();
+          return i.next();
+        }
+        throw new NoSuchElementException();
+      }

Review Comment:
   If the current list has no more items, and the next list is empty, then `hasNext()` returns `true`, but `next()` throws `NoSuchElementException`.
   
   I think a safe implementation would be:
   
   ```java
         private Iterator<T> i = emptyIterator();
   
         private Iterator<T> nextIterator() {
           if (i.hasNext()) {
             return i;
           }
           while (listIterator.hasNext()) {
             i = listIterator.next().iterator();
             if (i.hasNext()) {
               return i;
             }
           }
           return emptyIterator();
         }
   
         @Override
         public boolean hasNext() {
           return nextIterator().hasNext();
         }
   
         @Override
         public T next() {
           if (hasNext()) {
             return i.next();
           }
           throw new NoSuchElementException();
         }
   ```
   
   Test:
   
   ```java
   import org.junit.jupiter.api.Test;
   
   import java.util.ArrayList;
   import java.util.List;
   
   import static java.util.Arrays.asList;
   import static java.util.Collections.emptyList;
   import static org.junit.jupiter.api.Assertions.assertEquals;
   
   class TestCollectionUtils {
   
     @Test
     void someEmptyLists() {
       List<List<String>> listOfLists = new ArrayList<>();
       listOfLists.add(asList("a", "b"));
       listOfLists.add(emptyList());
       listOfLists.add(emptyList());
       listOfLists.add(asList("c", "d"));
       listOfLists.add(emptyList());
   
       assertIteration(asList("a", "b", "c", "d"), listOfLists);
     }
   
     @Test
     void allEmptyLists() {
       List<List<String>> listOfLists = new ArrayList<>();
       listOfLists.add(emptyList());
       listOfLists.add(emptyList());
   
       assertIteration(emptyList(), listOfLists);
     }
   
     @Test
     void empty() {
       assertIteration(emptyList(), emptyList());
     }
   
     private static <T> void assertIteration(List<T> expected,
         List<List<T>> listOfLists) {
       List<T> actual = new ArrayList<>();
       CollectionUtils.newIterator(listOfLists).forEachRemaining(actual::add);
       assertEquals(expected, actual);
     }
   
   }
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org