From 736bca2589cde8e87d99da0af17db0f2afcdecda Mon Sep 17 00:00:00 2001 From: MiguelRicardos <133383044+MiguelRicardos@users.noreply.github.com> Date: Fri, 18 Aug 2023 07:48:38 -0400 Subject: [PATCH] Updated first()/last() methods to not rely on a boolean, clean up based on Intellij. (#2004) Feature/2.1.1 (#2037) * move from accumulo 2.1.0 to accumulo2.1.1 --- .../query/util/sortedset/FileSortedSet.java | 85 ++++++------------- 1 file changed, 24 insertions(+), 61 deletions(-) diff --git a/warehouse/query-core/src/main/java/datawave/query/util/sortedset/FileSortedSet.java b/warehouse/query-core/src/main/java/datawave/query/util/sortedset/FileSortedSet.java index 7c136d56d7f..780c04c9974 100644 --- a/warehouse/query-core/src/main/java/datawave/query/util/sortedset/FileSortedSet.java +++ b/warehouse/query-core/src/main/java/datawave/query/util/sortedset/FileSortedSet.java @@ -25,20 +25,17 @@ /** * A sorted set that can be persisted into a file and still be read in its persisted state. The set can always be re-loaded and then all operations will work as - * expected. This will support null contained in the underlying sets iff a comparator is supplied that can handle null values. - * - * The persisted file will contain the serialized entries, followed by the actual size. + * expected. This will support null contained in the underlying sets iff a comparator is supplied that can handle null values. The persisted file will contain + * the serialized entries, followed by the actual size. * * @param * type of set */ public abstract class FileSortedSet implements SortedSet, Cloneable { - private static Logger log = Logger.getLogger(FileSortedSet.class); - protected boolean persisted = false; + private static final Logger log = Logger.getLogger(FileSortedSet.class); + protected boolean persisted; protected E[] range; - protected SortedSet set = null; - // A null entry placeholder - public static final NullObject NULL_OBJECT = new NullObject(); + protected SortedSet set; // The file handler that handles the underlying io public TypedSortedSetFileHandler handler; @@ -47,9 +44,6 @@ public abstract class FileSortedSet implements SortedSet, Cloneable { /** * A class that represents a null object within the set - * - * - * */ public static class NullObject implements Serializable { private static final long serialVersionUID = -5528112099317370355L; @@ -149,7 +143,7 @@ public FileSortedSet(SortedSet set, TypedSortedSetFileHandler handler, FileSo } /** - * Create an sorted set out of another sorted set. If persist is true, then the set will be directly persisted using the set's iterator which avoid pulling + * Create a sorted set out of another sorted set. If persist is true, then the set will be directly persisted using the set's iterator which avoid pulling * all of its entries into memory at once. * * @param set @@ -314,7 +308,6 @@ private void verifyPersistance(TypedSortedSetFileHandler handler, int size, List * for issues with read/write */ private int readSize() throws IOException { - long bytesToSkip = handler.getSize() - 4; try (SortedSetInputStream inStream = handler.getInputStream()) { return inStream.readSize(); } @@ -397,12 +390,9 @@ public boolean isEmpty() { public boolean contains(Object o) { if (persisted) { E t = (E) o; - for (Iterator it = iterator(); it.hasNext();) { - if (it.hasNext()) { - E next = it.next(); - if (equals(next, t)) { - return true; - } + for (E next : this) { + if (equals(next, t)) { + return true; } } return false; @@ -600,32 +590,24 @@ public SortedSet tailSet(E fromElement) { @Override public E first() { - boolean gotFirst = false; - E first = null; + E first; if (persisted) { try (SortedSetInputStream stream = getBoundedFileHandler().getInputStream(getStart(), getEnd())) { first = stream.readObject(); - gotFirst = true; + return first; } catch (Exception e) { - QueryException qe = new QueryException(DatawaveErrorCode.FETCH_FIRST_ELEMENT_ERROR, e); - throw (new IllegalStateException(qe)); + throw new IllegalStateException(new QueryException(DatawaveErrorCode.FETCH_FIRST_ELEMENT_ERROR, e)); } } else if (!set.isEmpty()) { first = set.first(); - gotFirst = true; - } - if (!gotFirst) { - QueryException qe = new QueryException(DatawaveErrorCode.FETCH_FIRST_ELEMENT_ERROR); - throw (NoSuchElementException) (new NoSuchElementException().initCause(qe)); - } else { return first; } + throw (NoSuchElementException) new NoSuchElementException().initCause(new QueryException(DatawaveErrorCode.FETCH_FIRST_ELEMENT_ERROR)); } @Override public E last() { - boolean gotLast = false; - E last = null; + E last; if (persisted) { try (SortedSetInputStream stream = getBoundedFileHandler().getInputStream(getStart(), getEnd())) { last = stream.readObject(); @@ -634,29 +616,20 @@ public E last() { last = next; next = stream.readObject(); } - gotLast = true; + return last; } catch (Exception e) { - throw new IllegalStateException("Unable to get last from file", e); + throw new IllegalStateException(new QueryException(DatawaveErrorCode.FETCH_LAST_ELEMENT_ERROR, e)); } } else if (!set.isEmpty()) { last = set.last(); - gotLast = true; - } - if (!gotLast) { - QueryException qe = new QueryException(DatawaveErrorCode.FETCH_LAST_ELEMENT_ERROR); - throw (NoSuchElementException) (new NoSuchElementException().initCause(qe)); - } else { return last; } + throw (NoSuchElementException) new NoSuchElementException().initCause(new QueryException(DatawaveErrorCode.FETCH_LAST_ELEMENT_ERROR)); } @Override public String toString() { - if (persisted) { - return handler.toString(); - } else { - return set.toString(); - } + return persisted ? handler.toString() : set.toString(); } /** @@ -722,25 +695,19 @@ private boolean isSubset() { } private int compare(E a, E b) { - if (this.set.comparator() != null) { - return this.set.comparator().compare(a, b); - } else { - return ((Comparable) a).compareTo(b); - } + return (this.set.comparator() != null) ? this.set.comparator().compare(a, b) : ((Comparable) a).compareTo(b); } public BoundedTypedSortedSetFileHandler getBoundedFileHandler() { return new DefaultBoundedTypedSortedSetFileHandler(); } - /********* Some sub classes ***********/ - /** * This is the iterator for a persisted FileSortedSet */ protected class FileIterator implements Iterator { - private SortedSetInputStream stream = null; - private E next = null; + private SortedSetInputStream stream; + private E next; public FileIterator() { try { @@ -1044,9 +1011,9 @@ public void deleteFile() { * An input stream that supports bounding the objects. Used when the underlying stream does not already support bounding. */ public class BoundedInputStream implements SortedSetInputStream { - private SortedSetInputStream delegate; - private E from; - private E to; + private final SortedSetInputStream delegate; + private final E from; + private final E to; public BoundedInputStream(SortedSetInputStream stream, E from, E to) { this.delegate = stream; @@ -1085,10 +1052,6 @@ public static class PersistOptions { public PersistOptions() {} - public PersistOptions(boolean verify) { - this.verifySize = this.verifyElements = verify; - } - public PersistOptions(boolean verifySize, boolean verifyElements) { this.verifySize = verifySize; this.verifyElements = verifyElements;