HDDS-8707. Avoid linear search in DBDefinition implementations.#4782
Merged
adoroszlai merged 4 commits intoapache:masterfrom Jun 2, 2023
Merged
HDDS-8707. Avoid linear search in DBDefinition implementations.#4782adoroszlai merged 4 commits intoapache:masterfrom
adoroszlai merged 4 commits intoapache:masterfrom
Conversation
adoroszlai
reviewed
Jun 1, 2023
Contributor
adoroszlai
left a comment
There was a problem hiding this comment.
Thanks @szetszwo for the improvement. LGTM, except an edge case in the multi-map iterator, which does not affect its current usage with the DB definitions. I can post a follow-up PR for that if you prefer.
Comment on lines
+63
to
+84
| 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(); | ||
| } |
Contributor
There was a problem hiding this 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:
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:
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);
}
}
Contributor
Author
There was a problem hiding this 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.
Contributor
Author
|
When merging this, please add |
Contributor
|
Thanks @szetszwo for updating the patch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
DBDefinition declares the following method returning an array.
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.