leave Iterables.flatten pre-caching the outer Iterable

This commit is contained in:
Mike McCandless 2016-05-14 17:10:17 -04:00
parent 53c2f8b4b6
commit 48dca45564
3 changed files with 16 additions and 5 deletions

View File

@ -54,8 +54,8 @@ public class Iterables {
} }
} }
/** Flattens the two level {@code Iterable} into a single {@code Iterable}. Note that this uses the original input iterable so if it /** Flattens the two level {@code Iterable} into a single {@code Iterable}. Note that this pre-caches the values from the outer {@code
* later changes, the flattened result here will reflect the change. */ * Iterable}, but not the values from the inner one. */
public static <T> Iterable<T> flatten(Iterable<? extends Iterable<T>> inputs) { public static <T> Iterable<T> flatten(Iterable<? extends Iterable<T>> inputs) {
Objects.requireNonNull(inputs); Objects.requireNonNull(inputs);
return new FlattenedIterables<>(inputs); return new FlattenedIterables<>(inputs);
@ -65,7 +65,11 @@ public class Iterables {
private final Iterable<? extends Iterable<T>> inputs; private final Iterable<? extends Iterable<T>> inputs;
FlattenedIterables(Iterable<? extends Iterable<T>> inputs) { FlattenedIterables(Iterable<? extends Iterable<T>> inputs) {
this.inputs = inputs; List<Iterable<T>> list = new ArrayList<>();
for (Iterable<T> iterable : inputs) {
list.add(iterable);
}
this.inputs = list;
} }
@Override @Override

View File

@ -181,7 +181,9 @@ public class IndicesService extends AbstractLifecycleComponent<IndicesService> i
this.namedWriteableRegistry = namedWriteableRegistry; this.namedWriteableRegistry = namedWriteableRegistry;
clusterSettings.addSettingsUpdateConsumer(IndexStoreConfig.INDICES_STORE_THROTTLE_TYPE_SETTING, indexStoreConfig::setRateLimitingType); clusterSettings.addSettingsUpdateConsumer(IndexStoreConfig.INDICES_STORE_THROTTLE_TYPE_SETTING, indexStoreConfig::setRateLimitingType);
clusterSettings.addSettingsUpdateConsumer(IndexStoreConfig.INDICES_STORE_THROTTLE_MAX_BYTES_PER_SEC_SETTING, indexStoreConfig::setRateLimitingThrottle); clusterSettings.addSettingsUpdateConsumer(IndexStoreConfig.INDICES_STORE_THROTTLE_MAX_BYTES_PER_SEC_SETTING, indexStoreConfig::setRateLimitingThrottle);
indexingMemoryController = new IndexingMemoryController(settings, threadPool, Iterables.flatten(this)); indexingMemoryController = new IndexingMemoryController(settings, threadPool,
// ensure we pull an iter with new shards - flatten makes a copy
() -> Iterables.flatten(this).iterator());
this.indexScopeSetting = indexScopedSettings; this.indexScopeSetting = indexScopedSettings;
this.circuitBreakerService = circuitBreakerService; this.circuitBreakerService = circuitBreakerService;
this.indicesFieldDataCache = new IndicesFieldDataCache(settings, new IndexFieldDataCache.Listener() { this.indicesFieldDataCache = new IndicesFieldDataCache(settings, new IndexFieldDataCache.Listener() {

View File

@ -60,6 +60,7 @@ public class IterablesTests extends ESTestCase {
public void testFlatten() { public void testFlatten() {
List<List<Integer>> list = new ArrayList<>(); List<List<Integer>> list = new ArrayList<>();
list.add(new ArrayList<>());
Iterable<Integer> allInts = Iterables.flatten(list); Iterable<Integer> allInts = Iterables.flatten(list);
int count = 0; int count = 0;
@ -67,13 +68,17 @@ public class IterablesTests extends ESTestCase {
count++; count++;
} }
assertEquals(0, count); assertEquals(0, count);
list.add(new ArrayList<>()); list.add(new ArrayList<>());
list.get(1).add(0);
// changes to the outer list are not seen since flatten pre-caches outer list on init:
count = 0;
for(int x : allInts) { for(int x : allInts) {
count++; count++;
} }
assertEquals(0, count); assertEquals(0, count);
// but changes to the original inner lists are seen:
list.get(0).add(0); list.get(0).add(0);
for(int x : allInts) { for(int x : allInts) {
count++; count++;