Skip to content

Commit

Permalink
CR: Avoid needlessly instantiating ArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
original-brownbear committed Jan 17, 2019
1 parent 15a218d commit ee15306
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,21 @@ public boolean addOrNotify(Translog.Location location, Consumer<Boolean> listene
throw new IllegalStateException("can't wait for refresh on a closed index");
}
List<Tuple<Translog.Location, Consumer<Boolean>>> listeners = refreshListeners;
if (listeners == null) {
listeners = new ArrayList<>();
}
if (refreshForcers == 0 && listeners.size() < getMaxRefreshListeners.getAsInt()) {
final int maxRefreshes = getMaxRefreshListeners.getAsInt();
if (refreshForcers == 0 && maxRefreshes > 0 && (listeners == null || listeners.size() < maxRefreshes)) {
ThreadContext.StoredContext storedContext = threadContext.newStoredContext(true);
Consumer<Boolean> contextPreservingListener = forced -> {
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
storedContext.restore();
listener.accept(forced);
}
};
if (refreshListeners == null) {
refreshListeners = listeners;
if (listeners == null) {
listeners = new ArrayList<>();
}
// We have a free slot so register the listener
listeners.add(new Tuple<>(location, contextPreservingListener));
refreshListeners = listeners;
return false;
}
}
Expand Down

0 comments on commit ee15306

Please sign in to comment.